[Home]WikiPatches/TableSyntax

UseModWiki | WikiPatches | RecentChanges | Preferences

MoinMoin-style tables. Moved from MeatBall:TableSyntax. -- SunirShah

NextRelease


Add the globals $TableMode, $TableSyntax.

Search for PATCH below and apply.

# As a major config variable
$TableSyntax = 1;                   # 1 = wiki syntax tables, 0 = no magic table syntax #PATCH

sub WikiToHTML {
  my ($pageText) = @_;
  $TableMode = 0; #PATCH
  ...
}

sub CommonMarkup {
    ...
  if ($doLines) { # 0 = no line-oriented, 1 or 2 = do line-oriented (PATCH)
    ...
    s/((\|\|)+)/"<\/TD><TD COLSPAN=\"" . (length($1)\/2) . "\">"/ge if $TableMode; #PATCH
  }
}

sub WikiLinesToHtml {
  my ($pageText) = @_;
  my ($pageHtml, @htmlStack, $code, $codeAttributes, $depth, $oldCode); #PATCH
 
  ...
  foreach (split(/\n/, $pageText)) {  # Process lines one-at-a-time
    $code = '';           #PATCH
    $codeAttributes = ''; #PATCH
    $TableMode = 0;

    ...

    } elsif (s/^(\#+)/<li>/) {
      $code = "OL";
      $depth = length $1;
#BEGIN------------------------------------------------ PATCH
    } elsif ($TableSyntax && s/^((\|\|)+)(.*)\|\|\s*$/"<TR VALIGN='CENTER' ALIGN='CENTER'><TD colspan='" . (length($1)\/2) . "'>$3<\/TD><\/TR>\n"/e) {
      $code = 'TABLE';
      $codeAttributes = "BORDER='1'";
      $TableMode = 1;
      $depth = 1;
#END-------------------------------------------------- PATCH
    } elsif (/^[ \t].*\S/) {
      $code = "PRE";
      $depth = 1;
    } else {
      $depth = 0;
    }
    ...
    if ($depth > 0) {
    ...
      while (@htmlStack < $depth) {
        push(@htmlStack, $code);
        $pageHtml .= "<$code $codeAttributes>\n"; #PATCH
      }
    ...
}


Tried this patch, getting some table code but not complete. I'll run through the code I've patched carefully again. --RobertBrook

I originally wrote this for our work intrawiki. Then I reapplied this patch to the MeatballWiki script. See MeatBall:TableSyntax for an example. -- SunirShah

Thanks Sunir, I'll take another look! --RobertBrook

Thanks from me as well. I did an error in positioning the last patch first so now I've added some more context here. --DavidAndel


Can something like the table format on PmWiki(http://www.sci.tamucc.edu/wiki/Main/TextFormattingRules) be implemented? -- DavidCollantes
Actually, I found more convenient just to allow full <table> html. Done on my wiki now -- DavidCollantes

Tables there are: Table are defined by enclosing cells with '||'. A cell with leading and trailing spaces is centered; a cell with leading spaces is right-aligned; all other cells are left-aligned. An empty cell will cause the previous cell to span multiple columns. (There is currently no mechanism for spanning multiple rows.) A line beginning with '||' specifies the table attributes for subsequent tables.

        ||align=center border=1 width=50%
        ||Left   || Center ||  Right||
        ||A      ||    B   ||      C||
        || multi span    ||||       ||

If the above patch is installed, the following should work

The patch above does not imply this syntax completely, see my modifications for different horizontal alignment in each cell (not only the whole row) below. -- FerdinandPrantl

||align=center border=1 width=50%

Left Center Right
A B C
multi span  


What about using three Bars on the beginning and end of a line to specify things like align, border, width, etc ? The idea about leading and trailing spaces seems interesting to naturally do alignment.


Here's a patch to allow table attributes to be set on a leading line that starts with two bars but doesn't end with bars. The attributes only apply to the immediately following table:

--- wiki.pl	Thu Sep 19 10:34:51 2002
+++ wiki.pl-test	Thu Sep 19 11:28:38 2002
@@ -1266,9 +1266,9 @@
   @htmlStack = ();
   $depth = 0;
   $pageHtml = "";
-  foreach (split(/\n/, $pageText)) {  # Process lines one-at-a-time
+  $codeAttributes = '';
+  foreach (split(/\r?\n/, $pageText)) {  # Process lines one-at-a-time
     $code = '';
-    $codeAttributes = '';
     $TableMode = 0;
     $_ .= "\n";
     if (s/^(\;+)([^:]+\:?)\:/<dt>$2<dd>/) {
@@ -1283,9 +1283,12 @@
     } elsif (s/^(\#+)/<li>/) {
       $code = "OL";
       $depth = length $1;
+    } elsif ($TableSyntax && s/^\|\|\s*(.*[^\|])\n\z//) {
+      $TableMode = 1;
+      $codeAttributes = $1;
     } elsif ($TableSyntax && s/^((\|\|)+)(.*)\|\|\s*$/"<TR VALIGN='CENTER' ALIGN='CENTER'><TD colspan='" . (length($1)\/2) . "'>$3<\/TD><\/TR>\n"/e) {
       $code = 'TABLE';
-      $codeAttributes = "BORDER='1'";
+      $codeAttributes ||= "BORDER='1'";
       #What is the meaning and purpose of the above line? I've tried to look up what ||= does but none of the Perl _
       # reference guides have it listed. EarlRuffa
       # it needs to be this       $codeAttributes = ($codeAttributes eq "") ? "BORDER='1'" : $codeAttributes;
       # --MarkButler
       $TableMode = 1;
       $depth = 1;
     } elsif (/^[ \t].*\S/) {
@@ -1294,6 +1297,7 @@
     } else {
       $depth = 0;
     }
+    $codeAttributes = '' unless $TableMode;
     while (@htmlStack > $depth) {   # Close tags as needed
       $pageHtml .=  "</" . pop(@htmlStack) . ">\n";
     }


Comment by DavidClaughton:

+    } elsif ($TableSyntax && s/^\|\|\s*(.*[^\|])\n\z//) {

In the above Table Attributes patch, the main regexp - shown in the above line - did not work for me.

I suspect this is because I am on Windows (double char line ending). This causes the regexp to match to all lines being with ||, thereby removing all the tables!

I replaced the above line with the following:

    } elsif ($TableSyntax && s/^\|\|\s*([^\|]*)$//) {
This works for me and I think it should work under UNIX as well - anyone care to try it?

Both work and I am on Windows, but yours is more 'correct'. --MarkButler


This change allows different horizontal alignment in each cell according to the syntax suggestion from DavidCollantes above. The alignment of a cell is driven by a pair of spaces (two spaces are better that one space only, some people prefer to use one space around "||" to make the source text more legible). More spaces do not matter and work too - two spaces is a minimum. Here we are the horizontal alignments supported, with the correct positions of the controlling spaces:

 ||left  ||  center  ||center||  right||

Firstly, find this line in the patch:

    s/((\|\|)+)/"<\/TD><TD COLSPAN=\"" . (length($1)\/2) . "\">"/ge if $TableMode; #PATCH
and replace it with the following lines:
    if ($TableMode) {
      while (/(\|\|)+([^\|<]+)/) {
        my $align = $2;
        $align = $align =~/^  / ? ($align =~/  $/ ? 'CENTER' : 'RIGHT') : ($align =~/  $/ ? 'LEFT' : 'CENTER');
        s/((\|\|)+)/"<\/TD><TD ALIGN='$align' COLSPAN=\"" . (length($1)\/2) . "\">"/e;
      }
    }

Secondly, find these lines in the patch:

#BEGIN------------------------------------------------ PATCH
    } elsif ($TableSyntax && s/^((\|\|)+)(.*)\|\|\s*$/"<TR VALIGN='CENTER' ALIGN='CENTER'><TD colspan='" . (length($1)\/2) . "'>$3<\/TD><\/TR>\n"/e) {
      $code = 'TABLE';
      $codeAttributes = "BORDER='1'";
      $TableMode = 1;
      $depth = 1;
#END-------------------------------------------------- PATCH
and replace it with the following lines:
#BEGIN------------------------------------------------ PATCH
    } elsif ($TableSyntax && /^(\|\|)+.*\|\|\s*$/) {
      /^(\|\|)+([^\|]+)/;
      my $align = $2;
      $align =  $align =~/^  / ? ($align =~/  $/ ? 'CENTER' : 'RIGHT') : ($align =~/  $/ ? 'LEFT' : 'CENTER');
      s/^((\|\|)+)(.*)\|\|\s*$/"<TR VALIGN='CENTER'><TD ALIGN='$align' COLSPAN='" . (length($1)\/2) . "'>$3<\/TD><\/TR>\n"/e;
      $code = "TABLE";
      $codeAttributes ||= "BORDER=\"1\"";
      $TableMode = 1;
      $depth = 1;
#END-------------------------------------------------- PATCH

This change works with the patch allowing table attributes to be set on a leading line that starts with two bars but doesn't end with bars (see it above).

There is a bug on the line above:

 while (/(\|\|)+([^\|<]+)/) {
the less-than sign is superfluous:
 while (/(\|\|)+([^\|]+)/) {
-- FerdinandPrantl

This is not correct! With your version (without "<") you can't format the rightest row of a table as "left"! The parser will interpret "left" as center!

||A thing  ||another long thing  ||
||left     ||left-bug  ||
-- MartinEbert? (mx300_at_gmx.net)
I've made another patch for this syntax (that is: for setting alignment with double-spaces) which only needs changes to one site of the code. I think it's a little bit easier to read.
sub CommonMarkup {
    ...
  if ($doLines) { # 0 = no line-oriented, 1 or 2 = do line-oriented (PATCH)
    ...
#BEGIN------------------------------------------------ PATCH
    if ($TableMode) {
      s/((\|\|)+)/"<\/td><td colspan=\"" . (length($1)\/2) . "\">"/ge;
      my %alignments = (',',      '',
			',  ',    ' align="left"',
			'  ,',    ' align="right"',
			'  ,  ',  ' align="center"',
			);
      s/<td(.*?)>(  )?(.*?)(  )?<\/td>/"<td".($alignments{"$2,$4"})."$1>$3<\/td>"/gei;
    }
#END-------------------------------------------------- PATCH
  }
}
The keys in the alignments-hash are composed by joining the leading and trailing space-pairs, if there is any. Without the spaces, it does not set the alignment in order to leave that for the stylesheet. Only two spaces are removed at each place so that other spaces can be used for other purposes (readability or other kinds of markup).

-- IsakJohnsson? (isak@nospam@hypergene.com)


I kind of like more control - not so wiki I guess, but it seems natural to have:

        |align=center border=1 width=50%|
        ||Left   |align=center| Center |align=right|  Right||
        ||A      ||    B   ||      C||
        |colspan=2| multi span    ||       ||

Is that too hard? --MarkButler

After more thought on this I understand why Wiki people want to have the control in the formatting. Well my compromise was to put the control for centre etc (as above) using the double space and add a standard table format via a class wikitable. Then allow the extra override with a leading ||class=wikitablecustom. This way all the formatting is either in the wiki style or the css style. The final results on this are in the patch TableFormatting --MarkButler 2004/01/16



UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited April 22, 2007 9:38 pm by MarkusLude (diff)
Search: