]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/Gwiki.pm
adding bzip2 support in ruby
[biopieces.git] / code_perl / Maasha / Gwiki.pm
index 3bfdc59f64835248e2ff0f47eb28ae5434d2c926..f9321c80148f7bf33e86d5643d7e2495cb5e477d 100644 (file)
@@ -1,6 +1,6 @@
 package Maasha::Gwiki;
 
-# Copyright (C) 2008 Martin A. Hansen.
+# Copyright (C) 2008-2009 Martin A. Hansen.
 
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -28,10 +28,12 @@ package Maasha::Gwiki;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+use warnings;
 use strict;
 use Data::Dumper;
 use Term::ANSIColor;
 use Maasha::Common;
+use Maasha::Filesys;
 use vars qw ( @ISA @EXPORT );
 
 @ISA = qw( Exporter );
@@ -51,7 +53,7 @@ sub gwiki2ascii
 
     # Returns a list of lines.
 
-    my ( $block, $triple, $line, @lines );
+    my ( $block, $triple, $line, @lines, $i );
 
     foreach $block ( @{ $wiki } )
     {
@@ -63,13 +65,31 @@ sub gwiki2ascii
         {
             push @lines, text_bold( "$block->[ 0 ]->{ 'TEXT' }" );
         }
+        elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "summary" )
+        {
+            $block->[ 0 ]->{ 'TEXT' } =~ s/^#summary\s+//;
+
+            push @lines, text_bold( "Summary" ), "\n$block->[ 0 ]->{ 'TEXT' }";
+        }
         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "level_3" )
         {
             push @lines, "$block->[ 0 ]->{ 'TEXT' }";
         }
         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "verbatim" )
         {
-            map { push @lines, "   $_->{ 'TEXT' }" } @{ $block };
+            map { push @lines, text_white( "   $_->{ 'TEXT' }" ) } @{ $block };
+        }
+        elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "itemize" )
+        {
+            for ( $i = 0; $i < @{ $block }; $i++ ) {
+                push @lines, "   * $block->[ $i ]->{ 'TEXT' }";
+            }
+        }
+        elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "enumerate" )
+        {
+            for ( $i = 0; $i < @{ $block }; $i++ ) {
+                push @lines, "   " . ( $i + 1 ) . ". $block->[ $i ]->{ 'TEXT' }";
+            }
         }
         elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "paragraph" )
         {
@@ -77,17 +97,22 @@ sub gwiki2ascii
             {
                 $line = $triple->{ 'TEXT' };
 
+                $line =~ s/!(\w)/$1/g;
                 $line =~ s/^\s*//;
                 $line =~ s/\s*$//;
                 $line =~ s/\s+/ /g;
                 $line =~ tr/`//d;
-                $line =~ s/\[([^\]]+?)\]/$1/g;
-                $line =~ s/\*([^\*]+?)\*/&text_bold($1)/ge;
-                $line =~ s/_([^_]+?)_/&text_underline($1)/ge;
+                $line =~ s/\[([^\]]+?)\]/&text_underline($1)/ge;
+                $line =~ s/\*(\w+)\*/&text_bold($1)/ge           if $line =~ /(^| )\*\w+\*( |$)/;
+                $line =~ s/_(\w+)_/&text_underline($1)/ge        if $line =~ /(^| )_\w+_( |$)/;
 
-                push @lines, $_ foreach &Maasha::Common::wrap_line( $line, 80 );
+                push @lines, $_ foreach Maasha::Common::wrap_line( $line, 80 );
             }
         }
+        elsif ( $block->[ 0 ]->{ 'FORMAT' } eq "whitespace" )
+        {
+            push @lines, "";
+        }
     }
 
     return wantarray ? @lines : \@lines;
@@ -110,9 +135,9 @@ sub gwiki_read
 
     # Returns data structure.
 
-    my ( $fh, @lines, $i, $c, $section, @block, @output );
+    my ( $fh, @lines, $i, $c, $section, $paragraph, @block, @output );
 
-    $fh = &Maasha::Common::read_open( $file );
+    $fh = Maasha::Filesys::file_read_open( $file );
 
     @lines = <$fh>;
     
@@ -127,7 +152,17 @@ sub gwiki_read
     {
         undef @block;
 
-        if ( $lines[ $i ] =~ /^===\s*(.+)\s*===$/ )
+        if ( $lines[ $i ] =~ /(#summary.+)/ ) # TODO: unsolved problem with anchor!
+        {
+            $section = $1;
+
+            push @block, {
+                TEXT    => $section,
+                SECTION => $section,
+                FORMAT  => "summary",
+            };
+        }
+        elsif ( $lines[ $i ] =~ /^===\s*(.+)\s*===$/ )
         {
             $section = $1;
 
@@ -171,20 +206,71 @@ sub gwiki_read
 
                 $c++;
             }
+
+            $c++;
+        }
+        elsif ( $lines[ $i ] =~ /^\s{1,}\*\s*.+/ )
+        {
+            $c = $i;
+
+            while ( $lines[ $c ] =~ /^\s{1,}\*\s*(.+)/ )
+            {
+                push @block, {
+                    TEXT    => $1,
+                    SECTION => $section,
+                    FORMAT  => "itemize"
+                };
+
+                $c++;
+            }
         }
-        else
+        elsif ( $lines[ $i ] =~ /^\s{1,}#\s*.+/ )
         {
+            $c = $i;
+
+            while ( $lines[ $c ] =~ /^\s{1,}#\s*(.+)/ )
+            {
+                push @block, {
+                    TEXT    => $1,
+                    SECTION => $section,
+                    FORMAT  => "enumerate"
+                };
+
+                $c++;
+            }
+        }
+        elsif ( $lines[ $i ] !~ /^\s*$/ )
+        {
+            undef $paragraph;
+
+            $c = $i;
+
+            while ( defined $lines[ $c ] and $lines[ $c ] !~ /^\s*$/ )
+            {
+                $paragraph .= " $lines[ $c ]";
+
+                $c++;
+            }
+
             push @block, {
-                TEXT    => $lines[ $i ],
+                TEXT    => $paragraph,
                 SECTION => $section,
                 FORMAT  => "paragraph",
             };
         }
+        elsif ( $lines[ $i ] =~ /^\s*$/ )
+        {
+            push @block, {
+                TEXT    => "",
+                SECTION => $section,
+                FORMAT  => "whitespace",
+            };
+        }
 
         push @output, [ @block ], if @block;
 
         if ( $c > $i ) {
-            $i = $c + 1;
+            $i = $c;
         } else {
             $i++;
         }
@@ -199,7 +285,7 @@ sub text_bold
     my ( $txt,
        ) = @_;
 
-    return colored ( $txt, "bold white" );
+    return colored( $txt, "bold" );
 }
 
 
@@ -208,7 +294,15 @@ sub text_underline
     my ( $txt,
        ) = @_;
 
-    return colored ( $txt, "underline" );
+    return colored( $txt, "underline" );
+}
+
+sub text_white
+{
+    my ( $txt,
+       ) = @_;
+
+    return colored( $txt, "white" );
 }