|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use JSON; |
| 7 | + |
| 8 | +my $data; |
| 9 | +{ |
| 10 | + local $/; |
| 11 | + undef $/; |
| 12 | + $data = <>; |
| 13 | +} |
| 14 | +my $json = JSON->new->decode($data); |
| 15 | +my $sections = $json->{book}->{sections}; |
| 16 | +my $date = `date '+%d %B %Y'`; |
| 17 | +chomp $date; |
| 18 | + |
| 19 | +my $latex; |
| 20 | + |
| 21 | + |
| 22 | +$latex .= "\\documentclass{book}\n"; |
| 23 | +$latex .= "\\usepackage{hyperref}\n"; |
| 24 | +$latex .= "\\def\\code#1{\\texttt{#1}}\n"; |
| 25 | +$latex .= "\\def\\codeblock#1{\\texttt{#1}}\n"; |
| 26 | +$latex .= "\\begin{document}\n"; |
| 27 | +$latex .= "\\title{Void Handbook}\n"; |
| 28 | +$latex .= "\\date{$date}\n"; |
| 29 | +$latex .= "\\maketitle\n"; |
| 30 | + |
| 31 | +foreach my $i (@$sections) { |
| 32 | + process_json($i); |
| 33 | +} |
| 34 | + |
| 35 | +$latex .= "\\end{document}\n"; |
| 36 | + |
| 37 | +open(my $fh, ">", "handbook.tex") |
| 38 | + or die "Can't open handbook.tex for writing: $!"; |
| 39 | +print $fh $latex; |
| 40 | +close($fh) |
| 41 | + or die "Couldn't close handboox.tex after writing: $!"; |
| 42 | + |
| 43 | +exit 0; |
| 44 | + |
| 45 | +sub process_json { |
| 46 | + |
| 47 | + my $j = shift; |
| 48 | + my $type = ref($j); |
| 49 | + if ($type eq 'HASH') { |
| 50 | + if (defined $j->{content}) { |
| 51 | + process_content($j->{content}); |
| 52 | + } |
| 53 | + if (defined $j->{Chapter}) { |
| 54 | + process_json($j->{Chapter}); |
| 55 | + } |
| 56 | + if (defined $j->{sub_items}) { |
| 57 | + foreach my $k ($j->{sub_items}) { |
| 58 | + process_json($k); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + if ($type eq 'ARRAY') { |
| 63 | + foreach my $k (@{$j}) { |
| 64 | + process_json($k); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +sub process_content { |
| 70 | + |
| 71 | + my $content = shift; |
| 72 | + |
| 73 | + # Create section headings |
| 74 | + $content =~ s/^# ([^\n]+)/create_hyperref($1,"section")/eg; |
| 75 | + |
| 76 | + # Create subsection headings; |
| 77 | + $content =~ s/\n## ([^\n]+)/\\subsection\{$1\}/g; |
| 78 | + |
| 79 | + # Create subsubsection headings. |
| 80 | + $content =~ s/\n### ([^\n]+)/\\subsubsection\{$1\}/g; |
| 81 | + |
| 82 | + # Create paragraph headings. |
| 83 | + $content =~ s/\n#### ([^\n]+)/\\paragraph\{$1\}/g; |
| 84 | + |
| 85 | + # Create subparagraph headings. |
| 86 | + $content =~ s/\n##### ([^\n]+)/\\subparagraph\{$1\}/g; |
| 87 | + |
| 88 | + # Create inline code. |
| 89 | + $content =~ s/(?: |\n)`([^`]+)`/ \\code\{$1\}/g; |
| 90 | + |
| 91 | + # Create codeblocks. |
| 92 | + $content =~ s/\n```(.+?)\n```/\n\\codeblock\{$1\}/sg; |
| 93 | + |
| 94 | + # TODO: Create lists. |
| 95 | + |
| 96 | + # TODO: Create tables. |
| 97 | + |
| 98 | + # TODO: Create internal links. |
| 99 | + $content =~ s/\[(.+?)\]\((\..+?)\)/--internal link--/sg; |
| 100 | + |
| 101 | + # Create external links. |
| 102 | + $content =~ s/\[(.+?)\]\((.+?)\)/\\href\{$2\}\{$1\}/sg; |
| 103 | + |
| 104 | + # TODO: Create blockquotes. |
| 105 | + |
| 106 | + $latex .= $content; |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +sub create_hyperref { |
| 111 | + |
| 112 | + my $arg = shift; |
| 113 | + my $type = shift; |
| 114 | + my $ref; |
| 115 | + |
| 116 | + $ref .= "\\hypertarget{"; |
| 117 | + $ref .= lc($arg); |
| 118 | + $ref .= "}{}\n\\$type\{$arg\}" |
| 119 | + |
| 120 | +} |
0 commit comments