|
1 | 1 | #!/usr/bin/env ruby
|
2 | 2 |
|
| 3 | +require 'yaml' |
| 4 | + |
3 | 5 | @mainyamldone = false
|
4 | 6 | @inyaml = false
|
| 7 | +@inbox = false |
| 8 | +@yaml_block = "" |
| 9 | +@main_yaml = nil |
| 10 | +@section_yaml = nil |
5 | 11 |
|
6 | 12 | ARGF.each_with_index do |line, idx|
|
| 13 | + |
| 14 | + # handle yaml blocks |
7 | 15 | if /^---$/.match(line)
|
| 16 | + # pass the main yaml block through |
8 | 17 | print line if !@mainyamldone
|
9 | 18 | @inyaml = !@inyaml
|
10 |
| - @mainyamldone = true if !@inyaml |
| 19 | + if @inyaml |
| 20 | + # we're starting a yaml block |
| 21 | + @yaml_block = line + "\n" |
| 22 | + else |
| 23 | + # we've reached end of yaml block |
| 24 | + @yaml_block = @yaml_block + line + "\n" |
| 25 | + if @main_yaml.nil? |
| 26 | + @main_yaml = YAML.load(@yaml_block) |
| 27 | + else |
| 28 | + @section_yaml = YAML.load(@yaml_block) |
| 29 | + # add section h2 header to output |
| 30 | + # note: can't get header_attributes to work, so have to create |
| 31 | + # section anchor explicitly |
| 32 | + print "\n\n\\newpage\n" |
| 33 | + print "\\hypertarget{" + @section_yaml['id'].to_s + "}{}" |
| 34 | + print "\n\n## " + @section_yaml['title'].to_s + "\n" |
| 35 | + |
| 36 | + end |
| 37 | + @mainyamldone = true |
| 38 | + end |
| 39 | + elsif @inyaml |
| 40 | + print line unless @mainyamldone |
| 41 | + @yaml_block = @yaml_block + line + "\n" |
11 | 42 | else
|
12 |
| - print line unless (@inyaml && @mainyamldone) |
| 43 | + |
| 44 | + # replace internal links |
| 45 | + # match [text](./03-chapter2.html#s03n6) -> \hyperlink{s03n6}{text} |
| 46 | + line.gsub!(/\[([^\]]*)\]\(\.\/.+?\.html#(.+?)\)/, "\\hyperlink{\\2}{\\1}") |
| 47 | + # match [text](./03-chapter2.html) -> \hyperlink{s03}{text} |
| 48 | + line.gsub!(/\[([^\]]*)\]\(\.\/([0-9]*).+?\.html\)/, "\\hyperlink{s\\2}{\\1}") |
| 49 | + # match <span name="s00n3"></span> -> \hypertarget{s00n3}{} |
| 50 | + line.gsub!(/\<span name=['"]([A-Za-z0-9]*)['"]\>\<\/span\>/, "\\hypertarget{\\1}{} ") |
| 51 | + |
| 52 | + # handle catalog-card boxes |
| 53 | + if /<p class=\"right\".*/.match(line) |
| 54 | + print "\\rightline{\\texttt{" + /\>(.*)\</.match(line)[1] + "}}\n" |
| 55 | + elsif /^\<div class=\"box\"/.match(line) |
| 56 | + @inbox = true |
| 57 | + print "\\fboxsep=10pt\n\\fbox{\\begin{minipage}{30em}\n" |
| 58 | + elsif /^\<\/div\>/.match(line) |
| 59 | + @inbox = false |
| 60 | + print "\\end{minipage}}\n" |
| 61 | + elsif @inbox |
| 62 | + # handle text lines in catalogue card box |
| 63 | + print "\\texttt{" + line.gsub(/\*(.+?)\*/,"\\underline{\\1}").sub(/^(\ )*/,"\\hspace*{0.5ex}") + "\\\\}\n" |
| 64 | + else |
| 65 | + print line |
| 66 | + end |
13 | 67 | end
|
14 | 68 | end
|
0 commit comments