Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

renamed to mddia and added support of rdfdot

  • Loading branch information...
commit 3e9c554374c64aed16b2481ddf4317f03e0a7141 1 parent c235bf7
Jakob Voss authored January 24, 2012
86  README.html
... ...
@@ -0,0 +1,86 @@
  1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2
+<html xmlns="http://www.w3.org/1999/xhtml">
  3
+<head>
  4
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5
+  <meta name="generator" content="pandoc" />
  6
+  <meta name="author" content="Jakob Voss" />
  7
+  <title>Embedded diagrams in pandoc's markdown</title>
  8
+</head>
  9
+<body>
  10
+<div id="header">
  11
+<h1 class="title">Embedded diagrams in pandoc's markdown</h1>
  12
+<h3 class="author">Jakob Voss</h3>
  13
+</div>
  14
+<h1 id="introduction">Introduction</h1>
  15
+<p><strong>Pandoc</strong> is a Haskell program to convert between numerous document markup formats. It comes with an abstract document model and a serialization in extended markdown syntax. Pandoc was created by John MacFarlane and it is available as Open Source at <a href="http://johnmacfarlane.net/pandoc/"><code class="url">http://johnmacfarlane.net/pandoc/</code></a>.</p>
  16
+<p><strong>mddia</strong> is a simple, dirty Perl script to preprocess and convert diagrams embedded in pandoc's markdown syntax. This is a temporary hack because of lacking Haskell skills. Anyway, the script may be rewritten, but the underlying principles and data format will stay.<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup> Up to now the script supports the following diagram types:</p>
  17
+<!-- TODO: Percent characters in URLs break PDF generation?! -->
  18
+
  19
+<dl>
  20
+<dt>ditaa</dt>
  21
+<dd><p>a java program created by Stathis Sideris to convert diagrams in ASCII art to PNG images. It is available at <a href="http://ditaa.sourceforge.net/"><code class="url">http://ditaa.sourceforge.net/</code></a>. Mikael Brännström created an extension to convert diagrams to EPS, available at <a href="http://ditaa-addons.sourceforge.net/"><code class="url">http://ditaa-addons.sourceforge.net/</code></a>.</p>
  22
+</dd>
  23
+<dt>dot</dt>
  24
+<dd><p>GraphViz's graph description language. See <a href="http://en.wikipedia.org/wiki/DOT_language"><code class="url">http://en.wikipedia.org/wiki/DOT_language</code></a> for more information.</p>
  25
+</dd>
  26
+<dt>rdfdot</dt>
  27
+<dd><p>RDF graphs based on RDF/Turtle. The command line program <code>rdfdot</code> is included in the CPAN package <a href="http://search.cpan.org/perldoc?RDF::Trine::Exporter::GraphViz">RDF::Trine::Exporter::GraphViz</a>.</p>
  28
+</dd>
  29
+</dl>
  30
+<p>Ditaa is bundled with this script, while GraphViz and rdfdot must be installed manually. This script, pandoc, ditaa, dot, and rdfdot are all licensed as free software.</p>
  31
+<h1 id="usage">Usage</h1>
  32
+<p>mddia acts acts as filter that makes use of diagram creation programs to process a markdown source while emitting some image files.</p>
  33
+<p><img src="image-1.png" /> </p>
  34
+<p>Image files can be emitted in PNG format (the default) or in PDF format. You can choose the image format by command line option <code>-pdf</code> or <code>-png</code>. To create HTML from markdown via pandoc, call for instance:</p>
  35
+<pre><code>./mddia README.md | pandoc -s -t html &gt; README.html
  36
+</code></pre>
  37
+<p>Or to create PDF:</p>
  38
+<pre><code>./mddia -pdf README.md | markdown2pdf -o README.pdf 
  39
+</code></pre>
  40
+<p>Diagrams are embedded in Markdown as source code blocks. This way your documents are valid and processable even without <code>mddia</code> (your diagrams will only show up as source code). For instance the image above was created with this code block:</p>
  41
