forked from matt-h/dokuwiki-plugin-markdownextra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.php
148 lines (134 loc) · 5.11 KB
/
syntax.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
<?php
/**
* PHP Markdown Extra plugin for DokuWiki.
*
* @license GPL 3 (http://www.gnu.org/licenses/gpl.html) - NOTE: PHP Markdown
* Extra is licensed under the BSD license. See License.text for details.
* @version 1.03 - 24.11.2012 - PHP Markdown Extra 1.2.5 included.
* @author Joonas Pulakka <[email protected]>, Jiang Le <[email protected]>
*/
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once (DOKU_PLUGIN . 'syntax.php');
require_once (DOKU_PLUGIN . 'markdownextra/markdown.php');
class syntax_plugin_markdownextra extends DokuWiki_Syntax_Plugin {
function getType() {
return 'protected';
}
function getPType() {
return 'block';
}
function getSort() {
return 69;
}
function connectTo($mode) {
$this->Lexer->addEntryPattern('<markdown>(?=.*</markdown>)', $mode, 'plugin_markdownextra');
}
function postConnect() {
$this->Lexer->addExitPattern('</markdown>', 'plugin_markdownextra');
}
function handle($match, $state, $pos, Doku_Handler $handler) {
switch ($state) {
case DOKU_LEXER_ENTER : return array($state, '');
case DOKU_LEXER_UNMATCHED : return array($state, Markdown($match));
case DOKU_LEXER_EXIT : return array($state, '');
}
return array($state,'');
}
function render($mode, Doku_Renderer $renderer, $data) {
//dbg('function render($mode, &$renderer, $data)-->'.' mode = '.$mode.' data = '.$data);
//dbg($data);
if ($mode == 'xhtml') {
list($state,$match) = $data;
switch ($state) {
case DOKU_LEXER_ENTER : break;
case DOKU_LEXER_UNMATCHED :
$match = $this->_toc($renderer, $match);
$renderer->doc .= $match;
break;
case DOKU_LEXER_EXIT : break;
}
return true;
}else if ($mode == 'metadata') {
//dbg('function render($mode, &$renderer, $data)-->'.' mode = '.$mode.' data = '.$data);
//dbg($data);
list($state,$match) = $data;
switch ($state) {
case DOKU_LEXER_ENTER : break;
case DOKU_LEXER_UNMATCHED :
if (!$renderer->meta['title']){
$renderer->meta['title'] = $this->_markdown_header($match);
}
$this->_toc($renderer, $match);
$internallinks = $this->_internallinks($match);
#dbg($internallinks);
if (count($internallinks)>0){
foreach($internallinks as $internallink)
{
$renderer->internallink($internallink);
}
}
break;
case DOKU_LEXER_EXIT : break;
}
return true;
} else {
return false;
}
}
function _markdown_header($text)
{
$doc = new DOMDocument('1.0','UTF-8');
//dbg($doc);
$meta = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
$doc->loadHTML($meta.$text);
//dbg($doc->saveHTML());
if ($nodes = $doc->getElementsByTagName('h1')){
return $nodes->item(0)->nodeValue;
}
return false;
}
function _internallinks($text)
{
$links = array();
if ( ! $text ) return $links;
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadHTML($text);
if ($nodes = $doc->getElementsByTagName('a')){
foreach($nodes as $atag)
{
$href = $atag->getAttribute('href');
if (!preg_match('/^(https{0,1}:\/\/|ftp:\/\/|mailto:)/i',$href)){
$links[] = $href;
}
}
}
return $links;
}
function _toc(&$renderer, $text)
{
$doc = new DOMDocument('1.0','UTF-8');
//dbg($doc);
$meta = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
$doc->loadHTML($meta.$text);
if ($nodes = $doc->getElementsByTagName("*")){
foreach($nodes as $node)
{
if (preg_match('/h([1-7])/',$node->tagName,$match))
{
#dbg($node);
$node->setAttribute('class', 'sectionedit'.$match[1]);
$hid = $renderer->_headerToLink($node->nodeValue,'true');
$node->setAttribute('id',$hid);
$renderer->toc_additem($hid, $node->nodeValue, $match[1]);
}
}
}
//remove outer tags of content
$html = $doc->saveHTML();
$html = str_replace('<!DOCTYPE html>','',$html);
$html = preg_replace('/.+<body>/', '', $html);
$html = preg_replace('@</body>.*</html>@','', $html);
return $html;
}
}