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
|
|
|
class RstBuilder { |
30
|
|
|
|
31
|
|
|
private $indentLevel = 0; |
32
|
|
|
/** @var string */ |
33
|
|
|
protected $content = '.. rst-class:: phpdoctorst' . PHP_EOL . PHP_EOL ; |
34
|
|
|
|
35
|
|
|
public function getContent() { |
36
|
|
|
return $this->content; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function escape($text) { |
40
|
|
|
// escape all common reStructuredText control chars |
41
|
|
|
$text = preg_replace("/[\'\`\*\_\{\}\[\]\(\)\>\#\+\-\.\\\!]/", '\\\\$0', $text); |
42
|
|
|
return $text; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function indent() { |
46
|
|
|
$this->indentLevel++; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function unindent() { |
50
|
|
|
$this->indentLevel--; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function addFieldList($key, $value) { |
54
|
|
|
$this->addLine(':'.self::escape($key).':'); |
55
|
|
|
$this->addIndentMultiline(1, $value, true); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function addH1($text) { |
59
|
|
|
$this->addLine($text); |
60
|
|
|
$this->addLine(str_repeat('=', strlen((string)$text)))->addLine(); |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function addH2($text) { |
65
|
|
|
$this->addLine($text); |
66
|
|
|
$this->addLine(str_repeat('-', strlen((string)$text)))->addLine(); |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function addLine($text = '') { |
71
|
|
|
$this->add(str_repeat("\t", $this->indentLevel) . $text . PHP_EOL); |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addIndentLine($indent, $text) { |
76
|
|
|
$this->addLine(str_repeat("\t", $indent) . $text); |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function addIndentMultiline($indent, $text, $blockIndent = false) { |
81
|
|
|
// parse <code> / {@link seeMethod} / {@link https://} |
82
|
|
|
$lines = preg_split('/$\R?^/m', $text); |
83
|
|
|
$i = 0; |
84
|
|
|
foreach ($lines as $line) { |
85
|
|
|
if ($blockIndent && $i === 1) { |
86
|
|
|
$indent++; |
87
|
|
|
} |
88
|
|
|
$this->addIndentLine($this->indentLevel+$indent, $line); |
89
|
|
|
$i++; |
90
|
|
|
} |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function add($text) { |
95
|
|
|
$this->content .= $text; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |