1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
7
|
|
|
* |
8
|
|
|
* @license GNU General Public License version 3 or later. |
9
|
|
|
* For the full copyright and license information, please read the |
10
|
|
|
* LICENSE.txt file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Kitodo\DbDocs; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Simple utility to write .rst (reStructuredText). |
17
|
|
|
* |
18
|
|
|
* @author Kajetan Dvoracek <[email protected]> |
19
|
|
|
* @package TYPO3 |
20
|
|
|
* @subpackage dlf |
21
|
|
|
* @access public |
22
|
|
|
*/ |
23
|
|
|
class RstSection |
24
|
|
|
{ |
25
|
|
|
/** @var string */ |
26
|
|
|
protected $header = ''; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $text = ''; |
30
|
|
|
|
31
|
|
|
/** @var RstSection[] */ |
32
|
|
|
protected $subsections = []; |
33
|
|
|
|
34
|
|
|
public static function format(string $text, array $format = []) |
35
|
|
|
{ |
36
|
|
|
if (!empty($text)) { |
37
|
|
|
if ($format['bold'] ?? false) { |
38
|
|
|
$text = '**' . $text . '**'; |
39
|
|
|
} elseif ($format['italic'] ?? false) { |
40
|
|
|
$text = '*' . $text . '*'; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $text; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function paragraphs(array $paragraphs) |
48
|
|
|
{ |
49
|
|
|
$paragraphs = array_values(array_filter($paragraphs, function ($entry) { |
50
|
|
|
return !empty($entry); |
51
|
|
|
})); |
52
|
|
|
|
53
|
|
|
return implode("\n\n", $paragraphs); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function subsection() |
57
|
|
|
{ |
58
|
|
|
$section = new static(); |
59
|
|
|
$this->subsections[] = $section; |
60
|
|
|
return $section; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setHeader(string $text) |
64
|
|
|
{ |
65
|
|
|
$this->header = $text; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function addText(string $text) |
69
|
|
|
{ |
70
|
|
|
if (!empty($text)) { |
71
|
|
|
$this->text .= $text . "\n\n"; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addTable(array $rows, array $headerRows) |
76
|
|
|
{ |
77
|
|
|
$numHeaderRows = count($headerRows); |
78
|
|
|
|
79
|
|
|
$tableRst = <<<RST |
80
|
|
|
.. t3-field-list-table:: |
81
|
|
|
:header-rows: $numHeaderRows |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
RST; |
85
|
|
|
|
86
|
|
|
// Pattern for a row: |
87
|
|
|
// |
88
|
|
|
// - :key1: value |
89
|
|
|
// :key2: another |
90
|
|
|
// value that may |
91
|
|
|
// span multiple lines |
92
|
|
|
// |
93
|
|
|
foreach (array_merge($headerRows, $rows) as $row) { |
94
|
|
|
$entry = ''; |
95
|
|
|
foreach ($row as $key => $value) { |
96
|
|
|
$valueLines = explode("\n", $value); |
97
|
|
|
$numLines = count($valueLines); |
98
|
|
|
for ($i = 0; $i < $numLines; $i++) { |
99
|
|
|
$prefix = $i === 0 |
100
|
|
|
? ' :' . $key . ':' |
101
|
|
|
: ''; |
102
|
|
|
|
103
|
|
|
$entry .= str_pad($prefix, 32) . trim($valueLines[$i]) . "\n"; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!empty($entry)) { |
108
|
|
|
$entry[3] = '-'; |
109
|
|
|
$entry .= "\n"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$tableRst .= $entry; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->addText($tableRst); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function render(int $level = 0) |
119
|
|
|
{ |
120
|
|
|
$result = ''; |
121
|
|
|
|
122
|
|
|
$result .= $this->renderHeader($level); |
123
|
|
|
$result .= $this->text; |
124
|
|
|
|
125
|
|
|
foreach ($this->subsections as $section) { |
126
|
|
|
$result .= $section->render($level + 1); |
127
|
|
|
$result .= "\n"; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function renderHeader(int $level) |
134
|
|
|
{ |
135
|
|
|
$result = ''; |
136
|
|
|
|
137
|
|
|
$headerChar = ['=', '=', '-', '~', '"'][$level]; |
138
|
|
|
$headerSep = str_repeat($headerChar, mb_strlen($this->header)); |
139
|
|
|
|
140
|
|
|
if ($level === 0) { |
141
|
|
|
$result .= $headerSep . "\n"; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$result .= $this->header . "\n" . $headerSep . "\n\n"; |
145
|
|
|
|
146
|
|
|
return $result; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|