View Issue Details

IDProjectCategoryView StatusLast Update
0007859mantisbtfeaturepublic2017-07-12 05:02
ReporterMcWizard Assigned To 
PrioritynormalSeverityfeatureReproducibilityalways
Status newResolutionopen 
Product Version1.0.1 
Summary0007859: Patch: Code Highlighting in Mantis
Description

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!

Tagspatch, plugins

Relationships

related to 0004318 acknowledged Code tag for bugnotes 

Activities

deboutv

deboutv

2007-03-28 02:16

reporter   ~0014259

I think that highlight must be done by the server with a tag <code=language></code> by example.

McWizard

McWizard

2007-03-28 02:18

reporter   ~0014260

The nice thing about googles JavaScript is that it automagically detects the language the code is in, so the bug reporter does not have to provide that information himself!

deboutv

deboutv

2007-03-28 03:09

reporter   ~0014261

The google-code-prettify does not cover a lot of languages (and automatic detection could give strange behavior).

Maybe GeSHi http://qbnz.com/highlighter/ could be a good PHP class to do that (a lot of language are supported).

McWizard

McWizard

2007-03-28 07:34

reporter   ~0014263

I suppose this is not an ivory-tower solution, but a quick fix to add Code highlighting to mantis while it is not in the official releases. If you choose to add a more sophisticated solution to the next Mantis release, feel free. But since we are using a modified version of mantis anyhow, we were glad to have a simple and quick addon.

deboutv

deboutv

2007-03-28 07:57

reporter   ~0014264

This feature has been discussed in the Forum. It has been reported here for the next Mantis release and my comments apply to the next release.

If you want to add this feature now, yes the google-code-prettify is a good solution (patch is easy to do).

vboctor

vboctor

2007-03-28 10:52

manager   ~0014266

Thanks McWizard for your contribution. I think it is a good solution until we have one integrated into Mantis.

GeSHi was the one I had in mind for this feature. It is also used in DokuWiki. Depending on the size of the library (I didn't check that yet) and the license, we can either include it with Mantis or get a path to it and use it. I haven't researched other server side options. I recall that GeSHi was seen to be one of the best in terms of functionality, but not the best for performance.

vboctor

vboctor

2007-04-04 03:15

manager   ~0014310

Last edited: 2008-02-18 18:45

dp.SyntaxHighlighter is another JavaScript option. I haven't look at it in details, but it seems to require specifying the language.

http://code.google.com/p/syntaxhighlighter/

atrol

atrol

2010-08-16 15:27

developer   ~0026336

There is a plugin for 1.2
http://git.mantisforge.org/w/highlightcode.git