]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/Gwiki.pm
more wikis
[biopieces.git] / code_perl / Maasha / Gwiki.pm
1 package Maasha::Gwiki;
2
3 # Copyright (C) 2008 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24
25 # Routines for manipulation of Google's wiki format
26
27
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29
30
31 use strict;
32 use Data::Dumper;
33 use Term::ANSIColor;
34 use Maasha::Common;
35 use vars qw ( @ISA @EXPORT );
36
37 @ISA = qw( Exporter );
38
39
40 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41
42
43 sub gwiki2ascii
44 {
45     # Martin A. Hansen, June 2008.
46
47     # Convert Google style wiki as ASCII text lines.
48
49     my ( $wiki,  # wiki data structure 
50        ) = @_;
51
52     # Returns a list of lines.
53
54     my ( $block, $triple, $line, @lines, $i );
55
56     foreach $block ( @{ $wiki } )
57     {
58         if ( $block->[ 0 ]->{ 'FORMAT' } eq "heading" )
59         {
60             push @lines, text_underline( text_bold( "\n$block->[ 0 ]->{ 'TEXT' }" ) );
61         }
62         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "subheading" )
63         {
64             push @lines, text_bold( "$block->[ 0 ]->{ 'TEXT' }" );
65         }
66         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "level_3" )
67         {
68             push @lines, "$block->[ 0 ]->{ 'TEXT' }";
69         }
70         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "verbatim" )
71         {
72             map { push @lines, "   $_->{ 'TEXT' }" } @{ $block };
73         }
74         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "itemize" )
75         {
76             for ( $i = 0; $i < @{ $block }; $i++ ) {
77                 push @lines, "   * $block->[ $i ]->{ 'TEXT' }";
78             }
79         }
80         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "enumerate" )
81         {
82             for ( $i = 0; $i < @{ $block }; $i++ ) {
83                 push @lines, "   " . ( $i + 1 ) . ". $block->[ $i ]->{ 'TEXT' }";
84             }
85         }
86         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "paragraph" )
87         {
88             foreach $triple ( @{ $block } )
89             {
90                 $line = $triple->{ 'TEXT' };
91
92                 $line =~ s/^\s*//;
93                 $line =~ s/\s*$//;
94                 $line =~ s/\s+/ /g;
95                 $line =~ tr/`//d;
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+_( |$)/;
99
100                 push @lines, $_ foreach &Maasha::Common::wrap_line( $line, 80 );
101             }
102         }
103         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "whitespace" )
104         {
105             push @lines, "";
106         }
107     }
108
109     return wantarray ? @lines : \@lines;
110 }
111
112
113 sub gwiki_read
114 {
115     # Martin A. Hansen, June 2008.
116
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.
121
122     # http://code.google.com/p/support/wiki/WikiSyntax
123
124     my ( $file,   # file to parse
125        ) = @_;
126
127     # Returns data structure.
128
129     my ( $fh, @lines, $i, $c, $section, $paragraph, @block, @output );
130
131     $fh = &Maasha::Common::read_open( $file );
132
133     @lines = <$fh>;
134     
135     chomp @lines;
136
137     close $fh;
138
139     $i = 0;
140     $c = 0;
141
142     while ( $i < @lines )
143     {
144         undef @block;
145
146         if ( $lines[ $i ] =~ /^===\s*(.+)\s*===$/ )
147         {
148             $section = $1;
149
150             push @block, {
151                 TEXT    => $section,
152                 SECTION => $section,
153                 FORMAT  => "level_3",
154             };
155         }
156         elsif ( $lines[ $i ] =~ /^==\s*(.+)\s*==$/ )
157         {
158             $section = $1;
159
160             push @block, {
161                 TEXT    => $section,
162                 SECTION => $section,
163                 FORMAT  => "subheading",
164             };
165         }
166         elsif ( $lines[ $i ] =~ /^=\s*(.+)\s*=$/ )
167         {
168             $section = $1;
169
170             push @block, {
171                 TEXT    => $section,
172                 SECTION => $section,
173                 FORMAT  => "heading",
174             };
175         }
176         elsif ( $lines[ $i ] =~ /^\{\{\{$/ )
177         {
178             $c = $i + 1;
179
180             while ( $lines[ $c ] !~ /^\}\}\}$/ )
181             {
182                 push @block, {
183                     TEXT    => $lines[ $c ],
184                     SECTION => $section,
185                     FORMAT  => "verbatim",
186                 };
187
188                 $c++;
189             }
190
191             $c++;
192         }
193         elsif ( $lines[ $i ] =~ /^\s{1,}\*\s*.+/ )
194         {
195             $c = $i;
196
197             while ( $lines[ $c ] =~ /^\s{1,}\*\s*(.+)/ )
198             {
199                 push @block, {
200                     TEXT    => $1,
201                     SECTION => $section,
202                     FORMAT  => "itemize"
203                 };
204
205                 $c++;
206             }
207         }
208         elsif ( $lines[ $i ] =~ /^\s{1,}#\s*.+/ )
209         {
210             $c = $i;
211
212             while ( $lines[ $c ] =~ /^\s{1,}#\s*(.+)/ )
213             {
214                 push @block, {
215                     TEXT    => $1,
216                     SECTION => $section,
217                     FORMAT  => "enumerate"
218                 };
219
220                 $c++;
221             }
222         }
223         elsif ( $lines[ $i ] !~ /^\s*$/ )
224         {
225             undef $paragraph;
226
227             $c = $i;
228
229             while ( $lines[ $c ] !~ /^\s*$/ )
230             {
231                 $paragraph .= " $lines[ $c ]";
232
233                 $c++;
234             }
235
236             push @block, {
237                 TEXT    => $paragraph,
238                 SECTION => $section,
239                 FORMAT  => "paragraph",
240             };
241         }
242         elsif ( $lines[ $i ] =~ /^\s*$/ )
243         {
244             push @block, {
245                 TEXT    => "",
246                 SECTION => $section,
247                 FORMAT  => "whitespace",
248             };
249         }
250
251         push @output, [ @block ], if @block;
252
253         if ( $c > $i ) {
254             $i = $c;
255         } else {
256             $i++;
257         }
258     }
259
260     return wantarray ? @output : \@output;
261 }
262
263
264 sub text_bold
265 {
266     my ( $txt,
267        ) = @_;
268
269     return colored ( $txt, "bold" );
270 }
271
272
273 sub text_underline
274 {
275     my ( $txt,
276        ) = @_;
277
278     return colored ( $txt, "underline" );
279 }
280
281
282 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
283