From 54e03105e7c06c573dbd65372353920a91d51688 Mon Sep 17 00:00:00 2001 From: Pelle Nilsson Date: Mon, 10 Jun 2013 22:15:54 +0200 Subject: [PATCH] Default templates. Added plain text output. Made default section template constisting of sectionheader and sectionbody. Improved Makefile with more dependencies to rebuild examples when needed. --- Makefile | 14 +++++++----- examples/format.gamebook | 3 +++ formatgamebook.py | 1 + readme.org | 1 + templates.py | 22 ++++++++++++------- templates/DEFAULT/section.txt | 2 ++ templates/rtf/sectionbody.rtf | 2 ++ templates/rtf/sectionheading.rtf | 2 ++ .../tex/{section.tex => sectionbody.tex} | 4 ---- templates/tex/sectionheading.tex | 3 +++ templates/txt/section_ref.txt | 1 + templates/txt/sectionbody.txt | 2 ++ templates/txt/sectionheading.txt | 2 ++ 13 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 examples/format.gamebook create mode 100644 templates/DEFAULT/section.txt create mode 100644 templates/rtf/sectionbody.rtf create mode 100644 templates/rtf/sectionheading.rtf rename templates/tex/{section.tex => sectionbody.tex} (57%) create mode 100644 templates/tex/sectionheading.tex create mode 100644 templates/txt/section_ref.txt create mode 100644 templates/txt/sectionbody.txt create mode 100644 templates/txt/sectionheading.txt diff --git a/Makefile b/Makefile index b318ab2..c3f7b02 100644 --- a/Makefile +++ b/Makefile @@ -7,20 +7,24 @@ pdf: $(examples:.gamebook=.pdf) html: $(examples:.gamebook=.html) debug: $(examples:.gamebook=.debug) png: $(examples:.gamebook=.png) +txt: $(examples:.gamebook=.txt) -%.rtf: %.gamebook formatgamebook.py +%.rtf: %.gamebook *.py templates/rtf/*.rtf ./formatgamebook.py --verify $< $@ -%.html: %.gamebook formatgamebook.py +%.html: %.gamebook *.py templates/html/*.html ./formatgamebook.py --verify $< $@ -%.tex: %.gamebook formatgamebook.py +%.tex: %.gamebook *.py templates/tex/*.tex ./formatgamebook.py --verify $< $@ -%.dot: %.gamebook formatgamebook.py +%.dot: %.gamebook *.py templates/dot/*.dot ./formatgamebook.py --verify $< $@ -%.debug: %.gamebook formatgamebook.py +%.debug: %.gamebook *.py templates/debug/*.debug + ./formatgamebook.py --verify $< $@ + +%.txt: %.gamebook *.py templates/txt/*.txt ./formatgamebook.py --verify $< $@ %.pdf: %.tex diff --git a/examples/format.gamebook b/examples/format.gamebook new file mode 100644 index 0000000..ab0a825 --- /dev/null +++ b/examples/format.gamebook @@ -0,0 +1,3 @@ +* 1 start +This examples tests gamebook formatting, not so much game mechanics or +references. Currently there is nothing here really. diff --git a/formatgamebook.py b/formatgamebook.py index 8fc88c1..438c3c1 100755 --- a/formatgamebook.py +++ b/formatgamebook.py @@ -50,6 +50,7 @@ OUTPUT_FORMATS = [ of('rtf', 'Rich Text Format'), of('dot', 'Graphviz section flowchart'), of('html', 'HTML+JS playable in browser'), + of('txt', 'Plain text'), of('debug', 'Gamebook Debug Output'), ] diff --git a/readme.org b/readme.org index b34a158..f906eca 100644 --- a/readme.org +++ b/readme.org @@ -25,6 +25,7 @@ gamebook on paper or a screen (or for debugging it). | Rich Text Format | .rtf | Supported because the Windhammer Prize requires it. | | Graphviz DOT | .dot | Use with the Graphviz dot tool to generate a flowchart graph of all sections in the gamebook. | | HTML | .html | Play gamebook in browser. | +| Plain Text | .txt | Raw plain text without formatting. | | Debug Plain Text | .debug | Plain text debug output of gamebook contents. | More to be added. diff --git a/templates.py b/templates.py index 86227b5..4a2cae2 100644 --- a/templates.py +++ b/templates.py @@ -18,14 +18,18 @@ class Templates (object): for templatedir in self.templatedirs: if self.has_template_in(templatedir, name): return self.get_in(templatedir, name) + elif self.has_template_in(templatedir, name, 'DEFAULT', 'txt'): + return self.get_in(templatedir, name, 'DEFAULT', 'txt') return "" - def has_template_in(self, templatedir, name): + def has_template_in(self, templatedir, name, subdir=None, extension=None): # FIXME better test - return os.path.exists(self.get_template_filename(templatedir, name)) + return os.path.exists(self.get_template_filename(templatedir, name, + subdir, + extension)) - def get_in(self, templatedir, name): - filename = self.get_template_filename(templatedir, name) + def get_in(self, templatedir, name, subdir=None, extension=None): + filename = self.get_template_filename(templatedir, name, subdir, extension) f = open(filename, "r") template = self.read_template(f); f.close() @@ -48,7 +52,9 @@ class Templates (object): else: raise Exception("Bad preprocessor line '%s' in template." % line) - def get_template_filename(self, templatedir, name): - return os.path.join(templatedir, - self.extension, - name + "." + self.extension) + def get_template_filename(self, templatedir, name, subdir=None, extension=None): + if not subdir: + subdir = self.extension + if not extension: + extension = self.extension + return os.path.join(templatedir, subdir, name + "." + extension) diff --git a/templates/DEFAULT/section.txt b/templates/DEFAULT/section.txt new file mode 100644 index 0000000..06e633b --- /dev/null +++ b/templates/DEFAULT/section.txt @@ -0,0 +1,2 @@ +#include "sectionheading" +#include "sectionbody" diff --git a/templates/rtf/sectionbody.rtf b/templates/rtf/sectionbody.rtf new file mode 100644 index 0000000..0db7fd3 --- /dev/null +++ b/templates/rtf/sectionbody.rtf @@ -0,0 +1,2 @@ +\ql %(text)s \ +\ diff --git a/templates/rtf/sectionheading.rtf b/templates/rtf/sectionheading.rtf new file mode 100644 index 0000000..3c28fcf --- /dev/null +++ b/templates/rtf/sectionheading.rtf @@ -0,0 +1,2 @@ +\b \qc %(nr)d +\b0\ diff --git a/templates/tex/section.tex b/templates/tex/sectionbody.tex similarity index 57% rename from templates/tex/section.tex rename to templates/tex/sectionbody.tex index fd91d5a..14822c5 100644 --- a/templates/tex/section.tex +++ b/templates/tex/sectionbody.tex @@ -1,9 +1,5 @@ -\phantomsection -\refstepcounter{sectionnr} -\label{section%(nr)d} \subsection*{\begin{center} \textbf{%(nr)d} \end{center}} \noindent %(text)s \newline - diff --git a/templates/tex/sectionheading.tex b/templates/tex/sectionheading.tex new file mode 100644 index 0000000..a8bb1dd --- /dev/null +++ b/templates/tex/sectionheading.tex @@ -0,0 +1,3 @@ +\phantomsection +\refstepcounter{sectionnr} +\label{section%(nr)d} diff --git a/templates/txt/section_ref.txt b/templates/txt/section_ref.txt new file mode 100644 index 0000000..59d1f95 --- /dev/null +++ b/templates/txt/section_ref.txt @@ -0,0 +1 @@ +%(nr)d \ No newline at end of file diff --git a/templates/txt/sectionbody.txt b/templates/txt/sectionbody.txt new file mode 100644 index 0000000..fd190c1 --- /dev/null +++ b/templates/txt/sectionbody.txt @@ -0,0 +1,2 @@ +%(text)s + diff --git a/templates/txt/sectionheading.txt b/templates/txt/sectionheading.txt new file mode 100644 index 0000000..fb48eff --- /dev/null +++ b/templates/txt/sectionheading.txt @@ -0,0 +1,2 @@ +%(nr) +