WARNING: the following code is a hack and doesn't meet many quality standards.˛
Footnotes:
Hi
Emacs's org-mode is very convenient to organize and transform documents.
So when asked to produce 2 articles for the German Perl magazine $foo I started sketching and outlining ideas in org-mode. But then I needed to produce a special POD format for printing
It was easier to hack the following script to just translate the markups I needed.
There are already ORG-Parsers on CPANš but I needed a lightweight solution which allows to mix in some POD markups, and to DWIM-add some other markups to facilitate the creative process. It's far from being complete or error free, but fits my needs.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper qw/Dumper/; my $outfile= my $infile= $ARGV[0]; $outfile =~ s/\.org$/.pod/; my $f_in; if ($infile) { print "* Processing $infile => $outfile\n"; open $f_in , "<", "$infile" ; } else { $outfile="/tmp/test.pod"; print "* Processing __DATA__ => $outfile\n"; $f_in=\*DATA; } open my $f_out, ">", "$outfile"; my $OUT; my $r_begin_src='^\s*#\+BEGIN_SRC (\w*)\s*$'; my $r_end_src = '^\s*#\+END_SRC\s*$'; print $f_out "\n=encoding utf8\n\n"; #--- First pass while(<$f_in>) { #--- Codeblock? if ( /$r_begin_src/ .. /$r_end_src/) { s/($r_end_src|$r_begin_src)/\n/; $_=" $_"; # add indentation s#\s*Listing{(\w+)}#listing_grep($1)#gie; } else { #--- Heading? if (s/^(\*+)(\s.*)/"\n=head".length($1)."$2\n"/e ) { #--- Heading == "__DATA__" ? last if $2 =~ /^\s*__DATA__\s*$/; # stop parsing } else { #--- Textbody s/^\s+(\S)/$1/; # delete indentation s/^\s+$/\n/; # delete empty lines convert_markup(); } } $OUT.=$_; } listing_dump(); #--- Second pass $OUT =~ s#Listing{(\w+)}#listing_ref($1)#gie; #--- Output print $f_out $OUT; ##--- Process pod-file #my $do=`make.pl $outfile`; exit; # ---------------------------------------- sub convert_markup { my $in=$_; my $out; my $notPOD=0; # flipflop #--- ignore POD markups for (split /([CBIEZ]<.*?>)/,$in){ if ( $notPOD ^= 1 ) { # odd => not Pod #--- translate s#/(.+?)/#I<$1>#g; # / -> Italic s#\*(.+?)\*#B<$1>#g; # * -> Bold # s#_(.+?)_#I<$1>#g; # _ -> Underline #--- add markup DWIM s#(?<!<)(\w+(::\w+)+)(?!>)# L<$1> #g; # -> L<Mod::ule> s#([\$\%\@\&]\w+)#C<$1>#g; # -> C<$var> } $out.=$_; } $_=$out; } # ---------------------------------------- # name and refrence listings by name # "LISTING{label}" in code -> incremented number # "LISTING{label}" in text -> reference code listing # TODO: # * support org reference markup instead # like <<label>> or (ref: label) # * more checks for possible typos in labels my %listing_nr; # Number hash my $listing_c; # Counter sub listing_dump { # check Listing hash print Dumper \%listing_nr; } sub listing_grep { my $name=uc(shift); # insensitif $listing_nr{$name} = ++$listing_c; $name=qq{Listing $listing_c}; $name= " "x(40-length($name)) .$name; # align right return $name; } sub listing_ref { my $name=uc(shift); # insensitif $listing_c = $listing_nr{$name}; warn "Listing $name unknown!" unless defined $listing_c; $name=qq{Listing $listing_c}; return $name; } __DATA__ * Example Org for testing ** heading 2 Text in C<Path/path/path> might be /Italic/ or *Bold* and org-markup nested in POD-markup is I<*ignored*>. #+BEGIN_SRC Perl print("huhu") while(1); LISTING{huhu} #+END_SRC bla bla ... and the code in LISTING{huhu} prints "huhu" * __DATA__ this text will not be processed anymore
Cheers Rolf
1) which I found too late like Org::Parser
2) DOGMA: Release_early,_release_often
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: ORG to POD translator
by MidLifeXis (Monsignor) on Apr 07, 2011 at 16:35 UTC | |
by LanX (Saint) on Apr 07, 2011 at 17:08 UTC | |
by MidLifeXis (Monsignor) on Apr 07, 2011 at 22:33 UTC | |
by LanX (Saint) on Apr 07, 2011 at 22:42 UTC | |
by MidLifeXis (Monsignor) on Apr 07, 2011 at 22:59 UTC | |
by MidLifeXis (Monsignor) on Apr 07, 2011 at 23:12 UTC | |
by LanX (Saint) on Apr 08, 2011 at 01:15 UTC | |
Re: ORG to POD translator
by educated_foo (Vicar) on Apr 07, 2011 at 17:37 UTC | |
by LanX (Saint) on Apr 12, 2011 at 13:08 UTC |
Back to
Cool Uses for Perl