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