From 7501ced65b30d94d69fd0748e7dd0984a426b167 Mon Sep 17 00:00:00 2001 From: vargenau Date: Thu, 31 Jan 2008 20:40:10 +0000 Subject: [PATCH] Implemented Mediawiki-like syntax for tables git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@5981 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/IniConfig.php | 6 +- lib/InlineParser.php | 27 +++- lib/plugin/MediawikiTable.php | 228 ++++++++++++++++++++++++++++++ pgsrc/Help%2FMediawikiTablePlugin | 87 ++++++++++++ 4 files changed, 346 insertions(+), 2 deletions(-) create mode 100644 lib/plugin/MediawikiTable.php create mode 100644 pgsrc/Help%2FMediawikiTablePlugin diff --git a/lib/IniConfig.php b/lib/IniConfig.php index 17fe9289b..95e373c0f 100644 --- a/lib/IniConfig.php +++ b/lib/IniConfig.php @@ -1,5 +1,5 @@ * Copyright (C) 2004,2005,2006,2007 Reini Urban * @@ -820,6 +820,25 @@ class Markup_template_plugin extends SimpleMarkup } } +/** ENABLE_MARKUP_MEDIAWIKI_TABLE + * Table syntax similar to mediawiki + * {| + * => ?> + */ +class Markup_mediawikitable_plugin extends SimpleMarkup +{ + var $_match_regexp = '\{\|.*?\|\}'; + + function markup ($match) { + $s = str_replace("{|", "", $match); + $s = str_replace("|}", "", $s); + $s = ''; + return new Cached_PluginInvocation($s); + } +} + // "..." => "…" browser specific display (not cached?) // Support some HTML::Entities: (C) for copy, --- for mdash, -- for ndash // TODO: "--" => "&emdash;" browser specific display (not cached?) @@ -911,6 +930,8 @@ class InlineTransformer $this->_addMarkup(new Markup_color); if (ENABLE_MARKUP_TEMPLATE and !$non_default) $this->_addMarkup(new Markup_template_plugin); + if (ENABLE_MARKUP_MEDIAWIKI_TABLE) + $this->_addMarkup(new Markup_mediawikitable_plugin); // This does not work yet if (0 and PLUGIN_MARKUP_MAP and !$non_default) $this->_addMarkup(new Markup_xml_plugin); @@ -1077,6 +1098,10 @@ function TransformInlineNowiki($text, $markup = 2.0, $basepage=false) { // $Log: not supported by cvs2svn $ +// Revision 1.93 2007/09/26 16:54:34 rurban +// Fix Bug#1802827 Template does not get expanded with {{ }} syntax. +// by vargenau +// // Revision 1.92 2007/08/10 21:58:01 rurban // Improve SemanticLink parsings: // No units seperated by space allowed without [] diff --git a/lib/plugin/MediawikiTable.php b/lib/plugin/MediawikiTable.php new file mode 100644 index 000000000..1910b758c --- /dev/null +++ b/lib/plugin/MediawikiTable.php @@ -0,0 +1,228 @@ +_parse_attr($line); + foreach ($attrs as $key => $value) { + if (in_array ($key, array("id", "class", "title", "style", + "bgcolor", "frame", "rules", "border", + "cellspacing", "cellpadding", + "summary", "align", "width"))) { + $table->setAttr($key, $value); + } + } + } + + foreach ($lines as $line){ + if (substr($line,0,2) == "|-") { + if (isset($row)) { + if (isset($cell)) { + if (isset($content)) { + $cell->pushContent(TransformText(trim($content), $markup, $basepage)); + unset($content); + } + $row->pushContent($cell); + unset($cell); + } + $table->pushContent($row); + } + $row = HTML::tr(); + $attrs = $this->_parse_attr(substr($line,2)); + foreach ($attrs as $key => $value) { + if (in_array ($key, array("id", "class", "title", "style", + "bgcolor", "align", "valign"))) { + $row->setAttr($key, $value); + } + } + continue; + } + + // Table caption + if (substr($line,0,2) == "|+") { + + $caption = HTML::caption(); + $line = substr($line,2); + $pospipe = strpos($line, "|"); + $posbracket = strpos($line, "["); + if (($pospipe) && ((!$posbracket) || ($posbracket > $pospipe))) { + $attrs = $this->_parse_attr(substr($line, 0, $pospipe)); + foreach ($attrs as $key => $value) { + if (in_array ($key, array("id", "class", "title", "style", + "align", "lang"))) { + $caption->setAttr($key, $value); + } + } + $line=substr($line, $pospipe+1); + } + + $caption->pushContent(trim($line)); + $table->pushContent($caption); + } + + if (substr($line,0,1) == "|" and isset($row)) { + if (isset($cell)) { + if (isset ($content)) { + $cell->pushContent(TransformText(trim($content), $markup, $basepage)); + unset($content); + } + $row->pushContent($cell); + } + $cell = HTML::td(); + $line = substr($line, 1); + + // If there is a "|" in the line, the start of line + // (before the "|") is made of attributes. + // The end of the line (after the "|") is the cell content + // This is not true if the pipe is inside [] + // | [foo|bar] + // The following cases must work: + // | foo + // | [foo|bar] + // | class="xxx" | foo + // | class="xxx" | [foo|bar] + $pospipe = strpos($line, "|"); + $posbracket = strpos($line, "["); + if (($pospipe) && ((!$posbracket) || ($posbracket > $pospipe))) { + $attrs = $this->_parse_attr(substr($line, 0, $pospipe)); + foreach ($attrs as $key => $value) { + if (in_array ($key, array("id", "class", "title", "style", + "colspan", "rowspan", "width", "height", + "bgcolor", "align", "valign"))) { + $cell->setAttr($key, $value); + } + } + $line=substr($line, $pospipe+1); + $cell->pushContent(TransformText(trim($line), $markup, $basepage)); + continue; + } + } + if (isset($row) and isset($cell)) { + $line = str_replace("?\>", "?>", $line); + $line = str_replace("\~", "~", $line); + if (empty($content)) $content = ''; + $content .= $line . "\n"; + } + } + if (isset($row)) { + if (isset($cell)) { + if (isset($content)) + $cell->pushContent(TransformText(trim($content))); + $row->pushContent($cell); + } + $table->pushContent($row); + } + return $table; + } + + function _parse_attr($line) { + // We allow attributes with or without quotes (") + // border=1, cellpadding="5" + // style="font-family: sans-serif; border-top:1px solid #dddddd;" + // What will not work is style with comma inside, e. g. + // style="font-family: Verdana, Arial, Helvetica, sans-serif" + $attr_chunks = preg_split("/\s*,\s*/", strtolower($line)); + $options = array(); + foreach ($attr_chunks as $attr_pair) { + if (empty($attr_pair)) continue; + $key_val = preg_split("/\s*=\s*/", $attr_pair); + if (!empty($key_val[1])) + $options[trim($key_val[0])] = trim(str_replace("\"", "", $key_val[1])); + } + return $options; + } +} + +// $Log: not supported by cvs2svn $ +// + +// For emacs users +// Local Variables: +// mode: php +// tab-width: 8 +// c-basic-offset: 4 +// c-hanging-comment-ender-p: nil +// indent-tabs-mode: nil +// End: +?> diff --git a/pgsrc/Help%2FMediawikiTablePlugin b/pgsrc/Help%2FMediawikiTablePlugin new file mode 100644 index 000000000..9a3cdaf00 --- /dev/null +++ b/pgsrc/Help%2FMediawikiTablePlugin @@ -0,0 +1,87 @@ +Date: Thu, 31 Jan 2008 17:09:40 +0100 +Mime-Version: 1.0 (Produced by PhpWiki 1.3.12p3) +Content-Type: application/x-phpwiki; + pagename=Help%2FMediawikiTablePlugin; + flags=""; + author=Marc-Etienne%20VARGENAU; + version=1; + lastmodified=1201795780; + created=1201795780; + author_id=Marc-Etienne%20VARGENAU; + markup=2; + hits=2; + charset=UTF-8 +Content-Transfer-Encoding: binary + +~MediawikiTablePlugin is a plugin that takes off from the [RichTablePlugin|Help:RichTablePlugin]. It allows a user to specify a with (a subset of) the syntax used by Mediawiki. + +* The table starts with a line {~| and ends with a line |~}. +* An optional table caption is made with a line starting with a pipe and a plus sign |+ followed by the caption. +* A table row starts with a pipe and a hyphen: |-. +* A table cell starts with a pipe on a new line, or a double bar || on the same line. + +HTML attributes: +* for the table: +
+{| border="1"
+
+* for the caption: +
+|+ style="font-weight: bold;"
+
+* for a row: +
+|- style="height:100px"
+
+* for a cell: +
+|align="right" |Cell 2 (right aligned)
+
+ +The attributes might be put with or without double quotes. + +The Mediawiki syntax for headers: +
+! Header 1
+
+is not (yet) supported. + +!!! Example + +{| border="1", style="width: 100%" +|+ style="font-weight: bold; font-size: 150%;" | This is the table caption +|- style="white-space: nowrap" +| Header 1 +| Header 2 +| Header 3 +|- style=height:100px +| Cell I +| Cell II, in bold +|align=right, width="100%" |Cell III +|- bgcolor=#f0f0ff, align=center +|Cell 1||Cell 2||Cell 3 +|} + +The above table is rendered from: + + +{| border="1", style="width: 100%" +|+ style="font-weight: bold; font-size: 150%;" | This is the table caption +|- style="white-space: nowrap" +| Header 1 +| Header 2 +| Header 3 +|- style=height:100px +| Cell I +| Cell II, in bold +|align=right, width="100%" |Cell III +|- bgcolor=#f0f0ff, align=center +|Cell 1||Cell 2||Cell 3 +|} + + +;__Author__: Marc-Etienne Vargenau, Alcatel-Lucent + +------------- + +PhpWikiDocumentation Help:WikiPlugin -- 2.45.0