mirror of
https://github.com/Oreolek/gamebookformat.git
synced 2024-06-16 23:20:44 +03:00
Flag -t to add custom template dirs to override default templates.
This commit is contained in:
parent
f8ba31300a
commit
8a4a3401b4
|
@ -40,7 +40,8 @@ from output import OutputFormat
|
|||
USAGE = "usage: %prog [options] inputfile(s)... outputfile"
|
||||
|
||||
def of(extension, name):
|
||||
return OutputFormat(templates.Templates(extension), extension, name)
|
||||
return {'extension' : extension,
|
||||
'name' : name}
|
||||
|
||||
OUTPUT_FORMATS = [
|
||||
of('tex', 'LaTeX'),
|
||||
|
@ -56,8 +57,9 @@ def make_supported_formats_list_string():
|
|||
|
||||
def format_gamebook(inputfilenames,
|
||||
outputfilename,
|
||||
import_default_map_file):
|
||||
output_format = find_output_format(outputfilename)
|
||||
import_default_map_file,
|
||||
templatedirs):
|
||||
output_format = make_output(outputfilename, templatedirs)
|
||||
book = sections.Book()
|
||||
for inputfilename in inputfilenames:
|
||||
parse_file_to_book(open(inputfilename, 'r'), book)
|
||||
|
@ -93,10 +95,11 @@ def add_section_to_book(book, name, text, number=None):
|
|||
if number:
|
||||
book.force_section_nr(name, number)
|
||||
|
||||
def find_output_format(outputfilename):
|
||||
def make_output(outputfilename, templatedirs):
|
||||
for of in OUTPUT_FORMATS:
|
||||
if of.supports(outputfilename):
|
||||
return of
|
||||
extension = of['extension']
|
||||
if outputfilename.endswith('.' + extension):
|
||||
return OutputFormat(templates.Templates(templatedirs, extension))
|
||||
raise Exception("Unsupported or unknown output format for %s."
|
||||
% outputfilename)
|
||||
|
||||
|
@ -136,8 +139,16 @@ if __name__ == '__main__':
|
|||
ap.add_argument('-M', '--no-default-map', action='store_false',
|
||||
dest='import_default_map_file',
|
||||
help='ignore default map file')
|
||||
ap.add_argument('-t', '--template', metavar='D', dest='templatedirs',
|
||||
action='append', help='Add custom template dir')
|
||||
args = ap.parse_args()
|
||||
templatedirs = ['templates',
|
||||
os.path.join(os.path.dirname(sys.argv[0]), 'templates')]
|
||||
if args.templatedirs:
|
||||
for t in args.templatedirs:
|
||||
templatedirs.insert(-2, t)
|
||||
format_gamebook(args.inputfiles,
|
||||
args.outputfile,
|
||||
args.import_default_map_file)
|
||||
args.import_default_map_file,
|
||||
templatedirs)
|
||||
|
||||
|
|
11
output.py
11
output.py
|
@ -3,14 +3,10 @@ import os.path
|
|||
import sys
|
||||
|
||||
class OutputFormat (object):
|
||||
def __init__(self, templates, extension, name):
|
||||
self.extension = extension
|
||||
self.name = name
|
||||
"Handles book output. Big FIXME required to make sense."
|
||||
def __init__(self, templates):
|
||||
self.templates = templates
|
||||
|
||||
def __str__(self):
|
||||
return ".%s: %s" % (self.extension, self.name)
|
||||
|
||||
def write_begin(self, book, output):
|
||||
print >> output, self.load_template("begin") % {
|
||||
'max' : book.max
|
||||
|
@ -35,9 +31,6 @@ class OutputFormat (object):
|
|||
def write_end(self, book, output):
|
||||
print >> output, self.load_template("end") % {},
|
||||
|
||||
def supports(self, filename):
|
||||
return filename.endswith('.' + self.extension)
|
||||
|
||||
def load_template(self, name):
|
||||
return self.templates.get(name)
|
||||
|
||||
|
|
25
templates.py
25
templates.py
|
@ -3,21 +3,34 @@ import os.path
|
|||
import sys
|
||||
|
||||
class Templates (object):
|
||||
def __init__(self, extension):
|
||||
def __init__(self, templatedirs, extension):
|
||||
self.extension = extension
|
||||
self.templates_dir = os.path.join(os.path.dirname(sys.argv[0]),
|
||||
"templates",
|
||||
extension)
|
||||
self.cached_templates = {}
|
||||
self.templatedirs = templatedirs
|
||||
|
||||
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)
|
||||
for templatedir in self.templatedirs:
|
||||
if self.has_template_in(templatedir, name):
|
||||
return self.get_in(templatedir, name)
|
||||
raise Exception("Found no template " + name + " for "
|
||||
+ self.extension + ".")
|
||||
|
||||
def has_template_in(self, templatedir, name):
|
||||
# FIXME better test
|
||||
return os.path.exists(self.get_template_filename(templatedir, name))
|
||||
|
||||
def get_in(self, templatedir, name):
|
||||
filename = self.get_template_filename(templatedir, name)
|
||||
f = open(filename, "r")
|
||||
template = f.read()
|
||||
f.close()
|
||||
self.cached_templates[name] = template
|
||||
return template
|
||||
|
||||
def get_template_filename(self, templatedir, name):
|
||||
return os.path.join(templatedir,
|
||||
self.extension,
|
||||
name + "." + self.extension)
|
||||
|
||||
|
|
4
todo.org
4
todo.org
|
@ -1,4 +1,4 @@
|
|||
* TODO [8/26] [30%]
|
||||
* TODO [9/26] [34%]
|
||||
- [X] Debug output
|
||||
- [X] DOT output
|
||||
- [X] LaTeX output
|
||||
|
@ -7,7 +7,7 @@
|
|||
- [X] Split out template hanlding into separate class from output
|
||||
- [X] Inject Output dependencies
|
||||
- [X] Save section-number mapping and reuse automatically
|
||||
- [ ] Add support for custom document templates.
|
||||
- [X] Add support for custom document templates.
|
||||
Fixed names and/or command-line options.
|
||||
- [ ] Add section links in LaTeX output.
|
||||
- [ ] Debug HTML output
|
||||
|
|
Loading…
Reference in a new issue