+<pre><code>~~~~~ {.ditaa .no-separation}
  42
+
  43
++-----------------+       +--------+           +--------------------+
  44
+| markdown source |------&gt;| mdddia |------*---&gt;| processed markdown |
  45
++-----------------+       +--------+      |    +--------------------+
  46
+                              |           \---&gt;| image files        |
  47
+                    +------------------+       +--------------------+
  48
+                    | diagram creation |
  49
+                    +------------------+
  50
+                    | ditaa/dot/rdfdot |
  51
+                    +------------------+
  52
+~~~~~
  53
+</code></pre>
  54
+<p>All &quot;class names&quot; (the strings starting with a dot) after the diagram type are passed are argument to the diagram creation. For instance</p>
  55
+<pre><code>~~~~~ {.ditaa .no-shadows .scale:0.4}
  56
+</code></pre>
  57
+<p>Is passed to <code>ditaa</code> as <code>--no-shadows --scale 0.4</code>.</p>
  58
+<h1 id="examples">Examples</h1>
  59
+<h2 id="input-source-code">Input source code</h2>
  60
+<pre><code>~~~~ {.dot .Grankdir:LR}
  61
+digraph {
  62
+  A -&gt; B -&gt; C;
  63
+  A -&gt; C;
  64
+}
  65
+~~~~
  66
+
  67
+~~~~ {.rdfdot}
  68
+@prefix foaf: &lt;http://xmlns.com/foaf/0.1/&gt; .
  69
+@base &lt;http://example.com/&gt; .
  70
+&lt;alice&gt; foaf:name &quot;Alice&quot; ;
  71
+        foaf:knows [ foaf:name &quot;Bob&quot; ] .
  72
+~~~~
  73
+</code></pre>
  74
+<h2 id="generated-diagrams">Generated diagrams</h2>
  75
+<p><img src="image-2.png" /> </p>
  76
+<p><img src="image-3.png" /> </p>
  77
+<h1 id="about">About</h1>
  78
+<p>mddia is available at <a href="https://github.com/nichtich/ditaa-markdown"><code class="url">https://github.com/nichtich/ditaa-markdown</code></a>. Feel free to fork and extend under any free viral open source license. The files README.html and README.pdf in the repository may be out of date compared to the original README.md.</p>
  79
+<div class="footnotes">
  80
+<hr />
  81
+<ol>
  82
+<li id="fn1"><p>A better implementation would be based on pandoc's scripting API: <a href="http://johnmacfarlane.net/pandoc/scripting.html"><code class="url">http://johnmacfarlane.net/pandoc/scripting.html</code></a>. See the dot plugin at <a href="http://gitit.net/"><code class="url">http://gitit.net/</code></a> for how to dot it. <a href="#fnref1" class="footnoteBackLink">↩</a></p></li>
  83
+</ol>
  84
+</div>
  85
+</body>
  86
+</html>
175  README.md
Source Rendered
... ...
@@ -1,80 +1,143 @@
1  
-% ditaa in pandoc's markdown
2  
-
3  
-*ditaa* is a java program created by Stathis Sideris to convert diagrams in
4  
-ASCII art to PNG images. It is available at <http://ditaa.sourceforge.net/>.
5  
-Mikael Brännström created an extension to convert diagrams to EPS, available at
6  
-<http://ditaa-addons.sourceforge.net/>. *Pandoc* is a Haskell program created
7  
-by John MacFarlane to convert between numerous document markup formats,
8  
-available at <http://johnmacfarlane.net/pandoc/>. It comes with several extensions
9  
-of markdown markup syntax. All these programs are licensed under GPL.
10  
-
11  
-*ditaa-markdown* is a simple, dirty Perl script to preprocess and convert ditaa
12  
-diagrams embedded in pandoc's markdown syntax.
13  
-
14  
-~~~~~ {.ditaa}
15  
-                                              +--------------------+
16  
-                                          /-->| processed markdown |
17  
-+-----------------+   +----------------+  |   +--------------------+
18  
-| markdown source |-->| ditaa markdown |--*
19  
-+-----------------+   +-------o--------+  |   +--------------------+
20  
-                              |           \-->| image files        |
21  
-                            ditaa             +--------------------+
22  
-~~~~~
  1
