1 | <?php |
||
9 | class PageOutput |
||
10 | { |
||
11 | /** |
||
12 | * @var OutputInterface |
||
13 | */ |
||
14 | private $output; |
||
15 | |||
16 | public function __construct(OutputInterface $output) |
||
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 | $this->output->writeln(""); |
||
32 | } |
||
33 | |||
34 | protected function writeLine($line) |
||
35 | { |
||
36 | if (empty(trim($line))) { |
||
37 | return false; |
||
38 | } |
||
39 | |||
40 | if ($this->isHeading($line)) { |
||
41 | return $this->outputHeading($line); |
||
42 | } |
||
43 | |||
44 | if ($this->isDescription($line)) { |
||
45 | return $this->outputDescription($line); |
||
46 | } |
||
47 | |||
48 | if ($this->isHowToIntro($line)) { |
||
49 | return $this->outputHowToIntro($line); |
||
50 | } |
||
51 | |||
52 | return $this->outputHowToCommand($line); |
||
53 | } |
||
54 | |||
55 | protected function isHeading($line) |
||
56 | { |
||
57 | return substr($line, 0, 2) === "# "; |
||
58 | } |
||
59 | |||
60 | protected function isDescription($line) |
||
61 | { |
||
62 | return substr($line, 0, 2) === "> "; |
||
63 | } |
||
64 | |||
65 | protected function isHowToIntro($line) |
||
66 | { |
||
67 | return substr($line, 0, 2) === "- "; |
||
68 | } |
||
69 | |||
70 | protected function isHowToCommand($line) |
||
71 | { |
||
72 | return substr($line, 0, 1) === "`" && substr($line, strlen($line) - 1, 1) === "`"; |
||
73 | } |
||
74 | |||
75 | protected function outputHeading($line) |
||
76 | { |
||
77 | $this->setHeadingStyle(); |
||
78 | $this->output->writeln($this->getOutputString($line, "heading")); |
||
79 | } |
||
80 | |||
81 | protected function outputDescription($line) |
||
82 | { |
||
83 | $this->setDescriptionStyle(); |
||
84 | $this->output->writeln($this->getOutputString($line, "description", true)); |
||
85 | } |
||
86 | |||
87 | protected function outputHowToIntro($line) |
||
88 | { |
||
89 | $this->setHowToIntroStyle(); |
||
90 | $this->output->writeln($this->getOutputString($line, "howtointro")); |
||
91 | } |
||
92 | |||
93 | protected function outputHowToCommand($line) |
||
94 | { |
||
95 | $this->setHowToCommandStyle(); |
||
96 | $this->output->writeln($this->getOutputString($line, "howtocommand", true)); |
||
97 | } |
||
98 | |||
99 | protected function stripMarkdown($line) |
||
118 | |||
119 | protected function setHeadingStyle() |
||
124 | |||
125 | protected function setDescriptionStyle() |
||
130 | |||
131 | protected function setHowToIntroStyle() |
||
136 | |||
137 | protected function setHowToCommandStyle() |
||
142 | |||
143 | protected function getOutputString(String $line, String $tag, Bool $newLineAfter = false): String |
||
144 | { |
||
145 | $indent = str_repeat(" ", $this->isHowToCommand($line) ? 4 : 2); |
||
146 | return $indent . "<{$tag}>" . $this->stripMarkdown($line). "</{$tag}>" |
||
147 | . ($newLineAfter ? PHP_EOL : null); |
||
148 | } |
||
149 | } |
||
150 |