Open Journal Systems  3.3.0
Text.php
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 
11 
25 use SimpleXMLElement;
26 use stdClass;
27 
35 class Text implements Rendering
36 {
37  use FormattingTrait,
38  AffixesTrait,
39  TextCaseTrait,
40  DisplayTrait,
41  ConsecutivePunctuationCharacterTrait,
42  QuotesTrait;
43 
47  private $toRenderType;
48 
52  private $toRenderTypeValue;
53 
57  private $form = "long";
58 
64  public function __construct(SimpleXMLElement $node)
65  {
66  foreach ($node->attributes() as $attribute) {
67  $name = $attribute->getName();
68  if (in_array($name, ['value', 'variable', 'macro', 'term'])) {
69  $this->toRenderType = $name;
70  $this->toRenderTypeValue = (string) $attribute;
71  }
72  if ($name === "form") {
73  $this->form = (string) $attribute;
74  }
75  }
76  $this->initFormattingAttributes($node);
77  $this->initDisplayAttributes($node);
78  $this->initTextCaseAttributes($node);
79  $this->initAffixesAttributes($node);
80  $this->initQuotesAttributes($node);
81  }
82 
88  public function render($data, $citationNumber = null)
89  {
90  $lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en';
91 
92  $renderedText = "";
93  switch ($this->toRenderType) {
94  case 'value':
95  $renderedText = $this->applyTextCase($this->toRenderTypeValue, $lang);
96  break;
97  case 'variable':
98  if ($this->toRenderTypeValue === "citation-number") {
99  $renderedText = $this->renderCitationNumber($data, $citationNumber);
100  break;
101  } elseif ($this->toRenderTypeValue === "page") {
102  $renderedText = $this->renderPage($data);
103  // for test sort_BibliographyCitationNumberDescending.json
104  } else {
105  $renderedText = $this->renderVariable($data, $lang);
106  }
107  if (CiteProc::getContext()->getRenderingState()->getValue() === RenderingState::SUBSTITUTION) {
108  unset($data->{$this->toRenderTypeValue});
109  }
110  $renderedText = $this->applyAdditionalMarkupFunction($data, $renderedText);
111  break;
112  case 'macro':
113  $renderedText = $this->renderMacro($data);
114  break;
115  case 'term':
116  $term = CiteProc::getContext()
117  ->getLocale()
118  ->filter("terms", $this->toRenderTypeValue, $this->form)
119  ->single;
120  $renderedText = !empty($term) ? $this->applyTextCase($term, $lang) : "";
121  }
122  if (!empty($renderedText)) {
123  $renderedText = $this->formatRenderedText($renderedText);
124  }
125  return $renderedText;
126  }
127 
131  public function getSource()
132  {
133  return $this->toRenderType;
134  }
135 
139  public function getVariable()
140  {
141  return $this->toRenderTypeValue;
142  }
143 
144  private function renderPage($data)
145  {
146  if (empty($data->page)) {
147  return "";
148  }
149 
150  if (preg_match(NumberHelper::PATTERN_COMMA_AMPERSAND_RANGE, $data->page)) {
151  $data->page = $this->normalizeDateRange($data->page);
152  $ranges = preg_split("/[-–]/", trim($data->page));
153  if (count($ranges) > 1) {
154  if (!empty(CiteProc::getContext()->getGlobalOptions())
155  && !empty(CiteProc::getContext()->getGlobalOptions()->getPageRangeFormat())
156  ) {
158  $ranges,
159  CiteProc::getContext()->getGlobalOptions()->getPageRangeFormat()
160  );
161  }
162  list($from, $to) = $ranges;
163  return "$from-$to";
164  }
165  }
166  return $data->page;
167  }
168 
169  private function normalizeDateRange($page)
170  {
171  if (preg_match("/^(\d+)--(\d+)$/", trim($page), $matches)) {
172  return $matches[1]."-".$matches[2];
173  }
174  return $page;
175  }
176 
182  private function applyAdditionalMarkupFunction($data, $renderedText)
183  {
184  return CiteProcHelper::applyAdditionMarkupFunction($data, $this->toRenderTypeValue, $renderedText);
185  }
186 
192  private function renderVariable($data, $lang)
193  {
194  // check if there is an attribute with prefix short or long e.g. shortTitle or longAbstract
195  // test case group_ShortOutputOnly.json
196  $renderedText = "";
197  if (in_array($this->form, ["short", "long"])) {
198  $attrWithPrefix = $this->form.ucfirst($this->toRenderTypeValue);
199  $attrWithSuffix = $this->toRenderTypeValue."-".$this->form;
200  if (isset($data->{$attrWithPrefix}) && !empty($data->{$attrWithPrefix})) {
201  $renderedText = $this->applyTextCase(
203  str_replace(" & ", " &#38; ", $data->{$attrWithPrefix})
204  ),
205  $lang
206  );
207  } else {
208  if (isset($data->{$attrWithSuffix}) && !empty($data->{$attrWithSuffix})) {
209  $renderedText = $this->applyTextCase(
210  StringHelper::clearApostrophes(
211  str_replace(" & ", " &#38; ", $data->{$attrWithSuffix})
212  ),
213  $lang
214  );
215  } else {
216  if (isset($data->{$this->toRenderTypeValue})) {
217  $renderedText = $this->applyTextCase(
218  StringHelper::clearApostrophes(
219  str_replace(" & ", " &#38; ", $data->{$this->toRenderTypeValue})
220  ),
221  $lang
222  );
223  }
224  }
225  }
226  } else {
227  if (!empty($data->{$this->toRenderTypeValue})) {
228  $renderedText = $this->applyTextCase(
229  StringHelper::clearApostrophes(
230  str_replace(" & ", " &#38; ", $data->{$this->toRenderTypeValue})
231  ),
232  $lang
233  );
234  }
235  }
236  return $renderedText;
237  }
238 
243  private function formatRenderedText($renderedText)
244  {
245  $text = $this->format($renderedText);
246  $res = $this->addAffixes($text);
247  if (!empty($res)) {
248  $res = $this->removeConsecutiveChars($res);
249  }
250  $res = $this->addSurroundingQuotes($res);
251  return $this->wrapDisplayBlock($res);
252  }
253 
259  private function renderCitationNumber($data, $citationNumber)
260  {
261  $renderedText = $citationNumber + 1;
262  $renderedText = $this->applyAdditionalMarkupFunction($data, $renderedText);
263  return $renderedText;
264  }
265 
270  private function renderMacro($data)
271  {
272  $macro = CiteProc::getContext()->getMacro($this->toRenderTypeValue);
273  if (is_null($macro)) {
274  try {
275  throw new CiteProcException("Macro \"".$this->toRenderTypeValue."\" does not exist.");
276  } catch (CiteProcException $e) {
277  $renderedText = "";
278  }
279  } else {
280  $renderedText = $macro->render($data);
281  }
282  return $renderedText;
283  }
284 }
Seboettg\CiteProc\Util\NumberHelper\PATTERN_COMMA_AMPERSAND_RANGE
const PATTERN_COMMA_AMPERSAND_RANGE
Definition: NumberHelper.php:31
Seboettg\CiteProc\Rendering\Text\getVariable
getVariable()
Definition: Text.php:148
Seboettg\CiteProc\Styles\DisplayTrait
trait DisplayTrait
Definition: DisplayTrait.php:20
Seboettg\CiteProc\Util\PageHelper\processPageRangeFormats
static processPageRangeFormats($ranges, $pageRangeFormat)
Definition: PageHelper.php:26
Seboettg\CiteProc\Styles\AffixesTrait
trait AffixesTrait
Definition: AffixesTrait.php:21
Seboettg\CiteProc\Styles\FormattingTrait
trait FormattingTrait
Definition: FormattingTrait.php:21
Seboettg\CiteProc\Rendering\Text\__construct
__construct(SimpleXMLElement $node)
Definition: Text.php:73
Seboettg\CiteProc\Rendering\Text\render
render($data, $citationNumber=null)
Definition: Text.php:97
Seboettg\CiteProc\Styles\addSurroundingQuotes
addSurroundingQuotes($text)
Definition: QuotesTrait.php:48
Seboettg\CiteProc\Styles\wrapDisplayBlock
wrapDisplayBlock($text)
Definition: DisplayTrait.php:58
Seboettg\CiteProc\Util\NumberHelper
Definition: NumberHelper.php:20
Seboettg\CiteProc\Rendering\Text\getSource
getSource()
Definition: Text.php:140
Seboettg\CiteProc\Util\PageHelper
Definition: PageHelper.php:19
Seboettg\CiteProc\Styles\removeConsecutiveChars
removeConsecutiveChars($string)
Definition: ConsecutivePunctuationCharacterTrait.php:87
Seboettg\CiteProc\Util\StringHelper
Definition: StringHelper.php:22
Seboettg\CiteProc\CiteProc
Definition: CiteProc.php:32
Seboettg\CiteProc\Styles\TextCaseTrait
trait TextCaseTrait
Definition: TextCaseTrait.php:23
Seboettg\CiteProc\Exception\CiteProcException
Definition: CiteProcException.php:20
Seboettg\CiteProc\Util\CiteProcHelper\applyAdditionMarkupFunction
static applyAdditionMarkupFunction($dataItem, $valueToRender, $renderedText)
Definition: CiteProcHelper.php:26
Seboettg\CiteProc\RenderingState
Definition: RenderingState.php:27
Seboettg\CiteProc\Rendering
Seboettg\CiteProc\Styles\addAffixes
addAffixes($text)
Definition: AffixesTrait.php:75
Seboettg\CiteProc\CiteProc\getContext
static getContext()
Definition: CiteProc.php:45
Seboettg\CiteProc\Util\StringHelper\clearApostrophes
static clearApostrophes($string)
Definition: StringHelper.php:237
Seboettg\CiteProc\RenderingState\SUBSTITUTION
const SUBSTITUTION
Definition: RenderingState.php:33
Seboettg\CiteProc\Styles\applyTextCase
applyTextCase($text, $lang="en")
Definition: TextCaseTrait.php:49
Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait
trait ConsecutivePunctuationCharacterTrait
Definition: ConsecutivePunctuationCharacterTrait.php:18
Seboettg\CiteProc\Util\CiteProcHelper
Definition: CiteProcHelper.php:15
Seboettg\CiteProc\Rendering\Text
Definition: Text.php:35
Seboettg\CiteProc\Styles\QuotesTrait
trait QuotesTrait
Definition: QuotesTrait.php:27
Seboettg\CiteProc\Rendering\Rendering
Definition: Rendering.php:22
Seboettg\CiteProc\Styles\format
format($text)
Definition: FormattingTrait.php:81