+% Embedded diagrams in pandoc's markdown
  2
+% Jakob Voss
23 3
 
24  
-Figure: ditaa-markdown conversion process
  4
+# Introduction
25 5
 
26  
-To create HTML, just call:
  6
+**Pandoc** is a Haskell program to convert between numerous document markup
  7
+formats. It comes with an abstract document model and a serialization in
  8
+extended markdown syntax. Pandoc was created by John MacFarlane and it is
  9
+available as Open Source at <http://johnmacfarlane.net/pandoc/>.
27 10
 
28  
-    ./ditaa-markdown.pl README.md | pandoc -f markdown -t html > README.html
  11
+**mddia** is a simple, dirty Perl script to preprocess and convert diagrams
  12
+embedded in pandoc's markdown syntax. This is a temporary hack because of
  13
+lacking Haskell skills. Anyway, the script may be rewritten, but the underlying
  14
+principles and data format will stay.[^1] Up to now the script supports the
  15
+following diagram types:
29 16
 
30  
-You can pass any of ditaa's options to ditaa-markdown after the input/output
31  
-file, for instance README.pdf from this file was created via
  17
+[^1]: A better implementation would be based on pandoc's scripting API:
  18
+<http://johnmacfarlane.net/pandoc/scripting.html>. See the dot plugin at
  19
+<http://gitit.net/> for how to dot it.
32 20
 
33  
-    ./ditaa-markdown.pl -pdf README.md -S -s '0,4' | \
34  
-	markdown2pdf -o README.pdf.
  21
+<!-- TODO: Percent characters in URLs break PDF generation?! -->
35 22
 
36  
-You can also pass any of ditaa's options for a specific image by using this syntax:
  23
+ditaa
37 24
 
38  
-~~~~~~
39  
-~~~~~ {.ditaa .no-shadows .scale:0,4}
40  
-...
  25
+:    a java program created by Stathis Sideris to convert diagrams in ASCII art 
  26
+     to PNG images. It is available at <http://ditaa.sourceforge.net/>.  Mikael
  27
+     Brännström created an extension to convert diagrams to EPS, available at
  28
+     <http://ditaa-addons.sourceforge.net/>.
  29
+
  30
+dot
  31
+
  32
+:    GraphViz's graph description language. See 
  33
+     <http://en.wikipedia.org/wiki/DOT_language> for more information.
  34
+
  35
+rdfdot
  36
+
  37
+:    RDF graphs based on RDF/Turtle. The command line program `rdfdot` is
  38
