+ This should have number 10, as specified in first map file. The end, at 99, should be at 99.
+
+
+
+
99
+
+ Number 99, as specified in second map file.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
(show instructions)
+
+
+
diff --git a/expected/withmaps.rtf b/expected/withmaps.rtf
new file mode 100644
index 0000000..5e500b6
--- /dev/null
+++ b/expected/withmaps.rtf
@@ -0,0 +1,27 @@
+{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
+{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
+{\colortbl;\red255\green255\blue255;}
+\paperw11900\paperh16840\margl1440\margr1440\vieww14140\viewh14860\viewkind0
+\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
+
+\f0\b\fs24 \cf0
+
+\b \qc Example Gamebook With Maps
+\b0\
+
+\b Turn to 1 to begin.
+\b0\
+\
+\b \qc 10
+\b0\
+\ql This should have number 10, as specified in first map file. The end, at \b 99
+\b0
+, should be at 99. \
+\
+
+\b \qc 99
+\b0\
+\ql Number 99, as specified in second map file. \
+\
+
+}
diff --git a/expected/withmaps.tex b/expected/withmaps.tex
new file mode 100644
index 0000000..6806e52
--- /dev/null
+++ b/expected/withmaps.tex
@@ -0,0 +1,151 @@
+\documentclass[a5paper,onecolumn]{book}
+
+\usepackage[utf8]{inputenc}
+\usepackage[T1]{fontenc}
+\usepackage[hidelinks]{hyperref}
+\usepackage{graphicx}
+
+\usepackage[top=3.3cm, bottom=3.3cm, left=2cm, right=2cm]{geometry}
+\newif\ifpdf
+\ifx\pdfoutput\undefined
+ \pdffalse
+\else
+ \ifnum\pdfoutput=1
+ \pdftrue
+ \else
+ \pdffalse
+ \fi
+\fi
+
+\title{Example Gamebook With Maps}
+\author{Pelle Nilsson}
+\date{}
+
+\newcounter{sectionnr}
+
+\begin{document}
+
+\maketitle
+
+\clearpage
+
+\thispagestyle{empty}
+
+\pagestyle{empty}
+
+
+Turn to 1 to begin.
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\phantomsection
+\refstepcounter{sectionnr}
+\label{section10}
+\subsection*{\begin{center} \textbf{10} \end{center}}
+
+ \noindent
+ This should have number 10, as specified in first map file. The end, at \textbf{\autoref{section99}}, should be at 99.
+\vspace{1em}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\refstepcounter{sectionnr}
+\phantomsection
+\refstepcounter{sectionnr}
+\label{section99}
+\subsection*{\begin{center} \textbf{99} \end{center}}
+
+ \noindent
+ Number 99, as specified in second map file.
+\vspace{1em}
+\end{document}
diff --git a/expected/withmaps.txt b/expected/withmaps.txt
new file mode 100644
index 0000000..7586d5c
--- /dev/null
+++ b/expected/withmaps.txt
@@ -0,0 +1,11 @@
+ Example Gamebook With Maps
+
+
+
+Turn to 1 to begin.
+10
+ This should have number 10, as specified in first map file. The end, at 99, should be at 99.
+
+99
+ Number 99, as specified in second map file.
+
diff --git a/expected/withoutdemo.tex b/expected/withoutdemo.tex
index fd7f3c0..5923f99 100644
--- a/expected/withoutdemo.tex
+++ b/expected/withoutdemo.tex
@@ -27,13 +27,12 @@
\maketitle
+\clearpage
+
\thispagestyle{empty}
\pagestyle{empty}
-\clearpage
-
-
Turn to 1 to begin.
\phantomsection
diff --git a/formatgamebook.py b/formatgamebook.py
index 0e03e3c..1841476 100755
--- a/formatgamebook.py
+++ b/formatgamebook.py
@@ -68,13 +68,15 @@ def format_gamebook(inputfilenames,
import_default_map_file,
templatedirs,
shuffle,
- includetag):
+ includetag,
+ mapfilenames):
output_format = make_output(outputfilename, templatedirs)
book = sections.Book(make_bookid(outputfilename), includetag)
for inputfilename in inputfilenames:
parse_file_to_book(open(inputfilename, 'r'), book)
if import_default_map_file:
import_default_nr_map(outputfilename, book)
+ import_nr_maps(mapfilenames, book)
write_book(book, shuffle, output_format, outputfilename)
def make_bookid(filename):
@@ -162,7 +164,13 @@ def write_book(book, shuffle, output_format, outputfilename):
save_section_mapping(shuffled_sections, outputfilename)
def import_default_nr_map(outputfilename, book):
- mapfilename = make_default_map_filename(outputfilename)
+ import_nr_map(make_default_map_filename(outputfilename), book)
+
+def import_nr_maps(mapfilenames, book):
+ for m in mapfilenames:
+ import_nr_map(m, book)
+
+def import_nr_map(mapfilename, book):
if os.path.exists(mapfilename): #FIXME better check
for name,nr in json.load(open(mapfilename)).iteritems():
book.force_section_nr(name, nr)
@@ -198,6 +206,8 @@ if __name__ == '__main__':
ap.add_argument('-S', '--no-shuffle', action='store_false',
dest='shuffle',
help='do not shuffle sections')
+ ap.add_argument('-m', '--map-file', metavar='F', dest='mapfiles',
+ action='append', help='number map file')
args = ap.parse_args()
templatedirs = ['templates',
os.path.join(os.path.dirname(sys.argv[0]), 'templates')]
@@ -213,4 +223,5 @@ if __name__ == '__main__':
args.import_default_map_file,
templatedirs,
args.shuffle,
- args.includetag)
+ args.includetag,
+ args.mapfiles or [])
diff --git a/readme.org b/readme.org
index 8bcaf10..3f22af1 100644
--- a/readme.org
+++ b/readme.org
@@ -17,7 +17,7 @@ gamebook on paper or a screen (or for debugging it).
: -t D, --template D add custom template dir
: -o D, --option D add template override options dir
: -S, --no-shuffle do not shuffle sections
-
+: -m F, --map-file F number map file
** Supported Output Formats
| Name | Extension | Description |
@@ -49,6 +49,23 @@ produce LaTeX files in Letter size rather than the default A5 size.
| letter | tex | Letter paper size documents (PDF). |
| static | html | Remove script link from HTML output. |
+** Number Map Files
+Whenever formatgamebook.py runs it looks for a file with the same
+name as the output file, but with a .map suffix (eg output.map if
+the output file is output.html). That file is expected to contain
+a JSON object mapping section names to numbers, and all sections
+included in that file are given those numbers rather than being
+randomly shuffled like other paragraphs. You can also add more
+map files using the --map-file (or -m) option on the command-line.
+The -M flag disables the default map file. Unless it has been
+disabled a default map file is also written, containing all
+the sections output. This way if you generate the same output
+file (or files with the same name but different types) they
+always get the same section numbers assigned. The generated
+map file can also be used from other scripts to easily read
+what numbers were assigned to sections, if you need to
+use the number of some section for something outside of
+the gamebook itself.
** Gamebook Format
The input file expected by the formatgamebook.py script must be in a