1
0
Fork 0
mirror of https://github.com/Oreolek/gamebookformat.git synced 2024-05-15 23:48:21 +03:00
gamebookformat/output.py

64 lines
2.1 KiB
Python
Raw Normal View History

import os
import os.path
import sys
class OutputFormat (object):
def __init__(self, templates, extension, name):
self.extension = extension
self.name = name
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
},
def write_shuffled_sections(self, shuffled_sections, output):
for p in shuffled_sections.as_list[1:]:
if p:
self.write_section(p, shuffled_sections, output)
def write_section(self, section, shuffled_sections, output):
2013-05-29 01:00:30 +03:00
refs = []
refsdict = ReferenceFormatter(section, shuffled_sections,
self.load_template("section_ref"))
formatted_text = section.format(refsdict)
print >> output, self.load_template("section") % {
'nr' : shuffled_sections.to_nr[section],
2013-05-29 01:00:30 +03:00
'text' : formatted_text,
'refs' : '\n'.join(refsdict.getfound()) # hack for DOT output
},
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)
class ReferenceFormatter (object):
"There is probably a better way, but this hack seems to work."
def __init__(self, section, shuffled_sections, ref_template):
self.section = section
self.shuffled_sections = shuffled_sections
self.found = set()
self.ref_template = ref_template
def __getitem__(self, key):
to_section = self.shuffled_sections.from_name[key]
res = self.ref_template % {
'nr' : self.shuffled_sections.to_nr[to_section],
'from_nr' : self.shuffled_sections.to_nr[self.section]
}
if key in self.shuffled_sections.name_to_nr:
self.found.add(res)
return res
def getfound(self):
return list(self.found)