Completed
Push — master ( c0591e...5a76be )
by Gareth
13:56
created

PageOutput::writeLine()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
1
<?php
2
3
namespace GarethEllis\Tldr\Console\Output;
4
5
use GarethEllis\Tldr\Page\TldrPage;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
8
9
class PageOutput
10
{
11
    /**
12
     * @var OutputInterface
13
     */
14
    private $output;
15
16
    public function __construct(OutputInterface $output)
17
    {
18
        $this->output = $output;
19
    }
20
21
    public function write(TldrPage $page)
22
    {
23
        $this->output->writeln("");
24
25
        $lines = explode(PHP_EOL, $page->getContent());
26
27
        foreach ($lines as $line) {
28
            $this->writeLine($line);
29
        }
30
    }
31
32
    protected function writeLine($line)
33
    {
34
        if (empty(trim($line))) {
35
            return false;
36
        }
37
38
        if ($this->isHeading($line)) {
39
            return $this->outputHeading($line);
40
        }
41
42
        if ($this->isDescription($line)) {
43
            return $this->outputDescription($line);
44
        }
45
46
        if ($this->isHowToIntro($line)) {
47
            return $this->outputHowToIntro($line);
48
        }
49
50
        return $this->outputHowToCommand($line);
51
    }
52
53
    protected function isHeading($line)
54
    {
55
        return substr($line, 0, 2) === "# ";
56
    }
57
58
    protected function isDescription($line)
59
    {
60
        return substr($line, 0, 2) === "> ";
61
    }
62
63
    protected function isHowToIntro($line)
64
    {
65
        return substr($line, 0, 2) === "- ";
66
    }
67
68
    protected function isHowToCommand($line)
69
    {
70
        return substr($line, 0, 1) === "`" && substr($line, strlen($line) - 1, 1) === "`";
71
    }
72
73
    protected function outputHeading($line)
74
    {
75
        $this->setHeadingStyle();
76
        $this->output->writeln($this->doOutput($line, "heading"));
77
    }
78
79
    protected function outputDescription($line)
80
    {
81
        $this->setDescriptionStyle();
82
        $this->output->writeln($this->doOutput($line, "description"));
83
        $this->output->writeln("");
84
    }
85
86
    protected function outputHowToIntro($line)
87
    {
88
        $this->setHowToIntroStyle();
89
        $this->output->writeln($this->doOutput($line, "howtointro"));
90
    }
91
92
    protected function outputHowToCommand($line)
93
    {
94
        $this->setHowToCommandStyle();
95
        $this->output->writeln($this->doOutput($line, "howtocommand"));
96
        $this->output->writeln("");
97
    }
98
99
    protected function stripMarkdown($line)
100
    {
101
        if ($this->isHeading($line)) {
102
            return substr_replace($line, "", 0, 2);
103
        }
104
105
        if ($this->isDescription($line)) {
106
            return substr_replace($line, "", 0, 2);
107
        }
108
109
        if ($this->isHowToIntro($line)) {
110
            return $line;
111
        }
112
113
        $line = substr_replace($line, "", 0, 1);
114
        $line = substr_replace($line, "", strlen($line) -1, 1);
115
        $line = str_replace("{{", "<", $line);
116
        return str_replace("}}", ">", $line);
117
    }
118
119
    protected function setHeadingStyle()
120
    {
121
        $style = new OutputFormatterStyle('green', 'black', array('bold'));
122
        $this->output->getFormatter()->setStyle('heading', $style);
123
    }
124
125
    protected function setDescriptionStyle()
126
    {
127
        $style = new OutputFormatterStyle('white', 'black', array('bold'));
128
        $this->output->getFormatter()->setStyle('description', $style);
129
    }
130
131
    protected function setHowToIntroStyle()
132
    {
133
        $style = new OutputFormatterStyle('green', 'black', array('bold'));
134
        $this->output->getFormatter()->setStyle('howtointro', $style);
135
    }
136
137
    protected function setHowToCommandStyle()
138
    {
139
        $style = new OutputFormatterStyle('red', 'black', array('bold'));
140
        $this->output->getFormatter()->setStyle('howtocommand', $style);
141
    }
142
143
    protected function doOutput($line, $tag)
144
    {
145
        $indent = str_repeat(" ", $this->isHowToCommand($line) ? 4 : 2);
146
        return $indent . "<{$tag}>" . $this->stripMarkdown($line). "</{$tag}>";
147
    }
148
}
149