Skip to content

Commit 46c8af8

Browse files
author
Ben Ward
committed
Checked in microformats.org version of SemanticHTML extension
0 parents  commit 46c8af8

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

SemanticHTML.class.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/** @class SemanticHTMLParser.
3+
* Simple function to take an element name, render its attributes and return
4+
* the complete HTML element to MediaWiki.
5+
*
6+
* PHP < 5.3 requires each element to have its own callback method, but
7+
* future versions could use the __callStatic magic method to handle
8+
* unlimited elements from the single array above, which could in turn be
9+
* separated into a tidy config file for super-easy administration.
10+
*
11+
* Notes: I'm new to MediaWiki hacking. Pretty sure there should be an
12+
* easier way to say ‘don't drop these elements’, rather than having to
13+
* re-render them. Oh well.
14+
*
15+
* @author Ben Ward
16+
*/
17+
class SemanticHTMLParser {
18+
19+
private static $elements = array(
20+
'parseAbbr' => 'abbr',
21+
'parseAcronym' => 'acronym',
22+
'parseDfn' => 'dfn',
23+
'parseKbd' => 'kbd',
24+
'parseSamp' => 'samp',
25+
'parseVar' => 'var',
26+
);
27+
28+
// __callStatic isn't implemented until 5.3, so need explicit methods:
29+
public static function parseAbbr($text, $attributes, $parser) {
30+
return SemanticHTMLParser::parseElement('abbr', $text, $attributes, $parser);
31+
}
32+
public static function parseAcronym($text, $attributes, $parser) {
33+
return SemanticHTMLParser::parseElement('acronym', $text, $attributes, $parser);
34+
}
35+
public static function parseDfn($text, $attributes, $parser) {
36+
return SemanticHTMLParser::parseElement('dfn', $text, $attributes, $parser);
37+
}
38+
public static function parseKbd($text, $attributes, $parser) {
39+
return SemanticHTMLParser::parseElement('kbd', $text, $attributes, $parser);
40+
}
41+
public static function parseSamp($text, $attributes, $parser) {
42+
return SemanticHTMLParser::parseElement('samp', $text, $attributes, $parser);
43+
}
44+
public static function parseVar($text, $attributes, $parser) {
45+
return SemanticHTMLParser::parseElement('var', $text, $attributes, $parser);
46+
}
47+
48+
private static function parseElement($element, $text, $attributes, $parser) {
49+
$return = "<$element";
50+
if(is_array($attributes)) {
51+
foreach($attributes as $name=>$value) {
52+
$return .= " $name=\"$value\"";
53+
}
54+
}
55+
$text = $parser->recursiveTagParse( $text );
56+
$return .= ">$text</$element>";
57+
return $return;
58+
}
59+
60+
public static function registerHooks() {
61+
global $wgParser;
62+
foreach(SemanticHTMLParser::$elements as $method=>$tag) {
63+
$wgParser->setHook($tag, array('SemanticHTMLParser', $method));
64+
}
65+
}
66+
}
67+
?>

SemanticHTML.i18n.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/** Extension: SemanticHTMLParser.
3+
* Simple function to take an element name, render its attributes and return
4+
* the complete HTML element to MediaWiki.
5+
*
6+
* English strings
7+
* @lang English
8+
* @author Ben Ward
9+
*/
10+
11+
12+
$messages = array();
13+
14+
$messages['en'] = array(
15+
'semantichtml-desc' => 'Enables use of HTML4 phrase elements in MediaWiki'
16+
);
17+
18+
?>

SemanticHTML.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/** Extension: SemanticHTMLParser.
4+
* Simple extension to take an element name, render its attributes and return
5+
* the complete HTML element to MediaWiki.
6+
*
7+
*
8+
* Notes: I'm new to MediaWiki hacking. Pretty sure there should be an
9+
* easier way to say ‘don't drop these elements’, rather than having to
10+
* re-render them. Oh well.
11+
*
12+
* Add new element names and method hooks to SemanticHTML.class.php
13+
*
14+
* @author Ben Ward
15+
*/
16+
17+
if( !defined( 'MEDIAWIKI' ) )
18+
die();
19+
20+
$wgExtensionCredits['parserhook']['SemanticHTML'] = array(
21+
'name' => 'Semantic HTML',
22+
'svn-date' => '$LastChangedDate: 2008-07-06 16:49:00 +0100 (Sun, 6 July 2008) $',
23+
'svn-revision' => '$LastChangedRevision: $',
24+
'author' => array( 'Ben Ward' ),
25+
'description' => 'Enables use of HTML4 phrase elements in MediaWiki',
26+
'descriptionmsg' => 'semantichtml-desc',
27+
'url' => 'http://www.mediawiki.org/wiki/Extension:SemanticHTML',
28+
);
29+
30+
$dir = dirname(__FILE__) . '/';
31+
$wgExtensionMessagesFiles['SemanticHTML'] = $dir . 'SemanticHTML.i18n.php';
32+
$wgAutoloadClasses['SemanticHTMLParser'] = $dir . 'SemanticHTML.class.php';
33+
$wgHooks['ParserFirstCallInit'][] = 'initSemanticHTML';
34+
35+
/**
36+
* Register parser hook
37+
*/
38+
function initSemanticHTML() {
39+
require_once('SemanticHTML.class.php');
40+
SemanticHTMLParser::registerHooks();
41+
return true;
42+
}
43+
44+
?>

0 commit comments

Comments
 (0)