nextguidefmt 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nextguidefmt/__init__.py +22 -0
- nextguidefmt/__main__.py +148 -0
- nextguidefmt/doc.py +453 -0
- nextguidefmt/index.py +733 -0
- nextguidefmt/node.py +433 -0
- nextguidefmt/set.py +358 -0
- nextguidefmt/token.py +197 -0
- nextguidefmt-2.0.0.dist-info/METADATA +225 -0
- nextguidefmt-2.0.0.dist-info/RECORD +11 -0
- nextguidefmt-2.0.0.dist-info/WHEEL +5 -0
- nextguidefmt-2.0.0.dist-info/top_level.txt +1 -0
nextguidefmt/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# nextguidefmt/__init__
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
from .doc import GuideDoc
|
|
5
|
+
from .index import GuideIndex, indextermkey_factory
|
|
6
|
+
from .node import GuideNode
|
|
7
|
+
from .set import GuideSet, CONSISTENT_DOC_CMDS
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
__version__ = "2.0.0"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"GuideDoc",
|
|
17
|
+
"GuideIndex",
|
|
18
|
+
"GuideNode",
|
|
19
|
+
"GuideSet",
|
|
20
|
+
"indextermkey_factory",
|
|
21
|
+
"CONSISTENT_DOC_CMDS",
|
|
22
|
+
]
|
nextguidefmt/__main__.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
from . import __version__, GuideSet, CONSISTENT_DOC_CMDS, indextermkey_factory
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# --- parse arguments ---
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
parser = argparse.ArgumentParser(
|
|
17
|
+
# override the program name as running this as a __main__ inside a
|
|
18
|
+
# module directory will use '__main__' by default - this name isn't
|
|
19
|
+
# necessarily correct, but it looks better than that
|
|
20
|
+
prog="nextguidefmt",
|
|
21
|
+
|
|
22
|
+
# we want the epilog help output to be printed as it and not
|
|
23
|
+
# reformatted or line wrapped
|
|
24
|
+
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
25
|
+
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
"-o", "--output-dir",
|
|
28
|
+
help="directory to write out formatted NextGuide files with '.gde'"
|
|
29
|
+
" extensions; the default is to print a 'readable' version,"
|
|
30
|
+
" with markup removed, to standard output")
|
|
31
|
+
|
|
32
|
+
parser.add_argument(
|
|
33
|
+
"-i", "--index",
|
|
34
|
+
action="store_true",
|
|
35
|
+
help="recreate index pages, including common index across set")
|
|
36
|
+
|
|
37
|
+
parser.add_argument(
|
|
38
|
+
"-I", "--subindex",
|
|
39
|
+
default=[],
|
|
40
|
+
action="append",
|
|
41
|
+
help="additional node name to process as a subindex, besides the"
|
|
42
|
+
" one named in the '@index' document command")
|
|
43
|
+
|
|
44
|
+
parser.add_argument(
|
|
45
|
+
"-p", "--index-term-prefix",
|
|
46
|
+
metavar="PREFIX",
|
|
47
|
+
default=[],
|
|
48
|
+
action="append",
|
|
49
|
+
help="leading strings to ignore when sorting and grouping index"
|
|
50
|
+
" terms - e.g. if set to '.' then '.term' as a term will be"
|
|
51
|
+
" sorted and grouped under 't'; note this can be a string and"
|
|
52
|
+
" not just a single character, and this option can be used"
|
|
53
|
+
" multiple times for multiple prefixes")
|
|
54
|
+
|
|
55
|
+
parser.add_argument(
|
|
56
|
+
"-c", "--no-check-consistency",
|
|
57
|
+
action="store_true",
|
|
58
|
+
help="don't check document level commands across the set for"
|
|
59
|
+
" consistency (commands checked are:"
|
|
60
|
+
f" { ', '.join(CONSISTENT_DOC_CMDS) }"
|
|
61
|
+
")")
|
|
62
|
+
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
"-w", "--no-warnings",
|
|
65
|
+
action="store_true",
|
|
66
|
+
help="suppress printing of warnings encountered during formatting"
|
|
67
|
+
" to stdandard error")
|
|
68
|
+
|
|
69
|
+
parser.add_argument(
|
|
70
|
+
"-n", "--nodes",
|
|
71
|
+
action="store_true",
|
|
72
|
+
help="print a list of all nodes in the set to standard output")
|
|
73
|
+
|
|
74
|
+
parser.add_argument(
|
|
75
|
+
"-t", "--text-file",
|
|
76
|
+
help="in addition to any other output, write a plain text"
|
|
77
|
+
" 'readable' version of the entire guide set to a single file"
|
|
78
|
+
" whose name is specified; this is the same as the output that"
|
|
79
|
+
" would be written to standard output, if the -o and -G"
|
|
80
|
+
" options were not used")
|
|
81
|
+
|
|
82
|
+
parser.add_argument(
|
|
83
|
+
"-G", "--guide",
|
|
84
|
+
action="store_true",
|
|
85
|
+
default=False,
|
|
86
|
+
help="when printing to standard output (-o is not specified), print"
|
|
87
|
+
" the output in guide (.gde) format, with filename separators"
|
|
88
|
+
" (default is to print a plain text, 'readable' version'")
|
|
89
|
+
|
|
90
|
+
parser.add_argument(
|
|
91
|
+
"-v", "--version",
|
|
92
|
+
action="version",
|
|
93
|
+
version=__version__)
|
|
94
|
+
|
|
95
|
+
parser.add_argument(
|
|
96
|
+
"file",
|
|
97
|
+
nargs='+',
|
|
98
|
+
help="filename of NextGuide document(s) to read (typically having"
|
|
99
|
+
"'.ugde' extensions)")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
args = parser.parse_args()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# --- process arguments ---
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# read in the specified list of NextGuide files
|
|
111
|
+
guide_set = GuideSet(args.file, subindex_names=args.subindex)
|
|
112
|
+
|
|
113
|
+
# unless suppressed, check the set for consistency
|
|
114
|
+
if not args.no_check_consistency:
|
|
115
|
+
guide_set.checkconsistency()
|
|
116
|
+
|
|
117
|
+
# use a factory function to make a function which will return the key
|
|
118
|
+
# for index term sorting and grouping
|
|
119
|
+
indextermkey_fn = indextermkey_factory(args.index_term_prefix)
|
|
120
|
+
|
|
121
|
+
# parse, recreate and replace the indices with formatted, set-wide ones
|
|
122
|
+
if args.index:
|
|
123
|
+
guide_set.makeindices(indextermkey=indextermkey_fn)
|
|
124
|
+
|
|
125
|
+
# if we're writing out formatted guide files, do that, otherwise just
|
|
126
|
+
# print the results to stdout (in readable format, if requested)
|
|
127
|
+
if args.output_dir:
|
|
128
|
+
guide_set.writefiles(args.output_dir)
|
|
129
|
+
else:
|
|
130
|
+
guide_set.print(markup=args.guide)
|
|
131
|
+
|
|
132
|
+
# if the option to write a readable, plain text version of the guide set
|
|
133
|
+
# to a file is used, do that
|
|
134
|
+
if args.text_file:
|
|
135
|
+
guide_set.writetextfile(args.text_file)
|
|
136
|
+
|
|
137
|
+
# if the 'list nodes' option is used, print the names of all nodes,
|
|
138
|
+
# alongside the document(s) in which they are defined
|
|
139
|
+
if args.nodes:
|
|
140
|
+
node_docs = guide_set.getnodedocs()
|
|
141
|
+
for node_name in sorted(node_docs):
|
|
142
|
+
print(f"{node_name:20}", ", ".join(sorted(node_docs[node_name])))
|
|
143
|
+
|
|
144
|
+
# if warnings haven't been disabled, print those to stderr
|
|
145
|
+
if not args.no_warnings:
|
|
146
|
+
warnings = guide_set.getwarnings()
|
|
147
|
+
for warning in warnings:
|
|
148
|
+
print(warning, file=sys.stderr)
|
nextguidefmt/doc.py
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
# nextguidefmt/doc.py
|
|
2
|
+
|
|
3
|
+
# Documents are NextGuide files containing one or more nodes.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
|
|
10
|
+
from .node import GuideNode, LINE_MAXLEN
|
|
11
|
+
|
|
12
|
+
from .token import (
|
|
13
|
+
IGNORE_RE,
|
|
14
|
+
NODE_LINK_CMDS_RE,
|
|
15
|
+
NODE_CMDS_RE,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# --- constants ---
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# DOCCMD_x = string
|
|
25
|
+
#
|
|
26
|
+
# Constants identifying document commands.
|
|
27
|
+
|
|
28
|
+
DOC_CMD_INDEX = "index"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# DOC_CMDS = list
|
|
33
|
+
#
|
|
34
|
+
# This defines the available commands at the document (file) level.
|
|
35
|
+
# They are used to construct a regular expression to match lines
|
|
36
|
+
# containing them.
|
|
37
|
+
#
|
|
38
|
+
# In addition, the order of this list determines the order in which they
|
|
39
|
+
# are included in the output document file.
|
|
40
|
+
|
|
41
|
+
DOC_CMDS = [
|
|
42
|
+
"title",
|
|
43
|
+
"author",
|
|
44
|
+
"copyright",
|
|
45
|
+
"version",
|
|
46
|
+
"date",
|
|
47
|
+
"build",
|
|
48
|
+
DOC_CMD_INDEX,
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# regular expression to match document-level commands
|
|
53
|
+
|
|
54
|
+
DOC_CMDS_RE = re.compile(
|
|
55
|
+
r"@(?P<cmd>" + '|'.join(DOC_CMDS) + r")( (?P<value>.+))?")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# maximum size of a document in bytes
|
|
59
|
+
|
|
60
|
+
DOC_MAXSIZE = 65_535
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# --- classes ---
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class GuideDoc(object):
|
|
69
|
+
"""Class representing a NextGuide document, which is a single file
|
|
70
|
+
containing one or more nodes (pages).
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def __init__(self, filename, *, subindex_names=set()):
|
|
75
|
+
"""Initialise a new document, reading in data from a file.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
super().__init__()
|
|
79
|
+
|
|
80
|
+
# --- set up initial variables ---
|
|
81
|
+
|
|
82
|
+
# store the name and subindex node names of this document
|
|
83
|
+
self._setname(filename)
|
|
84
|
+
|
|
85
|
+
# initialise the list of index node names to the subindex set;
|
|
86
|
+
# when we read the file in later, we'll also add on the node
|
|
87
|
+
# named in the '@index' command, if there is one
|
|
88
|
+
self._index_names = set(subindex_names)
|
|
89
|
+
|
|
90
|
+
# initialise document-level commands as empty
|
|
91
|
+
self._cmds = {}
|
|
92
|
+
|
|
93
|
+
# initialise the list of nodes as empty
|
|
94
|
+
self._nodes = []
|
|
95
|
+
|
|
96
|
+
# initialise an empty dictionary of indices - this will be
|
|
97
|
+
# populated by parseindices(), if called
|
|
98
|
+
self._indices = {}
|
|
99
|
+
|
|
100
|
+
# initialise a list of warnings encountered when processing this
|
|
101
|
+
# document
|
|
102
|
+
self._warnings = []
|
|
103
|
+
|
|
104
|
+
# --- read and process the document ---
|
|
105
|
+
|
|
106
|
+
# read in the document file
|
|
107
|
+
self.readfile(filename)
|
|
108
|
+
|
|
109
|
+
# check the links to make sure they're not broken - we do this
|
|
110
|
+
# here, before we set the default links, as we don't want to
|
|
111
|
+
# report on broken links we fill in automatically; only the ones
|
|
112
|
+
# supplied in the source file (to help the user find the initial
|
|
113
|
+
# location of the problem)
|
|
114
|
+
self.checklinks()
|
|
115
|
+
|
|
116
|
+
# set the default links between nodes and to the contents node
|
|
117
|
+
self.setdefaultlinks()
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _setname(self, name):
|
|
121
|
+
"""Sets the name of the guide. This will 'normalise' the
|
|
122
|
+
supplied name, removing '.gde' from the end, if it is present;
|
|
123
|
+
if not, the complete name will be stored.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
# get the leaf part of the name, split into root and extension
|
|
127
|
+
root, ext = os.path.splitext(os.path.basename(name))
|
|
128
|
+
|
|
129
|
+
# strip off the extension if it's '.gde' or '.ugde' (unformatted
|
|
130
|
+
# guide - a term invented for this tool); the NextGuide viewer
|
|
131
|
+
# add on '.gde' if the file is not found, with the plain name
|
|
132
|
+
if ext in [".gde", ".ugde"]:
|
|
133
|
+
self._name = root
|
|
134
|
+
else:
|
|
135
|
+
self._name = root + ext
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def getname(self):
|
|
139
|
+
"""Return the name of the guide. This can be used as the
|
|
140
|
+
document name when making links from other guide documents.
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
return self._name
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def getcmd(self, name):
|
|
147
|
+
"""Get the value of a document level command. If the command is
|
|
148
|
+
not specified, None will be returned.
|
|
149
|
+
"""
|
|
150
|
+
|
|
151
|
+
return self._cmds.get(name)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def getnodenames(self):
|
|
155
|
+
"""Return a list containing the names of all the nodes in the
|
|
156
|
+
document in order.
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
return [ node.name for node in self._nodes ]
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def getnode(self, name):
|
|
163
|
+
"""Return the node of the specified name. If the node doesn't
|
|
164
|
+
exist, None will be returned.
|
|
165
|
+
"""
|
|
166
|
+
|
|
167
|
+
for node in self._nodes:
|
|
168
|
+
if node.name == name:
|
|
169
|
+
return node
|
|
170
|
+
|
|
171
|
+
return None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def getindices(self):
|
|
175
|
+
"""Get the list of index names.
|
|
176
|
+
"""
|
|
177
|
+
|
|
178
|
+
return list(self._indices)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def getindex(self, name):
|
|
182
|
+
"""Get the index under the specified name.
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
return self._indices[name]
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def addwarning(self, warning):
|
|
189
|
+
"""Add a warning to the list of warnings about this document.
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
self._warnings.append(warning)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def getwarnings(self):
|
|
196
|
+
"""Returns the list of warnings encountered when building the
|
|
197
|
+
document. This will include document-level warnings, as well as
|
|
198
|
+
warnings from all nodes in the document.
|
|
199
|
+
|
|
200
|
+
If there are no warnings, an empty list will be returned.
|
|
201
|
+
"""
|
|
202
|
+
|
|
203
|
+
# copy our list of document-level warnings as we want to extend
|
|
204
|
+
# and then return it, but not affect the original list
|
|
205
|
+
warnings = self._warnings.copy()
|
|
206
|
+
|
|
207
|
+
# add warnings from the index, prefixed with 'index:'
|
|
208
|
+
for index in self._indices:
|
|
209
|
+
warnings.extend(
|
|
210
|
+
[ f"index: {warning}"
|
|
211
|
+
for warning in self._indices[index].getwarnings() ])
|
|
212
|
+
|
|
213
|
+
# extend the copied list with the warnings from each node in the
|
|
214
|
+
# document, prefixed by 'node: @name'
|
|
215
|
+
for node in self._nodes:
|
|
216
|
+
warnings.extend([ f"node: @{node.name} {warning}"
|
|
217
|
+
for warning in node.getwarnings() ])
|
|
218
|
+
|
|
219
|
+
# return the composite list of warnings
|
|
220
|
+
return warnings
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def readfile(self, filename):
|
|
224
|
+
"""Read a source NextGuide file, parsing document-level comamnds
|
|
225
|
+
and nodes and storing it as this document.
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
# start with no current node
|
|
229
|
+
current_node = None
|
|
230
|
+
|
|
231
|
+
# open the source file and work through the lines in it
|
|
232
|
+
with open(filename) as f:
|
|
233
|
+
for l in f:
|
|
234
|
+
# strip any trailing whitespace as we never want that
|
|
235
|
+
l = l.rstrip()
|
|
236
|
+
|
|
237
|
+
# skip lines we want to ignore
|
|
238
|
+
if IGNORE_RE.match(l):
|
|
239
|
+
continue
|
|
240
|
+
|
|
241
|
+
# match document-level commands
|
|
242
|
+
m = DOC_CMDS_RE.match(l)
|
|
243
|
+
if m:
|
|
244
|
+
if current_node:
|
|
245
|
+
# we got a document-level command but are in a
|
|
246
|
+
# node - record a warning and ignore it
|
|
247
|
+
current_node.addwarning(
|
|
248
|
+
f"document token: '{l}' in node - ignored")
|
|
249
|
+
|
|
250
|
+
else:
|
|
251
|
+
# we're not in a node, record the command in the
|
|
252
|
+
# document
|
|
253
|
+
self._cmds[m.group("cmd")] = m.group("value")
|
|
254
|
+
|
|
255
|
+
# skip to the next line in the file
|
|
256
|
+
continue
|
|
257
|
+
|
|
258
|
+
# try to match the @node command at the start of a new node
|
|
259
|
+
m = NODE_CMDS_RE.match(l)
|
|
260
|
+
if m:
|
|
261
|
+
# if we've got a node we're building, we're done
|
|
262
|
+
# with that, so append it to the list of nodes in
|
|
263
|
+
# this document
|
|
264
|
+
if current_node:
|
|
265
|
+
self._nodes.append(current_node)
|
|
266
|
+
|
|
267
|
+
# start a new node
|
|
268
|
+
current_node = GuideNode(m.group("name"))
|
|
269
|
+
|
|
270
|
+
# skip to the next line in the file
|
|
271
|
+
continue
|
|
272
|
+
|
|
273
|
+
# try to match node-level commands linking to another node
|
|
274
|
+
m = NODE_LINK_CMDS_RE.match(l)
|
|
275
|
+
if m:
|
|
276
|
+
# store the link and skip to the next line in the file
|
|
277
|
+
current_node.setlink(*m.group("link", "name"))
|
|
278
|
+
continue
|
|
279
|
+
|
|
280
|
+
# anything else is a line of markup data in the node ...
|
|
281
|
+
|
|
282
|
+
# we haven't started a node yet so we can't store this
|
|
283
|
+
# line - add a warning and skip the line
|
|
284
|
+
if not current_node:
|
|
285
|
+
self.addwarning(f"node data: '{l} outside node - ignoring")
|
|
286
|
+
continue
|
|
287
|
+
|
|
288
|
+
# add this line to the current node
|
|
289
|
+
current_node.appendline(l)
|
|
290
|
+
|
|
291
|
+
# we're finished with the file - if we have a node we're
|
|
292
|
+
# assembling, that's complete, so append that to list of nodes
|
|
293
|
+
# in this document
|
|
294
|
+
if current_node:
|
|
295
|
+
self._nodes.append(current_node)
|
|
296
|
+
|
|
297
|
+
# if the document had an index named in the '@index' document
|
|
298
|
+
# command, add that to the list of index node names
|
|
299
|
+
doc_index_name = self._cmds.get(DOC_CMD_INDEX)
|
|
300
|
+
if doc_index_name:
|
|
301
|
+
self._index_names.add(doc_index_name)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def setdefaultlinks(self):
|
|
305
|
+
"""Complete any missing inter-node links using some assumed
|
|
306
|
+
defaults:
|
|
307
|
+
|
|
308
|
+
prev = the previous node in the document
|
|
309
|
+
|
|
310
|
+
next = the next node in the document
|
|
311
|
+
|
|
312
|
+
toc = the most recently-defined 'toc' entry
|
|
313
|
+
"""
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
# work through the nodes in order, filling in missing 'prev' and
|
|
317
|
+
# 'toc' links from the previous node
|
|
318
|
+
|
|
319
|
+
prev_node = None
|
|
320
|
+
toc_node = None
|
|
321
|
+
|
|
322
|
+
for node in self._nodes:
|
|
323
|
+
# set missing links for this node
|
|
324
|
+
node.setdefaultlink("prev", prev_node)
|
|
325
|
+
node.setdefaultlink("toc", toc_node)
|
|
326
|
+
|
|
327
|
+
# the default previous link for the next one is this node
|
|
328
|
+
prev_node = node.name
|
|
329
|
+
|
|
330
|
+
# the default toc link is the one we used for this node
|
|
331
|
+
# (which may have been explicitly specified, or set the same
|
|
332
|
+
# as the previous node)
|
|
333
|
+
toc_node = node.getlink("toc")
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
# work through the nodes in REVERSE order, filling in the 'next'
|
|
337
|
+
# link from the next node
|
|
338
|
+
|
|
339
|
+
next_node = None
|
|
340
|
+
|
|
341
|
+
for node in reversed(self._nodes):
|
|
342
|
+
node.setdefaultlink("next", next_node)
|
|
343
|
+
next_node = node.name
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def checklinks(self):
|
|
347
|
+
"""Check links in the document and generate warnings if any are
|
|
348
|
+
broken (to nodes which do not exist).
|
|
349
|
+
"""
|
|
350
|
+
|
|
351
|
+
# get the list of node names as we're going to use it several
|
|
352
|
+
# times
|
|
353
|
+
node_names = self.getnodenames()
|
|
354
|
+
|
|
355
|
+
# record a warning if the index node is defined but does not exist
|
|
356
|
+
index_name = self._cmds.get(DOC_CMD_INDEX)
|
|
357
|
+
if index_name and (index_name not in node_names):
|
|
358
|
+
self.addwarning(f"index node: @{index_name} does not exist")
|
|
359
|
+
|
|
360
|
+
# check node-level links for all nodes in the document
|
|
361
|
+
for node in self._nodes:
|
|
362
|
+
node.checklinks(node_names)
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def parseindices(self):
|
|
366
|
+
"""Parse the index nodes of the document, if it is defined and
|
|
367
|
+
exists, into a GuideIndex. The index is stored in .index and
|
|
368
|
+
True returned; if the node was not defined, or defined but not
|
|
369
|
+
present, None is returned.
|
|
370
|
+
"""
|
|
371
|
+
|
|
372
|
+
# --- make the list indices for this document ---
|
|
373
|
+
|
|
374
|
+
# initialise the list of index names to empty
|
|
375
|
+
index_names = []
|
|
376
|
+
|
|
377
|
+
# if the document has an '@index' then add that as the first index
|
|
378
|
+
index_name = self.getcmd(DOC_CMD_INDEX)
|
|
379
|
+
if index_name:
|
|
380
|
+
index_names.append(index_name)
|
|
381
|
+
|
|
382
|
+
# add any additional subindex names supplied to this function
|
|
383
|
+
index_names.extend(i for i in self._index_names if i != index_name)
|
|
384
|
+
|
|
385
|
+
# --- process the indices ---
|
|
386
|
+
|
|
387
|
+
# initialise the index as empty
|
|
388
|
+
self._indices = {}
|
|
389
|
+
|
|
390
|
+
# go through the list of indices we built above
|
|
391
|
+
for index_name in index_names:
|
|
392
|
+
# try to get the node with the same name as the index
|
|
393
|
+
index_node = self.getnode(index_name)
|
|
394
|
+
|
|
395
|
+
# if it exists - parse the node as an index and store the
|
|
396
|
+
# returned GuideIndex in the document
|
|
397
|
+
if index_node:
|
|
398
|
+
self._indices[index_name] = index_node.parseindex()
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def format(self, *, node_docs={}, line_maxlen=LINE_MAXLEN, markup=True,
|
|
402
|
+
skip_index=False):
|
|
403
|
+
|
|
404
|
+
"""Format the document for output, with the document commands
|
|
405
|
+
first, then the nodes, handling word wrap for the specified
|
|
406
|
+
specified maximum line length, and qualifying links with
|
|
407
|
+
document names, if required.
|
|
408
|
+
|
|
409
|
+
The output is returned as a list of lines as strings.
|
|
410
|
+
|
|
411
|
+
Keyword arguments:
|
|
412
|
+
|
|
413
|
+
line_maxlen -- the maximum line length; lines longer than this
|
|
414
|
+
will be word-wrapped (unless matching the 'literal' format).
|
|
415
|
+
|
|
416
|
+
markup -- if not set, a readable, plain text version without
|
|
417
|
+
markup, will be generated.
|
|
418
|
+
|
|
419
|
+
skip_index -- will omit index nodes in the output.
|
|
420
|
+
"""
|
|
421
|
+
|
|
422
|
+
# initialise the output as an empty list of lines
|
|
423
|
+
output = []
|
|
424
|
+
|
|
425
|
+
# go through the document commands and record them in the
|
|
426
|
+
# output, if they are present and not the empty string or None
|
|
427
|
+
if markup:
|
|
428
|
+
for cmd in DOC_CMDS:
|
|
429
|
+
if cmd in self._cmds:
|
|
430
|
+
output.append(f"@{cmd} {self._cmds[cmd]}")
|
|
431
|
+
|
|
432
|
+
# go through the nodes in the document in order
|
|
433
|
+
for node in self._nodes:
|
|
434
|
+
# if the name of this node is in the list of indices for
|
|
435
|
+
# this document, and we're skipping those, ignore this node
|
|
436
|
+
if skip_index and (node.name in self.getindices()):
|
|
437
|
+
continue
|
|
438
|
+
|
|
439
|
+
# add a line of dashes before this node as a separator
|
|
440
|
+
if markup:
|
|
441
|
+
output.append('@' + ('-' * (line_maxlen - 1)))
|
|
442
|
+
else:
|
|
443
|
+
node_banner = "---[ " + node.name + " ]"
|
|
444
|
+
node_banner += '-' * (line_maxlen - len(node_banner))
|
|
445
|
+
|
|
446
|
+
output.extend(['', node_banner, ''])
|
|
447
|
+
|
|
448
|
+
# format this node and add the lines to the output
|
|
449
|
+
output.extend(node.format(doc=self, node_docs=node_docs,
|
|
450
|
+
line_maxlen=LINE_MAXLEN, markup=markup))
|
|
451
|
+
|
|
452
|
+
# return the list of formatted lines
|
|
453
|
+
return output
|