1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Julius Härtl <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Julius Härtl <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace JuliusHaertl\PHPDocToRst\Builder; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use phpDocumentor\Reflection\Fqsen; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Helper class to build reStructuredText files |
31
|
|
|
* |
32
|
|
|
* @package JuliusHaertl\PHPDocToRst\Builder |
33
|
|
|
*/ |
34
|
|
|
class RstBuilder { |
35
|
|
|
|
36
|
|
|
private $indentLevel = 0; |
37
|
|
|
/** @var string */ |
38
|
|
|
protected $content = '.. rst-class:: phpdoctorst' . PHP_EOL . PHP_EOL ; |
39
|
|
|
|
40
|
|
|
public function getContent() { |
41
|
|
|
return $this->content; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function escape($text) { |
45
|
|
|
// escape all common reStructuredText control chars |
46
|
|
|
$text = preg_replace("/[\'\`\*\_\{\}\[\]\(\)\|\>\#\+\-\.\\\!]/", '\\\\$0', $text); |
47
|
|
|
return $text; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function indent() { |
51
|
|
|
$this->indentLevel++; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function unindent() { |
56
|
|
|
$this->indentLevel--; |
57
|
|
|
$this->addLine(); |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function addFieldList($key, $value) { |
62
|
|
|
$this->addLine(':'.self::escape($key).':'); |
63
|
|
|
$this->indent()->addMultiline($value, false)->unindent(); |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $text |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function addH1(string $text) { |
72
|
|
|
$this->addLine($text); |
73
|
|
|
$this->addLine(str_repeat('=', strlen((string)$text)))->addLine(); |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function addH2($text) { |
78
|
|
|
$this->addLine($text); |
79
|
|
|
$this->addLine(str_repeat('-', strlen((string)$text)))->addLine(); |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function addH3($text) { |
84
|
|
|
$this->addLine($text); |
85
|
|
|
$this->addLine(str_repeat('~', strlen((string)$text)))->addLine(); |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function addLine($text = '') { |
90
|
|
|
$this->add(str_repeat("\t", $this->indentLevel) . $text . PHP_EOL); |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function addMultiline($text = '', $blockIndent = false) { |
95
|
|
|
$lines = preg_split('/$\R?^/m', $text); |
96
|
|
|
$i = 0; |
97
|
|
|
foreach ($lines as $line) { |
98
|
|
|
if ($blockIndent && $i++ === 1) { |
99
|
|
|
$this->indent(); |
100
|
|
|
} |
101
|
|
|
$this->addLine($line); |
102
|
|
|
} |
103
|
|
|
if ($blockIndent && count($lines) > 1) { |
|
|
|
|
104
|
|
|
$this->unindent(); |
105
|
|
|
} |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function addMultilineWithoutRendering($text) { |
110
|
|
|
$lines = preg_split('/$\R?^/m', $text); |
111
|
|
|
foreach ($lines as $line) { |
112
|
|
|
$this->addLine('| ' . $line); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function add($text) { |
119
|
|
|
$this->content .= $text; |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
} |