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' };
96 $line =~ s/\[([^\]]+?)\]/&text_underline($1)/ge;
97 $line =~ s/\*(\w+)\*/&text_bold($1)/ge if $line =~ /(^| )\*\w+\*( |$)/;
98 $line =~ s/_(\w+)_/&text_underline($1)/ge if $line =~ /(^| )_\w+_( |$)/;
100 push @lines, $_ foreach &Maasha::Common::wrap_line( $line, 80 );
103 elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "whitespace" )
109 return wantarray ? @lines : \@lines;
115 # Martin A. Hansen, June 2008.
117 # Parses a subset of features from Googles wiki format
118 # into a data structure. The structure consists of a
119 # list of blocks. Each block consists of one or more lines,
120 # represented as triples with the line text, section, and format option.
122 # http://code.google.com/p/support/wiki/WikiSyntax
124 my ( $file, # file to parse
127 # Returns data structure.
129 my ( $fh, @lines, $i, $c, $section, $paragraph, @block, @output );
131 $fh = &Maasha::Common::read_open( $file );
142 while ( $i < @lines )
146 if ( $lines[ $i ] =~ /^===\s*(.+)\s*===$/ )
156 elsif ( $lines[ $i ] =~ /^==\s*(.+)\s*==$/ )
163 FORMAT => "subheading",
166 elsif ( $lines[ $i ] =~ /^=\s*(.+)\s*=$/ )
176 elsif ( $lines[ $i ] =~ /^\{\{\{$/ )
180 while ( $lines[ $c ] !~ /^\}\}\}$/ )
183 TEXT => $lines[ $c ],
185 FORMAT => "verbatim",
193 elsif ( $lines[ $i ] =~ /^\s{1,}\*\s*.+/ )
197 while ( $lines[ $c ] =~ /^\s{1,}\*\s*(.+)/ )
208 elsif ( $lines[ $i ] =~ /^\s{1,}#\s*.+/ )
212 while ( $lines[ $c ] =~ /^\s{1,}#\s*(.+)/ )
217 FORMAT => "enumerate"
223 elsif ( $lines[ $i ] !~ /^\s*$/ )
229 while ( $lines[ $c ] !~ /^\s*$/ )
231 $paragraph .= " $lines[ $c ]";
239 FORMAT => "paragraph",
242 elsif ( $lines[ $i ] =~ /^\s*$/ )
247 FORMAT => "whitespace",
251 push @output, [ @block ], if @block;
260 return wantarray ? @output : \@output;
269 return colored ( $txt, "bold" );
278 return colored ( $txt, "underline" );
282 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<