Sections tagged to only be included for specific format(s).

This commit is contained in:
Pelle Nilsson 2014-03-04 23:35:54 +01:00
parent 8beda35169
commit 7162851ee3
12 changed files with 111 additions and 47 deletions

View File

@ -22,6 +22,7 @@ like } and { and " and ' and \.
HTML will probably not like <div> or &boom;.
You can make named references [[named1][like this]] (just happens
to be exactly the same syntax as in emacs org-mode btw).
This book also includes a [[formatspecific][format-specific section]].
There should be an image below as well.
If something broke, turn to [[bad]],
otherwise turn to [[good]].[footnote]Good footnote.[/footnote]
@ -40,6 +41,18 @@ to the [[good][good end]].
* named2
Very good. [[good][You win]].[footnote]Win footnote.[/footnote]
* formatspecific :TEX:
This is the format-specific LaTeX section. Now you [[good][win]].
* formatspecific :HTML:
This is the format-specific HTML section. Now you [[good][win]].
* formatspecific :TXT:
This is the format-specific TXT section. Now you [[good][win]].
*formatspecific :DOT:RTF:DEBUG:
This is the format-specific section for no other formats. Now you [[bad][lose]].
* good
Good!

View File

@ -1,16 +1,17 @@
BEGIN DEBUG OUTPUT
Book title: Format
Number of sections: 6
Number of sections: 7
Introduction
Adding an introduction to the gamebook here. This will create a section, but it will not be shuffled nor numbered with the gamebook sections below.
Another Heading
This starts another non-shuffled section. [FOOTNOTE]A footnote in Another Heading.[/FOOTNOTE]
Adventure begins in section 1.
1 (start) - This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like } and { and " and ' and \. HTML will probably not like <div> or &boom;. You can make named references like this (5) (just happens to be exactly the same syntax as in emacs org-mode btw). There should be an image below as well. If something broke, turn to 2, otherwise turn to 3.[FOOTNOTE]Good footnote.[/FOOTNOTE] [IMG]testimage.png[/IMG]
1 (start) - This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like } and { and " and ' and \. HTML will probably not like <div> or &boom;. You can make named references like this (6) (just happens to be exactly the same syntax as in emacs org-mode btw). This book also includes a format-specific section (4). There should be an image below as well. If something broke, turn to 2, otherwise turn to 3.[FOOTNOTE]Good footnote.[/FOOTNOTE] [IMG]testimage.png[/IMG]
2 (bad) - Bad.
3 (good) - Good!
4 (named2) - Very good. You win (3).[FOOTNOTE]Win footnote.[/FOOTNOTE]
5 (named1) - This is where you should end up when you follow the named reference from the starting section. From here you can go to the second named section (4) or to the good end (3).
6 () - (EMPTY)
4 (formatspecific) - This is the format-specific section for no other formats. Now you lose (2).
5 (named2) - Very good. You win (3).[FOOTNOTE]Win footnote.[/FOOTNOTE]
6 (named1) - This is where you should end up when you follow the named reference from the starting section. From here you can go to the second named section (5) or to the good end (3).
7 () - (EMPTY)
END DEBUG OUTPUT

View File

@ -1,8 +1,10 @@
digraph gamebook {
1->5
1->6
1->4
1->2
1->3
4->3
5->4
4->2
5->3
6->5
6->3
}

View File

@ -47,8 +47,9 @@
<div class="section" id="section1">
<div class="sectionnumber" id="para1">1</div>
<div class="sectiontext">
This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like } and { and " and ' and \. HTML will probably not like &lt;div&gt; or &amp;boom;. You can make named references <a class="sectionref enabledlink" data-ref="5"
href="#section5">like this</a> (just happens to be exactly the same syntax as in emacs org-mode btw). There should be an image below as well. If something broke, turn to <a class="sectionref enabledlink" data-ref="2"
This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like } and { and " and ' and \. HTML will probably not like &lt;div&gt; or &amp;boom;. You can make named references <a class="sectionref enabledlink" data-ref="6"
href="#section6">like this</a> (just happens to be exactly the same syntax as in emacs org-mode btw). This book also includes a <a class="sectionref enabledlink" data-ref="4"
href="#section4">format-specific section</a>. There should be an image below as well. If something broke, turn to <a class="sectionref enabledlink" data-ref="2"
href="#section2">2</a>, otherwise turn to <a class="sectionref enabledlink" data-ref="3"
href="#section3">3</a>. (Good footnote.)
<img src="testimage.png" class="sectionimage"></img>
@ -81,9 +82,8 @@ if (typeof gamebook !== 'undefined') {
</script><div class="section" id="section4">
<div class="sectionnumber" id="para4">4</div>
<div class="sectiontext">
Very good. <a class="sectionref enabledlink" data-ref="3"
href="#section3">You win</a>. (Win footnote.)
This is the format-specific HTML section. Now you <a class="sectionref enabledlink" data-ref="3"
href="#section3">win</a>.
</div>
</div>
<script>
@ -93,15 +93,27 @@ if (typeof gamebook !== 'undefined') {
</script><div class="section" id="section5">
<div class="sectionnumber" id="para5">5</div>
<div class="sectiontext">
This is where you should end up when you follow the named reference from the starting section. From here you can go to <a class="sectionref enabledlink" data-ref="4"
href="#section4">the second named section</a> or to the <a class="sectionref enabledlink" data-ref="3"
href="#section3">good end</a>.
Very good. <a class="sectionref enabledlink" data-ref="3"
href="#section3">You win</a>. (Win footnote.)
</div>
</div>
<script>
if (typeof gamebook !== 'undefined') {
gamebook.addSection(5, document.getElementById('section5'));
}
</script><div class="section" id="section6">
<div class="sectionnumber" id="para6">6</div>
<div class="sectiontext">
This is where you should end up when you follow the named reference from the starting section. From here you can go to <a class="sectionref enabledlink" data-ref="5"
href="#section5">the second named section</a> or to the <a class="sectionref enabledlink" data-ref="3"
href="#section3">good end</a>.
</div>
</div>
<script>
if (typeof gamebook !== 'undefined') {
gamebook.addSection(6, document.getElementById('section6'));
}
</script> <div id="counters" class="counters">
</div>
<div id="counterTemplate" class="counterTemplate">

View File

@ -31,10 +31,14 @@
\b \qc 1
\b0\
\ql This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like \} and \{ and " and ' and \\. HTML will probably not like <div> or &boom;. You can make named references like this (
\b 5
\b 6
\b0
)
(just happens to be exactly the same syntax as in emacs org-mode btw). There should be an image below as well. If something broke, turn to \b 2
(just happens to be exactly the same syntax as in emacs org-mode btw). This book also includes a format-specific section (
\b 4
\b0
)
. There should be an image below as well. If something broke, turn to \b 2
\b0
, otherwise turn to \b 3
\b0
@ -55,6 +59,15 @@
\b \qc 4
\b0\
\ql This is the format-specific section for no other formats. Now you lose (
\b 2
\b0
)
. \
\
\b \qc 5
\b0\
\ql Very good. You win (
\b 3
\b0
@ -63,10 +76,10 @@
\
\
\b \qc 5
\b \qc 6
\b0\
\ql This is where you should end up when you follow the named reference from the starting section. From here you can go to the second named section (
\b 4
\b 5
\b0
)
or to the good end (

View File

@ -53,8 +53,9 @@ Adventure begins in section 1.
\subsection*{\begin{center} \textbf{1} \end{center}}
\noindent
This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like \} and \{ and " and ' and \textbackslash. HTML will probably not like <div> or \&boom;. You can make named references like this (\textbf{\autoref{section5}})
(just happens to be exactly the same syntax as in emacs org-mode btw). There should be an image below as well. If something broke, turn to \textbf{\autoref{section2}}, otherwise turn to \textbf{\autoref{section3}}.\footnote{Good footnote.}
This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like \} and \{ and " and ' and \textbackslash. HTML will probably not like <div> or \&boom;. You can make named references like this (\textbf{\autoref{section6}})
(just happens to be exactly the same syntax as in emacs org-mode btw). This book also includes a format-specific section (\textbf{\autoref{section4}})
. There should be an image below as well. If something broke, turn to \textbf{\autoref{section2}}, otherwise turn to \textbf{\autoref{section3}}.\footnote{Good footnote.}
\begin{center}
\includegraphics[width=.9\textwidth]{testimage.png}
\end{center}
@ -83,9 +84,8 @@ Adventure begins in section 1.
\subsection*{\begin{center} \textbf{4} \end{center}}
\noindent
Very good. You win (\textbf{\autoref{section3}})
.\footnote{Win footnote.}
This is the format-specific LaTeX section. Now you win (\textbf{\autoref{section3}})
.
\vspace{1em}
\phantomsection
\refstepcounter{sectionnr}
@ -93,7 +93,17 @@ Adventure begins in section 1.
\subsection*{\begin{center} \textbf{5} \end{center}}
\noindent
This is where you should end up when you follow the named reference from the starting section. From here you can go to the second named section (\textbf{\autoref{section4}})
Very good. You win (\textbf{\autoref{section3}})
.\footnote{Win footnote.}
\vspace{1em}
\phantomsection
\refstepcounter{sectionnr}
\label{section6}
\subsection*{\begin{center} \textbf{6} \end{center}}
\noindent
This is where you should end up when you follow the named reference from the starting section. From here you can go to the second named section (\textbf{\autoref{section5}})
or to the good end (\textbf{\autoref{section3}})
.
\vspace{1em}

View File

@ -10,7 +10,7 @@ Another Heading
Adventure begins in section 1.
1
This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like } and { and " and ' and \. HTML will probably not like <div> or &boom;. You can make named references like this (5) (just happens to be exactly the same syntax as in emacs org-mode btw). There should be an image below as well. If something broke, turn to 2, otherwise turn to 3. (Good footnote.)
This examples tests gamebook formatting, not so much game mechanics or references. Currently there is nothing here really. This section contains some tricky characters to quote, like } and { and " and ' and \. HTML will probably not like <div> or &boom;. You can make named references like this (6) (just happens to be exactly the same syntax as in emacs org-mode btw). This book also includes a format-specific section (4). There should be an image below as well. If something broke, turn to 2, otherwise turn to 3. (Good footnote.)
@ -21,9 +21,12 @@ Adventure begins in section 1.
Good!
4
This is the format-specific TXT section. Now you win (3).
5
Very good. You win (3). (Win footnote.)
5
This is where you should end up when you follow the named reference from the starting section. From here you can go to the second named section (4) or to the good end (3).
6
This is where you should end up when you follow the named reference from the starting section. From here you can go to the second named section (5) or to the good end (3).

View File

@ -74,7 +74,7 @@ def format_gamebook(inputfilenames,
output_format = make_output(outputfilename, templatedirs)
book = sections.Book(make_bookid(outputfilename), includetags, excludetags)
for inputfilename in inputfilenames:
parse_file_to_book(open(inputfilename, 'r'), book)
parse_file_to_book(open(inputfilename, 'r'), output_format.name, book)
if import_default_map_file:
import_default_nr_map(outputfilename, book)
import_nr_maps(mapfilenames, book)
@ -83,17 +83,17 @@ def format_gamebook(inputfilenames,
def make_bookid(filename):
return os.path.splitext(os.path.basename(filename))[0]
def parse_file_to_book(inputfile, book):
def parse_file_to_book(inputfile, output_format_tag, book):
before_first_section = True
name = None
number = None
text = ""
tags = None
tags = set()
intro_section = False
for line in inputfile.readlines():
if line.startswith('*'):
before_first_section = False
if name:
if name and include_for_output_format(tags, output_format_tag):
add_section_to_book(book, name, text, intro_section, number,
tags)
number = None
@ -104,10 +104,10 @@ def parse_file_to_book(inputfile, book):
if not heading[-1].endswith(':'):
raise Exception('Section heading tags syntax error: %s' %
heading)
tags = [t.strip() for t in heading[-1][1:-1].split(':')]
tags = set([t.strip() for t in heading[-1][1:-1].split(':')])
heading = heading[:-1]
else:
tags = None
tags = set()
if len(heading) == 1:
name = heading[0]
elif len(heading) == 2:
@ -116,7 +116,7 @@ def parse_file_to_book(inputfile, book):
if not name or not SECTION_NAME_RE.match(name):
raise Exception("bad section heading: %s" % str(heading))
elif line.startswith('='):
if name:
if name and include_for_output_format(tags, output_format_tag):
add_section_to_book(book, name, text, intro_section, number)
name = line[1:].strip()
intro_section = True
@ -129,14 +129,23 @@ def parse_file_to_book(inputfile, book):
elif len(line.strip()):
raise Exception("unknown content before sections: %s"
% line.strip())
if name:
if name and include_for_output_format(tags, output_format_tag):
add_section_to_book(book, name, text, intro_section, number, tags)
def include_for_output_format(tags, output_format_tag):
include = True
for tag in tags:
if tag.isupper():
if tag == output_format_tag:
return True
else:
include = False
return include
def add_section_to_book(book, name, text, intro_section=False,
number=None, tags=None):
number=None, tags=set()):
section = sections.Section(name, text)
if tags:
section.add_tags(tags)
section.add_tags(tags)
if intro_section:
book.addintro(section)
else:
@ -149,7 +158,7 @@ def make_output(outputfilename, templatedirs):
extension = of['extension']
if outputfilename.endswith('.' + extension):
return OutputFormat(templates.Templates(templatedirs, extension),
of['quote'])
of['quote'], extension.upper())
raise Exception("Unsupported or unknown output format for %s."
% outputfilename)

View File

@ -8,9 +8,10 @@ COUNTER_USE_TAGS = set(['set', 'inc', 'dec', 'min',
class OutputFormat (object):
"Handles book output. Big FIXME required to make sense."
def __init__(self, templates, quote):
def __init__(self, templates, quote, name):
self.templates = templates
self.format_quote = quote
self.name = name
self.counter_names = {}
def quote(self, s):

View File

@ -8,7 +8,7 @@ class Section:
self.tags = set()
def add_tags(self, tags):
self.tags.update(set(tags))
self.tags.update(tags)
def hastag(self, tag):
return tag in self.tags

View File

@ -20,10 +20,10 @@ class TestOutputFormat(TestCase):
pass
def test_create(self):
of = output.OutputFormat(FakeTemplates({}), str)
of = output.OutputFormat(FakeTemplates({}), "TEST", str)
def test_format_begin(self):
of = output.OutputFormat(FakeTemplates({'begin' : 'b %(max)d'}), str)
of = output.OutputFormat(FakeTemplates({'begin' : 'b %(max)d'}), "TEST", str)
self.assertEqual(of.format_begin({'max' : 2}), 'b 2')
class TestReferenceFormatter(TestCase):

View File

@ -1,4 +1,4 @@
* TODO [57/79] [72%]
* TODO [58/78] [74%]
** DONE Debug output
** DONE DOT output
** DONE LaTeX output
@ -145,7 +145,7 @@ listed even if it is not tagged.
followed by cost tag to specify what it will cost, eg [cost gold]2[/cost]
Might want to wait with this and add normal drop function first?
This is like payPrice but a negative cost? Sort of.
** TODO Tags for current format
** DONE Tags for current format
Automatic upper-case tags excluded not matching output format, eg when
generating a HTML file all sections tagged PDF or TXT are excluded
(unless also tagged as HTML of course). Sections with no format