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
+ ?>
0 commit comments