1 | <?php |
||
12 | class WPCom_GHF_Markdown_Parser extends MarkdownExtra_Parser { |
||
13 | |||
14 | /** |
||
15 | * Hooray somewhat arbitrary numbers that are fearful of 1.0.x. |
||
16 | */ |
||
17 | const WPCOM_GHF_MARDOWN_VERSION = '0.9.0'; |
||
18 | |||
19 | /** |
||
20 | * Use a [code] shortcode when encountering a fenced code block |
||
21 | * @var boolean |
||
22 | */ |
||
23 | public $use_code_shortcode = true; |
||
24 | |||
25 | /** |
||
26 | * Preserve shortcodes, untouched by Markdown. |
||
27 | * This requires use within a WordPress installation. |
||
28 | * @var boolean |
||
29 | */ |
||
30 | public $preserve_shortcodes = true; |
||
31 | |||
32 | /** |
||
33 | * Preserve the legacy $latex your-latex-code-here$ style |
||
34 | * LaTeX markup |
||
35 | */ |
||
36 | public $preserve_latex = true; |
||
37 | |||
38 | /** |
||
39 | * Preserve single-line <code> blocks. |
||
40 | * @var boolean |
||
41 | */ |
||
42 | public $preserve_inline_code_blocks = true; |
||
43 | |||
44 | /** |
||
45 | * Strip paragraphs from the output. This is the right default for WordPress, |
||
46 | * which generally wants to create its own paragraphs with `wpautop` |
||
47 | * @var boolean |
||
48 | */ |
||
49 | public $strip_paras = true; |
||
50 | |||
51 | // Will run through sprintf - you can supply your own syntax if you want |
||
52 | public $shortcode_start = '[code lang=%s]'; |
||
53 | public $shortcode_end = '[/code]'; |
||
54 | |||
55 | // Stores shortcodes we remove and then replace |
||
56 | protected $preserve_text_hash = array(); |
||
57 | |||
58 | /** |
||
59 | * Set environment defaults based on presence of key functions/classes. |
||
60 | */ |
||
61 | public function __construct() { |
||
69 | |||
70 | /** |
||
71 | * Overload to specify heading styles only if the hash has space(s) after it. This is actually in keeping with |
||
72 | * the documentation and eases the semantic overload of the hash character. |
||
73 | * #Will Not Produce a Heading 1 |
||
74 | * # This Will Produce a Heading 1 |
||
75 | * |
||
76 | * @param string $text Markdown text |
||
77 | * @return string HTML-transformed text |
||
78 | */ |
||
79 | public function transform( $text ) { |
||
131 | |||
132 | /** |
||
133 | * Prevents blocks like <code>__this__</code> from turning into <code><strong>this</strong></code> |
||
134 | * @param string $text Text that may need preserving |
||
135 | * @return string Text that was preserved if needed |
||
136 | */ |
||
137 | public function single_line_code_preserve( $text ) { |
||
140 | |||
141 | /** |
||
142 | * Regex callback for inline code presevation |
||
143 | * @param array $matches Regex matches |
||
144 | * @return string Hashed content for later restoration |
||
145 | */ |
||
146 | public function do_single_line_code_preserve( $matches ) { |
||
149 | |||
150 | /** |
||
151 | * Preserve code block contents by HTML encoding them. Useful before getting to KSES stripping. |
||
152 | * @param string $text Markdown/HTML content |
||
153 | * @return string Markdown/HTML content with escaped code blocks |
||
154 | */ |
||
155 | public function codeblock_preserve( $text ) { |
||
158 | |||
159 | /** |
||
160 | * Regex callback for code block preservation. |
||
161 | * @param array $matches Regex matches |
||
162 | * @return string Codeblock with escaped interior |
||
163 | */ |
||
164 | public function do_codeblock_preserve( $matches ) { |
||
171 | |||
172 | /** |
||
173 | * Restore previously preserved (i.e. escaped) code block contents. |
||
174 | * @param string $text Markdown/HTML content with escaped code blocks |
||
175 | * @return string Markdown/HTML content |
||
176 | */ |
||
177 | public function codeblock_restore( $text ) { |
||
180 | |||
181 | /** |
||
182 | * Regex callback for code block restoration (unescaping). |
||
183 | * @param array $matches Regex matches |
||
184 | * @return string Codeblock with unescaped interior |
||
185 | */ |
||
186 | public function do_codeblock_restore( $matches ) { |
||
191 | |||
192 | /** |
||
193 | * Called to preserve legacy LaTeX like $latex some-latex-text $ |
||
194 | * @param string $text Text in which to preserve LaTeX |
||
195 | * @return string Text with LaTeX replaced by a hash that will be restored later |
||
196 | */ |
||
197 | protected function latex_preserve( $text ) { |
||
211 | |||
212 | /** |
||
213 | * Called to preserve WP shortcodes from being formatted by Markdown in any way. |
||
214 | * @param string $text Text in which to preserve shortcodes |
||
215 | * @return string Text with shortcodes replaced by a hash that will be restored later |
||
216 | */ |
||
217 | protected function shortcode_preserve( $text ) { |
||
221 | |||
222 | /** |
||
223 | * Restores any text preserved by $this->hash_block() |
||
224 | * @param string $text Text that may have hashed preservation placeholders |
||
225 | * @return string Text with hashed preseravtion placeholders replaced by original text |
||
226 | */ |
||
227 | protected function do_restore( $text ) { |
||
236 | |||
237 | /** |
||
238 | * Regex callback for text preservation |
||
239 | * @param array $m Regex $matches array |
||
240 | * @return string A placeholder that will later be replaced by the original text |
||
241 | */ |
||
242 | protected function _doRemoveText( $m ) { |
||
245 | |||
246 | /** |
||
247 | * Call this to store a text block for later restoration. |
||
248 | * @param string $text Text to preserve for later |
||
249 | * @return string Placeholder that will be swapped out later for the original text |
||
250 | */ |
||
251 | protected function hash_block( $text ) { |
||
257 | |||
258 | /** |
||
259 | * Less glamorous than the Keymaker |
||
260 | * @param string $hash An md5 hash |
||
261 | * @return string A placeholder hash |
||
262 | */ |
||
263 | protected function hash_maker( $hash ) { |
||
266 | |||
267 | /** |
||
268 | * Remove bare <p> elements. <p>s with attributes will be preserved. |
||
269 | * @param string $text HTML content |
||
270 | * @return string <p>-less content |
||
271 | */ |
||
272 | public function unp( $text ) { |
||
275 | |||
276 | /** |
||
277 | * A regex of all shortcodes currently registered by the current |
||
278 | * WordPress installation |
||
279 | * @uses get_shortcode_regex() |
||
280 | * @return string A regex for grabbing shortcodes. |
||
281 | */ |
||
282 | protected function get_shortcode_regex() { |
||
290 | |||
291 | /** |
||
292 | * Since we escape unspaced #Headings, put things back later. |
||
293 | * @param string $text text with a leading escaped hash |
||
294 | * @return string text with leading hashes unescaped |
||
295 | */ |
||
296 | protected function restore_leading_hash( $text ) { |
||
299 | |||
300 | /** |
||
301 | * Overload to support ```-fenced code blocks for pre-Markdown Extra 1.2.8 |
||
302 | * https://help.github.com/articles/github-flavored-markdown#fenced-code-blocks |
||
303 | */ |
||
304 | public function doFencedCodeBlocks( $text ) { |
||
349 | |||
350 | /** |
||
351 | * Callback for pre-processing start of line hashes to slyly escape headings that don't |
||
352 | * have a leading space |
||
353 | * @param array $m preg_match matches |
||
354 | * @return string possibly escaped start of line hash |
||
355 | */ |
||
356 | public function _doEscapeForHashWithoutSpacing( $m ) { |
||
361 | |||
362 | /** |
||
363 | * Overload to support Viper's [code] shortcode. Because awesome. |
||
364 | */ |
||
365 | public function _doFencedCodeBlocks_callback( $matches ) { |
||
388 | |||
389 | } |
||
390 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.