Code in Habari

Using <code>, I can post short code snippets without relying on Github. e.g,

// my first program in C++
#include 
using namespace std;
int main ()
{
  cout << "Hello World!";
  return 0;
}

Habari has two plug-ins that cater especially to code snippets:

google-code-prettify allows syntax highlighting of source code snippets.

code_escape runs htmlspecialchars() on any code blocks in your post.

Whilst in user/plugins, fetch both plug-ins from their subversion repositories:

$ svn checkout http://svn.habariproject.org/habari-extras/plugins/google-code-prettify/trunk google-code-prettify $ svn checkout http://svn.habariproject.org/habari-extras/plugins/code_escape/trunk code_escape

Make the following changes to plugins/code_escape/code_escape.plugin.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

Index: code_escape.plugin.php
===================================================================
--- code_escape.plugin.php (revision 3226)
+++ code_escape.plugin.php (working copy)
@@ -4,7 +4,7 @@

public function filter_post_content_out ( $content, $post ) {

- $content = preg_replace_callback('/<code>(.*?)<\/code>/s', array( 'self', 'escape_code' ), $content);
+ $content = preg_replace_callback('/<code class="prettyprint">(.*?)&lt;\/code&gt;/s', array( 'self', 'escape_code' ), $content);

return $content;

@@ -16,7 +16,7 @@

$string = htmlspecialchars( $string );

- $string = '&lt;code&gt;' . $string . '</code>';
+ $string = '<code class="prettyprint">' . $string . '</code>';

return $string;

@@ -30,4 +30,4 @@

}

-?>
\ No newline at end of file
+?>