-
Notifications
You must be signed in to change notification settings - Fork 12
/
ContentRightSidebar.php
148 lines (129 loc) · 5.27 KB
/
ContentRightSidebar.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Monaco skin
*
* @package MediaWiki
* @subpackage Skins
*
* @author Daniel Friesen
*/
if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." );
$wgExtensionCredits['skin'][] = array (
'path' => __FILE__,
'name' => 'ContentRightSidebar',
'author' => array('[http://mediawiki.org/wiki/User:Dantman Daniel Friesen]'),
'descriptionmsg' => 'contentrightsidebar-desc',
'url' => "https://github.com/dantman/monaco-port",
);
$wgExtensionMessagesFiles['ContentRightSidebar'] = dirname(__FILE__).'/ContentRightSidebar.i18n.php';
/**
* Register parser extensions
*/
$wgHooks['ParserFirstCallInit'][] = array( 'efContentRightSidebarRegisterParser' );
function efContentRightSidebarRegisterParser( &$parser ) {
$parser->setHook('right-sidebar', 'efContentRightSidebarTag', 0);
return true;
}
define('RIGHT_SIDEBAR_START_TOKEN', "<!-- RIGHT SIDEBAR START -->");
define('RIGHT_SIDEBAR_END_TOKEN', "<!-- RIGHT SIDEBAR END -->");
define('RIGHT_SIDEBAR_WITHBOX_TOKEN', "<!-- RIGHT SIDEBAR WITHBOX -->");
define('RIGHT_SIDEBAR_TITLE_START_TOKEN', "<!-- RIGHT SIDEBAR TITLE START>");
define('RIGHT_SIDEBAR_TITLE_END_TOKEN', "<RIGHT SIDEBAR TITLE END -->");
define('RIGHT_SIDEBAR_CLASS_START_TOKEN', "<!-- RIGHT SIDEBAR CLASS START>");
define('RIGHT_SIDEBAR_CLASS_END_TOKEN', "<RIGHT SIDEBAR CLASS END -->");
define('RIGHT_SIDEBAR_CONTENT_START_TOKEN', "<!-- RIGHT SIDEBAR CONTENT START -->");
define('RIGHT_SIDEBAR_CONTENT_END_TOKEN', "<!-- RIGHT SIDEBAR CONTENT END -->");
function efContentRightSidebarTag( $input, $arg, $parser, $frame ) {
$isContentTagged = false;
$m = array();
if ( preg_match( '#^(.*)<content>(.*?)</content>(.*)$#is', $input, $m ) ) {
$isContentTagged = true;
$startUniq = $parser->uniqPrefix() . "-right-sidebar-content-start-" . Parser::MARKER_SUFFIX;
$endUniq = $parser->uniqPrefix() . "-right-sidebar-content-end-" . Parser::MARKER_SUFFIX;
$input = "{$m[1]}{$startUniq}{$m[2]}{$endUniq}{$m[3]}";
$input = $parser->recursiveTagParse( $input, $frame );
$input = str_replace($startUniq, RIGHT_SIDEBAR_CONTENT_START_TOKEN, $input);
$input = str_replace($endUniq, RIGHT_SIDEBAR_CONTENT_END_TOKEN, $input);
} else {
$input = $parser->recursiveTagParse( $input, $frame );
}
$with_box = (isset($arg["with-box"]) ? $arg["with-box"] : (isset($arg["withbox"]) ? $arg["withbox"] : null));
$out = RIGHT_SIDEBAR_START_TOKEN;
if ( $with_box && !in_array(strtolower($with_box), array("false", "off", "no", "none")) ) {
$out .= RIGHT_SIDEBAR_WITHBOX_TOKEN;
}
if ( isset($arg["title"]) ) {
$out .= RIGHT_SIDEBAR_TITLE_START_TOKEN . urlencode($arg["title"]) . RIGHT_SIDEBAR_TITLE_END_TOKEN;
}
if ( isset($arg["class"]) ) {
$out .= RIGHT_SIDEBAR_CLASS_START_TOKEN . urlencode($arg["class"]) . RIGHT_SIDEBAR_CLASS_END_TOKEN;
}
if ( $isContentTagged ) {
$out .= $input;
} else {
$out .= '<div style="float: right; clear: right; position: relative;">';
$out .= RIGHT_SIDEBAR_CONTENT_START_TOKEN . $input . RIGHT_SIDEBAR_CONTENT_END_TOKEN;
$out .= '</div>';
}
$out .= RIGHT_SIDEBAR_END_TOKEN;
return $out;
}
function efExtractRightSidebarBoxes( &$html ) {
$boxes = array();
while(true) {
$withBox = false;
$title = '';
$class = null;
$start = strpos($html, RIGHT_SIDEBAR_START_TOKEN);
if ( $start === false )
break;
$end = strpos($html, RIGHT_SIDEBAR_END_TOKEN, $start);
if ( $end === false )
break;
$content = substr($html, $start, $end-$start);
if ( strpos($content, RIGHT_SIDEBAR_WITHBOX_TOKEN) !== false ) {
$withBox = true;
}
$startTitle = strpos($content, RIGHT_SIDEBAR_TITLE_START_TOKEN);
if ( $startTitle !== false ) {
$endTitle = strpos($content, RIGHT_SIDEBAR_TITLE_END_TOKEN, $startTitle);
if ( $endTitle !== false ) {
$title = urldecode(substr($content, $startTitle+strlen(RIGHT_SIDEBAR_TITLE_START_TOKEN), $endTitle-$startTitle-strlen(RIGHT_SIDEBAR_TITLE_START_TOKEN)));
}
}
$startClass = strpos($content, RIGHT_SIDEBAR_CLASS_START_TOKEN);
if ( $startClass !== false ) {
$endClass = strpos($content, RIGHT_SIDEBAR_CLASS_END_TOKEN, $startClass);
if ( $endClass !== false ) {
$class = urldecode(substr($content, $startClass+strlen(RIGHT_SIDEBAR_CLASS_START_TOKEN), $endClass-$startClass-strlen(RIGHT_SIDEBAR_CLASS_START_TOKEN)));
}
}
$contentStart = strpos($content, RIGHT_SIDEBAR_CONTENT_START_TOKEN);
if ( $contentStart !== false ) {
$content = substr($content, $contentStart+strlen(RIGHT_SIDEBAR_CONTENT_START_TOKEN));
}
$contentEnd = strpos($content, RIGHT_SIDEBAR_CONTENT_END_TOKEN);
if ( $contentStart !== false ) {
$content = substr($content, 0, $contentEnd);
}
$boxes[] = array( "with-box" => $withBox, "title" => $title, "class" => $class, "content" => $content );
$html = substr($html, 0, $start) . substr($html, $end+strlen(RIGHT_SIDEBAR_END_TOKEN));
}
return $boxes;
}
$wgHooks['MonacoRightSidebar'][] = 'efContentRightSidebarMonacoRightSidebar';
function efContentRightSidebarMonacoRightSidebar( $sk ) {
$boxes = efExtractRightSidebarBoxes( $sk->data['bodytext'] );
foreach ( $boxes as $box ) {
if ( $box["with-box"] ) {
$attrs = array();
if ( isset($box["class"]) ) {
$attrs["class"] = $box["class"];
}
$sk->sidebarBox( $box["title"], $box["content"], $attrs );
} else {
echo $box["content"];
}
}
return true;
}