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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
36 use vars qw ( @ISA @EXPORT );
38 @ISA = qw( Exporter );
41 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
46 # Martin A. Hansen, June 2008.
48 # Convert Google style wiki as ASCII text lines.
50 my ( $wiki, # wiki data structure
53 # Returns a list of lines.
55 my ( $block, $triple, $line, @lines, $i );
57 foreach $block ( @{ $wiki } )
59 if ( $block->[ 0 ]->{ 'FORMAT' } eq "heading" )
61 push @lines, text_underline( text_bold( "\n$block->[ 0 ]->{ 'TEXT' }" ) );
63 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "subheading" )
65 push @lines, text_bold( "$block->[ 0 ]->{ 'TEXT' }" );
67 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "summary" )
69 $block->[ 0 ]->{ 'TEXT' } =~ s/^#summary\s+//;
71 push @lines, text_bold( "Summary" ), "\n$block->[ 0 ]->{ 'TEXT' }";
73 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "level_3" )
75 push @lines, "$block->[ 0 ]->{ 'TEXT' }";
77 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "verbatim" )
79 map { push @lines, " $_->{ 'TEXT' }" } @{ $block };
81 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "itemize" )
83 for ( $i = 0; $i < @{ $block }; $i++ ) {
84 push @lines, " * $block->[ $i ]->{ 'TEXT' }";
87 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "enumerate" )
89 for ( $i = 0; $i < @{ $block }; $i++ ) {
90 push @lines, " " . ( $i + 1 ) . ". $block->[ $i ]->{ 'TEXT' }";
93 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "paragraph" )
95 foreach $triple ( @{ $block } )
97 $line = $triple->{ 'TEXT' };
99 $line =~ s/!(\w)/$1/g;
104 $line =~ s/\[([^\]]+?)\]/&text_underline($1)/ge;
105 $line =~ s/\*(\w+)\*/&text_bold($1)/ge if $line =~ /(^| )\*\w+\*( |$)/;
106 $line =~ s/_(\w+)_/&text_underline($1)/ge if $line =~ /(^| )_\w+_( |$)/;
108 push @lines, $_ foreach Maasha::Common::wrap_line( $line, 80 );
111 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "whitespace" )
117 return wantarray ? @lines : \@lines;
123 # Martin A. Hansen, June 2008.
125 # Parses a subset of features from Googles wiki format
126 # into a data structure. The structure consists of a
127 # list of blocks. Each block consists of one or more lines,
128 # represented as triples with the line text, section, and format option.
130 # http://code.google.com/p/support/wiki/WikiSyntax
132 my ( $file, # file to parse
135 # Returns data structure.
137 my ( $fh, @lines, $i, $c, $section, $paragraph, @block, @output );
139 $fh = Maasha::Filesys::file_read_open( $file );
150 while ( $i < @lines )
154 if ( $lines[ $i ] =~ /^(#summary.+)/ )
164 elsif ( $lines[ $i ] =~ /^===\s*(.+)\s*===$/ )
174 elsif ( $lines[ $i ] =~ /^==\s*(.+)\s*==$/ )
181 FORMAT => "subheading",
184 elsif ( $lines[ $i ] =~ /^=\s*(.+)\s*=$/ )
194 elsif ( $lines[ $i ] =~ /^\{\{\{$/ )
198 while ( $lines[ $c ] !~ /^\}\}\}$/ )
201 TEXT => $lines[ $c ],
203 FORMAT => "verbatim",
211 elsif ( $lines[ $i ] =~ /^\s{1,}\*\s*.+/ )
215 while ( $lines[ $c ] =~ /^\s{1,}\*\s*(.+)/ )
226 elsif ( $lines[ $i ] =~ /^\s{1,}#\s*.+/ )
230 while ( $lines[ $c ] =~ /^\s{1,}#\s*(.+)/ )
235 FORMAT => "enumerate"
241 elsif ( $lines[ $i ] !~ /^\s*$/ )
247 while ( defined $lines[ $c ] and $lines[ $c ] !~ /^\s*$/ )
249 $paragraph .= " $lines[ $c ]";
257 FORMAT => "paragraph",
260 elsif ( $lines[ $i ] =~ /^\s*$/ )
265 FORMAT => "whitespace",
269 push @output, [ @block ], if @block;
278 return wantarray ? @output : \@output;
287 return colored ( $txt, "bold" );
296 return colored ( $txt, "underline" );
300 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<