Passed
Push — master ( 005105...a5b1eb )
by Julius
01:50
created

RstBuilder::addH3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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, true)->unindent();
64
        return $this;
65
    }
66
67
    public function addH1($text) {
68
        $this->addLine($text);
69
        $this->addLine(str_repeat('=', strlen((string)$text)))->addLine();
70
        return $this;
71
    }
72
73
    public function addH2($text) {
74
        $this->addLine($text);
75
        $this->addLine(str_repeat('-', strlen((string)$text)))->addLine();
76
        return $this;
77
    }
78
79
    public function addH3($text) {
80
        $this->addLine($text);
81
        $this->addLine(str_repeat('~', strlen((string)$text)))->addLine();
82
        return $this;
83
    }
84
85
    public function addLine($text = '') {
86
        $this->add(str_repeat("\t", $this->indentLevel) . $text . PHP_EOL);
87
        return $this;
88
    }
89
90
    public function addMultiline($text = '', $blockIndent = false) {
91
        $lines = preg_split('/$\R?^/m', $text);
92
        $i = 0;
93
        foreach ($lines as $line) {
94
            if ($blockIndent && $i++ === 1) {
95
                $this->indent();
96
            }
97
            $this->addLine($line);
98
        }
99
        if ($blockIndent && count($lines) > 1) {
100
            $this->unindent();
101
        }
102
        return $this;
103
    }
104
105
    public function add($text) {
106
        $this->content .= $text;
107
        return $this;
108
    }
109
110
111
}