Jump to content

User:Jiangxin/Patch CharInsert

From Meta, a Wikimedia project coordination wiki

charinsert beside editor toolbar

[edit]
CharInsert extension is a useful extension in the MediaWiki source tree. This patch can add some editor shortcut beside the editor toolbar. CharInsert 插件可以为页面编辑界面提供编辑快捷,就像编辑页面时的输入框上部的编辑工具栏所提供的功能那样。原插件建议修改 MediaWiki:copyrightwarning 和 MediaWiki:copyrightwarning2 字符串。但是如果能够在编辑工具栏旁边增加扩展编辑快捷岂不更方便?
Notes: charinsert extesion's output is raw html, and may be broken by other parser process in includes/Parser.php. Pay attension to another patch: User:Jiangxin/Patch_RawHtmlTag. 注意: charinsert 插件的输出是原始 HTML 代码,输出的原始HTML代码可能会被 includes/Parser.php 中的 Parser 过程的其它处理过程搞乱,因此可能需要应用我提交的这一个补丁: User:Jiangxin/Patch_RawHtmlTag

patch for MediaWiki 1.6.2

[edit]

Index: includes/EditPage.php
===================================================================
--- includes/EditPage.php	(revision 51)
+++ includes/EditPage.php	(working copy)
@@ -1429,7 +1429,7 @@
 	 * The necessary JavaScript code can be found in style/wikibits.js.
 	 */
 	function getEditToolbar() {
-		global $wgStylePath, $wgContLang, $wgJsMimeType;
+		global $wgStylePath, $wgContLang, $wgJsMimeType, $wgOut;
 
 		/**
 		 * toolarray an array of arrays which each include the filename of
@@ -1521,7 +1521,7 @@
 					'key'	=>	'R'
 				)
 		);
-		$toolbar ="<script type='$wgJsMimeType'>\n/*<![CDATA[*/\n";
+		$toolbar ="<table><tr><td>\n<script type='$wgJsMimeType'>\n/*<![CDATA[*/\n";
 
 		$toolbar.="document.writeln(\"<div id='toolbar'>\");\n";
 		foreach($toolarray as $tool) {
@@ -1544,6 +1544,9 @@
 
 		$toolbar.="document.writeln(\"</div>\");\n";
 		$toolbar.="/*]]>*/\n</script>";
+		$toolbar.="</td><td>&nbsp;&nbsp;</td><td>";
+		$toolbar.=$wgOut->parse( wfMsg('edittoolbar'));
+		$toolbar.="</td></table>";
 		return $toolbar;
 	}
 
Index: languages/Messages.php
===================================================================
--- languages/Messages.php	(revision 51)
+++ languages/Messages.php	(working copy)
@@ -515,6 +515,7 @@
 'editingsection' => 'Editing $1 (section)',
 'editingcomment' => 'Editing $1 (comment)',
 'editconflict' => 'Edit conflict: $1',
+'edittoolbar'	=> "<charinsert>[+] [[+]] {{+}} {+} [[Category:+]] <nowiki><pre></nowiki>\\n+\\n<nowiki></pre></nowiki> <nowiki><pre>&lt;nowiki></nowiki>\\n+\\n<nowiki>&lt;/nowiki></pre></nowiki></charinsert>",
 'explainconflict' => 'Someone else has changed this page since you started editing it.
 The upper text area contains the page text as it currently exists.
 Your changes are shown in the lower text area.
Index: extensions/charinsert/CharInsert.php
===================================================================
--- extensions/charinsert/CharInsert.php	(revision 93)
+++ extensions/charinsert/CharInsert.php	(working copy)
@@ -40,6 +40,7 @@
 function setupSpecialChars() {
     global $wgParser;
     $wgParser->setHook( 'charinsert', 'charInsert' );
+    wfRawHtmlTag('charinsert');
 }
 
 function charInsert( $data ) {
@@ -78,6 +79,7 @@
 }
 
 function charInsertChar( $start, $end = '', $title = null ) {
+	static $div_style = true;
 	$estart = htmlspecialchars( charInsertJsString( $start ) );
 	$eend   = htmlspecialchars( charInsertJsString( $end   ) );
 	if( $eend == '' ) {
@@ -90,14 +92,21 @@
 	} else {
 		$extra = '';
 	}
-	return "<a href=\"javascript:insertTags('$estart','$eend','')\">$inline</a>";
+	if ( $div_style = !$div_style )
+	{
+		return "<span style='background:#dddddd;'><a href=\"javascript:insertTags('$estart','$eend','')\">$inline</a></span>";
+	}
+	else
+	{
+		return "<a href=\"javascript:insertTags('$estart','$eend','')\">$inline</a>";
+	}	
 }
 
 function charInsertJsString( $text ) {
 	return strtr(
 		$text,
 		array(
-			"\\"   => "\\\\",
+#			"\\"   => "\\\\", # 让 \n 能够输出换行符
 			"\""   => "\\\"",
 			"'"    => "\\'",
 			"\r\n" => "\\n",
@@ -107,8 +116,9 @@
 }
 
 function charInsertDisplay( $text ) {
-	static $invisibles = array(     '&nbsp;',     '&#160;' );
-	static $visibles   = array( '&amp;nbsp;', '&amp;#160;' );
+	# 不显示 '\\n', 会让 “<pre>\n\n</pre>”标签看起来更简洁 
+	static $invisibles = array(     '&nbsp;',     '&#160;' , '\\n' );
+	static $visibles   = array( '&amp;nbsp;', '&amp;#160;' ,    '' );
 	return str_replace( $invisibles, $visibles, $text );
 }


patch for MediaWiki 1.5.x

[edit]

Index: includes/EditPage.php
===================================================================
--- includes/EditPage.php	(revision 30)
+++ includes/EditPage.php	(working copy)
@@ -1009,7 +1009,7 @@
 	 * The necessary JavaScript code can be found in style/wikibits.js.
 	 */
 	function getEditToolbar() {
-		global $wgStylePath, $wgContLang, $wgMimeType, $wgJsMimeType;
+		global $wgStylePath, $wgContLang, $wgMimeType, $wgJsMimeType, $wgOut;
 
 		/**
 		 * toolarray an array of arrays which each include the filename of
@@ -1101,7 +1101,7 @@
 					'key'	=>	'R'
 				)
 		);
-		$toolbar ="<script type='$wgJsMimeType'>\n/*<![CDATA[*/\n";
+		$toolbar ="<table><tr><td>\n<script type='$wgJsMimeType'>\n/*<![CDATA[*/\n";
 
 		$toolbar.="document.writeln(\"<div id='toolbar'>\");\n";
 		foreach($toolarray as $tool) {
@@ -1127,6 +1127,9 @@
 		$toolbar.="document.writeln(\"</div>\");\n";
 
 		$toolbar.="/*]]>*/\n</script>";
+		$toolbar.="</td><td>&nbsp;&nbsp;</td><td>";
+		$toolbar.=$wgOut->parse( wfMsg('edittoolbar'));
+		$toolbar.="</td></table>";
 		return $toolbar;
 	}
 	
Index: languages/Language.php
===================================================================
--- languages/Language.php	(revision 30)
+++ languages/Language.php	(working copy)
@@ -719,6 +719,7 @@
 'previewconflict' => 'This preview reflects the text in the upper
 text editing area as it will appear if you choose to save.',
 'editing'		=> "Editing $1",
+'edittoolbar'	=> "<charinsert>[+] [[+]] {{+}} {+} [[Category:+]] <nowiki><pre></nowiki>\\n+\\n<nowiki></pre></nowiki></charinsert>",
 'editingsection'		=> "Editing $1 (section)",
 'editingcomment'		=> "Editing $1 (comment)",
 'editconflict'	=> 'Edit conflict: $1',

-- Jiangxin 16:52, 14 November 2005 (UTC)