mirror of
https://github.com/Oreolek/gamebookformat.git
synced 2024-06-28 20:55:13 +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:
parent
3791e8c688
commit
f0ad6ea094
8
.gitignore
vendored
8
.gitignore
vendored
|
@ -1,12 +1,12 @@
|
||||||
*~
|
*~
|
||||||
*.pyc
|
*.pyc
|
||||||
*.debug
|
test.debug
|
||||||
*.tex
|
test.tex
|
||||||
*.dot
|
test.dot
|
||||||
*.log
|
*.log
|
||||||
*.aux
|
*.aux
|
||||||
*.toc
|
*.toc
|
||||||
*.pdf
|
*.pdf
|
||||||
*.rtf
|
test.rtf
|
||||||
*.png
|
*.png
|
||||||
*.out
|
*.out
|
7
debug.py
7
debug.py
|
@ -3,10 +3,3 @@ from output import OutputFormat
|
||||||
class DebugFormat (OutputFormat):
|
class DebugFormat (OutputFormat):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(DebugFormat, self).__init__('debug', 'Gamebook Debug Output')
|
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
26
dot.py
|
@ -1,33 +1,7 @@
|
||||||
from output import OutputFormat
|
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):
|
class DotFormat (OutputFormat):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(DotFormat, self).__init__('dot', 'Graphviz paragraph flowchart')
|
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, "}"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,33 @@
|
||||||
#!/usr/bin/env python2
|
#!/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 sys
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
49
latex.py
49
latex.py
|
@ -1,54 +1,5 @@
|
||||||
from output import OutputFormat
|
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):
|
class LatexFormat (OutputFormat):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(LatexFormat, self).__init__('tex', 'LaTeX')
|
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}"
|
|
||||||
|
|
42
output.py
42
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):
|
from paragraphs import paragraph_refs_format
|
||||||
return str(shuffled_paragraphs.to_nr[paragraph])
|
|
||||||
|
|
||||||
class OutputFormat (object):
|
class OutputFormat (object):
|
||||||
def __init__(self, extension, name):
|
def __init__(self, extension, name):
|
||||||
self.extension = extension
|
self.extension = extension
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.cached_templates = {}
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return ".%s: %s" % (self.extension, self.name)
|
return ".%s: %s" % (self.extension, self.name)
|
||||||
|
@ -17,19 +19,43 @@ class OutputFormat (object):
|
||||||
self.write_end(book, output)
|
self.write_end(book, output)
|
||||||
|
|
||||||
def write_begin(self, 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):
|
def write_shuffled_paragraphs(self, shuffled_paragraphs, output):
|
||||||
for p in shuffled_paragraphs.as_list[1:]:
|
for p in shuffled_paragraphs.as_list[1:]:
|
||||||
self.write_paragraph(p, shuffled_paragraphs, output)
|
self.write_paragraph(p, shuffled_paragraphs, output)
|
||||||
|
|
||||||
def write_paragraph(self, paragraph, shuffled_paragraphs, output):
|
def write_paragraph(self, paragraph, shuffled_paragraphs, output):
|
||||||
print >> output, shuffled_paragraphs.to_nr[paragraph]
|
def paragraph_link_render(to_paragraph, shuffled_paragraphs):
|
||||||
print >> output, paragraph.format(shuffled_paragraphs,
|
return self.load_template("paragraph_ref") % {
|
||||||
default_paragraph_link_render)
|
'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):
|
def write_end(self, book, output):
|
||||||
pass
|
print >> output, self.load_template("end") % {},
|
||||||
|
|
||||||
def supports(self, filename):
|
def supports(self, filename):
|
||||||
return filename.endswith('.' + self.extension)
|
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
|
||||||
|
|
||||||
|
|
2
output_formats/debug/begin.debug
Normal file
2
output_formats/debug/begin.debug
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
BEGIN DEBUG OUTPUT
|
||||||
|
Number of paragraphs: %(max)d
|
1
output_formats/debug/end.debug
Normal file
1
output_formats/debug/end.debug
Normal file
|
@ -0,0 +1 @@
|
||||||
|
END DEBUG OUTPUT
|
1
output_formats/debug/paragraph.debug
Normal file
1
output_formats/debug/paragraph.debug
Normal file
|
@ -0,0 +1 @@
|
||||||
|
%(nr)d - %(text)s
|
1
output_formats/debug/paragraph_ref.debug
Normal file
1
output_formats/debug/paragraph_ref.debug
Normal file
|
@ -0,0 +1 @@
|
||||||
|
%(nr)d
|
1
output_formats/dot/begin.dot
Normal file
1
output_formats/dot/begin.dot
Normal file
|
@ -0,0 +1 @@
|
||||||
|
digraph gamebook {
|
1
output_formats/dot/end.dot
Normal file
1
output_formats/dot/end.dot
Normal file
|
@ -0,0 +1 @@
|
||||||
|
}
|
0
output_formats/dot/paragraph.dot
Normal file
0
output_formats/dot/paragraph.dot
Normal file
1
output_formats/dot/paragraph_ref.dot
Normal file
1
output_formats/dot/paragraph_ref.dot
Normal file
|
@ -0,0 +1 @@
|
||||||
|
%(from_nr)d->%(nr)d
|
7
output_formats/rtf/begin.rtf
Normal file
7
output_formats/rtf/begin.rtf
Normal 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
|
1
output_formats/rtf/end.rtf
Normal file
1
output_formats/rtf/end.rtf
Normal file
|
@ -0,0 +1 @@
|
||||||
|
}
|
4
output_formats/rtf/paragraph.rtf
Normal file
4
output_formats/rtf/paragraph.rtf
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
\b %(nr)d
|
||||||
|
\b0 - %(text)s
|
||||||
|
\\
|
||||||
|
\\
|
2
output_formats/rtf/paragraph_ref.rtf
Normal file
2
output_formats/rtf/paragraph_ref.rtf
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
\b %(nr)d
|
||||||
|
\b0
|
26
output_formats/tex/begin.tex
Normal file
26
output_formats/tex/begin.tex
Normal 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
|
1
output_formats/tex/end.tex
Normal file
1
output_formats/tex/end.tex
Normal file
|
@ -0,0 +1 @@
|
||||||
|
\end{document}
|
6
output_formats/tex/paragraph.tex
Normal file
6
output_formats/tex/paragraph.tex
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
\noindent
|
||||||
|
\textbf{%(nr)d}
|
||||||
|
--
|
||||||
|
%(text)s
|
||||||
|
\newline
|
||||||
|
|
1
output_formats/tex/paragraph_ref.tex
Normal file
1
output_formats/tex/paragraph_ref.tex
Normal file
|
@ -0,0 +1 @@
|
||||||
|
\textbf{%(nr)d}
|
28
rtf.py
28
rtf.py
|
@ -1,33 +1,5 @@
|
||||||
from output import OutputFormat
|
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):
|
class RtfFormat (OutputFormat):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(RtfFormat, self).__init__('rtf', 'Rich Text Format')
|
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, "}"
|
|
||||||
|
|
Loading…
Reference in a new issue