Permalink
Browse files

proof of concept

  • Loading branch information...
0 parents commit f3e8d94296ce6e40919c2133ae310afe0e184b12 @nichtich committed Jan 15, 2012
Showing with 115 additions and 0 deletions.
  1. BIN DitaaEps.jar
  2. +44 −0 README.md
  3. +71 −0 ditaa-markdown.pl
  4. BIN ditaa0_6b.jar
Binary file not shown.
@@ -0,0 +1,44 @@
+% ditaa in pandoc's markdown
+
+*ditaa* is a java program created by Stathis Sideris to convert diagrams in
+ASCII art to PNG images. It is available at <http://ditaa.sourceforge.net/>.
+Mikael Brännström created an extension to convert diagrams to EPS, available at
+<http://ditaa-addons.sourceforge.net/>. *Pandoc* is a Haskell program created
+by John MacFarlane to convert between numerous document markup formats,
+available at <http://johnmacfarlane.net/pandoc/>. It comes with several extensions
+of markdown markup syntax. All these programs are licensed under GPL.
+
+*ditaa-markdown* is a simple Perl script to preprocess and convert ditaa
+diagrams embedded in pandoc's markdown syntax.
+
+~~~~~ {.ditaa}
+ +--------------------+
+ /-->| processed markdown |
++-----------------+ +----------------+ | +--------------------+
+| markdown source |-->| ditaa markdown |--*
++-----------------+ +-------o--------+ | +--------------------+
+ | \-->| image files |
+ ditaa +--------------------+
+~~~~~
+
+Figure: ditaa-markdown conversion process
+
+You can pass any of ditaa's options to ditaa-markdown after the input/output
+file, for instance README.pdf from this file was created via
+
+ ./ditaa-markdown.pl -pdf README.md -S -s '0,4' | \
+ markdown2pdf -o README.pdf.
+
+To convert all ditaa diagrams to EPS and PDF vector images and create a PDF,
+run:
+
+ ./ditaa-markdown.pl -pdf example.md | markdown2pdf -o example.pdf
+
+To convert all diagrams to PNG bitmap images and create an HTML, run:
+
+ ./ditaa-markdown.pl example.md | pandoc -f markdown -t html > example.html
+
+This code repository contains a copy of ditaa and ditaa eps as compiled jar
+files. Feel free to copy, fork, reuse, and modify from
+<http://github.com/nichtich/ditaa-markdown>!
+
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+# This file has been licensed under GPL by Jakob Voss
+
+use strict;
+use warnings;
+use Pod::Usage;
+use File::Basename;
+use Cwd 'abs_path';
+
+=head1 NAME
+
+ditaa-markdown - preprocess ditaa diagrams embedded in pandoc markdown
+
+=head1 SYNOPSIS
+
+ditaa-markdown [-png|-pdf] [ditaa-options] input [output]
+
+=cut
+
+my $loc = dirname(abs_path($0));
+my %ditaa = ( png => "$loc/ditaa0_6b.jar", pdf => "$loc/DitaaEps.jar" );
+my ($file, $format, $blank);
+my $count = 0;
+my $depth = 0;
+
+pod2usage(1) if grep /^-(help|h)$/, @ARGV;
+
+if (@ARGV and $ARGV[0] =~ /^-(pdf|png)$/) {
+ $format = $1;
+ shift @ARGV;
+} else {
+ $format = 'png';
+}
+
+my $infile = shift @ARGV;
+open (IN, "<", $infile) or die "failed to open $infile";
+
+if (@ARGV and $ARGV[0] !~ /^-/) {
+ my $outfile = shift @ARGV;
+ open (OUT, ">", $outfile) or die "failed to open $outfile";
+} else {
+ *OUT = *STDOUT;
+}
+
+pod2usage("unknown format") unless $ditaa{$format};
+pod2usage("missing ".$ditaa{$format}) unless -f $ditaa{$format};
+
+while (<IN>) {
+ if ($depth) {
+ if ( /^(~{3,})\s*$/ and length($1) >= $depth ) {
+ $depth = 0;
+ close IMG;
+ my $img = $format eq 'png' ? "$file.png" : "$file.eps";
+ system join ' ', 'java', '-jar', $ditaa{$format}, @ARGV, "$file.ditaa", $img, ">/dev/null";
+ system "epstopdf", "-o", "$file.$format", $img if $format eq 'pdf';
+ print OUT "![]($file.$format)\n";
+ } else {
+ print IMG $_;
+ }
+ } else {
+ if ( $blank and /^(~{3,})\s+\{\.ditaa\}/ ) { # ~~~ {.dita}
+ $depth = length($1);
+ $count++;
+ $file = "image-$count";
+ open (IMG, ">", "$file.ditaa") or die "failed to open $file.ditaa";
+ } else {
+ $blank = ($_ =~ /^\s*$/);
+ print OUT $_;
+ }
+ }
+}
Binary file not shown.

0 comments on commit f3e8d94

Please sign in to comment.