Extension:BacktickCode

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
BacktickCode

Release status:Extension status stable

ImplementationTemplate:Extension#type Tag
DescriptionTemplate:Extension#description Wrap `text between backticks` in <code> tags.
Author(s)Template:Extension#username Joel Thornton (joelpttalk)
Latest versionTemplate:Extension#version 1.0 (2012-12-30)
MediaWikiTemplate:Extension#mediawiki 1.5+
PHPTemplate:Extension#php 5.2+
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license GNU General Public License 2.0 or later
Download See the code section
Hooks usedTemplate:Extension#hook
ParserBeforeStripManual:Hooks/ParserBeforeStrip

Translate the BacktickCode extension if it is available at translatewiki.net

Check usage and version matrix.

The BacktickCode extension wraps <code> tags around wikitext which is placed `between backtick characters`.

This provides a handy wiki-editing shortcut for wikis that expect a lot of inlined <code> snippets in its pages, and functions similarly to the standard MediaWiki ''' -> <b> bold formatting shortcut.

Backtick characters within <pre> blocks will not be altered by this extension. Backticks outside of <pre> blocks can also be output to the page by escaping them as \`.

Installing[edit]

  • Copy the code into a file and place the file(s) in a directory called BacktickCode in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/BacktickCode/BacktickCode.php";
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

BacktickCode.php (PHP 5.3+)
<?php
/**
 * @author Joel Thornton <mediawiki@joelpt.net>
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */

if(!defined('MEDIAWIKI'))
    die("This is an extension to the MediaWiki package and cannot be run standalone.");

// Register as an extention
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'BacktickCode',
        'version' => '1.0',
        'url' => 'https://www.mediawiki.org/wiki/Extension:BacktickCode',
        'author' => 'Joel Thornton',
        'description' => 'Allows to show text as <code> between backticks (`)',
);

// Register hooks
$wgHooks['InternalParseBeforeLinks'][] = 'backtickCodeParse';

// Parse function
function backtickCodeParse( &$parser, &$text, &$stripState ) {
        // We replace '`...`' by '<code>...</code>' and '\`' by '`'.
        $text = preg_replace('/([^\\\\]|^)`([^`]*)`/', '$1<code>$2</code>', $text);
        $text = preg_replace('/\\\\\`/', '`', $text);

        return true;
}
BacktickCode.php (PHP 5.2+)
<?php
/**
 * @author Joel Thornton <mediawiki@joelpt.net>
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */

if(!defined('MEDIAWIKI'))
    die("This is an extension to the MediaWiki package and cannot be run standalone.");

// Register as an extention
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'BacktickCode',
        'version' => '1.0',
        'url' => 'https://www.mediawiki.org/wiki/Extension:BacktickCode',
        'author' => 'Joel Thornton',
        'description' => 'Allows to show text as <code> between backticks (`)',
);

// Register hooks
$wgHooks['InternalParseBeforeLinks'][] = 'backtickCodeParse';

// Extracted from backtickCodeParse()
function replaceBacktickCode($match) {
	return '<pre>' . preg_replace('/`/', '\`', $match[1]) . '</pre>';
}

// Modified Parse function
function backtickCodeParse( &$parser, &$text, &$stripState ) {
        // We replace '`...`' by '<code>...</code>' and '\`' by '`'.
        $text = preg_replace('/([^\\\\]|^)`([^`]*)`/', '$1<code>$2</code>', $text);
        $text = preg_replace('/\\\\\`/', '`', $text);

        return true;
}

Credits[edit]

This extension is derived from Extension:DollarSign.