+     included in the CPAN package [RDF::Trine::Exporter::GraphViz](http://search.cpan.org/perldoc?RDF::Trine::Exporter::GraphViz).
  39
+
  40
+Ditaa is bundled with this script, while GraphViz and rdfdot must be installed
  41
+manually. This script, pandoc, ditaa, dot, and rdfdot are all licensed as free
  42
+software.
  43
+
  44
+# Usage
  45
+
  46
+mddia acts acts as filter that makes use of diagram creation programs
  47
+to process a markdown source while emitting some image files.
  48
+
  49
+~~~~~ {.ditaa .no-separation}
  50
+                                  
  51
++-----------------+       +--------+           +--------------------+
  52
+| markdown source |------>| mdddia |------*--->| processed markdown |
  53
++-----------------+       +--------+      |    +--------------------+
  54
+                              |           \--->| image files        |
  55
+                    +------------------+       +--------------------+
  56
+                    | diagram creation |
  57
+                    +------------------+
  58
+                    | ditaa/dot/rdfdot |
  59
+                    +------------------+
41 60
 ~~~~~
42  
-~~~~~~
43 61
 
44  
-For instance the image above with `.no-shadows`:
45  
- 
46  
-~~~~~ {.ditaa .no-shadows}
47  
-                                              +--------------------+
48  
-                                          /-->| processed markdown |
49  
-+-----------------+   +----------------+  |   +--------------------+
50  
-| markdown source |-->| ditaa markdown |--*
51  
-+-----------------+   +-------o--------+  |   +--------------------+
52  
-                              |           \-->| image files        |
53  
-                            ditaa             +--------------------+
  62
+Image files can be emitted in PNG format (the default) or in PDF format.
  63
+You can choose the image format by command line option `-pdf` or `-png`.
  64
+To create HTML from markdown via pandoc, call for instance:
  65
+
  66
+    ./mddia README.md | pandoc -s -t html > README.html
  67
+
  68
+Or to create PDF:
  69
+
  70
+    ./mddia -pdf README.md | markdown2pdf -o README.pdf 
  71
+
  72
+Diagrams are embedded in Markdown as source code blocks. This way your
  73
+documents are valid and processable even without `mddia` (your diagrams will
  74
+only show up as source code). For instance the image above was created 
  75
+with this code block:
  76
+
  77
+~~~~~~~~
  78
+~~~~~ {.ditaa .no-separation}
  79
+                                  
  80
++-----------------+       +--------+           +--------------------+
  81
+| markdown source |------>| mdddia |------*--->| processed markdown |
  82
++-----------------+       +--------+      |    +--------------------+
  83
+                              |           \--->| image files        |
  84
+                    +------------------+       +--------------------+
  85
+                    | diagram creation |
  86
+                    +------------------+
  87
+                    | ditaa/dot/rdfdot |
  88
+                    +------------------+
54 89
 ~~~~~
  90
+~~~~~~~~
  91
+
  92
+All "class names" (the strings starting with a dot) after the diagram type are passed
  93
+are argument to the diagram creation. For instance  
55 94
 
56  
-To convert all ditaa diagrams to EPS and PDF vector images and create a PDF,
57  
-run:
  95
+~~~~~~
  96
+~~~~~ {.ditaa .no-shadows .scale:0.4}
  97
+~~~~~~
58 98
 
59  
-    ./ditaa-markdown.pl -pdf example.md | markdown2pdf -o example.pdf
  99
+Is passed to `ditaa` as `--no-shadows --scale 0.4`.
60 100
 
61  
-To convert all diagrams to PNG bitmap images and create an HTML, run:
  101
+# Examples
62 102
 
63  
-    ./ditaa-markdown.pl example.md | pandoc -f markdown -t html > example.html
  103
+## Input source code
64 104
 
65  
-This code repository contains a copy of ditaa and ditaa eps as compiled jar
66  
-files. The current solution is a temporary hack. A better solution is to use
67  
-pandoc's scripting API: <http://johnmacfarlane.net/pandoc/scripting.html>.
  105
+~~~~~~
  106
+~~~~ {.dot .Grankdir:LR}
  107
+digraph {
  108
+  A -> B -> C;
  109
+  A -> C;
  110
+}
  111
+~~~~
68 112
 
69  
-## GraphViz
  113
+~~~~ {.rdfdot}
  114
+@prefix foaf: <http://xmlns.com/foaf/0.1/> .
  115
+@base <http://example.com/> .
  116
+<alice> foaf:name "Alice" ;
  117
+        foaf:knows [ foaf:name "Bob" ] .
  118
+~~~~
  119
+~~~~~~
70 120
 
71  
-In addition to ditaa you can also use GraphViz to create inline graph diagrams
72  
-like this:
  121
+## Generated diagrams
73 122
 
74  
-~~~~ {.dot}
  123
+~~~~ {.dot .Grankdir:LR}
75 124
 digraph {
76 125
   A -> B -> C;
77 126
   A -> C;
78 127
 }
79 128
 ~~~~
80 129
 
  130
+~~~~ {.rdfdot}
  131
+@prefix foaf: <http://xmlns.com/foaf/0.1/> .
  132
+@base <http://example.com/> .
  133
+<alice> foaf:name "Alice" ;
  134
+        foaf:knows [ foaf:name "Bob" ] .
  135
+~~~~
  136
+
  137
+# About
  138
+
  139
+mddia is available at <https://github.com/nichtich/ditaa-markdown>. Feel free
  140
+to fork and extend under any free viral open source license. The files
  141
+README.html and README.pdf in the repository may be out of date compared to the
  142
+original README.md.
  143
+
BIN  README.pdf
Binary file not shown
102  ditaa-markdown.pl
... ...
@@ -1,102 +0,0 @@
1  
-#!/usr/bin/perl
2  
-# This file has been licensed under GPL by Jakob Voss
3  
-
4  
-use strict;
5  
-use warnings;
6  
-use Pod::Usage;
7  
-use File::Basename;
8  
-use Cwd 'abs_path';
9  
-
10  
-=head1 NAME
11  
-
12  
-ditaa-markdown - preprocess ditaa diagrams embedded in pandoc markdown
13  
-
14  
-=head1 SYNOPSIS
15  
-
16  
-ditaa-markdown [-png|-pdf] [ditaa-options] input [output]
17  
-
18  
-=cut
19  
-
20  
-my $loc = dirname(abs_path($0));
21  
-my %ditaa = ( png => "$loc/ditaa0_6b.jar", pdf => "$loc/DitaaEps.jar" );
22  
-my ($file, $format, $blank);
23  
-
24  
-my $count = 0;       # image counter to get unique image file names
25  
-my $depth = 0;       # number of tildes if inside a code block
26  
-my $justcode;        # true if inside a non-ditaa code block
27  
-my $convopts = "";   # command line options for the current image
28  
-my $alt = "";        # alt text of current image
29  
-my $caption = "";    # caption of current image
30  
-my $type;            # ditaa or dot
31  
-
32  
-pod2usage(1) if grep /^-(help|h)$/, @ARGV;
33  
-
34  
-if (@ARGV and $ARGV[0] =~ /^-(pdf|png)$/) {
35  
-	$format = $1;
36  
-	shift @ARGV;
37  
-} else {
38  
-	$format = 'png';
39  
-}
40  
-
41  
-my $infile = shift @ARGV;
42  
-open (IN, "<", $infile) or die "failed to open $infile";
43  
-
44  
-if (@ARGV and $ARGV[0] !~ /^-/) {
45  
-	my $outfile = shift @ARGV;
46  
-	open (OUT, ">", $outfile) or die "failed to open $outfile";
47  
-} else {
48  
-	*OUT = *STDOUT;
49  
-}
50  
-
51  
-pod2usage("unknown format") unless $ditaa{$format};
52  
-pod2usage("missing ".$ditaa{$format}) unless -f $ditaa{$format}; 
53  
-
54  
-while (<IN>) {
55  
-	if ($depth) {
56  
-		if ( /^(~{3,})\s*$/ and length($1) >= $depth ) {
57  
-			$depth = 0;
58  
-            if ($justcode) {
59  
-                $justcode = 0;
60  
-                print OUT $_;
61  
-            } else {
62  
-                close IMG;
63  
-                my $img = $format eq 'png' ? "$file.png" : "$file.eps";
64  
-                if ( $type eq 'ditaa' ) {
65  
-                    system join ' ', 'java', '-jar', $ditaa{$format}, @ARGV, "$convopts", "$file.ditaa", $img, ">/dev/null";
66  
-                } else { # type eq 'dot'
67  
-                    system join ' ', 'dot', ($format eq 'png' ? '-Tpng' : '-Teps'), "-o$img", "$file.dot", ">/dev/null";
68  
-                }
69  
-                system "epstopdf", "-o", "$file.$format", $img if $format eq 'pdf';
70  
-                my $md = "![$alt]($file.$format";
71  
-                $caption = " \"$caption\"" if $caption;
72  
-                $md .= "$caption)";
73  
-                $md .= '\ ' unless $alt;
74  
-                print OUT "$md\n";
75  
-            }
76  
-		} else {
77  
-            if ($justcode) {
78  
-    			print OUT $_;
79  
-            } else {
80  
-    			print IMG $_;
81  
-            }
82  
-		}
83  
-	} else {
84  
-    	if ( $blank and /^(~{3,})(\s+\{(.*)\})/ ) {
85  
-            $depth = length($1);
86  
-            my $args = $3;
87  
-            if ($args and $args =~ /^\.(ditaa|dot)(\s+(.*))?/) { # ~~~ {.ditaa <opts>}
88  
-                $type = $1;
89  
-                my @classes = $3 ? grep /\.[^ ]+/, split(/\s+/, "$3") : ();
90  
-	    		$count++;
91  
-		    	$file = "image-$count";
92  
-                $convopts = join ' ', map { s/^\.//; s/:/ /; "--$_"; } @classes;
93  
-      			open (IMG, ">", "$file.$type") or die "failed to open $file.$type";
94  
-            } else {
95  
-                $justcode = 1;
96  
-            }
97  
-		} else {
98  
-			$blank = ($_ =~ /^\s*$/);
99  
-			print OUT $_;
100  
-		}
101  
-	}
102  
-}
142  mddia
... ...
@@ -0,0 +1,142 @@
  1
+#!/usr/bin/perl
  2
+# This file has been licensed under GPL by Jakob Voss
  3
+
  4
+use strict;
  5
+use warnings;
  6
+use Pod::Usage;
  7
+use File::Basename;
  8
+use Cwd 'abs_path';
  9
+
  10
+=head1 NAME
  11
+
  12
+mddia - preprocess diagrams embedded in pandoc markdown
  13
+
  14
+=head1 SYNOPSIS
  15
+
  16
+mddia [-png|-pdf] [options] input [output]
  17
+
  18
+=cut
  19
+
  20
+# required jar files
  21
+my $loc = dirname(abs_path($0));
  22
+foreach ("$loc/ditaa0_6b.jar", "$loc/DitaaEps.jar") {
  23
+    -f $_ || pod2usage("missing $_");
  24
+}
  25
+
  26
+my ($file, $format, $blank);
  27
+
  28
+my $count = 0;       # image counter to get unique image file names
  29
+my $depth = 0;       # number of tildes if inside a code block
  30
+my $justcode;        # true if inside a non-ditaa code block
  31
+my $alt = "";        # alt text of current image
  32
+my $caption = "";    # caption of current image
  33
+my $type;            # diagram type
  34
+my @classes;         # additional "class" options (.foo .bar ...)
  35
+
  36
+my %diagramtypes = (
  37
+    ditaa  => sub {
  38
+        my $file = shift;
  39
+        my ($jar, $img);
  40
+        if ($format eq 'png') {
  41
+            $img = "$file.png";
  42
+            $jar = "$loc/ditaa0_6b.jar";
  43
+        } else {
  44
+            $img = "$file.eps";
  45
+            $jar = "$loc/DitaaEps.jar";
  46
+        }
  47
+        my $args = join ' ', map { s/^\.//; s/:/ /; "--$_"; } @classes;
  48
+        system join ' ', 'java', '-jar', $jar, @ARGV, "$args", "$file.ditaa", $img, ">/dev/null";
  49
+        system "epstopdf", "-o", "$file.$format", $img if $format eq 'pdf';
  50
+    },
  51
+    dot    => sub {
  52
+        my $file = shift;
  53
+        my $img = $format eq 'png' ? "$file.png" : "$file.eps";
  54
+        my $args = join ' ', map { s/^\.//; s/:/=/; "-$_"; } @classes;
  55
+        system join ' ', 'dot', ($format eq 'png' ? '-Tpng' : '-Teps'), "$args", "-o$img", "$file.dot", ">/dev/null";
  56
+        system "epstopdf", "-o", "$file.$format", $img if $format eq 'pdf';
  57
+    },
  58
+    rdfdot => sub { 
  59
+        my $file = shift;
  60
+        my @args = map { s/^\.//; s/:/ /; "-$_"; } @classes;
  61
+        if ($format eq 'png') {
  62
+            system 'rdfdot', @args, '-ttl', "$file.rdfdot", "$file.png";
  63
+        } else {
  64
+            system 'rdfdot', @args, '-ttl', "$file.rdfdot", "$file.dot";
  65
+            system join ' ', 'dot', '-Teps', "-o$file.eps", "$file.dot", ">/dev/null";
  66
+            #system 'rdfdot', @args, '-ttl', "$file.rdfdot", "$file.ps";
  67
+            #system 'ps2pdf', "$file.ps", "$file.pdf";
  68
+            system 'epstopdf', "-o=$file.pdf", "$file.eps";
  69
+        }
  70
+    },
  71
+);
  72
+
  73
+pod2usage(1) if grep /^-(help|h)$/, @ARGV;
  74
+
  75
+if (@ARGV and $ARGV[0] =~ /^-(pdf|png)$/) {
  76
+	$format = $1;
  77
+	shift @ARGV;
  78
+} else {
  79
+	$format = 'png';
  80
+}
  81
+
  82
+my $infile = shift @ARGV;
  83
+open (IN, "<", $infile) or die "failed to open $infile";
  84
+
  85
+if (@ARGV and $ARGV[0] !~ /^-/) {
  86
+	my $outfile = shift @ARGV;
  87
+	open (OUT, ">", $outfile) or die "failed to open $outfile";
  88
+} else {
  89
+	*OUT = *STDOUT;
  90
+}
  91
+
  92
+pod2usage("unknown format") if $format !~ /^(png|pdf)$/;
  93
+
  94
+my %ditaa = ( png => "$loc/ditaa0_6b.jar", pdf => "$loc/DitaaEps.jar" );
  95
+
  96
+my $line = 0;
  97
+while (<IN>) {
  98
+    $line++;
  99
+    #no warnings;  print STDERR "$line: $depth $justcode: $_\n";
  100
+	if ($depth) { # in a code block
  101
+		if ( /^(~{3,})\s*$/ and length($1) >= $depth ) {
  102
+			$depth = 0;
  103
+            if ($justcode) {
  104
+                $justcode = 0;
  105
+                print OUT $_;
  106
+            } else {
  107
+                close IMG;
  108
+                $diagramtypes{$type}->($file);
  109
+
  110
+                my $md = "![$alt]($file.$format";
  111
+                $caption = " \"$caption\"" if $caption;
  112
+                $md .= "$caption)";
  113
+                $md .= '\ ' unless $alt;
  114
+                print OUT "$md\n";
  115
+            }
  116
+		} else {
  117
+            if ($justcode) {
  118
+    			print OUT $_;
  119
+            } else {
  120
+    			print IMG $_;
  121
+            }
  122
+		}
  123
+	} else {
  124
+    	if ( $blank and /^(~{3,})(\s+\{(.*)\})?/ ) {
  125
+            $depth = length($1);
  126
+            my $args = $3;
  127
+            if ($args and $args =~ /^\.([a-z]+)(\s+(.*))?/ and $diagramtypes{$1}) {
  128
+                $type = $1;
  129
+                @classes = $3 ? grep /\.[^ ]+/, split(/\s+/, "$3") : ();
  130
+	    		$count++;
  131
+		    	$file = "image-$count";
  132
+      			open (IMG, ">", "$file.$type") or die "failed to open $file.$type";
  133
+            } else {
  134
+                $justcode = 1;
  135
+			    print OUT $_;
  136
+            }
  137
+		} else {
  138
+			$blank = ($_ =~ /^\s*$/);
  139
+			print OUT $_;
  140
+		}
  141
+	}
  142
+}

0 notes on commit 3e9c554

Please sign in to comment.
Something went wrong with that request. Please try again.