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