Skip to content

# publicsitaramc/showpdf

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

executable file 282 lines (233 sloc) 9.819 kb
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 #!/usr/bin/perl -wuse strict;use HTML::Entities;# input is HTML output from Markdown with the following extras:# - * @@pause before this bullet# - (those spaces are needed. A comment can span lines,# but any non-comment stuff on the same line will get deleted)# - (each metadata item extends to end of line)# - , which you then use like this (although the width /# height spec itself is optional, the brackets are part of its syntax).# Then you say# # TODO: ideally we should just use normal Markdown syntax, like# ![Figure 1](aa.png "basic setup")# which comes here as#

# but since beamer also wants a height spec, we have to figure out how to# put that into the Markdown syntax without polluting *that* output# output is designed to go to pdflatex# take the preamble from this very program (see the end of the program)my $preamble = join("", );# read the input text, all in one variable, newlines and allundef$/;my $content = <>;my %metadata=();$|++;# get metadata from $content; see example above for what it looks like in the# actual input fileif ($content =~ s///s) {    my $metadata =$1;    for my $i qw(title shorttitle subtitle occasion shortoccasion date style toctoc tictoc) {$metadata{$i} =$1 if ($metadata =~ /$i *= *(.*?) *$/m); }}# die "title, occasion not optional" unless$metadata{title} and $metadata{occasion};# shorttitle defaults to title if not given etc...$metadata{shorttitle} ||= $metadata{title};$metadata{shortoccasion} ||= $metadata{occasion};chomp($metadata{date} ||= date -I`); # perl not brief enough for me ;-)$metadata{subtitle} ||= "";$metadata{style} ||= "Darmstadt";# an "occasion" field should be optional$preamble =~ s({#date / #occasion})({#date}) unless$metadata{occasion};# now substitute those in the preamble we pulled in earlierfor my $i qw(title shorttitle subtitle occasion shortoccasion date style) {$preamble =~ s/#$i/$metadata{$i}/;}# if the "tictoc" flag is set...$preamble =~ s/% (.beamerdefaultoverlayspecification)/$1/ if$metadata{tictoc};# if the "toctoc" flag is set in the metadata, and the content has any H1 or# H2 tags, uncomments the parts of the preamble which give you the outline# pages before every section/subsection$preamble =~ s/%%#section//g if$content =~ // and $metadata{toctoc};# done with preamble, get it out of the wayprint$preamble;# there's a very specific need in LaTeX/Beamer, if you have "verbatim" (aka# "code") sections inside a frame, that frame needs to be marked "fragile".# This subroutine is called within a regex for each frame and if it has a# verbatim section then [fragile] is inserted.## we also cheat a little by using the same place to undo the HTML entity# coding (like "<" for "<") that Markdown does in code, because Beamer is# not expecting HTML entities in LaTeX input :)sub fragile{    my $fc=shift;$fc =~ s/{frame}/{frame}[fragile]/        if ($fc =~ /\\begin{verbatim}/); # a bit of cheating here$fc =~ s/\\begin{verbatim}.*?\\end{verbatim}/decode_entities($&)/ges if ($fc =~ /\\begin{verbatim}/);    return $fc;}for ($content) # old topic-ing trick to avoid frequent "$content =~"{ # latex requires these changes; otherwise they look like metadata to it s/\^/\\^{}/g; s/\~/\\~{}/g; s/\%/\\%{}/g; # pause control. With tictoc, you don't need manually specified pauses, # otherwise you do. if ($metadata{tictoc}) {        s/
• \@\@/
• /g;    } else {        s/
• \@\@/\\pause\n
• /g;    }    # ok this is a big one; detect and extract inline graphviz...    while (s///s) {        my $figname =$1;        my $figcode =$2;        # do something to create the figure from $figcode open(DOT, "|dot -Tpng > /tmp/$figname.png") and        print DOT $figcode and close DOT; } # and use it... while (//s) { my$out='';        my $figname=$1;        my $caption=$2;        my $matched=$&;        my $wh = ""; # width/height spec$wh = $1 if$caption =~ s/($\S+$) ?//;        $out .= "\\begin{figure}\n";$out .= "\\pgfdeclareimage" . $wh . "{$figname}{/tmp/$figname}\n";$out .= "\\pgfuseimage{$figname}\n";$out .= "\\caption{$caption}\n" if$caption;        $out .= "\\end{figure}\n\n"; # Now you have to make the actual change. If you attempted to reuse # the pattern to make the actual change, you'd have to keep the two in # sync if there were any changes later. So we use$matched        s/\Q$matched/$out/;    }    # delete HTML comments; they confuse Latex/Beamer    s///gs;    # each H3 is a frame, but you have to put an endframe tag just before any    # succeeding H1/2/3, so we just do that first. Note that the "end" match    # could be the start of the next set, so it must be zero-width, otherwise    # the /g skips past that one on the next round    s/(

.*?)(?=|$)/$1\n\n\\end{frame}\n\n/gs;    # H1 is a section, H2 a subsection, H3 a frame    s/

(.*?)<\/h1>/\\section{$1}/gs; s/ (.*?)<\/h2>/\\subsection{$1}/gs;    s/

(.*?)<\/h3>/\\begin{frame}{\$1}/gs;    # bullet lists; for now we don't support numbered lists    s/

• Something went wrong with that request. Please try again.