From 0d108e74d9b48ba080c128407eed3c787edec1bd Mon Sep 17 00:00:00 2001 From: joey Date: Wed, 7 Mar 2007 19:07:36 +0000 Subject: [PATCH] further refinement --- IkiWiki/Plugin/table.pm | 121 ++++++++++++++-------------------------- doc/plugins/table.mdwn | 6 +- 2 files changed, 44 insertions(+), 83 deletions(-) diff --git a/IkiWiki/Plugin/table.pm b/IkiWiki/Plugin/table.pm index dfa595812..c5404aa85 100644 --- a/IkiWiki/Plugin/table.pm +++ b/IkiWiki/Plugin/table.pm @@ -15,16 +15,9 @@ sub preprocess (@) { #{{{ my %params =( format => 'auto', header => 'yes', - sep_char => { - 'csv' => ',', - 'dsv' => '|', - }, @_ ); - if (exists $params{delimiter}) { - $params{sep_char}->{$params{format}} = $params{delimiter}; - } if (exists $params{file}) { if (! $pagesources{$params{file}}) { return "[[table ".gettext("cannot find file")."]]"; @@ -46,15 +39,15 @@ sub preprocess (@) { #{{{ my @data; if (lc $params{format} eq 'csv') { - @data=read_csv(\%params); + @data=split_csv($params{data}, $params{delimiter}); } elsif (lc $params{format} eq 'dsv') { - @data=read_dsv(\%params); + @data=split_dsv($params{data}, $params{delimiter}); } else { return "[[table ".gettext("unknown data format")."]]"; } - + my $header; if (lc($params{header}) eq "yes") { $header=shift @data; @@ -63,9 +56,30 @@ sub preprocess (@) { #{{{ return "[[table ".gettext("empty data")."]]"; } - my $html = tidy_up(open_table(\%params, $header), - build_rows(\%params, @data), - close_table(\%params, $header)); + my @lines; + push @lines, defined $params{class} + ? "' + : '
'; + push @lines, "\t","\t\t", + (map { + "\t\t\t" + } @$header), + "\t\t", "\t" if defined $header; + push @lines, "\t"; + foreach my $record (@data) { + push @lines, "\t\t", + (map { + "\t\t\t" + } @$record), + "\t\t"; + } + push @lines, "\t" if defined $header; + push @lines, '
". + htmlize($params{page}, $params{destpage}, $_). + "
". + htmlize($params{page}, $params{destpage}, $_). + "
'; + my $html = join("\n", @lines); if (exists $params{file}) { return $html."\n\n". @@ -77,20 +91,6 @@ sub preprocess (@) { #{{{ } } #}}} -sub tidy_up (@) { #{{{ - my $html=""; - - foreach my $text (@_) { - my $indentation = $text =~ m{thead>|tbody>} ? 0 : - $text =~ m{tr>} ? 4 : - $text =~ m{td>|th>} ? 8 : - 0; - $html .= (' ' x $indentation)."$text\n"; - } - - return $html; -} #}}} - sub is_dsv_data ($) { #{{{ my $text = shift; @@ -98,14 +98,14 @@ sub is_dsv_data ($) { #{{{ return $line =~ m{.+\|}; } -sub read_csv ($) { #{{{ - my $params=shift; - my @text_lines = split(/\n/, $params->{data}); +sub split_csv ($$) { #{{{ + my @text_lines = split(/\n/, shift); + my $delimiter = shift; eval q{use Text::CSV}; error($@) if $@; my $csv = Text::CSV->new({ - sep_char => $params->{sep_char}->{csv}, + sep_char => defined $delimiter ? $delimiter : ",", binary => 1, }) || error("could not create a Text::CSV object"); @@ -125,65 +125,26 @@ sub read_csv ($) { #{{{ return @data; } #}}} -sub read_dsv ($) { #{{{ - my $params = shift; - my @text_lines = split(/\n/, $params->{data}); +sub split_dsv ($$) { #{{{ + my @text_lines = split(/\n/, shift); + my $delimiter = shift; + $delimiter="|" unless defined $delimiter; my @data; - my $splitter = qr{\Q$params->{sep_char}->{dsv}\E}; foreach my $line (@text_lines) { - push @data, [ split($splitter, $line) ]; + push @data, [ split(/\Q$delimiter\E/, $line) ]; } return @data; } #}}} -sub open_table ($$) { #{{{ - my $params = shift; - my $header = shift; - - my @items; - push @items, defined $params->{class} - ? "{class}.'">' - : '
'; - push @items, '','', - (map { "" } @$header), - '','' if defined $header; - push @items, ''; - - return @items; -} - -sub build_rows ($@) { #{{{ - my $params = shift; - - my @items; - foreach my $record (@_) { - push @items, '', - (map { "" } @$record), - ''; - } - return @items; -} #}}} - -sub close_table ($$) { #{{{ - my $params = shift; - my $header = shift; - - my @items; - push @items, '' if defined $header; - push @items, '
".htmlize($params, $_)."
".htmlize($params, $_)."
'; - return @items; -} #}}} - -sub htmlize { #{{{ - my $params = shift; +sub htmlize ($$$){ #{{{ + my $page = shift; + my $destpage = shift; my $text = shift; - $text=IkiWiki::preprocess($params->{page}, - $params->{destpage}, $text); - $text=IkiWiki::htmlize($params->{page}, - pagetype($pagesources{$params->{page}}), $text); + $text=IkiWiki::htmlize($page, pagetype($pagesources{$page}), + IkiWiki::preprocess($page, $destpage, $text)); # hack to get rid of enclosing junk added by markdown $text=~s!^

!!; diff --git a/doc/plugins/table.mdwn b/doc/plugins/table.mdwn index 569e56982..e84f6f947 100644 --- a/doc/plugins/table.mdwn +++ b/doc/plugins/table.mdwn @@ -9,9 +9,9 @@ It needs the perl module [[cpan Text::CSV]] for the CSV data. ## examples \[[table data=""" - Customer|Amount| - Fulanito|134,34| - Menganito|234,56| + Customer|Amount + Fulanito|134,34 + Menganito|234,56 """]] \[[table class="book_record" format=csv file="data/books/record1"]] -- 2.44.0