1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Changelog keeper |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/chkipper |
6
|
|
|
* @package chkipper |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\chkipper\lib\renderers; |
12
|
|
|
|
13
|
|
|
use hiqdev\chkipper\lib\Commit; |
14
|
|
|
use hiqdev\chkipper\lib\History; |
15
|
|
|
use hiqdev\chkipper\lib\Note; |
16
|
|
|
use hiqdev\chkipper\lib\Tag; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Markdown history renderer. |
20
|
|
|
* |
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class MarkdownRenderer extends AbstractRenderer |
24
|
|
|
{ |
25
|
|
|
public $indent = ' '; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
2 |
|
public function render(History $history) |
31
|
|
|
{ |
32
|
2 |
|
$this->setHistory($history); |
33
|
|
|
|
34
|
2 |
|
return $this->renderSparse([ |
35
|
2 |
|
$this->renderHeaders(), |
36
|
2 |
|
$this->renderTags(), |
37
|
2 |
|
$this->renderLinks(), |
38
|
2 |
|
]); |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
public function renderHeaders() |
42
|
|
|
{ |
43
|
2 |
|
return $this->renderText($this->getHistory()->getHeaders()); |
44
|
1 |
|
} |
45
|
|
|
|
46
|
2 |
|
public function renderTags() |
47
|
|
|
{ |
48
|
2 |
|
return $this->renderObjects('renderTag', $this->getHistory()->getTags(), true); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
public function renderLinks($links = null) |
52
|
|
|
{ |
53
|
2 |
|
if ($links === null) { |
54
|
2 |
|
$links = $this->getHistory()->getLinks(); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
2 |
|
return $this->renderObjects('renderLink', $links); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function renderObjects($method, $objects, $sparse = false) |
61
|
|
|
{ |
62
|
2 |
|
$res = []; |
63
|
2 |
|
foreach ($objects as $key => $value) { |
64
|
2 |
|
$res[$key] = call_user_func([$this, $method], $value, $key); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
return $this->renderText($res, $sparse); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function renderSparse(array $lines) |
71
|
|
|
{ |
72
|
2 |
|
$res = rtrim(implode("\n", $lines)); |
73
|
|
|
|
74
|
2 |
|
return $res ? $res . "\n" : ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
public function renderText(array $lines, $sparse = false) |
78
|
|
|
{ |
79
|
2 |
|
if (!$sparse) { |
80
|
2 |
|
foreach ($lines as &$line) { |
81
|
2 |
|
$line = rtrim($line); |
82
|
2 |
|
} |
83
|
2 |
|
$lines = array_filter($lines); |
84
|
2 |
|
} |
85
|
|
|
|
86
|
2 |
|
return $this->renderSparse($lines); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
public function renderLink($href, $link) |
90
|
|
|
{ |
91
|
2 |
|
return "[$link]: $href"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function renderHeader($header) |
95
|
|
|
{ |
96
|
|
|
return $header; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function renderTag(Tag $tag) |
100
|
|
|
{ |
101
|
2 |
|
return $this->renderSparse([ |
102
|
2 |
|
$this->renderTagHead($tag), |
103
|
2 |
|
$this->renderObjects('renderNote', $tag->getNotes()), |
104
|
2 |
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
public function renderTagHead(Tag $tag) |
108
|
|
|
{ |
109
|
2 |
|
$res = '## [' . $tag->getName() . ']'; |
110
|
2 |
|
if ($tag->getDate()) { |
|
|
|
|
111
|
2 |
|
$res .= ' - ' . $tag->getDate(); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
2 |
|
return $res . "\n"; |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
public function renderNote(Note $note) |
118
|
|
|
{ |
119
|
2 |
|
return $this->renderText([ |
120
|
2 |
|
$this->renderNoteHead($note), |
121
|
2 |
|
$this->renderObjects('renderCommit', $note->getCommits()), |
122
|
2 |
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
public function renderNoteHead(Note $note) |
126
|
|
|
{ |
127
|
2 |
|
return $note->getNote() ? '- ' . $note->getNote() : ''; |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
public function renderCommit(Commit $commit) |
131
|
|
|
{ |
132
|
2 |
|
return $this->renderText([ |
133
|
2 |
|
$this->renderCommitHead($commit), |
134
|
2 |
|
$this->renderText($commit->getComments()), |
135
|
2 |
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
public function renderCommitHead(Commit $commit) |
139
|
|
|
{ |
140
|
2 |
|
return $this->indent . '- [' . $commit->getHash() . '] ' . $commit->getLabel(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: