forked from bharley/mw-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdown.php
185 lines (154 loc) · 5.08 KB
/
Markdown.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
<?php
/**
* Markdown
* Markdown MediaWiki parser extension.
*
* This is a (rather simple) MediaWiki extension that uses erusev's Parsedown.
*
* @author Blake Harley <[email protected]>
* @version 0.2
* @copyright Copyright (C) 2014 Blake Harley
* @license MIT License
* @link https://github.com/bharley/mw-markdown
*/
// Prevent global hijackingengine
if (!defined('MEDIAWIKI')) die();
// Credits
$wgExtensionCredits['parserhook'][] = array(
'name' => 'Markdown',
'description' => 'Uses Markdown for wiki parsing',
'version' => '0.2',
'author' => 'Blake Harley',
'url' => 'https://github.com/bharley/mw-markdown',
'license-name' => 'MIT',
);
// Available config options
$wgMarkdownDefaultOn = true;
$wgMarkdownToggleFormat = '{{%s}}';
$wgMarkdownWikiLinks = true;
$wgMarkdownExtra = false;
$wgMarkdownHighlight = false;
$wgMarkdownHighlightJs = null;
$wgMarkdownHighlightCss = null;
// Hook
$wgHooks['ParserBeforeInternalParse'][] = 'MarkdownExtension::onParserBeforeInternalParse';
$wgHooks['BeforePageDisplay'][] = 'MarkdownExtension::onBeforePageDisplay';
// Load Parsedown (https://github.com/erusev/parsedown)
require_once('Parsedown.php');
if (file_exists(__DIR__ . '/ParsedownExtra.php'))
{
// Optionally, load Parsedown Extra (https://github.com/erusev/parsedown-extra)
require_once('ParsedownExtra.php');
}
/**
* Wrap the hook function in a class so we don't pollute the global namespace.
*/
class MarkdownExtension
{
public static function onBeforePageDisplay(OutputPage &$out)
{
global $wgMarkdownHighlight;
global $wgMarkdownHighlightJs;
global $wgMarkdownHighlightCss;
if ($wgMarkdownHighlight)
{
$out->addScriptFile($wgMarkdownHighlightJs);
$out->addStyle($wgMarkdownHighlightCss);
$out->addInlineScript('hljs.initHighlightingOnLoad();');
}
return true;
}
/**
* If everything checks out, this hook will parse the given text for Markdown.
*
* @param Parser $parser MediaWiki's parser
* @param string $text The text to parse
*/
public static function onParserBeforeInternalParse($parser, &$text)
{
global $wgMarkdownDefaultOn;
if (static::shouldParseText($text))
{
if (!$wgMarkdownDefaultOn)
{
$text = substr($text, strlen(static::getSearchString()));
}
$text = static::parseMarkdown($parser, $text);
return false;
}
else
{
if ($wgMarkdownDefaultOn)
{
$text = substr($text, strlen(static::getSearchString()));
}
return true;
}
}
/**
* Converts the given text into markdown.
*
* @param Parser $parser MediaWiki's parser
* @param string $text The text to parse
* @return string The parsed text
*/
protected static function parseMarkdown($parser, $text)
{
global $wgMarkdownWikiLinks;
$html = $text;
// Post-Markdown wiki parsing
$html = $parser->replaceVariables($html);
$html = $parser->doDoubleUnderscore($html);
// Parse Markdown
$html = static::getParser()->text($html);
// Attempt to use Wiki-style links if turned on
if ($wgMarkdownWikiLinks)
{
$html = preg_replace_callback('#<a href="(.+?)"(?: title=".+?")?>(.+?)</a>#i', function ($matches) {
list($match, $url, $text) = $matches;
$external = (bool) preg_match('#^[a-z]+://#i', $url);
return sprintf($external ? '[%s %s]' : '[[%s|%s]]', $url, $text);
}, $html);
$html = $parser->replaceInternalLinks($html);
$html = $parser->replaceExternalLinks($html);
$parser->replaceLinkHolders($html);
}
// Post-Markdown wiki parsing
$html = $parser->formatHeadings($html, $text);
$html = $parser->doMagicLinks($html);
return $html;
}
/**
* @param string $text The text to check over for our tags if necessary
* @return bool Whether or not to parse the given text
*/
protected static function shouldParseText($text)
{
global $wgMarkdownDefaultOn;
$search = static::getSearchString();
return (($wgMarkdownDefaultOn && strpos($text, $search) !== 0)
|| (!$wgMarkdownDefaultOn && strpos($text, $search) === 0));
}
/**
* @return string The search string
*/
protected static function getSearchString()
{
global $wgMarkdownDefaultOn;
global $wgMarkdownToggleFormat;
return sprintf($wgMarkdownToggleFormat, $wgMarkdownDefaultOn ? 'WIKI' : 'MARKDOWN');
}
/**
* @return Parsedown
*/
protected static function getParser()
{
static $parser;
global $wgMarkdownExtra;
if (!$parser)
{
$parser = $wgMarkdownExtra ? new ParsedownExtra : new Parsedown;
}
return $parser;
}
}