14 * |
14 * |
15 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
15 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
17 */ |
17 */ |
18 |
18 |
|
19 $plugins->attachHook('render_wikiformat_pre', 'mediafier_draw_toc($text);'); |
19 $plugins->attachHook('render_wikiformat_post', 'mediafy($result);'); |
20 $plugins->attachHook('render_wikiformat_post', 'mediafy($result);'); |
20 $plugins->attachHook('compile_template', 'mediafier_add_headers();'); |
21 $plugins->attachHook('compile_template', 'mediafier_add_headers();'); |
21 $plugins->attachHook('html_attribute_whitelist', '$whitelist["ref"] = array(); $whitelist["references"] = array("/");'); |
22 $plugins->attachHook('html_attribute_whitelist', '$whitelist["ref"] = array(); $whitelist["references"] = array("/");'); |
22 |
23 |
23 function mediafy(&$text) |
24 function mediafy(&$text) |
24 { |
25 { |
25 global $db, $session, $paths, $template, $plugins; // Common objects |
26 global $db, $session, $paths, $template, $plugins; // Common objects |
26 mediafy_highlight_search_words($text); |
27 mediafy_highlight_search_words($text); |
27 mediafy_process_references($text); |
28 mediafy_process_references($text); |
|
29 } |
|
30 |
|
31 function mediafier_draw_toc(&$text) |
|
32 { |
|
33 if ( strstr($text, '__NOTOC__') ) |
|
34 return true; |
|
35 |
|
36 if ( !preg_match_all('/^\s*([=]{1,6})([^\r\n]+)\\1\s*$/m', $text, $matches) ) |
|
37 return true; |
|
38 |
|
39 $heading_map = array(); |
|
40 foreach ( $matches[1] as $heading ) |
|
41 { |
|
42 $heading_map[] = strlen($heading); |
|
43 } |
|
44 |
|
45 if ( count($heading_map) < 4 && !strstr($text, '__TOC__') ) |
|
46 return true; |
|
47 |
|
48 $prev = 0; |
|
49 $levels = 0; |
|
50 $treenum = array(); |
|
51 $toc = ''; |
|
52 foreach ( $heading_map as $i => $head ) |
|
53 { |
|
54 if ( $head > $prev ) |
|
55 { |
|
56 $treenum[] = 0; |
|
57 $levels++; |
|
58 $toc .= '<dl>'; |
|
59 } |
|
60 else if ( $head < $prev ) |
|
61 { |
|
62 if ( $levels > 1 ) |
|
63 { |
|
64 $toc .= '</dl>'; |
|
65 $levels--; |
|
66 unset($treenum[count($treenum)-1]); |
|
67 } |
|
68 } |
|
69 $treenum[count($treenum)-1]++; |
|
70 if ( $i > 0 ) |
|
71 $toc .= '</dd>'; |
|
72 $toc .= '<dd><a href="#toc' . ($i + 1) . '">' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . '</a>'; |
|
73 $prev = $head; |
|
74 } |
|
75 while ( $levels > 0 ) |
|
76 { |
|
77 $toc .= '</dd></dl>'; |
|
78 $levels--; |
|
79 } |
|
80 $toc_body = "<nowiki><div class=\"toc mdg-comment\"> |
|
81 <dl><dd><b>Contents</b> <small>[<a href=\"#\" onclick=\"collapseTOC(this); return false;\">hide</a>]</small></dd></dl> |
|
82 <div>$toc</div> |
|
83 </div></nowiki>"; |
|
84 |
|
85 if ( strstr($text, '__TOC__') ) |
|
86 $text = str_replace_once('__TOC__', $toc_body, $text); |
|
87 else if ( ($text = preg_replace('/^=/', "$toc_body\n\n=", $text)) === $text ) |
|
88 $text = str_replace_once("\n=", "\n$toc_body\n=", $text); |
28 } |
89 } |
29 |
90 |
30 function mediafier_add_headers() |
91 function mediafier_add_headers() |
31 { |
92 { |
32 global $db, $session, $paths, $template, $plugins; // Common objects |
93 global $db, $session, $paths, $template, $plugins; // Common objects |
35 .refbak { background-color: #E0E0FF; font-weight: bold; } |
96 .refbak { background-color: #E0E0FF; font-weight: bold; } |
36 .references { font-size: smaller; } |
97 .references { font-size: smaller; } |
37 </style>"); |
98 </style>"); |
38 $ref_script = <<<EOF |
99 $ref_script = <<<EOF |
39 <enano:no-opt> |
100 <enano:no-opt> |
|
101 <style type="text/css"> |
|
102 div.toc { |
|
103 display: table; |
|
104 max-width: 70%; |
|
105 padding: 0.7em 1.7em 0.7em 0.7em; |
|
106 margin: 10px 0 0 0; |
|
107 } |
|
108 div.toc dl { |
|
109 margin: 2px 0; |
|
110 } |
|
111 div.toc dd { |
|
112 margin-left: 1em; |
|
113 } |
|
114 </style> |
40 <script type="text/javascript"> |
115 <script type="text/javascript"> |
41 // <![CDATA[ |
116 // <![CDATA[ |
42 function refsOff() |
117 function refsOff() |
43 { |
118 { |
44 var divs = getElementsByClassName(document, '*', 'refbottom'); |
119 var divs = getElementsByClassName(document, '*', 'refbottom'); |
45 for ( var i in divs ) |
120 for ( var i in divs ) |
46 { |
121 { |
47 $(divs[i]).rmClass('refbak'); |
122 \$dynano(divs[i]).rmClass('refbak'); |
48 } |
123 } |
49 divs = getElementsByClassName(document, '*', 'reftop'); |
124 divs = getElementsByClassName(document, '*', 'reftop'); |
50 for ( var i in divs ) |
125 for ( var i in divs ) |
51 { |
126 { |
52 $(divs[i]).rmClass('refbak'); |
127 \$dynano(divs[i]).rmClass('refbak'); |
53 } |
128 } |
54 } |
129 } |
55 function refToBottom(id) |
130 function refToBottom(id) |
56 { |
131 { |
57 refsOff(); |
132 refsOff(); |
58 $('ref_'+id+'_b').addClass('refbak'); |
133 \$dynano('ref_'+id+'_b').addClass('refbak'); |
59 } |
134 } |
60 function refToTop(id) |
135 function refToTop(id) |
61 { |
136 { |
62 refsOff(); |
137 refsOff(); |
63 $('cite_'+id).addClass('refbak'); |
138 \$dynano('cite_'+id).addClass('refbak'); |
|
139 } |
|
140 function collapseTOC(el) |
|
141 { |
|
142 var toc_inner = el.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('div')[0]; |
|
143 if ( toc_inner.style.display == 'none' ) |
|
144 { |
|
145 el.innerHTML = 'hide'; |
|
146 toc_inner.style.display = 'block'; |
|
147 } |
|
148 else |
|
149 { |
|
150 el.innerHTML = 'show'; |
|
151 toc_inner.style.display = 'none'; |
|
152 } |
64 } |
153 } |
65 // ]]> |
154 // ]]> |
66 </script> |
155 </script> |
67 </enano:no-opt> |
156 </enano:no-opt> |
68 |
157 |