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
|
|
|
$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) |
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 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
|
|
|
|