|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* citeproc-php |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
|
6
|
|
|
* @copyright Copyright (c) 2016 Sebastian Böttger. |
|
7
|
|
|
* @license https://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Seboettg\CiteProc\Rendering; |
|
11
|
|
|
|
|
12
|
|
|
use Seboettg\CiteProc\CiteProc; |
|
13
|
|
|
use Seboettg\CiteProc\Exception\CiteProcException; |
|
14
|
|
|
use Seboettg\CiteProc\RenderingState; |
|
15
|
|
|
use Seboettg\CiteProc\Styles\AffixesTrait; |
|
16
|
|
|
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait; |
|
17
|
|
|
use Seboettg\CiteProc\Styles\DisplayTrait; |
|
18
|
|
|
use Seboettg\CiteProc\Styles\FormattingTrait; |
|
19
|
|
|
use Seboettg\CiteProc\Styles\QuotesTrait; |
|
20
|
|
|
use Seboettg\CiteProc\Styles\TextCaseTrait; |
|
21
|
|
|
use Seboettg\CiteProc\Terms\Locator; |
|
22
|
|
|
use Seboettg\CiteProc\Util\CiteProcHelper; |
|
23
|
|
|
use Seboettg\CiteProc\Util\NumberHelper; |
|
24
|
|
|
use Seboettg\CiteProc\Util\PageHelper; |
|
25
|
|
|
use Seboettg\CiteProc\Util\StringHelper; |
|
26
|
|
|
use SimpleXMLElement; |
|
27
|
|
|
use stdClass; |
|
28
|
|
|
use function Seboettg\CiteProc\ucfirst; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class Term |
|
32
|
|
|
* |
|
33
|
|
|
* @package Seboettg\CiteProc\Node\Style |
|
34
|
|
|
* |
|
35
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class Text implements Rendering |
|
38
|
|
|
{ |
|
39
|
|
|
use FormattingTrait, |
|
|
|
|
|
|
40
|
|
|
AffixesTrait, |
|
41
|
|
|
TextCaseTrait, |
|
42
|
|
|
DisplayTrait, |
|
43
|
|
|
ConsecutivePunctuationCharacterTrait, |
|
44
|
|
|
QuotesTrait; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $toRenderType; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $toRenderTypeValue; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $form = "long"; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Text constructor. |
|
63
|
|
|
* |
|
64
|
|
|
* @param SimpleXMLElement $node |
|
65
|
|
|
*/ |
|
66
|
125 |
|
public function __construct(SimpleXMLElement $node) |
|
67
|
|
|
{ |
|
68
|
125 |
|
foreach ($node->attributes() as $attribute) { |
|
69
|
125 |
|
$name = $attribute->getName(); |
|
70
|
125 |
|
if (in_array($name, ['value', 'variable', 'macro', 'term'])) { |
|
71
|
125 |
|
$this->toRenderType = $name; |
|
72
|
125 |
|
$this->toRenderTypeValue = (string) $attribute; |
|
73
|
|
|
} |
|
74
|
125 |
|
if ($name === "form") { |
|
75
|
47 |
|
$this->form = (string) $attribute; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
125 |
|
$this->initFormattingAttributes($node); |
|
79
|
125 |
|
$this->initDisplayAttributes($node); |
|
80
|
125 |
|
$this->initTextCaseAttributes($node); |
|
81
|
125 |
|
$this->initAffixesAttributes($node); |
|
82
|
125 |
|
$this->initQuotesAttributes($node); |
|
83
|
125 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param stdClass $data |
|
87
|
|
|
* @param int|null $citationNumber |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
109 |
|
public function render($data, $citationNumber = null) |
|
91
|
|
|
{ |
|
92
|
109 |
|
$lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en'; |
|
93
|
|
|
|
|
94
|
109 |
|
$renderedText = ""; |
|
95
|
109 |
|
switch ($this->toRenderType) { |
|
96
|
109 |
|
case 'value': |
|
97
|
27 |
|
$renderedText = $this->applyTextCase($this->toRenderTypeValue, $lang); |
|
98
|
27 |
|
break; |
|
99
|
101 |
|
case 'variable': |
|
100
|
91 |
|
if ($this->toRenderTypeValue === "locator" && CiteProc::getContext()->isModeCitation()) { |
|
101
|
8 |
|
$renderedText = $this->renderLocator($data, $citationNumber); |
|
102
|
|
|
// for test sort_BibliographyCitationNumberDescending.json |
|
103
|
91 |
|
} elseif ($this->toRenderTypeValue === "citation-number") { |
|
104
|
17 |
|
$renderedText = $this->renderCitationNumber($data, $citationNumber); |
|
105
|
17 |
|
break; |
|
106
|
87 |
|
} elseif (in_array($this->toRenderTypeValue, ["page", "chapter-number", "folio"])) { |
|
107
|
38 |
|
$renderedText = !empty($data->{$this->toRenderTypeValue}) ? |
|
108
|
38 |
|
$this->renderPage($data->{$this->toRenderTypeValue}) : ''; |
|
109
|
|
|
} else { |
|
110
|
83 |
|
$renderedText = $this->renderVariable($data, $lang); |
|
111
|
|
|
} |
|
112
|
89 |
|
if (CiteProc::getContext()->getRenderingState()->getValue() === RenderingState::SUBSTITUTION) { |
|
113
|
9 |
|
unset($data->{$this->toRenderTypeValue}); |
|
114
|
|
|
} |
|
115
|
89 |
|
if (!CiteProcHelper::isUsingAffixesByMarkupExtentsion($data, $this->toRenderTypeValue)) { |
|
116
|
89 |
|
$renderedText = $this->applyAdditionalMarkupFunction($data, $renderedText); |
|
117
|
|
|
} |
|
118
|
89 |
|
break; |
|
119
|
75 |
|
case 'macro': |
|
120
|
68 |
|
$renderedText = $this->renderMacro($data); |
|
121
|
68 |
|
break; |
|
122
|
30 |
|
case 'term': |
|
123
|
30 |
|
$term = CiteProc::getContext() |
|
124
|
30 |
|
->getLocale() |
|
125
|
30 |
|
->filter("terms", $this->toRenderTypeValue, $this->form) |
|
126
|
30 |
|
->single; |
|
127
|
30 |
|
$renderedText = !empty($term) ? $this->applyTextCase($term, $lang) : ""; |
|
128
|
|
|
} |
|
129
|
109 |
|
if (!empty($renderedText)) { |
|
130
|
109 |
|
$renderedText = $this->formatRenderedText($data, $renderedText); |
|
131
|
|
|
} |
|
132
|
109 |
|
return $renderedText; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
72 |
|
public function getSource() |
|
139
|
|
|
{ |
|
140
|
72 |
|
return $this->toRenderType; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
72 |
|
public function getVariable() |
|
147
|
|
|
{ |
|
148
|
72 |
|
return $this->toRenderTypeValue; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
25 |
|
private function renderPage($page) |
|
152
|
|
|
{ |
|
153
|
25 |
|
if (preg_match(NumberHelper::PATTERN_COMMA_AMPERSAND_RANGE, $page)) { |
|
154
|
22 |
|
$page = $this->normalizeDateRange($page); |
|
155
|
22 |
|
$ranges = preg_split("/[-–]/", trim($page)); |
|
156
|
22 |
|
if (count($ranges) > 1) { |
|
157
|
22 |
|
if (!empty(CiteProc::getContext()->getGlobalOptions()) |
|
158
|
22 |
|
&& !empty(CiteProc::getContext()->getGlobalOptions()->getPageRangeFormat()) |
|
159
|
|
|
) { |
|
160
|
11 |
|
return PageHelper::processPageRangeFormats( |
|
161
|
11 |
|
$ranges, |
|
162
|
11 |
|
CiteProc::getContext()->getGlobalOptions()->getPageRangeFormat() |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
11 |
|
list($from, $to) = $ranges; |
|
166
|
11 |
|
return $from . "–" . $to; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
4 |
|
return $page; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
8 |
|
private function renderLocator($data, $citationNumber) |
|
173
|
|
|
{ |
|
174
|
8 |
|
$citationItem = CiteProc::getContext()->getCitationItemById($data->id); |
|
175
|
8 |
|
if (!empty($citationItem->label)) { |
|
176
|
4 |
|
$locatorData = new stdClass(); |
|
177
|
4 |
|
$propertyName = Locator::mapLocatorLabelToRenderVariable($citationItem->label); |
|
178
|
4 |
|
$locatorData->{$propertyName} = trim($citationItem->locator); |
|
179
|
4 |
|
$renderTypeValueTemp = $this->toRenderTypeValue; |
|
180
|
4 |
|
$this->toRenderTypeValue = $propertyName; |
|
181
|
4 |
|
$result = $this->render($locatorData, $citationNumber); |
|
182
|
4 |
|
$this->toRenderTypeValue = $renderTypeValueTemp; |
|
183
|
4 |
|
return $result; |
|
184
|
|
|
} |
|
185
|
4 |
|
return isset($citationItem->locator) ? trim($citationItem->locator) : ''; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
22 |
|
private function normalizeDateRange($page) |
|
189
|
|
|
{ |
|
190
|
22 |
|
if (preg_match("/^(\d+)\s?--?\s?(\d+)$/", trim($page), $matches)) { |
|
191
|
22 |
|
return $matches[1]."-".$matches[2]; |
|
192
|
|
|
} |
|
193
|
|
|
return $page; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param $data |
|
198
|
|
|
* @param $renderedText |
|
199
|
|
|
* @return mixed |
|
200
|
|
|
*/ |
|
201
|
91 |
|
private function applyAdditionalMarkupFunction($data, $renderedText) |
|
202
|
|
|
{ |
|
203
|
91 |
|
return CiteProcHelper::applyAdditionMarkupFunction($data, $this->toRenderTypeValue, $renderedText); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param $data |
|
208
|
|
|
* @param $lang |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
83 |
|
private function renderVariable($data, $lang) |
|
212
|
|
|
{ |
|
213
|
|
|
// check if there is an attribute with prefix short or long e.g. shortTitle or longAbstract |
|
214
|
|
|
// test case group_ShortOutputOnly.json |
|
215
|
83 |
|
$value = ""; |
|
216
|
83 |
|
if (in_array($this->form, ["short", "long"])) { |
|
217
|
83 |
|
$attrWithPrefix = $this->form . ucfirst($this->toRenderTypeValue); |
|
218
|
83 |
|
$attrWithSuffix = $this->toRenderTypeValue . "-" . $this->form; |
|
219
|
83 |
|
if (isset($data->{$attrWithPrefix}) && !empty($data->{$attrWithPrefix})) { |
|
220
|
1 |
|
$value = $data->{$attrWithPrefix}; |
|
221
|
|
|
} else { |
|
222
|
82 |
|
if (isset($data->{$attrWithSuffix}) && !empty($data->{$attrWithSuffix})) { |
|
223
|
3 |
|
$value = $data->{$attrWithSuffix}; |
|
224
|
|
|
} else { |
|
225
|
82 |
|
if (isset($data->{$this->toRenderTypeValue})) { |
|
226
|
83 |
|
$value = $data->{$this->toRenderTypeValue}; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
} else { |
|
231
|
|
|
if (!empty($data->{$this->toRenderTypeValue})) { |
|
232
|
|
|
$value = $data->{$this->toRenderTypeValue}; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
83 |
|
return $this->applyTextCase( |
|
236
|
83 |
|
StringHelper::clearApostrophes( |
|
237
|
83 |
|
htmlspecialchars($value, ENT_HTML5) |
|
238
|
|
|
), |
|
239
|
|
|
$lang |
|
240
|
|
|
); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param $data |
|
245
|
|
|
* @param $renderedText |
|
246
|
|
|
* @return string |
|
247
|
|
|
*/ |
|
248
|
109 |
|
private function formatRenderedText($data, $renderedText) |
|
249
|
|
|
{ |
|
250
|
109 |
|
$text = $this->format($renderedText); |
|
251
|
109 |
|
$res = $this->addAffixes($text); |
|
252
|
109 |
|
if (CiteProcHelper::isUsingAffixesByMarkupExtentsion($data, $this->toRenderTypeValue)) { |
|
253
|
1 |
|
$res = $this->applyAdditionalMarkupFunction($data, $res); |
|
254
|
|
|
} |
|
255
|
109 |
|
if (!empty($res)) { |
|
256
|
109 |
|
$res = $this->removeConsecutiveChars($res); |
|
257
|
|
|
} |
|
258
|
109 |
|
$res = $this->addSurroundingQuotes($res); |
|
259
|
109 |
|
return $this->wrapDisplayBlock($res); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @param $data |
|
264
|
|
|
* @param $citationNumber |
|
265
|
|
|
* @return int|mixed |
|
266
|
|
|
*/ |
|
267
|
17 |
|
private function renderCitationNumber($data, $citationNumber) |
|
268
|
|
|
{ |
|
269
|
17 |
|
$renderedText = $citationNumber + 1; |
|
270
|
17 |
|
if (!CiteProcHelper::isUsingAffixesByMarkupExtentsion($data, $this->toRenderTypeValue)) { |
|
271
|
16 |
|
$renderedText = $this->applyAdditionalMarkupFunction($data, $renderedText); |
|
272
|
|
|
} |
|
273
|
17 |
|
return $renderedText; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @param $data |
|
278
|
|
|
* @return string |
|
279
|
|
|
*/ |
|
280
|
68 |
|
private function renderMacro($data) |
|
281
|
|
|
{ |
|
282
|
68 |
|
$macro = CiteProc::getContext()->getMacro($this->toRenderTypeValue); |
|
283
|
68 |
|
if (is_null($macro)) { |
|
284
|
|
|
try { |
|
285
|
1 |
|
throw new CiteProcException("Macro \"".$this->toRenderTypeValue."\" does not exist."); |
|
286
|
1 |
|
} catch (CiteProcException $e) { |
|
287
|
1 |
|
$renderedText = ""; |
|
288
|
|
|
} |
|
289
|
|
|
} else { |
|
290
|
68 |
|
$renderedText = $macro->render($data); |
|
291
|
|
|
} |
|
292
|
68 |
|
return $renderedText; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|