Created
April 24, 2015 20:01
-
-
Save Quintus/9ddcdc7dce5cad6e38f9 to your computer and use it in GitHub Desktop.
Schnell gehacktes Skript zur Konvertierung von openJUR-JSON-Dokumenten in ein für LuaLaTeX verständliches, ZWEISPALTIGES TeX-Dokument.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require "json" | |
require "optionparser" | |
require "open-uri" | |
VERSION = "0.0.1" | |
ESCAPES = { | |
/&/ => "\\textampersand{}", | |
/%/ => "\\textpercent{}", | |
/"/ => "\\replacequote{}", | |
} | |
$options= { | |
:format => "TeX", | |
:output => $stdout, | |
:input => $stdin | |
} | |
OptionParser.new do |op| | |
op.banner = "BENUTZUNG: | |
openjur2pdf OPTIONEN | |
Nimmt einen Link auf ein openJUR-JSON-Dokument entgegen und macht ein leserliches | |
Dokument daraus. | |
OPTIONEN:" | |
op.on("-f", "--format FORMAT", "Ausgabeformat. Entweder HTML oder TeX."){|str| $options[:format] = str} | |
op.on("-o", "--output PFAD", "Ausgabedatei. - für Standardausgabe."){|str| $options[:output] = str == "-" ? $stdout : File.open(str, "w")} | |
op.on("-i", "--input URL", "Eingabe. - für Standardeingabe. Kann auch eine URL sein.") do |str| | |
if str == "-" | |
$options[:input] = $stdin | |
else | |
begin | |
$options[:input] = open(str) | |
rescue => e | |
$stderr.puts "Ungülte URL: #{e.message}" | |
end | |
end | |
end | |
op.on_tail("-h", "--help", "Diese Nachricht anzeigen.") do | |
puts op | |
exit 0 | |
end | |
op.on_tail("-v", "--version", "Versionsnummer anzeigen.") do | |
puts VERSION | |
exit 0 | |
end | |
end.parse!(ARGV) | |
$data = JSON.parse($options[:input].read) | |
def out(str) | |
$options[:output].puts(str) | |
end | |
def escape(str) | |
str = str.dup | |
ESCAPES.each_pair do |regexp, repl| | |
str.gsub!(regexp, repl) | |
end | |
str | |
end | |
def fixspace(str) | |
str.gsub(/§\s+(\w)/){"§\\,#$1"} | |
end | |
def format_TeX | |
out <<'EOF' | |
\documentclass[fontsize=11pt,paper=a4,twocolumn,DIV=calc]{scrartcl} | |
\usepackage{fontspec} | |
\usepackage[ngerman]{babel} | |
\usepackage[hidelinks]{hyperref} | |
\newcounter{randnummer} | |
\renewcommand\therandnummer{\arabic{randnummer}} | |
\newcommand\rn{\stepcounter{randnummer}\textit{[\therandnummer]}~} | |
\newcommand\replacequote{"} | |
\KOMAoptions{DIV=last} | |
\begin{document} | |
EOF | |
out "\\title{#{$data['dokumenttyp']}\\footnote{\\url{#{$data['permalink']}} (= #{$data['fundstelle']})}}" | |
out "\\subtitle{#{$data['aktenzeichen']}}" | |
out "\\author{#{$data['gericht']}}" | |
out "\\date{#{$data['datum']}}" | |
out '\maketitle{}' | |
if $data["leitsaetze"] | |
out '\section*{Leitsätze}' | |
out '\begin{enumerate}' | |
$data["leitsaetze"].scan(/^\d\d*\.(.*?)$/) do |match| | |
out "\\item #{fixspace(escape(match[0].strip))}\n\n" | |
end | |
out '\end{enumerate}' | |
end | |
out "\n\n\\section*{Tenor}" | |
out '\rn{}' + fixspace(escape($data["tenor"].strip.gsub(/\n\n/){"\n\n\\rn{}"})) | |
out "\n\n\\section*{Gründe}" | |
out '\rn{}' + fixspace(escape($data["gruende"].strip.gsub(/\n\n/){"\n\n\\rn{}"})) | |
out '\end{document}' | |
end | |
def format_HTML | |
end | |
if $options[:format] == "TeX" | |
format_TeX | |
elsif $options[:format] == "HTML" | |
format_HTML | |
else | |
$stderr.puts "Unbekanntes Format. Siehe -h für Hilfe." | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment