Skip to content

Commit

Permalink
feat: Add option to update datatracker references
Browse files Browse the repository at this point in the history
With new commandline option `--use-bib`, xinclude with
datatracker.ietf.org are replaced with bib.ietf.org.

Fixes ietf-tools#1167
  • Loading branch information
kesara committed Sep 26, 2024
1 parent b606ad9 commit 6cb5462
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
4 changes: 2 additions & 2 deletions xml2rfc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from xml2rfc.writers import ( BaseRfcWriter, RawTextRfcWriter, PaginatedTextRfcWriter,
HtmlRfcWriter, NroffRfcWriter, ExpandedXmlWriter, RfcWriterError,
V2v3XmlWriter, PrepToolWriter, TextWriter, HtmlWriter, PdfWriter,
ExpandV3XmlWriter, UnPrepWriter, DocWriter
ExpandV3XmlWriter, UnPrepWriter, DocWriter, DatatrackerToBibConverter,
)

# This defines what 'from xml2rfc import *' actually imports:
Expand All @@ -25,7 +25,7 @@
'HtmlRfcWriter', 'NroffRfcWriter', 'ExpandedXmlWriter',
'RfcWriterError', 'V2v3XmlWriter', 'PrepToolWriter', 'TextWriter',
'HtmlWriter', 'PdfWriter', 'ExpandV3XmlWriter', 'UnPrepWriter',
'DocWriter',
'DocWriter', 'DatatrackerToBibConverter',
]

try:
Expand Down
13 changes: 12 additions & 1 deletion xml2rfc/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ def main():
help='outputs formatted text to file, unpaginated (only v2 input)')
formatgroup.add_argument('--expand', action='store_true',
help='outputs XML to file with all references expanded')
formatgroup.add_argument('--use-bib', action='store_true',
help='update all datatracker references with bib.ietf.org')
formatgroup.add_argument('--v2v3', action='store_true',
help='convert vocabulary version 2 XML to version 3')
formatgroup.add_argument('--preptool', action='store_true',
Expand Down Expand Up @@ -456,7 +458,7 @@ def main():
options.output_path = options.basename
options.basename = None
#
num_formats = len([ o for o in [options.raw, options.text, options.nroff, options.html, options.expand, options.v2v3, options.preptool, options.info, options.pdf, options.unprep ] if o])
num_formats = len([ o for o in [options.raw, options.text, options.nroff, options.html, options.expand, options.use_bib, options.v2v3, options.preptool, options.info, options.pdf, options.unprep ] if o])
if num_formats > 1 and (options.filename or options.output_filename):
sys.exit('Cannot use an explicit output filename when generating more than one format, '
'use --path instead.')
Expand Down Expand Up @@ -662,6 +664,15 @@ def main():
options.output_filename = None

# --- End of legacy formatter invocations ---
if options.use_bib:
xmlrfc = parser.parse(remove_comments=False, quiet=True, normalize=False, strip_cdata=False, add_xmlns=True)
filename = options.output_filename
if not filename:
filename = basename + '.bib.xml'
options.output_filename = filename
expander = xml2rfc.DatatrackerToBibConverter(xmlrfc, options=options, date=options.date)
expander.write(filename)
options.output_filename = None

if options.expand and not options.legacy:
xmlrfc = parser.parse(remove_comments=False, quiet=True, normalize=False, strip_cdata=False, add_xmlns=True)
Expand Down
3 changes: 2 additions & 1 deletion xml2rfc/writers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
from xml2rfc.writers.pdf import PdfWriter
from xml2rfc.writers.unprep import UnPrepWriter
from xml2rfc.writers.doc import DocWriter
from xml2rfc.writers.bib import DatatrackerToBibConverter

# This defines what 'from xml2rfc.writers import *' actually imports:
__all__ = ['BaseRfcWriter', 'RawTextRfcWriter', 'PaginatedTextRfcWriter',
'HtmlRfcWriter', 'NroffRfcWriter', 'ExpandedXmlWriter',
'RfcWriterError', 'V2v3XmlWriter', 'PrepToolWriter', 'TextWriter',
'HtmlWriter', 'PdfWriter', 'ExpandV3XmlWriter', 'UnPrepWriter',
'DocWriter',
'DocWriter', 'DatatrackerToBibConverter',
]
1 change: 1 addition & 0 deletions xml2rfc/writers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
'template_dir': os.path.join(os.path.dirname(os.path.dirname(__file__)), 'templates'),
'text': True,
'unprep': False,
'use_bib': False,
'utf8': False,
'values': False,
'verbose': False,
Expand Down
43 changes: 43 additions & 0 deletions xml2rfc/writers/bib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright The IETF Trust 2024, All Rights Reserved
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, division

from io import open
from lxml import etree
from urllib.parse import urlparse

from xml2rfc.writers.preptool import PrepToolWriter


class DatatrackerToBibConverter(PrepToolWriter):
"""Writes a duplicate XML file but with datratracker references replaced with bib.ietf.org"""

def write(self, filename):
"""Public method to write the XML document to a file"""
self.convert()
with open(filename, "w", encoding="utf-8") as file:
text = etree.tostring(self.tree, encoding="unicode")
file.write("<?xml version='1.0' encoding='utf-8'?>\n")
file.write(text)
if not self.options.quiet:
self.log(" Created file %s" % filename)

def convert(self):
version = self.root.get("version", "3")
if version not in [
"3",
]:
self.die(self.root, 'Expected <rfc> version="3", but found "%s"' % version)
self.convert_xincludes()

def convert_xincludes(self):
ns = {"xi": b"http://www.w3.org/2001/XInclude"}
xincludes = self.root.xpath("//xi:include", namespaces=ns)
for xinclude in xincludes:
href = urlparse(xinclude.get("href"))

if href.netloc == "datatracker.ietf.org":
reference_file = href.path.split("/")[-1]
xinclude.set(
"href", f"https://bib.ietf.org/public/rfc/bibxml-ids/{reference_file}"
)

0 comments on commit 6cb5462

Please sign in to comment.