Projet

Général

Profil

Paste
Télécharger (3,92 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / highlightjs / contrib / highlight_js_filter / highlight_js_filter.module @ 5721e759

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides the Highlight JS filter.
6
 *
7
 * @author Juned Kazi
8
 */
9

    
10
/**
11
 * Implement hook_filter_info().
12
 */
13
function highlight_js_filter_filter_info() {
14
  $filters['highlight_js'] = array(
15
    'title' => t('Highlight JS Filter'),
16
    'description' => t('Allows users to post code verbatim using &lt;code&gt; and &lt;?php ?&gt; tags.'),
17
    'prepare callback' => '_highlight_js_filter_prepare',
18
    'process callback' => '_highlight_js_filter_process',
19
    'tips callback' => '_highlight_js_filter_tips',
20
    'cache' => FALSE,
21
  );
22
  return $filters;
23
}
24

    
25

    
26
/**
27
 * Tips callback for Highlight JS filter.
28
 */
29
function _highlight_js_filter_tips($format, $long = FALSE) {
30
  if ($long) {
31
    return t('To post pieces of code, surround them with &lt;code&gt;...&lt;/code&gt; tags.');
32
  }
33
  else {
34
    return t('You may post code using &lt;code&gt;...&lt;/code&gt; (generic) tags.');
35
  }
36
}
37

    
38
/**
39
 * Prepare callback for Highlight JS filter.
40
 */
41
function _highlight_js_filter_prepare($text, $format) {
42
  $text = preg_replace_callback('@<code>(.+?)</code>@s', '_highlight_js_filter_escape_code_tag_callback', $text);
43
  $text = preg_replace_callback('@<code class="(.+?)">(.+?)</code>@s', '_highlight_js_filter_escape_code_tag_lang_callback', $text);
44
  return $text;
45
}
46

    
47
/**
48
 * Callback to escape content of <code> elements.
49
 */
50
function _highlight_js_filter_escape_code_tag_callback($matches) {
51
  return highlight_js_filter_escape($matches[1], 'code');
52
}
53

    
54
/**
55
 * Callback to escape content of <code lang="php"> elements.
56
 */
57
function _highlight_js_filter_escape_code_tag_lang_callback($matches) {
58
  return highlight_js_filter_escape($matches[2], 'code', $matches[1]);
59
}
60

    
61

    
62
/**
63
 * Escape code blocks during input filter 'prepare'.
64
 *
65
 * @param $text
66
 *   The string to escape.
67
 * @param $type
68
 *   The type of code block, either 'code' or 'php'.
69
 * @return
70
 *   The escaped string.
71
 */
72
function highlight_js_filter_escape($text, $type = 'code', $lang = '') {
73
  // Note, pay attention to odd preg_replace-with-/e behaviour on slashes
74
  $text = check_plain(str_replace('\"', '"', $text));
75
  // Protect newlines from line break converter
76
  $text = str_replace(array("\r", "\n"), array('', '&#10;'), $text);
77
  $lang = empty($lang) ? '' : ' class="' . $lang . '"';
78
  // Add codefilter escape tags
79
  $text = "[highlightjs_$type$lang]{$text}[/highlightjs_$type]";
80
  return $text;
81
}
82

    
83

    
84

    
85
function _highlight_js_filter_process($text, $format) {
86
  $text = preg_replace_callback('@\[highlightjs_code\](.+?)\[/highlightjs_code\]@s', '_highlight_js_filter_process_code_callback', $text);
87
  $text = preg_replace_callback('@\[highlightjs_code class="(.+?)"\](.+?)\[/highlightjs_code\]@s', '_highlight_js_filter_process_code_lang_callback', $text);
88
  return $text;
89
}
90

    
91
/**
92
 * Callback to replace content of the <code> elements.
93
 */
94
function _highlight_js_filter_process_code_callback($matches) {
95
  return highlight_js_filter_process_code($matches[1]);
96
}
97

    
98
/**
99
 * Callback to replace content of the <code> elements.
100
 */
101
function _highlight_js_filter_process_code_lang_callback($matches) {
102
  $lang = $matches[1];
103
  $text = $matches[2];
104
  return highlight_js_filter_process_code($text, $lang);
105
}
106

    
107

    
108
/**
109
 * Processes chunks of escaped code into HTML.
110
 */
111
function highlight_js_filter_process_code($text, $lang = '') {
112
  // Undo linebreak escaping
113
  $text = str_replace('&#10;', "\n", $text);
114
  // Inline or block level piece?
115
  $multiline = strpos($text, "\n") !== FALSE;
116
  // Note, pay attention to odd preg_replace-with-/e behaviour on slashes
117
  $text = preg_replace("/^\n/", '', preg_replace('@</?(br|p)\s*/?>@', '', str_replace('\"', '"', $text)));
118
  // Trim leading and trailing linebreaks
119
  $text = trim($text, "\n");
120
  $class = empty($lang) ? '' : ' class="' . $lang . '"';
121
  $text = '<pre><code' . $class . '>' . highlight_js_filter_fix_spaces(str_replace(' ', '&nbsp;', $text)) . '</code></pre>';
122
  return $text;
123
}
124

    
125

    
126
function highlight_js_filter_fix_spaces($text) {
127
  return preg_replace('@&nbsp;(?!&nbsp;)@', ' ', $text);
128
}