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
Footnotes:
1) which I found too late like Org::Parser
2) DOGMA: Release_early,_release_often
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|