1
0
Fork 0
mirror of https://github.com/Oreolek/gamebookformat.git synced 2024-06-26 03:41:04 +03:00

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.
This commit is contained in:
Pelle Nilsson 2013-05-28 23:31:09 +02:00
parent 3791e8c688
commit f0ad6ea094
23 changed files with 122 additions and 122 deletions

8
.gitignore vendored
View file

@ -1,12 +1,12 @@
*~
*.pyc
*.debug
*.tex
*.dot
test.debug
test.tex
test.dot
*.log
*.aux
*.toc
*.pdf
*.rtf
test.rtf
*.png
*.out

View file

@ -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"

26
dot.py
View file

@ -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, "}"

View file

@ -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

View file

@ -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}"

View file

@ -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

View file

@ -0,0 +1,2 @@
BEGIN DEBUG OUTPUT
Number of paragraphs: %(max)d

View file

@ -0,0 +1 @@
END DEBUG OUTPUT

View file

@ -0,0 +1 @@
%(nr)d - %(text)s

View file

@ -0,0 +1 @@
%(nr)d

View file

@ -0,0 +1 @@
digraph gamebook {

View file

@ -0,0 +1 @@
}

View file

View file

@ -0,0 +1 @@
%(from_nr)d->%(nr)d

View file

@ -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

View file

@ -0,0 +1 @@
}

View file

@ -0,0 +1,4 @@
\b %(nr)d
\b0 - %(text)s
\\
\\

View file

@ -0,0 +1,2 @@
\b %(nr)d
\b0

View file

@ -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

View file

@ -0,0 +1 @@
\end{document}

View file

@ -0,0 +1,6 @@
\noindent
\textbf{%(nr)d}
--
%(text)s
\newline

View file

@ -0,0 +1 @@
\textbf{%(nr)d}

28
rtf.py
View file

@ -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, "}"