3 # Copyright (C) 2008-2009 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Routines for manipulation of Google's wiki format
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37 use vars qw ( @ISA @EXPORT );
39 @ISA = qw( Exporter );
42 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
47 # Martin A. Hansen, June 2008.
49 # Convert Google style wiki as ASCII text lines.
51 my ( $wiki, # wiki data structure
54 # Returns a list of lines.
56 my ( $block, $triple, $line, @lines, $i );
58 foreach $block ( @{ $wiki } )
60 if ( $block->[ 0 ]->{ 'FORMAT' } eq "heading" )
62 push @lines, text_underline( text_bold( "\n$block->[ 0 ]->{ 'TEXT' }" ) );
64 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "subheading" )
66 push @lines, text_bold( "$block->[ 0 ]->{ 'TEXT' }" );
68 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "summary" )
70 $block->[ 0 ]->{ 'TEXT' } =~ s/^#summary\s+//;
72 push @lines, text_bold( "Summary" ), "\n$block->[ 0 ]->{ 'TEXT' }";
74 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "level_3" )
76 push @lines, "$block->[ 0 ]->{ 'TEXT' }";
78 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "verbatim" )
80 map { push @lines, text_white( " $_->{ 'TEXT' }" ) } @{ $block };
82 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "itemize" )
84 for ( $i = 0; $i < @{ $block }; $i++ ) {
85 push @lines, " * $block->[ $i ]->{ 'TEXT' }";
88 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "enumerate" )
90 for ( $i = 0; $i < @{ $block }; $i++ ) {
91 push @lines, " " . ( $i + 1 ) . ". $block->[ $i ]->{ 'TEXT' }";
94 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "paragraph" )
96 foreach $triple ( @{ $block } )
98 $line = $triple->{ 'TEXT' };
100 $line =~ s/!(\w)/$1/g;
105 $line =~ s/\[([^\]]+?)\]/&text_underline($1)/ge;
106 $line =~ s/\*(\w+)\*/&text_bold($1)/ge if $line =~ /(^| )\*\w+\*( |$)/;
107 $line =~ s/_(\w+)_/&text_underline($1)/ge if $line =~ /(^| )_\w+_( |$)/;
109 push @lines, $_ foreach Maasha::Common::wrap_line( $line, 80 );
112 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "whitespace" )
118 return wantarray ? @lines : \@lines;
124 # Martin A. Hansen, June 2008.
126 # Parses a subset of features from Googles wiki format
127 # into a data structure. The structure consists of a
128 # list of blocks. Each block consists of one or more lines,
129 # represented as triples with the line text, section, and format option.
131 # http://code.google.com/p/support/wiki/WikiSyntax
133 my ( $file, # file to parse
136 # Returns data structure.
138 my ( $fh, @lines, $i, $c, $section, $paragraph, @block, @output );
140 $fh = Maasha::Filesys::file_read_open( $file );
151 while ( $i < @lines )
155 if ( $lines[ $i ] =~ /(#summary.+)/ ) # TODO: unsolved problem with anchor!
165 elsif ( $lines[ $i ] =~ /^===\s*(.+)\s*===$/ )
175 elsif ( $lines[ $i ] =~ /^==\s*(.+)\s*==$/ )
182 FORMAT => "subheading",
185 elsif ( $lines[ $i ] =~ /^=\s*(.+)\s*=$/ )
195 elsif ( $lines[ $i ] =~ /^\{\{\{$/ )
199 while ( $lines[ $c ] !~ /^\}\}\}$/ )
202 TEXT => $lines[ $c ],
204 FORMAT => "verbatim",
212 elsif ( $lines[ $i ] =~ /^\s{1,}\*\s*.+/ )
216 while ( $lines[ $c ] =~ /^\s{1,}\*\s*(.+)/ )
227 elsif ( $lines[ $i ] =~ /^\s{1,}#\s*.+/ )
231 while ( $lines[ $c ] =~ /^\s{1,}#\s*(.+)/ )
236 FORMAT => "enumerate"
242 elsif ( $lines[ $i ] !~ /^\s*$/ )
248 while ( defined $lines[ $c ] and $lines[ $c ] !~ /^\s*$/ )
250 $paragraph .= " $lines[ $c ]";
258 FORMAT => "paragraph",
261 elsif ( $lines[ $i ] =~ /^\s*$/ )
266 FORMAT => "whitespace",
270 push @output, [ @block ], if @block;
279 return wantarray ? @output : \@output;
288 return colored( $txt, "bold" );
297 return colored( $txt, "underline" );
305 return colored( $txt, "white" );
309 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<