-
Notifications
You must be signed in to change notification settings - Fork 31
/
Ciconia.php
197 lines (166 loc) · 4.44 KB
/
Ciconia.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Ciconia;
use Ciconia\Common\Collection;
use Ciconia\Common\Text;
use Ciconia\Event\EmitterAwareInterface;
use Ciconia\Extension\Core;
use Ciconia\Extension\ExtensionInterface;
use Ciconia\Renderer\RendererAwareInterface;
use Ciconia\Renderer\HtmlRenderer;
use Ciconia\Renderer\RendererInterface;
/**
* Ciconia - The New Markdown Parser
*
* This is just the central point to manage `renderer` and `extensions`.
*
* The `Core` extensions are based on Markdown.pl
* The `Gfm` extensions are based on Github Flavored Markdown
*
* @author Kazuyuki Hayashi <hayashi@valnur.net>
*/
class Ciconia
{
const VERSION = '1.1.x-dev';
/**
* @var RendererInterface
*/
private $renderer;
/**
* @var Collection|ExtensionInterface[]
*/
private $extensions;
/**
* @param RendererInterface $renderer
*/
public function __construct(RendererInterface $renderer = null)
{
$this->extensions = new Collection();
$this->renderer = $renderer;
if (is_null($this->renderer)) {
$this->setRenderer($this->getDefaultRenderer());
}
$this->addExtensions($this->getDefaultExtensions());
}
/**
* @param string $text
* @param array $options
*
* @return string
*/
public function render($text, array $options = array())
{
$text = new Text($text);
$markdown = new Markdown($this->renderer, $text, $options);
$this->registerExtensions($markdown);
$markdown->emit('initialize', array($text));
$markdown->emit('block', array($text));
$markdown->emit('finalize', array($text));
return (string) $text;
}
/**
* @param \Ciconia\Renderer\RendererInterface $renderer
*
* @return Ciconia
*/
public function setRenderer($renderer)
{
$this->renderer = $renderer;
return $this;
}
/**
* @return \Ciconia\Renderer\RendererInterface
*/
public function getRenderer()
{
return $this->renderer;
}
/**
* @param ExtensionInterface $extension
*
* @return Ciconia
*/
public function addExtension(ExtensionInterface $extension)
{
$this->extensions->set($extension->getName(), $extension);
return $this;
}
/**
* @param ExtensionInterface[] $extensions
*
* @return Ciconia
*/
public function addExtensions(array $extensions)
{
foreach ($extensions as $extension) {
$this->addExtension($extension);
}
return $this;
}
/**
* @param string|object $extension
*
* @return Ciconia
*/
public function removeExtension($extension)
{
if ($extension instanceof ExtensionInterface) {
$extension = $extension->getName();
}
$this->extensions->remove($extension);
return $this;
}
/**
* @param string|object $extension
*
* @return boolean
*/
public function hasExtension($extension)
{
if ($extension instanceof ExtensionInterface) {
$extension = $extension->getName();
}
return $this->extensions->exists($extension);
}
/**
* @return RendererInterface
*/
protected function getDefaultRenderer()
{
return new HtmlRenderer();
}
/**
* @return ExtensionInterface[]
*/
protected function getDefaultExtensions()
{
return array(
new Core\WhitespaceExtension(),
new Core\HeaderExtension(),
new Core\ParagraphExtension(),
new Core\HtmlBlockExtension(),
new Core\LinkExtension(),
new Core\HorizontalRuleExtension(),
new Core\ListExtension(),
new Core\CodeExtension(),
new Core\BlockQuoteExtension(),
new Core\ImageExtension(),
new Core\InlineStyleExtension(),
new Core\EscaperExtension()
);
}
/**
* @param Markdown $markdown
*/
protected function registerExtensions(Markdown $markdown)
{
foreach ($this->extensions as $extension) {
if ($extension instanceof RendererAwareInterface) {
$extension->setRenderer($this->renderer);
}
if ($extension instanceof EmitterAwareInterface) {
$extension->setEmitter($markdown);
}
$extension->register($markdown);
}
}
}