开发者

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.


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

There is <source lang="mylanguage" enclose="none">inline code</source> in this paragraph.


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


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.


First, tag the characters you care about with span, code, source, div, p, etc. For inline with minimal changes, span is probably what you are looking for.

Second, apply a style to the tagged characters. For highlighting you probably want something like background: yellow

Example:

Highlights like <span style="border:thin solid green; background: yellow;">this</span> really draw the eye.


I found that enclosing the entire block with <pre></pre> showed the format the best.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