1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kitodo\Dlf\Common\DbDocs; |
4
|
|
|
|
5
|
|
|
use Kitodo\Dlf\Common\Helper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Simple utility to write .rst (reStructuredText). |
9
|
|
|
*/ |
10
|
|
|
class RstSection |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
protected $header = ''; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $text = ''; |
17
|
|
|
|
18
|
|
|
/** @var RstSection[] */ |
19
|
|
|
protected $subsections = []; |
20
|
|
|
|
21
|
|
|
public static function format(string $text, array $format = []) |
22
|
|
|
{ |
23
|
|
|
if (!empty($text)) { |
24
|
|
|
if ($format['bold'] ?? false) { |
25
|
|
|
$text = '**' . $text . '**'; |
26
|
|
|
} elseif ($format['italic'] ?? false) { |
27
|
|
|
$text = '*' . $text . '*'; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $text; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function paragraphs(array $paragraphs) |
35
|
|
|
{ |
36
|
|
|
return implode("\n\n", Helper::withoutEmpty($paragraphs)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function subsection() |
40
|
|
|
{ |
41
|
|
|
$section = new static(); |
42
|
|
|
$this->subsections[] = $section; |
43
|
|
|
return $section; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setHeader(string $text) |
47
|
|
|
{ |
48
|
|
|
$this->header = $text; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function addText(string $text) |
52
|
|
|
{ |
53
|
|
|
if (!empty($text)) { |
54
|
|
|
$this->text .= $text . "\n\n"; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function addTable(array $rows, array $headerRows) |
59
|
|
|
{ |
60
|
|
|
$numHeaderRows = count($headerRows); |
61
|
|
|
|
62
|
|
|
$tableRst = <<<RST |
63
|
|
|
.. t3-field-list-table:: |
64
|
|
|
:header-rows: $numHeaderRows |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
RST; |
68
|
|
|
|
69
|
|
|
// Pattern for a row: |
70
|
|
|
// |
71
|
|
|
// - :key1: value |
72
|
|
|
// :key2: another |
73
|
|
|
// value that may |
74
|
|
|
// span multiple lines |
75
|
|
|
// |
76
|
|
|
foreach (array_merge($headerRows, $rows) as $row) { |
77
|
|
|
$entry = ''; |
78
|
|
|
foreach ($row as $key => $value) { |
79
|
|
|
$valueLines = explode("\n", $value); |
80
|
|
|
$numLines = count($valueLines); |
81
|
|
|
for ($i = 0; $i < $numLines; $i++) { |
82
|
|
|
$prefix = $i === 0 |
83
|
|
|
? ' :' . $key . ':' |
84
|
|
|
: ''; |
85
|
|
|
|
86
|
|
|
$entry .= str_pad($prefix, 32) . trim($valueLines[$i]) . "\n"; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!empty($entry)) { |
91
|
|
|
$entry[3] = '-'; |
92
|
|
|
$entry .= "\n"; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$tableRst .= $entry; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->addText($tableRst); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function render(int $level = 0) |
102
|
|
|
{ |
103
|
|
|
$result = ''; |
104
|
|
|
|
105
|
|
|
$result .= $this->renderHeader($level); |
106
|
|
|
$result .= $this->text; |
107
|
|
|
|
108
|
|
|
foreach ($this->subsections as $section) { |
109
|
|
|
$result .= $section->render($level + 1); |
110
|
|
|
$result .= "\n"; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $result; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function renderHeader(int $level) |
117
|
|
|
{ |
118
|
|
|
$result = ''; |
119
|
|
|
|
120
|
|
|
$headerChar = ['=', '=', '-', '~', '"'][$level]; |
121
|
|
|
$headerSep = str_repeat($headerChar, mb_strlen($this->header)); |
122
|
|
|
|
123
|
|
|
if ($level === 0) { |
124
|
|
|
$result .= $headerSep . "\n"; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$result .= $this->header . "\n" . $headerSep . "\n\n"; |
128
|
|
|
|
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|