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