forked from mdeltito/pagedown-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagedown.module
122 lines (98 loc) · 3.05 KB
/
pagedown.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
<?php
/**
* Implementation of hook_perm().
*/
function pagedown_perm() {
return array('access pagedown');
}
/**
* Implementation of hook_filter().
*/
function pagedown_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
switch ($op) {
case 'list':
return array(0 => t('Pagedown'));
case 'description':
return t('Allows users to post using the Pagedown editor with Markdown');
case 'process':
require_once('markdown.php');
$html = Markdown($text);
return $html;
default:
return $text;
}
}
/**
* Implementation of hook_filter_tips().
*/
function pagedown_filter_tips($delta, $format, $long = FALSE) {
// TODO: add tips
return t('Pagedown tips coming soon.');
}
/**
* Implementation of hook_elements
*/
function pagedown_elements() {
$type = array();
if (user_access('access pagedown')) {
// Let Pagedown potentially process each textarea.
$type['textarea'] = array('#process' => array('pagedown_process_textarea'));
}
return $type;
}
/**
* Initialization for Pagedown javascript/css.
*/
function pagedown_asset_init() {
static $pd_init = false;
if(!$pd_init) {
$pd_init = true;
$mod_path = drupal_get_path('module', 'pagedown');
drupal_add_css($mod_path . '/pagedown.css', 'module', 'all', FALSE);
drupal_add_js($mod_path . '/js/Markdown.Converter.js');
drupal_add_js($mod_path . '/js/Markdown.Editor.js');
drupal_add_js($mod_path . '/js/Markdown.Sanitizer.js');
}
}
/**
* Process a textarea for use with Pagedown
*/
function pagedown_process_textarea($element) {
pagedown_asset_init();
// check if we are acting on this element
$targets = pagedown_get_targets();
if(!in_array($element['#id'], $targets)) return $element;
$selector = $element['#id'];
$pagedown_element_init = <<<EOD
(function () {
$(function() {
var bar = $('<div/>').attr('id', '$selector-bar');
var preview_wrap = $('<div/>').attr('id', '$selector-preview-wrap').addClass('wmd-preview-wrap');
var preview = $('<div/>').attr('id', '$selector-preview').addClass('wmd-preview');
preview.appendTo(preview_wrap);
$('#$selector').before(bar);
$('#$selector-wrapper').after(preview_wrap);
var textarea_converter = new Markdown.Converter();
var help = function () { alert("Not yet implemented"); }
var editor = new Markdown.Editor(textarea_converter, "{$element['#id']}", { handler: help });
editor.run();
});
})();
EOD;
drupal_add_js($pagedown_element_init, 'inline');
return $element;
}
/**
* TODO: this should collect individual values from the database. currently the form element targets are
* stored in a single variable. eventually, controls to enable this per-textarea should be added
*/
function pagedown_get_targets() {
static $targets = array();
static $checked = false;
if(!$checked) {
$checked = true;
// TODO: again, this should collect from db rather than a single variable
$targets = variable_get('pagedown_elements', array());
}
return $targets;
}