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

Cleaned up output and templates handling a bit.

This commit is contained in:
Pelle Nilsson 2013-06-03 22:19:06 +02:00
parent 2dfd2dca24
commit c17d00872b
9 changed files with 39 additions and 56 deletions

View file

@ -1,5 +0,0 @@
from output import OutputFormat
class DebugFormat (OutputFormat):
def __init__(self):
super(DebugFormat, self).__init__('debug', 'Gamebook Debug Output')

7
dot.py
View file

@ -1,7 +0,0 @@
from output import OutputFormat
class DotFormat (OutputFormat):
def __init__(self):
super(DotFormat, self).__init__('dot', 'Graphviz paragraph flowchart')

View file

@ -34,22 +34,21 @@ import sys
import json
import sections
import templates
from output import OutputFormat
from latex import LatexFormat
from rtf import RtfFormat
from dot import DotFormat
from html import HtmlFormat
from debug import DebugFormat
USAGE = "usage: %prog [options] inputfile(s)... outputfile"
OUTPUT_FORMATS = [LatexFormat(),
RtfFormat(),
DotFormat(),
HtmlFormat(),
DebugFormat()]
def of(extension, name):
return OutputFormat(templates.Templates(extension), extension, name)
OUTPUT_FORMATS = [
of('tex', 'LaTeX'),
of('rtf', 'Rich Text Format'),
of('dot', 'Graphviz section flowchart'),
of('html', 'HTML+JS playable in browser'),
of('debug', 'Gamebook Debug Output'),
]
def make_supported_formats_list_string():
return "Supported Output Formats:\n" + "\n".join(

View file

@ -1,6 +0,0 @@
from output import OutputFormat
class HtmlFormat (OutputFormat):
def __init__(self):
super(HtmlFormat, self).__init__('html',
'HTML+JS playable in browser')

View file

@ -1,5 +0,0 @@
from output import OutputFormat
class LatexFormat (OutputFormat):
def __init__(self):
super(LatexFormat, self).__init__('tex', 'LaTeX')

View file

@ -3,10 +3,10 @@ import os.path
import sys
class OutputFormat (object):
def __init__(self, extension, name):
def __init__(self, templates, extension, name):
self.extension = extension
self.name = name
self.cached_templates = {}
self.templates = templates
def __str__(self):
return ".%s: %s" % (self.extension, self.name)
@ -43,18 +43,7 @@ class OutputFormat (object):
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]),
"templates",
self.extension,
name + "." + self.extension)
f = open(filename, "r")
template = f.read()
f.close()
self.cached_templates[name] = template
return template
return self.templates.get(name)
class ReferenceFormatter (object):
"There is probably a better way, but this hack seems to work."

5
rtf.py
View file

@ -1,5 +0,0 @@
from output import OutputFormat
class RtfFormat (OutputFormat):
def __init__(self):
super(RtfFormat, self).__init__('rtf', 'Rich Text Format')

23
templates.py Normal file
View file

@ -0,0 +1,23 @@
import os
import os.path
import sys
class Templates (object):
def __init__(self, extension):
self.extension = extension
self.templates_dir = os.path.join(os.path.dirname(sys.argv[0]),
"templates",
extension)
self.cached_templates = {}
def get(self, name):
if name in self.cached_templates:
return self.cached_templates[name]
filename = os.path.join(self.templates_dir,
name + "." + self.extension)
f = open(filename, "r")
template = f.read()
f.close()
self.cached_templates[name] = template
return template

View file

@ -1,11 +1,11 @@
* TODO [5/26] [19%]
* TODO [7/26] [26%]
- [X] Debug output
- [X] DOT output
- [X] LaTeX output
- [X] RTF output
- [X] HTML output
- [ ] Split out template hanlding into separate class from output
- [ ] Inject Output dependencies
- [X] Split out template hanlding into separate class from output
- [X] Inject Output dependencies
- [ ] Save section-number mapping and reuse automatically
- [ ] Add support for custom document templates.
Fixed names and/or command-line options.