From f0ad6ea094f4ae7f51068e66d8ca8752d4f39ba0 Mon Sep 17 00:00:00 2001 From: Pelle Nilsson Date: Tue, 28 May 2013 23:31:09 +0200 Subject: [PATCH] Halfway through refactoring output formats. The idea now is that only simple plain-text template files are used for all formats, rather than having a Python class for each one. At least for the currently supported formats this seems to work very well. Need to clean up and remove all the reduntant class definitions sometime. --- .gitignore | 8 ++-- debug.py | 7 ---- dot.py | 26 ------------- formatgamebook.py | 28 ++++++++++++++ latex.py | 49 ------------------------ output.py | 42 ++++++++++++++++---- output_formats/debug/begin.debug | 2 + output_formats/debug/end.debug | 1 + output_formats/debug/paragraph.debug | 1 + output_formats/debug/paragraph_ref.debug | 1 + output_formats/dot/begin.dot | 1 + output_formats/dot/end.dot | 1 + output_formats/dot/paragraph.dot | 0 output_formats/dot/paragraph_ref.dot | 1 + output_formats/rtf/begin.rtf | 7 ++++ output_formats/rtf/end.rtf | 1 + output_formats/rtf/paragraph.rtf | 4 ++ output_formats/rtf/paragraph_ref.rtf | 2 + output_formats/tex/begin.tex | 26 +++++++++++++ output_formats/tex/end.tex | 1 + output_formats/tex/paragraph.tex | 6 +++ output_formats/tex/paragraph_ref.tex | 1 + rtf.py | 28 -------------- 23 files changed, 122 insertions(+), 122 deletions(-) create mode 100644 output_formats/debug/begin.debug create mode 100644 output_formats/debug/end.debug create mode 100644 output_formats/debug/paragraph.debug create mode 100644 output_formats/debug/paragraph_ref.debug create mode 100644 output_formats/dot/begin.dot create mode 100644 output_formats/dot/end.dot create mode 100644 output_formats/dot/paragraph.dot create mode 100644 output_formats/dot/paragraph_ref.dot create mode 100644 output_formats/rtf/begin.rtf create mode 100644 output_formats/rtf/end.rtf create mode 100644 output_formats/rtf/paragraph.rtf create mode 100644 output_formats/rtf/paragraph_ref.rtf create mode 100644 output_formats/tex/begin.tex create mode 100644 output_formats/tex/end.tex create mode 100644 output_formats/tex/paragraph.tex create mode 100644 output_formats/tex/paragraph_ref.tex diff --git a/.gitignore b/.gitignore index 5129ed4..995027f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,12 @@ *~ *.pyc -*.debug -*.tex -*.dot +test.debug +test.tex +test.dot *.log *.aux *.toc *.pdf -*.rtf +test.rtf *.png *.out \ No newline at end of file diff --git a/debug.py b/debug.py index 93b9505..d4fecfb 100644 --- a/debug.py +++ b/debug.py @@ -3,10 +3,3 @@ from output import OutputFormat class DebugFormat (OutputFormat): def __init__(self): super(DebugFormat, self).__init__('debug', 'Gamebook Debug Output') - - def write_begin(self, book, output): - print >> output, "BEGIN DEBUG OUTPUT" - print >> output, "Number of paragraphs: ", book.max - - def write_end(self, book, output): - print >> output, "END DEBUG OUTPUT" diff --git a/dot.py b/dot.py index a3584f1..bad2c62 100644 --- a/dot.py +++ b/dot.py @@ -1,33 +1,7 @@ from output import OutputFormat -class DotParagraphFormat (object): - def __init__(self, fromparagraph): - self.fromparagraph = fromparagraph - self.toparagraphs = [] - - def __call__(self, toparagraph, shuffled_paragraphs): - self.toparagraphs.append(toparagraph) - return '' - - def write(self, shuffled_paragraphs, output): - for toparagraph in self.toparagraphs: - print >> output, "%s->%s" % ( - shuffled_paragraphs.to_nr[self.fromparagraph], - shuffled_paragraphs.to_nr[toparagraph]) - class DotFormat (OutputFormat): def __init__(self): super(DotFormat, self).__init__('dot', 'Graphviz paragraph flowchart') - def write_begin(self, book, output): - print >> output, "digraph gamebook {" - - def write_paragraph(self, paragraph, shuffled_paragraphs, output): - paragraphformat = DotParagraphFormat(paragraph) - paragraph.format(shuffled_paragraphs, paragraphformat) - paragraphformat.write(shuffled_paragraphs, output) - - def write_end(self, book, output): - print >> output, "}" - diff --git a/formatgamebook.py b/formatgamebook.py index 80d9a1a..19cb6c7 100755 --- a/formatgamebook.py +++ b/formatgamebook.py @@ -1,5 +1,33 @@ #!/usr/bin/env python2 +""" +Copyright (c) 2013, Pelle Nilsson +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in +the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +""" + import sys import json diff --git a/latex.py b/latex.py index f9ba6a8..396ba27 100644 --- a/latex.py +++ b/latex.py @@ -1,54 +1,5 @@ from output import OutputFormat -DEFAULT_DOCUMENT_START = """ -\\documentclass[a4,onecolumn]{article} -\\usepackage[utf8]{inputenc} -\\usepackage[T1]{fontenc} -\\usepackage[hidelinks]{hyperref} - -\\usepackage[top=3.3cm, bottom=3.3cm, left=2cm, right=2cm]{geometry} - -\\newif\\ifpdf -\\ifx\\pdfoutput\\undefined - \\pdffalse -\\else - \\ifnum\\pdfoutput=1 - \\pdftrue - \\else - \\pdffalse - \\fi -\\fi - -\\title{Gamebook} -\\author{} -\\date{} - -\\begin{document} - -\\thispagestyle{empty} -\\clearpage - -""" - -def format_latex_paragraph_ref(paragraph, shuffled_paragraphs): - return ("\\textbf{%d}" % shuffled_paragraphs.to_nr[paragraph]) - class LatexFormat (OutputFormat): def __init__(self): super(LatexFormat, self).__init__('tex', 'LaTeX') - - def write_begin(self, book, output): - print >> output, DEFAULT_DOCUMENT_START - - def write_paragraph(self, paragraph, shuffled_paragraphs, output): - print >> output, " \\noindent" - print >> output, format_latex_paragraph_ref(paragraph, - shuffled_paragraphs) - print >> output, " -- " - print >> output, paragraph.format(shuffled_paragraphs, - format_latex_paragraph_ref) - print >> output, "\\newline" - print >> output - - def write_end(self, book, output): - print >> output, "\end{document}" diff --git a/output.py b/output.py index ea49383..6f220ff 100644 --- a/output.py +++ b/output.py @@ -1,12 +1,14 @@ -from paragraphs import paragraph_refs_format +import os +import os.path +import sys -def default_paragraph_link_render(paragraph, shuffled_paragraphs): - return str(shuffled_paragraphs.to_nr[paragraph]) +from paragraphs import paragraph_refs_format class OutputFormat (object): def __init__(self, extension, name): self.extension = extension self.name = name + self.cached_templates = {} def __str__(self): return ".%s: %s" % (self.extension, self.name) @@ -17,19 +19,43 @@ class OutputFormat (object): self.write_end(book, output) def write_begin(self, book, output): - pass + print >> output, self.load_template("begin") % { + 'max' : book.max + }, def write_shuffled_paragraphs(self, shuffled_paragraphs, output): for p in shuffled_paragraphs.as_list[1:]: self.write_paragraph(p, shuffled_paragraphs, output) def write_paragraph(self, paragraph, shuffled_paragraphs, output): - print >> output, shuffled_paragraphs.to_nr[paragraph] - print >> output, paragraph.format(shuffled_paragraphs, - default_paragraph_link_render) + def paragraph_link_render(to_paragraph, shuffled_paragraphs): + return self.load_template("paragraph_ref") % { + 'nr' : shuffled_paragraphs.to_nr[to_paragraph], + 'from_nr' : shuffled_paragraphs.to_nr[paragraph] + }; + print >> output, self.load_template("paragraph") % { + 'nr' : shuffled_paragraphs.to_nr[paragraph], + 'text' : paragraph.format(shuffled_paragraphs, + paragraph_link_render) + }, def write_end(self, book, output): - pass + print >> output, self.load_template("end") % {}, def supports(self, filename): return filename.endswith('.' + self.extension) + + def load_template(self, name): + "Templates is a mess and do not belong in the output class really." + if name in self.cached_templates: + return self.cached_templates[name] + filename = os.path.join(os.path.dirname(sys.argv[0]), + "output_formats", + self.extension, + name + "." + self.extension) + f = open(filename, "r") + template = f.read() + f.close() + self.cached_templates[name] = template + return template + diff --git a/output_formats/debug/begin.debug b/output_formats/debug/begin.debug new file mode 100644 index 0000000..0814207 --- /dev/null +++ b/output_formats/debug/begin.debug @@ -0,0 +1,2 @@ +BEGIN DEBUG OUTPUT +Number of paragraphs: %(max)d diff --git a/output_formats/debug/end.debug b/output_formats/debug/end.debug new file mode 100644 index 0000000..7d56363 --- /dev/null +++ b/output_formats/debug/end.debug @@ -0,0 +1 @@ +END DEBUG OUTPUT diff --git a/output_formats/debug/paragraph.debug b/output_formats/debug/paragraph.debug new file mode 100644 index 0000000..cc4bf3f --- /dev/null +++ b/output_formats/debug/paragraph.debug @@ -0,0 +1 @@ +%(nr)d - %(text)s diff --git a/output_formats/debug/paragraph_ref.debug b/output_formats/debug/paragraph_ref.debug new file mode 100644 index 0000000..59d1f95 --- /dev/null +++ b/output_formats/debug/paragraph_ref.debug @@ -0,0 +1 @@ +%(nr)d \ No newline at end of file diff --git a/output_formats/dot/begin.dot b/output_formats/dot/begin.dot new file mode 100644 index 0000000..f9295ea --- /dev/null +++ b/output_formats/dot/begin.dot @@ -0,0 +1 @@ +digraph gamebook { diff --git a/output_formats/dot/end.dot b/output_formats/dot/end.dot new file mode 100644 index 0000000..5c34318 --- /dev/null +++ b/output_formats/dot/end.dot @@ -0,0 +1 @@ +} diff --git a/output_formats/dot/paragraph.dot b/output_formats/dot/paragraph.dot new file mode 100644 index 0000000..e69de29 diff --git a/output_formats/dot/paragraph_ref.dot b/output_formats/dot/paragraph_ref.dot new file mode 100644 index 0000000..06e47b1 --- /dev/null +++ b/output_formats/dot/paragraph_ref.dot @@ -0,0 +1 @@ +%(from_nr)d->%(nr)d diff --git a/output_formats/rtf/begin.rtf b/output_formats/rtf/begin.rtf new file mode 100644 index 0000000..3e7fdf2 --- /dev/null +++ b/output_formats/rtf/begin.rtf @@ -0,0 +1,7 @@ +{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360 +{\fonttbl\f0\fswiss\fcharset0 Helvetica;} +{\colortbl;\red255\green255\blue255;} +\paperw11900\paperh16840\margl1440\margr1440\vieww14140\viewh14860\viewkind0 +\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural + +\f0\b\fs24 \cf0 diff --git a/output_formats/rtf/end.rtf b/output_formats/rtf/end.rtf new file mode 100644 index 0000000..5c34318 --- /dev/null +++ b/output_formats/rtf/end.rtf @@ -0,0 +1 @@ +} diff --git a/output_formats/rtf/paragraph.rtf b/output_formats/rtf/paragraph.rtf new file mode 100644 index 0000000..542abcd --- /dev/null +++ b/output_formats/rtf/paragraph.rtf @@ -0,0 +1,4 @@ +\b %(nr)d +\b0 - %(text)s +\\ +\\ diff --git a/output_formats/rtf/paragraph_ref.rtf b/output_formats/rtf/paragraph_ref.rtf new file mode 100644 index 0000000..9bfc222 --- /dev/null +++ b/output_formats/rtf/paragraph_ref.rtf @@ -0,0 +1,2 @@ +\b %(nr)d +\b0 diff --git a/output_formats/tex/begin.tex b/output_formats/tex/begin.tex new file mode 100644 index 0000000..8789f89 --- /dev/null +++ b/output_formats/tex/begin.tex @@ -0,0 +1,26 @@ +\documentclass[a4,onecolumn]{article} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage[hidelinks]{hyperref} + +\usepackage[top=3.3cm, bottom=3.3cm, left=2cm, right=2cm]{geometry} + +\newif\ifpdf +\ifx\pdfoutput\undefined + \pdffalse +\else + \ifnum\pdfoutput=1 + \pdftrue + \else + \pdffalse + \fi +\fi + +\title{Gamebook} +\author{} +\date{} + +\begin{document} + +\thispagestyle{empty} +\clearpage diff --git a/output_formats/tex/end.tex b/output_formats/tex/end.tex new file mode 100644 index 0000000..6b47932 --- /dev/null +++ b/output_formats/tex/end.tex @@ -0,0 +1 @@ +\end{document} diff --git a/output_formats/tex/paragraph.tex b/output_formats/tex/paragraph.tex new file mode 100644 index 0000000..791871d --- /dev/null +++ b/output_formats/tex/paragraph.tex @@ -0,0 +1,6 @@ + \noindent +\textbf{%(nr)d} + -- +%(text)s +\newline + diff --git a/output_formats/tex/paragraph_ref.tex b/output_formats/tex/paragraph_ref.tex new file mode 100644 index 0000000..0178626 --- /dev/null +++ b/output_formats/tex/paragraph_ref.tex @@ -0,0 +1 @@ +\textbf{%(nr)d} \ No newline at end of file diff --git a/rtf.py b/rtf.py index 101fbd1..1337134 100644 --- a/rtf.py +++ b/rtf.py @@ -1,33 +1,5 @@ from output import OutputFormat -def format_rtf_paragraph_ref(paragraph, shuffled_paragraphs): - return ("\\b %d\n\\b0" % shuffled_paragraphs.to_nr[paragraph]) - class RtfFormat (OutputFormat): def __init__(self): super(RtfFormat, self).__init__('rtf', 'Rich Text Format') - - def write_begin(self, book, output): - print >> output, """ -{\\rtf1\\ansi\\ansicpg1252\\cocoartf1038\\cocoasubrtf360 -{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;} -{\\colortbl;\\red255\\green255\\blue255;} -\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww14140\\viewh14860\\viewkind0 -\\pard\\tx566\\tx1133\\tx1700\\tx2267\\tx2834\\tx3401\\tx3968\\tx4535\\tx5102\\tx5669\\tx6236\\tx6803\\ql\\qnatural\\pardirnatural - -\\f0\\b\\fs24 \\cf0""", - - def write_shuffled_paragraphs(self, shuffled_paragraphs, output): - for p in shuffled_paragraphs.as_list[1:]: - self.write_paragraph(p, shuffled_paragraphs, output) - - def write_paragraph(self, paragraph, shuffled_paragraphs, output): - print >> output, format_rtf_paragraph_ref(paragraph, - shuffled_paragraphs) - print >> output, " - " - print >> output, paragraph.format(shuffled_paragraphs, - format_rtf_paragraph_ref) - print >> output, "\\\n\\" - - def write_end(self, book, output): - print >> output, "}"