3 # Copyright (C) 2008 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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
35 use vars qw ( @ISA @EXPORT );
37 @ISA = qw( Exporter );
40 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
45 # Martin A. Hansen, June 2008.
47 # Convert Google style wiki as ASCII text lines.
49 my ( $wiki, # wiki data structure
52 # Returns a list of lines.
54 my ( $block, $triple, $line, @lines, $i );
56 foreach $block ( @{ $wiki } )
58 if ( $block->[ 0 ]->{ 'FORMAT' } eq "heading" )
60 push @lines, text_underline( text_bold( "\n$block->[ 0 ]->{ 'TEXT' }" ) );
62 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "subheading" )
64 push @lines, text_bold( "$block->[ 0 ]->{ 'TEXT' }" );
66 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "level_3" )
68 push @lines, "$block->[ 0 ]->{ 'TEXT' }";
70 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "verbatim" )
72 map { push @lines, " $_->{ 'TEXT' }" } @{ $block };
74 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "itemize" )
76 for ( $i = 0; $i < @{ $block }; $i++ ) {
77 push @lines, " * $block->[ $i ]->{ 'TEXT' }";
80 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "enumerate" )
82 for ( $i = 0; $i < @{ $block }; $i++ ) {
83 push @lines, " " . ( $i + 1 ) . ". $block->[ $i ]->{ 'TEXT' }";
86 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "paragraph" )
88 foreach $triple ( @{ $block } )
90 $line = $triple->{ 'TEXT' };
92 $line =~ s/!(\w)/$1/g;
97 $line =~ s/\[([^\]]+?)\]/&text_underline($1)/ge;
98 $line =~ s/\*(\w+)\*/&text_bold($1)/ge if $line =~ /(^| )\*\w+\*( |$)/;
99 $line =~ s/_(\w+)_/&text_underline($1)/ge if $line =~ /(^| )_\w+_( |$)/;
101 push @lines, $_ foreach &Maasha::Common::wrap_line( $line, 80 );
104 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "whitespace" )
110 return wantarray ? @lines : \@lines;
116 # Martin A. Hansen, June 2008.
118 # Parses a subset of features from Googles wiki format
119 # into a data structure. The structure consists of a
120 # list of blocks. Each block consists of one or more lines,
121 # represented as triples with the line text, section, and format option.
123 # http://code.google.com/p/support/wiki/WikiSyntax
125 my ( $file, # file to parse
128 # Returns data structure.
130 my ( $fh, @lines, $i, $c, $section, $paragraph, @block, @output );
132 $fh = &Maasha::Common::read_open( $file );
143 while ( $i < @lines )
147 if ( $lines[ $i ] =~ /^===\s*(.+)\s*===$/ )
157 elsif ( $lines[ $i ] =~ /^==\s*(.+)\s*==$/ )
164 FORMAT => "subheading",
167 elsif ( $lines[ $i ] =~ /^=\s*(.+)\s*=$/ )
177 elsif ( $lines[ $i ] =~ /^\{\{\{$/ )
181 while ( $lines[ $c ] !~ /^\}\}\}$/ )
184 TEXT => $lines[ $c ],
186 FORMAT => "verbatim",
194 elsif ( $lines[ $i ] =~ /^\s{1,}\*\s*.+/ )
198 while ( $lines[ $c ] =~ /^\s{1,}\*\s*(.+)/ )
209 elsif ( $lines[ $i ] =~ /^\s{1,}#\s*.+/ )
213 while ( $lines[ $c ] =~ /^\s{1,}#\s*(.+)/ )
218 FORMAT => "enumerate"
224 elsif ( $lines[ $i ] !~ /^\s*$/ )
230 while ( $lines[ $c ] !~ /^\s*$/ )
232 $paragraph .= " $lines[ $c ]";
240 FORMAT => "paragraph",
243 elsif ( $lines[ $i ] =~ /^\s*$/ )
248 FORMAT => "whitespace",
252 push @output, [ @block ], if @block;
261 return wantarray ? @output : \@output;
270 return colored ( $txt, "bold" );
279 return colored ( $txt, "underline" );
283 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<