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