|
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
|
|
|
use Seboettg\CiteProc\CiteProc; |
|
12
|
|
|
use Seboettg\CiteProc\Exception\CiteProcException; |
|
13
|
|
|
use Seboettg\CiteProc\Styles\AffixesTrait; |
|
14
|
|
|
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait; |
|
15
|
|
|
use Seboettg\CiteProc\Styles\DisplayTrait; |
|
16
|
|
|
use Seboettg\CiteProc\Styles\FormattingTrait; |
|
17
|
|
|
use Seboettg\CiteProc\Styles\QuotesTrait; |
|
18
|
|
|
use Seboettg\CiteProc\Styles\TextCaseTrait; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Term |
|
23
|
|
|
* @package Seboettg\CiteProc\Node\Style |
|
24
|
|
|
* |
|
25
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class Text implements RenderingInterface |
|
28
|
|
|
{ |
|
29
|
|
|
use FormattingTrait, |
|
30
|
|
|
AffixesTrait, |
|
31
|
|
|
TextCaseTrait, |
|
32
|
|
|
DisplayTrait, |
|
33
|
|
|
ConsecutivePunctuationCharacterTrait; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $toRenderType; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $toRenderTypeValue; |
|
44
|
|
|
|
|
45
|
|
|
private $form = "long"; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct(\SimpleXMLElement $node) |
|
48
|
|
|
{ |
|
49
|
|
|
foreach ($node->attributes() as $attribute) { |
|
50
|
|
|
$name = $attribute->getName(); |
|
51
|
|
|
if (in_array($name, ['value', 'variable', 'macro', 'term'])) { |
|
52
|
|
|
$this->toRenderType = $name; |
|
53
|
|
|
$this->toRenderTypeValue = (string) $attribute; |
|
54
|
|
|
} |
|
55
|
|
|
if ($name === "form") { |
|
56
|
|
|
$this->form = (string) $attribute; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
$this->initFormattingAttributes($node); |
|
60
|
|
|
$this->initDisplayAttributes($node); |
|
61
|
|
|
$this->initTextCaseAttributes($node); |
|
62
|
|
|
$this->initAffixesAttributes($node); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param \stdClass $data |
|
67
|
|
|
* @param int|null $citationNumber |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function render($data, $citationNumber = null) |
|
71
|
|
|
{ |
|
72
|
|
|
$lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en'; |
|
73
|
|
|
|
|
74
|
|
|
$renderedText = ""; |
|
75
|
|
|
switch ($this->toRenderType) { |
|
76
|
|
|
case 'value': |
|
77
|
|
|
$renderedText = $this->applyTextCase($this->toRenderTypeValue, $lang); |
|
78
|
|
|
break; |
|
79
|
|
|
case 'variable': |
|
80
|
|
|
// check if there is an attribute with prefix short or long e.g. shortTitle or longAbstract |
|
81
|
|
|
// test case group_ShortOutputOnly.json |
|
82
|
|
|
if (in_array($this->form, ["short", "long"])) { |
|
83
|
|
|
$attrWithPrefix = $this->form . ucfirst($this->toRenderTypeValue); |
|
84
|
|
|
if (isset($data->{$attrWithPrefix}) && !empty($data->{$attrWithPrefix})) { |
|
85
|
|
|
$renderedText = $this->applyTextCase($data->{$attrWithPrefix}, $lang); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
if (!empty($data->{$this->toRenderTypeValue})) { |
|
89
|
|
|
$renderedText = $this->applyTextCase($data->{$this->toRenderTypeValue}, $lang); |
|
90
|
|
|
} |
|
91
|
|
|
// for test sort_BibliographyCitationNumberDescending.json |
|
92
|
|
|
if ($this->toRenderTypeValue === "citation-number" && !is_null($citationNumber)) { |
|
93
|
|
|
$renderedText = strval($citationNumber + 1); |
|
94
|
|
|
} |
|
95
|
|
|
break; |
|
96
|
|
|
case 'macro': |
|
97
|
|
|
$macro = CiteProc::getContext()->getMacro($this->toRenderTypeValue); |
|
98
|
|
|
if (is_null($macro)) { |
|
99
|
|
|
try { |
|
100
|
|
|
throw new CiteProcException("Macro \"" . $this->toRenderTypeValue . "\" does not exist."); |
|
101
|
|
|
} catch (CiteProcException $e) { |
|
102
|
|
|
$renderedText = ""; |
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
|
|
$renderedText = $macro->render($data); |
|
106
|
|
|
} |
|
107
|
|
|
break; |
|
108
|
|
|
case 'term': |
|
109
|
|
|
$term = CiteProc::getContext()->getLocale()->filter("terms", $this->toRenderTypeValue, $this->form)->single; |
|
110
|
|
|
$renderedText = !empty($term) ? $this->applyTextCase($term, $lang) : ""; |
|
111
|
|
|
} |
|
112
|
|
|
if (!empty($renderedText)) { |
|
113
|
|
|
//$renderedText = $this->applyTextCase($renderedText); |
|
|
|
|
|
|
114
|
|
|
$text = $this->format($renderedText); |
|
115
|
|
|
$res = $this->addAffixes($text, $this->quotes); |
|
|
|
|
|
|
116
|
|
|
if (!empty($res)) { |
|
117
|
|
|
$res = $this->removeConsecutiveChars($res); |
|
118
|
|
|
} |
|
119
|
|
|
return $res; |
|
120
|
|
|
} |
|
121
|
|
|
return ""; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function rendersVariable() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->toRenderType === "variable" || $this->toRenderType === "macro"; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getSource() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->toRenderTypeValue; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getVariable() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->toRenderTypeValue; |
|
137
|
|
|
} |
|
138
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.