Skip to content

Commit 8078c1b

Browse files
added configuration option to enable use of xml:id in title
1 parent 411a324 commit 8078c1b

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ extensions = [
6161
There are two configurable parameters for `conf.py` that correspond to
6262
`rst2db.py` parameters:
6363

64-
| Name | Description |
65-
|------|-------------|
66-
| *docbook_template_file* | template file that will be used to position the document parts. This should be a valid DocBook .xml file that contains Requires Jinja2 to be installed if specified. |
67-
| *docbook_default_root_element* | default root element for a file-level document. Default is 'section'. |
64+
| Name | Description | Default |
65+
|------|-------------|---------|
66+
| *docbook_template_file* | Template file that will be used to position the document parts. This should be a valid DocBook .xml file that contains Requires Jinja2 to be installed if specified. | "`section`" |
67+
| *docbook_default_root_element* | Default root element for a file-level document. Default is 'section'. | `None` |
68+
| *docbook_use_xml_id_in_titles* | Control to enable the use of `xml:id=...` attribute in `title` XML tags. | `False` |
6869

6970
For example:
7071

sphinx_docbook/docbook_builder.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ def process_with_template(self, contents):
3535
)
3636
sys.exit(1)
3737

38-
full_template_path = os.path.join(sphinx_app.env.srcdir,
39-
sphinx_app.config.docbook_template_file)
38+
full_template_path = os.path.join(
39+
sphinx_app.env.srcdir,
40+
sphinx_app.config.docbook_template_file
41+
)
4042

4143
if not os.path.exists(full_template_path):
4244
sys.stderr.write(
@@ -53,8 +55,8 @@ def process_with_template(self, contents):
5355
trim_blocks=True)
5456

5557
try:
56-
t = jinja2env.get_template(self.template_filename)
57-
t.render(data=data)
58+
template = jinja2env.get_template(self.template_filename)
59+
template.render(data=data)
5860
#pylint: disable=bare-except
5961
except:
6062
sys.stderr.write(
@@ -64,7 +66,7 @@ def process_with_template(self, contents):
6466
sys.exit(1)
6567
#pylint: enable=bare-except
6668

67-
return t.render(data=data)
69+
return template.render(data=data)
6870

6971

7072
def get_target_uri(self, docname, typ=None):
@@ -84,7 +86,8 @@ def write_doc(self, docname, doctree):
8486
docutils_writer = DocBookWriter(
8587
root_element=self.root_element,
8688
document_id=docname,
87-
output_xml_header=(self.template_filename == None)
89+
output_xml_header=(self.template_filename == None),
90+
use_xml_id_in_titles=sphinx_app.config.docbook_use_xml_id_in_titles,
8891
)
8992

9093
# get the docbook output.
@@ -109,4 +112,5 @@ def setup(app):
109112
# pylint: enable=global-variable-undefined, invalid-name
110113
app.add_config_value('docbook_default_root_element', 'section', 'env')
111114
app.add_config_value('docbook_template_file', None, 'env')
115+
app.add_config_value('docbook_use_xml_id_in_titles', False, 'env')
112116
app.add_builder(DocBookBuilder)

sphinx_docbook/docbook_writer.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def _print_error(text, node = None):
2222
if node:
2323
sys.stderr.write(f" {node}\n")
2424

25+
_NAMESPACE_ID = '{http://www.w3.org/XML/1998/namespace}id'
2526

2627
class DocBookWriter(writers.Writer):
2728
"""
@@ -38,21 +39,29 @@ class DocBookWriter(writers.Writer):
3839
"""
3940
# pylint: disable=attribute-defined-outside-init
4041

41-
def __init__(self, root_element, document_id=None, output_xml_header=True):
42+
def __init__(
43+
self,
44+
root_element: str,
45+
document_id: str = None,
46+
output_xml_header: bool = True,
47+
use_xml_id_in_titles: bool = False,
48+
):
4249
"""Initialize the writer. Takes the root element of the resulting
4350
DocBook output as its sole argument."""
4451
writers.Writer.__init__(self)
4552
self.document_type = root_element
4653
self.document_id = document_id
4754
self.output_xml_header = output_xml_header
55+
self.use_xml_id_in_titles = use_xml_id_in_titles
4856

4957
def translate(self):
5058
"""Call the translator to translate the document."""
5159
self.visitor = DocBookTranslator(
5260
self.document,
5361
self.document_type,
5462
self.document_id,
55-
self.output_xml_header
63+
self.output_xml_header,
64+
self.use_xml_id_in_titles,
5665
)
5766
self.document.walkabout(self.visitor)
5867
self.output = self.visitor.astext()
@@ -64,8 +73,14 @@ class DocBookTranslator(nodes.NodeVisitor):
6473
# pylint: disable=missing-function-docstring, unnecessary-pass
6574
# pylint: disable=unused-argument
6675

67-
def __init__(self, document, document_type, document_id = None,
68-
output_xml_header=True):
76+
def __init__(
77+
self,
78+
document,
79+
document_type: str,
80+
document_id: str = None,
81+
output_xml_header: bool = True,
82+
use_xml_id_in_titles: bool = False,
83+
):
6984
"""Initialize the translator. Takes the root element of the resulting
7085
DocBook output as its sole argument."""
7186
nodes.NodeVisitor.__init__(self, document)
@@ -75,6 +90,7 @@ def __init__(self, document, document_type, document_id = None,
7590
self.document_id = document_id
7691
self.in_first_section = False
7792
self.output_xml_header = output_xml_header
93+
self.use_xml_id_in_titles = use_xml_id_in_titles
7894

7995
self.in_pre_block = False
8096
self.in_figure = False
@@ -541,16 +557,15 @@ def depart_subtitle(self, node):
541557

542558
def visit_title(self, node):
543559
attribs = {}
544-
# first check to see if an {http://www.w3.org/XML/1998/namespace}id was
545-
# supplied.
546-
if len(node['ids']) > 0:
547-
attribs['{http://www.w3.org/XML/1998/namespace}id'] = node['ids'][0]
548-
elif len(node.parent['ids']) > 0:
549-
# If the parent node has an ID, we can use that and add '.title' at
550-
# the end to make a deterministic title ID.
551-
attribs[
552-
'{http://www.w3.org/XML/1998/namespace}id'
553-
] = f"{node.parent['ids'][0]}.title"
560+
if self.use_xml_id_in_titles:
561+
# first check to see if an {http://www.w3.org/XML/1998/namespace}id
562+
# was supplied.
563+
if len(node['ids']) > 0:
564+
attribs[_NAMESPACE_ID] = node['ids'][0]
565+
elif len(node.parent['ids']) > 0:
566+
# If the parent node has an ID, we can use that and add
567+
# '.title' at the end to make a deterministic title ID.
568+
attribs[_NAMESPACE_ID] = f"{node.parent['ids'][0]}.title"
554569
self._push_element('title', attribs)
555570

556571

0 commit comments

Comments
 (0)