Page 1 of 1

Code Highlighting for Mantis

Posted: 27 Mar 2007, 10:10
by mcwizard
Google released a nice javascript to highlight code in webpages. http://code.google.com/p/google-code-prettify/

I integrated it into mantis.

You only need the following changes:

in core/html_api.php:

Line ~258:
# --------------------
# (8) Begin the <body> section
function html_body_begin() {
echo '<body onload=prettyPrint()>', "\n";
}

and in bug_view_page.php
Line: ~53

<?php html_page_top1( bug_format_summary( $f_bug_id, SUMMARY_CAPTION ) ) ?>
<script type="text/javascript" src="pretty/prettify.js"></script>
<?php html_page_top2() ?>

You also need to copy the prettify.js (from the google link above) in a /pretty folder under your mantis installation and you need to add the contents of the prettify.css to your mantis css file (I bet there's a cleaner way to do that, ideas welcome!)

As a last step, I decided to change the .js file so that it highlights all kinds of code and not only those in a code class=prettyprint because you cannot add those tags into mantis.

So check prettify.cs line ~1381 and change this:
if (cs.className && cs.className.indexOf('prettyprint') >= 0) {
into
if (true) {

and in line ~1386:
if ((p.tagName == 'pre' || p.tagName == 'code' ||
p.tagName == 'xmp') &&
p.className && p.className.indexOf('prettyprint') >= 0) {
into
if ((p.tagName == 'pre' || p.tagName == 'code' ||
p.tagName == 'xmp')) {

From now on, whenever you add code to your mantis posts with the <code> tag, it'll highlight them.

If you got any questions, ask away!

Posted: 27 Mar 2007, 15:55
by vboctor
Sounds like a good idea. To use this or other syntax highlighters (e.g. there are server side ones in PHP) we probably need to support a tag that users can use to enclose code to be highlighted.

It would be great if you can submit this into the bug tracker so we can track it.

Posted: 28 Mar 2007, 07:12
by mcwizard
Added it to the Tracker as ID 7859
http://www.mantisbt.org/bugs/view.php?id=7859