mirror of
https://github.com/Oreolek/gamebookformat.git
synced 2024-06-16 23:20:44 +03:00
Export and import JSON mapping of section numbers.
Except if flag -M is given.
This commit is contained in:
parent
18f313666e
commit
8e1ac6353d
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -12,4 +12,5 @@ test.rtf
|
|||
*.out
|
||||
test.html
|
||||
.lein*
|
||||
target
|
||||
target
|
||||
*.map
|
||||
|
|
|
@ -54,12 +54,16 @@ def make_supported_formats_list_string():
|
|||
return "Supported Output Formats:\n" + "\n".join(
|
||||
[str(f) for f in OUTPUT_FORMATS])
|
||||
|
||||
def format_gamebook(inputfilenames, outputfilename):
|
||||
def format_gamebook(inputfilenames,
|
||||
outputfilename,
|
||||
import_default_map_file):
|
||||
output_format = find_output_format(outputfilename)
|
||||
book = sections.Book()
|
||||
for inputfilename in inputfilenames:
|
||||
parse_file_to_book(open(inputfilename, 'r'), book)
|
||||
output_format.write(book, open(outputfilename, 'w'))
|
||||
if import_default_map_file:
|
||||
import_default_nr_map(outputfilename, book)
|
||||
write_book(book, output_format, outputfilename)
|
||||
|
||||
def parse_file_to_book(inputfile, book):
|
||||
name = None
|
||||
|
@ -68,7 +72,7 @@ def parse_file_to_book(inputfile, book):
|
|||
for line in inputfile.readlines():
|
||||
if line.startswith('*'):
|
||||
if name:
|
||||
book.add(sections.Section(name, text), number)
|
||||
add_section_to_book(book, name, text, number)
|
||||
number = None
|
||||
text = ""
|
||||
heading = line[1:].strip().split(' ')
|
||||
|
@ -82,7 +86,12 @@ def parse_file_to_book(inputfile, book):
|
|||
else:
|
||||
text = text + " " + line.strip()
|
||||
if name:
|
||||
book.add(sections.Section(name, text), number)
|
||||
add_section_to_book(book, name, text, number)
|
||||
|
||||
def add_section_to_book(book, name, text, number=None):
|
||||
book.add(sections.Section(name, text))
|
||||
if number:
|
||||
book.force_section_nr(name, number)
|
||||
|
||||
def find_output_format(outputfilename):
|
||||
for of in OUTPUT_FORMATS:
|
||||
|
@ -91,6 +100,31 @@ def find_output_format(outputfilename):
|
|||
raise Exception("Unsupported or unknown output format for %s."
|
||||
% outputfilename)
|
||||
|
||||
def write_book(book, output_format, outputfilename):
|
||||
shuffled_sections = book.shuffle()
|
||||
output = open(outputfilename, 'w')
|
||||
output_format.write_begin(book, output)
|
||||
output_format.write_shuffled_sections(shuffled_sections, output)
|
||||
output_format.write_end(book, output)
|
||||
save_section_mapping(shuffled_sections, outputfilename)
|
||||
|
||||
def import_default_nr_map(outputfilename, book):
|
||||
mapfilename = make_default_map_filename(outputfilename)
|
||||
if os.path.exists(mapfilename): #FIXME better check
|
||||
for name,nr in json.load(open(mapfilename)).iteritems():
|
||||
book.force_section_nr(name, nr)
|
||||
|
||||
def save_section_mapping(shuffled_sections, outputfilename):
|
||||
mapfilename = make_default_map_filename(outputfilename)
|
||||
json.dump(shuffled_sections.name_to_nr, open(mapfilename, 'w'))
|
||||
|
||||
def make_default_map_filename(outputfilename):
|
||||
basename = outputfilename
|
||||
dotpos = outputfilename.rfind('.')
|
||||
if dotpos >= 1:
|
||||
basename = outputfilename[:dotpos]
|
||||
return basename + '.map'
|
||||
|
||||
if __name__ == '__main__':
|
||||
import argparse
|
||||
ap = argparse.ArgumentParser(epilog=make_supported_formats_list_string(),
|
||||
|
@ -99,6 +133,11 @@ if __name__ == '__main__':
|
|||
help='input gamebook file (eg test.json)')
|
||||
ap.add_argument('outputfile', metavar='outputfile',
|
||||
help='output file (eg test.tex or test.rtf)')
|
||||
ap.add_argument('-M', '--no-default-map', action='store_false',
|
||||
dest='import_default_map_file',
|
||||
help='ignore default map file')
|
||||
args = ap.parse_args()
|
||||
format_gamebook(args.inputfiles, args.outputfile)
|
||||
format_gamebook(args.inputfiles,
|
||||
args.outputfile,
|
||||
args.import_default_map_file)
|
||||
|
||||
|
|
21
sections.py
21
sections.py
|
@ -25,30 +25,32 @@ class ShuffledSections:
|
|||
class Book:
|
||||
def __init__(self):
|
||||
self.sections = []
|
||||
self.from_name = {}
|
||||
self.nr_sections = {}
|
||||
self.max = 0
|
||||
|
||||
def add(self, section, nr=None):
|
||||
def add(self, section):
|
||||
self.sections.append(section)
|
||||
self.from_name[section.name] = section
|
||||
if len(self.sections) > self.max:
|
||||
self.max = len(self.sections)
|
||||
if nr:
|
||||
self.nr_sections[nr] = section
|
||||
if nr > self.max:
|
||||
self.max = nr
|
||||
|
||||
def force_section_nr(self, name, nr):
|
||||
self.nr_sections[nr] = name
|
||||
if nr > self.max:
|
||||
self.max = nr
|
||||
|
||||
def shuffle(self):
|
||||
as_list = [None]
|
||||
from_nr = {}
|
||||
to_nr = {}
|
||||
from_name = {}
|
||||
shuffled = self.sections[:]
|
||||
for p in self.nr_sections.values():
|
||||
shuffled.remove(p)
|
||||
shuffled.remove(self.from_name[p])
|
||||
random.shuffle(shuffled)
|
||||
for nr in range(1, self.max + 1):
|
||||
if self.nr_sections.has_key(nr):
|
||||
section = self.nr_sections[nr]
|
||||
section = self.from_name[self.nr_sections[nr]]
|
||||
elif len(shuffled):
|
||||
section = shuffled.pop()
|
||||
else:
|
||||
|
@ -57,5 +59,4 @@ class Book:
|
|||
from_nr[nr] = section
|
||||
if section:
|
||||
to_nr[section] = nr
|
||||
from_name[section.name] = section
|
||||
return ShuffledSections(as_list, from_nr, to_nr, from_name)
|
||||
return ShuffledSections(as_list, from_nr, to_nr, self.from_name.copy())
|
||||
|
|
4
todo.org
4
todo.org
|
@ -1,4 +1,4 @@
|
|||
* TODO [7/26] [26%]
|
||||
* TODO [8/26] [30%]
|
||||
- [X] Debug output
|
||||
- [X] DOT output
|
||||
- [X] LaTeX output
|
||||
|
@ -6,7 +6,7 @@
|
|||
- [X] HTML output
|
||||
- [X] Split out template hanlding into separate class from output
|
||||
- [X] Inject Output dependencies
|
||||
- [ ] Save section-number mapping and reuse automatically
|
||||
- [X] Save section-number mapping and reuse automatically
|
||||
- [ ] Add support for custom document templates.
|
||||
Fixed names and/or command-line options.
|
||||
- [ ] Add section links in LaTeX output.
|
||||
|
|
Loading…
Reference in a new issue