Blockquote MediaWiki Extension

From SwinBrain

About the Extension

This extension was written by Clinton Woodward for SwinBrain, and we are happy to make it available to anyone who would like to use it, but without any guarantees of any kind what-so-ever. This code is simple and should remain "free" for any one to use as you wish. Feel free to let Clinton know if you are using the extension, or if you have suggested changes.

The Extension Code

<?php
/**
 * <blockquote/> MediaWiki extension
 * Written by Clinton Woodward cwoodward@swin.edu.au for SwinBrain
 *
 * Renders the <blockquote/> element, which MediaWiki by default does not support.
 * Requires MediaWiki 1.5 in order to parse the element's attributes.
 * Supports the standard title, class, id and cite attributes.
 * Supports a special citetext attribute - used to show a link to the cite URL
 *
 * Usage: <blockquote cite="http://google.com"> quoted text </blockquote>
 * Version 1.0 started 22 Nov 2006
 */
 
$wgExtensionFunctions[] = "wfBlockquoteElement";
 
function wfBlockquoteElement()
{
    global $wgParser;
    $wgParser->setHook("blockquote",           // the name of the tag
                       "renderBlockquote");    // the converter function
}
 
function renderBlockquote($input, $attrs = array())
{
    // get the global parser hook for later
    global $wgOut;
    
    // protect from potential nasty attrs
    foreach ($attrs as $key => $value)
	$attrs[$key] = htmlentities($value);
 
    // open element
    $output = "<blockquote";
    
    // show - title, class, id and cite only
    if (array_key_exists('title', $attrs))
        $output .= ' title="'.$attrs['title'].'"';
	
    if (array_key_exists('id', $attrs))
        $output .= ' id="'.$attrs['id'].'"';
 
    if (array_key_exists('class', $attrs))
        $output .= ' class="'.$attrs['class'].'"';
 
    if (array_key_exists('cite', $attrs)) {
        $output .= ' lang="'.$attrs['cite'].'"';
	if (array_key_exists('citetext',$attrs)) {
	    $input .= '<p class="cite">'
		     .'['.$attrs['cite'].' '.$attrs['citetext'].']'
		     .'</p>';
	}
    }    
    // output contents and close element
    $output .= '>'.$wgOut->parse($input).'</blockquote>';
    return $output;
 
} ?>

Example

Note that we can use a paragraph with the class="cite" attributes to show an aligned citation to the right inside the block. If the attribute citetext="text to show with cite URL" is set, the extension creates a paragraph and uses the cite CSS class in exactly the same way for display.

Standard use of cite attribute and the .cite CSS class.
Wiki Text

<blockquote cite="http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javadoc.html#leadingasterisks">"When javadoc parses a doc comment, leading asterisk (*) characters on each line are discarded; blanks and tabs preceding the initial asterisk (*) characters are also discarded. Starting with 1.4, if you omit the leading asterisk on a line, the leading white space is no longer removed. This enables you to paste code examples directly into a doc comment inside a <nowiki><PRE></nowiki> tag, and its indentation will be honored. Spaces are generally interpreted by browsers more uniformly than tabs. Indentation is relative to the left margin (rather than the separator /** or <nowiki><PRE></nowiki> tag)." <p class="cite">[http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javadoc.html#leadingasterisks javadoc - The Java API Documentation Generator]</p></blockquote>

Result
"When javadoc parses a doc comment, leading asterisk (*) characters on each line are discarded; blanks and tabs preceding the initial asterisk (*) characters are also discarded. Starting with 1.4, if you omit the leading asterisk on a line, the leading white space is no longer removed. This enables you to paste code examples directly into a doc comment inside a <PRE> tag, and its indentation will be honored. Spaces are generally interpreted by browsers more uniformly than tabs. Indentation is relative to the left margin (rather than the separator /** or <PRE> tag)."

javadoc - The Java API Documentation Generator

Use of the custom citetext attribute to automate the same behaviour.
Wiki Text using citetext

<blockquote cite="http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javadoc.html#leadingasterisks" citetext="javadoc - The Java API Documentation Generator"> "When javadoc parses a doc comment, leading asterisk (*) characters on each line are discarded; blanks and tabs preceding the initial asterisk (*) characters are also discarded. Starting with 1.4, if you omit the leading asterisk on a line, the leading white space is no longer removed. This enables you to paste code examples directly into a doc comment inside a <nowiki><PRE></nowiki> tag, and its indentation will be honored. Spaces are generally interpreted by browsers more uniformly than tabs. Indentation is relative to the left margin (rather than the separator /** or <nowiki><PRE></nowiki> tag)." </blockquote>

Result
"When javadoc parses a doc comment, leading asterisk (*) characters on each line are discarded; blanks and tabs preceding the initial asterisk (*) characters are also discarded. Starting with 1.4, if you omit the leading asterisk on a line, the leading white space is no longer removed. This enables you to paste code examples directly into a doc comment inside a <PRE> tag, and its indentation will be honored. Spaces are generally interpreted by browsers more uniformly than tabs. Indentation is relative to the left margin (rather than the separator /** or <PRE> tag)."

javadoc - The Java API Documentation Generator