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