Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline Syntax Highlighting in MediaWiki

Is there any MediaWiki extension that supports inline syntax highlighting? (i.e. with support for code snippets embedded within regular text paragraphs)

I currently use SyntaxHighlight GeSHi, but I'm not sure it supports inline highlighting.

like image 345
Amelio Vazquez-Reina Avatar asked Feb 02 '11 23:02

Amelio Vazquez-Reina


3 Answers

You can add enclose="none" to your <source> tag:

There is <source lang="mylanguage" enclose="none">inline code</source> in this paragraph.
like image 120
earldouglas Avatar answered Nov 16 '22 15:11

earldouglas


The simplest solution is using: <code>put your code here</code>

like image 33
Reinstate Monica - Goodbye SE Avatar answered Nov 16 '22 17:11

Reinstate Monica - Goodbye SE


While using <code>inline code</code> or, for example, <syntaxhighlight lang="groovy" inline>inline code</syntaxhighlight> works typing this in is a real pain, especially if you deal with a lot of code snippets.

If the wiki is under your control you can extend its markup. The example below shows how to shorten the above to <c>inline code</c> and <sg>inline code</sg> respectively using tag extensions method.

Create Customtags directory for your new extension in your MediaWiki extensions directory (MW_HOME/extensions/). In this directory create a customtags.php file with the following content:

<?php

$wgHooks['ParserFirstCallInit'][] = 'customtagsInit';

function customtagsInit(Parser $parser) { 

    // parameters: custom tag, custom renderer function
    $parser->setHook('c', 'customRenderShortCode');
    $parser->setHook('sg', 'customRenderSourceGroovy');

    return true;
}

function customRenderSourceGroovy($input, array $args, Parser $parser, PPFrame $frame) {
    $input = '<syntaxhighlight lang="groovy" inline>' . $input . '</syntaxhighlight>';
    $wikiparsed = $parser->recursiveTagParse($input, $frame);
    return $wikiparsed;
}

function customRenderShortCode($input, array $args, Parser $parser, PPFrame $frame) {
    $wikiparsed = $parser->recursiveTagParse($input, $frame);
    return '<code>' . $wikiparsed . '</code>';
}

?>

Finally register this extension in LocalSettings.php and you are good to go:

require_once "$IP/extensions/Customtags/customtags.php";

In a similar way you can create short tags for larger blocks of code.

like image 31
Johnny Baloney Avatar answered Nov 16 '22 15:11

Johnny Baloney