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