1
0
Fork 0
mirror of https://github.com/Oreolek/gamebookformat.git synced 2024-04-29 23:59:22 +03:00

Include multiple tags. Now also exclude tags.

This commit is contained in:
Pelle Nilsson 2014-02-28 23:23:18 +01:00
parent df7ea9838a
commit 23999c2373
8 changed files with 37 additions and 11 deletions

View file

@ -10,6 +10,13 @@ Go on to [[demo1]] or [[demo2]] or [[notindemo1]].
* notindemo1
This section is not included in the demo. Turn to [[notindemo2]].
* actuallynotindemoeither :demo:notthisone:
Tagged as demo, but excluded anyay because of the --exclude flag given.
* notatall :notthisone:
Not included at all, because excluded by command-line, and not
demo-tagged either.
* demo1 :demo:
This is the first demo section (not counting the start section).
You can go on to the end at [[theend]].

View file

@ -1 +1 @@
--include demo
--include demo --exclude notthisone

View file

@ -11,6 +11,13 @@ Go on to [[demo1]] or [[demo2]] or [[notindemo1]].
* notindemo1
This section is not included in the demo. Turn to [[notindemo2]].
* actuallynotindemoeither :demo:notthisone:
Tagged as demo, but excluded anyay because of the --exclude flag given.
* notatall :notthisone:
Not included at all, because excluded by command-line, and not
demo-tagged either.
* demo1 :demo:
This is the first demo section (not counting the start section).
You can go on to the end at [[theend]].

View file

@ -0,0 +1 @@
--exclude notthisone

View file

@ -68,10 +68,11 @@ def format_gamebook(inputfilenames,
import_default_map_file,
templatedirs,
shuffle,
includetag,
includetags,
excludetags,
mapfilenames):
output_format = make_output(outputfilename, templatedirs)
book = sections.Book(make_bookid(outputfilename), includetag)
book = sections.Book(make_bookid(outputfilename), includetags, excludetags)
for inputfilename in inputfilenames:
parse_file_to_book(open(inputfilename, 'r'), book)
if import_default_map_file:
@ -194,8 +195,10 @@ if __name__ == '__main__':
help='input gamebook file (eg test.gamebook)')
ap.add_argument('outputfile', metavar='outputfile',
help='output file (eg test.tex or test.rtf)')
ap.add_argument('-i', '--include', action='store', metavar='T',
dest='includetag', help='only include sections with tag')
ap.add_argument('-i', '--include', action='append', metavar='T',
dest='includetags', help='only include sections with tag')
ap.add_argument('-e', '--exclude', action='append', metavar='T',
dest='excludetags', help='exclude sections with tag')
ap.add_argument('-M', '--no-default-map', action='store_false',
dest='import_default_map_file',
help='ignore default map file')
@ -223,5 +226,6 @@ if __name__ == '__main__':
args.import_default_map_file,
templatedirs,
args.shuffle,
args.includetag,
args.includetags or [],
args.excludetags or [],
args.mapfiles or [])

View file

@ -13,6 +13,7 @@ gamebook on paper or a screen (or for debugging it).
: optional arguments:
: -h, --help show this help message and exit
: -i T, --include T only include sections with tag
: -e T, --exclude T exclude sections with tag
: -M, --no-default-map ignore default map file
: -t D, --template D add custom template dir
: -o D, --option D add template override options dir

View file

@ -13,6 +13,9 @@ class Section:
def hastag(self, tag):
return tag in self.tags
def hastag_in_set(self, tags):
return not self.tags.isdisjoint(tags)
def __repr__(self):
return "Section(%s, %s, %s)" % (repr(self.name), repr(self.text),
repr(self.tags))
@ -59,13 +62,14 @@ STR_BOOK_CONFIG = set(['id', 'title', 'author', 'starttext', 'hideintrotext',
INT_BOOK_CONFIG = set(['max'])
class Book:
def __init__(self, bookid='gamebook', includetag=None):
def __init__(self, bookid='gamebook', includetags=None, excludetags=None):
self.sections = []
self.introsections = []
self.from_name = {}
self.nr_sections = {}
self.codewords = set()
self.includetag = includetag
self.includetags = set(includetags or [])
self.excludetags = set(excludetags or [])
self.config = {'max' : 0,
'title' : 'Gamebook',
'author' : '',
@ -85,7 +89,9 @@ class Book:
raise Exception("Unknown book option '%s'." % name)
def add(self, section):
if self.includetag and not section.hastag(self.includetag):
if ((len(self.includetags) > 0
and not section.hastag_in_set(self.includetags))
or section.hastag_in_set(self.excludetags)):
return
if section.name in self.from_name:
raise Exception('Duplicate section names (%s) not allowed.' %

View file

@ -36,14 +36,14 @@ class TestBook(TestCase):
self.assertEqual(b.config['max'], 0)
def test_includetag(self):
b = sections.Book(includetag='test')
b = sections.Book(includetags=['test'])
sec = sections.Section("nnn", "text")
sec.add_tags(['some', 'test', 'other'])
b.add(sec)
self.assertEqual(b.sections, [sec])
def test_excludetag(self):
b = sections.Book(includetag='test')
b = sections.Book(includetags=['test'])
sec = sections.Section("nnn", "text")
sec.add_tags(['some', 'other'])
b.add(sec)