|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/* |
|
4
|
|
|
* citeproc-php |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/seboettg/citeproc-php for the source repository |
|
7
|
|
|
* @copyright Copyright (c) 2020 Sebastian Böttger. |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Seboettg\CiteProc\Styles; |
|
12
|
|
|
|
|
13
|
|
|
use Seboettg\Collection\ArrayList; |
|
14
|
|
|
use SimpleXMLElement; |
|
15
|
|
|
|
|
16
|
|
|
class FormattingRenderer implements StyleRendererInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
60 |
|
public static function factory(SimpleXMLElement $node) |
|
20
|
|
|
{ |
|
21
|
|
|
$formattingAttributes = [ |
|
22
|
60 |
|
'font-style', |
|
23
|
|
|
'font-family', |
|
24
|
|
|
'font-weight', |
|
25
|
|
|
'font-variant', |
|
26
|
|
|
'text-decoration', |
|
27
|
|
|
'vertical-align' |
|
28
|
|
|
]; |
|
29
|
60 |
|
$instance = new self(); |
|
30
|
60 |
|
foreach ($node->attributes() as $attribute) { |
|
31
|
59 |
|
$name = (string) $attribute->getName(); |
|
32
|
59 |
|
$value = (string) $attribute; |
|
33
|
59 |
|
if (in_array($name, $formattingAttributes)) { |
|
34
|
59 |
|
$instance->addFormattingOption($name, $value); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
60 |
|
return $instance; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ArrayList |
|
42
|
|
|
*/ |
|
43
|
|
|
private $formattingOptions; |
|
44
|
|
|
|
|
45
|
60 |
|
public function __construct() |
|
46
|
|
|
{ |
|
47
|
60 |
|
$this->formattingOptions = new ArrayList(); |
|
48
|
60 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function addFormattingOption($option, $optionValue) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->formattingOptions->add($option, $optionValue); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
18 |
|
public function render(string $text): string |
|
56
|
|
|
{ |
|
57
|
18 |
|
if (empty($text)) { |
|
58
|
|
|
return $text; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
18 |
|
if (!empty($this->formattingOptions)) { |
|
62
|
18 |
|
$format = []; |
|
63
|
18 |
|
foreach ($this->formattingOptions as $option => $optionValue) { |
|
64
|
|
|
switch ($optionValue) { |
|
65
|
1 |
|
case "italic": |
|
66
|
1 |
|
$text = sprintf("<i>%s</i>", $text); |
|
67
|
1 |
|
break; |
|
68
|
|
|
case "bold": |
|
69
|
|
|
$text = sprintf("<b>%s</b>", $text); |
|
70
|
|
|
break; |
|
71
|
|
|
case "normal": |
|
72
|
|
|
break; |
|
73
|
|
|
default: |
|
74
|
|
|
if ($option === "vertical-align") { |
|
75
|
|
|
if ($optionValue === "sub") { |
|
76
|
|
|
$text = sprintf("<sub>%s</sub>", $text); |
|
77
|
|
|
} elseif ($optionValue === "sup") { |
|
78
|
|
|
$text = sprintf("<sup>%s</sup>", $text); |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
if ($option !== "text-decoration" || $optionValue !== "none") { |
|
82
|
1 |
|
$format[] = "$option: $optionValue"; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
18 |
|
if (!empty($format)) { |
|
88
|
|
|
$text = sprintf('<span style="%s">%s</span>', implode(";", $format), $text); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
18 |
|
return $text; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|