Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
WARNING: the following code is a hack and doesn't meet many quality standards.˛

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

Footnotes:

1) which I found too late like Org::Parser

2) DOGMA: Release_early,_release_often


In reply to ORG to POD translator by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (14)
    As of 2014-07-29 10:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (213 votes), past polls