/
pandoc_filter.module
329 lines (288 loc) · 10.5 KB
/
pandoc_filter.module
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* @file
* Module file for pandoc_filter.
*/
function pandoc_filter_config_load() {
if (!class_exists('Pandoc', true)) {
if (module_exists('composer_manager')
&& function_exists('composer_manager_register_autoloader')) {
composer_manager_register_autoloader();
}
elseif (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
else {
drupal_set_message('Could not find the Pandoc Class.
Please use composer to grab the Pandoc-PHP library
or use composer_manager to help you.', 'error');
return FALSE;
}
}
return TRUE;
}
/**
* @defgroup pandoc_filter Pandoc Filter
* @{
* Pandoc filter for drupal.
*/
/**
* Implements hook_menu().
*/
function pandoc_filter_menu() {
$items['admin/config/content'] = array(
'title' => 'Pandoc Filter',
'page callback' => '_pandoc_filter_information',
'access callback' => TRUE,
);
return $items;
}
/**
* Implements hook_help().
*/
function pandoc_filter_help($path, $arg) {
switch ($path) {
case 'admin/help#pandoc_filter':
return _pandoc_filter_information();
}
}
/**
* Simply returns a little bit of information about the example.
*/
function _pandoc_filter_information() {
return t("<p>The Pandoc drupal filter replaces supported markup formats to html
to be displayed in content pages</p>",
array('!link' => l(t('admin/config/content/formats'), 'admin/config/content/formats'))
);
}
/**
* Implements hook_filter_info().
*
*/
function pandoc_filter_filter_info() {
$filters['pandoc_filter'] = array(
'title' => t('Pandoc Filter'),
'description' => t('Replaces a specified markup with html'),
'process callback' => '_pandoc_filter_filter_process',
'default settings' => array(
'input_format' => 'mediawiki',
'base_header_level' => '2',
'columns' => '72',
'toc_depth' => '3',
'highlight_style' => 'pygments',
'css' => NULL,
'use_html5' => FALSE,
'use_parse_raw' => FALSE,
'use_smart' => FALSE,
'use_normalize' => FALSE,
'use_preserve_tabs' => FALSE,
'use_toc' => FALSE,
'use_no_highlight' => FALSE,
'use_section_divs' => FALSE,
),
'settings callback' => '_pandoc_filter_filter_settings',
'tips callback' => '_pandoc_filter_filter_tips',
);
return $filters;
}
/**
* Settings callback for Pandoc filter.
*
*/
function _pandoc_filter_filter_settings($form, $form_state, $filter, $format, $defaults) {
$settings['input_format'] = array(
'#type' => 'select',
'#title' => t('Markup type'),
'#default_value' => isset($filter->settings['input_format'])
? $filter->settings['input_format'] : $defaults['input_format'],
'#description' => t('The markup type to be used in the filter'),
'#options' => array(
'native' => t('Native Haskell'),
'json' => t('JSON native AST'),
'markdown' => t('Pandoc Markdown'),
'markdown_strict' => t('Strict Pandoc Markdown'),
'markdown_phpextra' => t('Php Markdown'),
'markdown_github' => t('Github extended Markdown'),
'markdown_mmd' => t('MultiMarkdown'),
'rst' => t('reStructuredText'),
'mediawiki' => t('MediaWiki'),
'docbook' => t('DocBook'),
'textile' => t('Textile'),
'latex' => t('LaTeX'),
),
);
$settings['use_html5'] = array(
'#title' => t('Use html5 tags in output'),
'#type' => 'checkbox',
'#default_value' => isset($filter->settings['use_html5'])
? $filter->settings['use_html5'] : $defaults['use_html5'],
);
$settings['use_parse_raw'] = array(
'#title' => t('Parse untranslatable HTML codes and LaTeX enviroments as raw'),
'#type' => 'checkbox',
'#default_value' => isset($filter->settings['use_parse_raw'])
? $filter->settings['use_parse_raw'] : $defaults['use_parse_raws_raw'],
);
$settings['use_smart'] = array(
'#title' => t('Automatically produce typographically correct output'),
'#type' => 'checkbox',
'#default_value' => isset($filter->settings['use_smart'])
? $filter->settings['use_smart'] : $defaults['use_smart'],
);
$settings['use_preserve_tabs'] = array(
'#title' => t('Preserve tabs in formatting for code blocks'),
'#type' => 'checkbox',
'#default_value' => isset($filter->settings['use_preserve_tabs'])
? $filter->settings['use_preserve_tabs'] : $defaults['use_preserve_tabs'],
);
$settings['use_normalize'] = array(
'#title' => t('Normalize document after processing'),
'#type' => 'checkbox',
'#default_value' => isset($filter->settings['use_normalize'])
? $filter->settings['use_normalize'] : $defaults['use_normalize'],
);
$settings['use_toc'] = array(
'#title' => t('Include a table of contents at the top of the page'),
'#type' => 'checkbox',
'#default_value' => isset($filter->settings['use_toc'])
? $filter->settings['use_toc'] : $defaults['use_toc'],
);
$settings['use_no_highlight'] = array(
'#title' => t('Disables code highlighting'),
'#type' => 'checkbox',
'#default_value' => isset($filter->settings['use_no_highlight'])
? $filter->settings['use_no_highlight'] : $defaults['use_no_highlight'],
);
$settings['use_section_divs'] = array(
'#title' => t('Wrap sections in div tags (or section with html5 enabled)'),
'#type' => 'checkbox',
'#default_value' => isset($filter->settings['use_section_divs'])
? $filter->settings['use_section_divs'] : $defaults['use_section_divs'],
);
$settings['base_header_level'] = array(
'#type' => 'select',
'#title' => t('Base header level'),
'#default_value' => isset($filter->settings['base_header_level'])
? $filter->settings['base_header_level'] : $defaults['base_header_level'],
'#description' => t('The header tag to use for the top level titles'),
'#options' => array(
'1' => t('1'),
'2' => t('2'),
'3' => t('3'),
'4' => t('4'),
'5' => t('5'),
'6' => t('6'),
),
);
$settings['columns'] = array(
'#type' => 'textfield',
'#title' => t('Column width'),
'#default_value' => isset($filter->settings['columns'])
? $filter->settings['columns'] : $defaults['columns'],
'#description' => t('A positive integer that determines the number of characters per line on output'),
'#element_validate' => array('element_validate_integer_positive'),
'#width' => '20px',
'#maxlength' => 3,
);
$settings['toc_depth'] = array(
'#type' => 'select',
'#title' => t('Table of contents depth'),
'#default_value' => isset($filter->settings['toc_depth'])
? $filter->settings['toc_depth'] : $defaults['toc_depth'],
'#description' => t('The number of header levels below the base to include in the table of contents'),
'#options' => array(
'1' => t('1'),
'2' => t('2'),
'3' => t('3'),
'4' => t('4'),
'5' => t('5'),
'6' => t('6'),
),
);
$settings['highlight_style'] = array(
'#type' => 'select',
'#title' => t('Code highlight style'),
'#default_value' => isset($filter->settings['highlight_style'])
? $filter->settings['highlight_style'] : $defaults['highlight_style'],
'#description' => t('The highlight style to be used in fenced code blocks'),
'#options' => array(
'pygments' => t('pygments'),
'kate' => t('kate'),
'monochrome' => t('monochrome'),
'espresso' => t('expresso'),
'zenburn' => t('zenburn'),
'haddock' => t('haddock'),
'tango' => t('tango'),
),
);
$form['description'] = array(
'#type' => 'item',
'#title' => t('See the <a href="http://pandoc.org/README.html">Pandoc readme</a> for more details'),
);
return $settings;
}
function pandoc_filter_check_settings($element, $filter, $default) {
$value = isset($filter->settings[$element]) ? $filter->settings[$element] : $default;
return $value;
}
/**
* Pandoc filter process callback.
*
*/
function _pandoc_filter_filter_process($text, $filter, $format) {
$loaded = pandoc_filter_config_load();
if(!$loaded){
return $text;
}
// Creates a new Pandoc object from pandoc-php
$pandoc = New Pandoc\Pandoc();
// This array will store key value pairs that will be passed to the
// runWith pandoc-php function and match options for Pandoc
$options = array();
$options['from'] = pandoc_filter_check_settings('input_format', $filter, 'mediawiki');
if(pandoc_filter_check_settings('use_html5', $filter, FALSE) == TRUE) {
$options['to'] = 'html5';
}
else {
$options['to'] = 'html';
}
if(pandoc_filter_check_settings('use_parse_raw', $filter, FALSE) == TRUE)
$options['parse-raw'] = NULL;
if(pandoc_filter_check_settings('use_smart', $filter, FALSE) == TRUE)
$options['smart'] = NULL;
if(pandoc_filter_check_settings('use_normalize', $filter, FALSE) == TRUE)
$options['normalize'] = NULL;
if(pandoc_filter_check_settings('use_preserve_tabs', $filter, FALSE) == TRUE)
$options['preserve-tabs'] = NULL;
if(pandoc_filter_check_settings('use_toc', $filter, FALSE) == TRUE)
$options['toc'] = NULL;
if(pandoc_filter_check_settings('use_no_highlight', $filter, FALSE) == TRUE)
$options['no-highlight'] = NULL;
if(pandoc_filter_check_settings('use_section_divs', $filter, FALSE) == TRUE)
$options['section-divs'] = NULL;
$options['base-header-level'] = pandoc_filter_check_settings('base_header_level', $filter, 2);
$options['columns'] = pandoc_filter_check_settings('columns', $filter, 72);
$options['toc-depth'] = pandoc_filter_check_settings('toc_depth', $filter, 3);
$options['highlight-style'] = pandoc_filter_check_settings('highlight-style', $filter, 'pygments');
$text = $pandoc->runWith($text, $options);
return $text;
}
/**
* Filter tips callback for Pandoc filter.
*
*/
function _pandoc_filter_filter_tips($filter, $format, $long = FALSE) {
$replacement = isset($filter->settings['input_format'])
? $filter->settings['input_format'] : 'mediawiki';
if (!$long) {
// This string will be shown in the content add/edit form.
return t('Content in the %replacement format will be converted to html.', array('%replacement' => $replacement));
}
else {
return t('Content in the %replacement format will be converted to html through the pandoc program.',
array('%replacement' => $replacement));
}
}
/**
* @} End of "defgroup pandoc_filter".
*/