1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if (! function_exists('Markdown')) { |
4
|
|
|
require_once('markdown_helper.php'); |
5
|
|
|
} |
6
|
|
|
define('MARKDOWNEXTRAEXTENDED_VERSION', "0.3"); |
7
|
|
|
|
8
|
|
|
function MarkdownExtended($text, $default_classes = array()) |
9
|
|
|
{ |
10
|
|
|
$parser = new MarkdownExtraExtended_Parser($default_classes); |
11
|
|
|
return $parser->transform($text); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
class MarkdownExtraExtended_Parser extends MarkdownExtra_Parser |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
# Tags that are always treated as block tags: |
18
|
|
|
var $block_tags_re = 'figure|figcaption|p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend'; |
19
|
|
|
|
20
|
|
|
var $default_classes; |
21
|
|
|
|
22
|
|
|
function __construct($default_classes = array()) |
23
|
|
|
{ |
24
|
|
|
$default_classes = $default_classes; |
25
|
|
|
|
26
|
|
|
$this->block_gamut += array( |
27
|
|
|
"doFencedFigures" => 7, |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
parent::__construct(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
function transform($text) |
34
|
|
|
{ |
35
|
|
|
$text = parent::transform($text); |
36
|
|
|
return $text; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function doHardBreaks($text) |
40
|
|
|
{ |
41
|
|
|
# Do hard breaks: |
42
|
|
|
# EXTENDED: changed to allow breaks without two spaces and just one new line |
43
|
|
|
# original code /* return preg_replace_callback('/ {2,}\n/', */ |
44
|
|
|
return preg_replace_callback( |
45
|
|
|
'/ *\n/', |
46
|
|
|
array(&$this, '_doHardBreaks_callback'), |
47
|
|
|
$text |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
function doBlockQuotes($text) |
52
|
|
|
{ |
53
|
|
|
$text = preg_replace_callback( |
54
|
|
|
'/ |
55
|
|
|
(?>^[ ]*>[ ]? |
56
|
|
|
(?:\((.+?)\))? |
57
|
|
|
[ ]*(.+\n(?:.+\n)*) |
58
|
|
|
)+ |
59
|
|
|
/xm', |
60
|
|
|
array(&$this, '_doBlockQuotes_callback'), |
61
|
|
|
$text |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
return $text; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function _doBlockQuotes_callback($matches) |
68
|
|
|
{ |
69
|
|
|
$cite = $matches[1]; |
70
|
|
|
$bq = '> ' . $matches[2]; |
71
|
|
|
# trim one level of quoting - trim whitespace-only lines |
72
|
|
|
$bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq); |
73
|
|
|
$bq = $this->runBlockGamut($bq); # recurse |
74
|
|
|
|
75
|
|
|
$bq = preg_replace('/^/m', " ", $bq); |
76
|
|
|
# These leading spaces cause problem with <pre> content, |
77
|
|
|
# so we need to fix that: |
78
|
|
|
$bq = preg_replace_callback( |
79
|
|
|
'{(\s*<pre>.+?</pre>)}sx', |
80
|
|
|
array(&$this, '_doBlockQuotes_callback2'), |
81
|
|
|
$bq |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$res = "<blockquote"; |
85
|
|
|
$res .= empty($cite) ? ">" : " cite=\"$cite\">"; |
86
|
|
|
$res .= "\n$bq\n</blockquote>"; |
87
|
|
|
return "\n" . $this->hashBlock($res) . "\n\n"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
function doFencedCodeBlocks($text) |
91
|
|
|
{ |
92
|
|
|
$less_than_tab = $this->tab_width; |
93
|
|
|
|
94
|
|
|
$text = preg_replace_callback( |
95
|
|
|
'{ |
96
|
|
|
(?:\n|\A) |
97
|
|
|
# 1: Opening marker |
98
|
|
|
( |
99
|
|
|
~{3,}|`{3,} # Marker: three tilde or more. |
100
|
|
|
) |
101
|
|
|
|
102
|
|
|
[ ]?(\w+)?(?:,[ ]?(\d+))?[ ]* \n # Whitespace and newline following marker. |
103
|
|
|
|
104
|
|
|
# 3: Content |
105
|
|
|
( |
106
|
|
|
(?> |
107
|
|
|
(?!\1 [ ]* \n) # Not a closing marker. |
108
|
|
|
.*\n+ |
109
|
|
|
)+ |
110
|
|
|
) |
111
|
|
|
|
112
|
|
|
# Closing marker. |
113
|
|
|
\1 [ ]* \n |
114
|
|
|
}xm', |
115
|
|
|
array(&$this, '_doFencedCodeBlocks_callback'), |
116
|
|
|
$text |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return $text; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
function _doFencedCodeBlocks_callback($matches) |
123
|
|
|
{ |
124
|
|
|
$codeblock = $matches[4]; |
125
|
|
|
$codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES); |
126
|
|
|
$codeblock = preg_replace_callback( |
127
|
|
|
'/^\n+/', |
128
|
|
|
array(&$this, '_doFencedCodeBlocks_newlines'), |
129
|
|
|
$codeblock |
130
|
|
|
); |
131
|
|
|
//$codeblock = "<pre><code>$codeblock</code></pre>"; |
|
|
|
|
132
|
|
|
//$cb = "<pre><code"; |
|
|
|
|
133
|
|
|
$cb = empty($matches[3]) ? "<pre><code" : "<pre class=\"linenums:$matches[3]\"><code"; |
134
|
|
|
$cb .= empty($matches[2]) ? ">" : " class=\"language-$matches[2]\">"; |
135
|
|
|
$cb .= "$codeblock</code></pre>"; |
136
|
|
|
return "\n\n" . $this->hashBlock($cb) . "\n\n"; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function doFencedFigures($text) |
140
|
|
|
{ |
141
|
|
|
$text = preg_replace_callback( |
142
|
|
|
'{ |
143
|
|
|
(?:\n|\A) |
144
|
|
|
# 1: Opening marker |
145
|
|
|
( |
146
|
|
|
={3,} # Marker: equal sign. |
147
|
|
|
) |
148
|
|
|
|
149
|
|
|
[ ]?(?:\[([^\]]+)\])?[ ]* \n # Whitespace and newline following marker. |
150
|
|
|
|
151
|
|
|
# 3: Content |
152
|
|
|
( |
153
|
|
|
(?> |
154
|
|
|
(?!\1 [ ]?(?:\[([^\]]+)\])?[ ]* \n) # Not a closing marker. |
155
|
|
|
.*\n+ |
156
|
|
|
)+ |
157
|
|
|
) |
158
|
|
|
|
159
|
|
|
# Closing marker. |
160
|
|
|
\1 [ ]?(?:\[([^\]]+)\])?[ ]* \n |
161
|
|
|
}xm', |
162
|
|
|
array(&$this, '_doFencedFigures_callback'), |
163
|
|
|
$text |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
return $text; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
function _doFencedFigures_callback($matches) |
170
|
|
|
{ |
171
|
|
|
# get figcaption |
172
|
|
|
$topcaption = empty($matches[2]) ? null : $this->runBlockGamut($matches[2]); |
173
|
|
|
$bottomcaption = empty($matches[5]) ? null : $this->runBlockGamut($matches[5]); |
174
|
|
|
$figure = $matches[3]; |
175
|
|
|
$figure = $this->runBlockGamut($figure); # recurse |
176
|
|
|
|
177
|
|
|
$figure = preg_replace('/^/m', " ", $figure); |
178
|
|
|
# These leading spaces cause problem with <pre> content, |
179
|
|
|
# so we need to fix that - reuse blockqoute code to handle this: |
180
|
|
|
$figure = preg_replace_callback( |
181
|
|
|
'{(\s*<pre>.+?</pre>)}sx', |
182
|
|
|
array(&$this, '_doBlockQuotes_callback2'), |
183
|
|
|
$figure |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$res = "<figure>"; |
187
|
|
|
if (!empty($topcaption)) { |
188
|
|
|
$res .= "\n<figcaption>$topcaption</figcaption>"; |
189
|
|
|
} |
190
|
|
|
$res .= "\n$figure\n"; |
191
|
|
|
if (!empty($bottomcaption) && empty($topcaption)) { |
192
|
|
|
$res .= "<figcaption>$bottomcaption</figcaption>"; |
193
|
|
|
} |
194
|
|
|
$res .= "</figure>"; |
195
|
|
|
return "\n" . $this->hashBlock($res) . "\n\n"; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
?> |
200
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.