Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor attributes and improve argument parsing #8

Merged
merged 6 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# doxygentoasciidoc: A Doxygen > Asciidoc Converter
# doxygentoasciidoc: A Doxygen to AsciiDoc Converter

This project converts doxygen XML output to asciidoc.

Allowed args:

`-f`: the full path to the file to be converted
```
usage: doxygentoasciidoc [-h] [-o OUTPUT] [-c] file

`-o`: the full path the the output file (will print to STDOUT if not specified)
Convert Doxygen XML to AsciiDoc

`-c`: process a node other than `doxygenindex`
positional arguments:
file The path of the Doxygen XML file to convert

The following attributes from the XML will be preserved in the generated asciidoc: role, tag, type.
optional arguments:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
Write to file instead of stdout
-c, --child Is NOT the root index file
```

## Development

Expand Down
49 changes: 25 additions & 24 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys
import argparse

from bs4 import BeautifulSoup
Expand All @@ -8,40 +7,42 @@

def main():
"""Convert the given Doxygen index.xml to AsciiDoc and output the result."""
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(
prog="doxygentoasciidoc", description="Convert Doxygen XML to AsciiDoc"
)
parser.add_argument(
"-f", "--file", help="The path of the file to convert", default=None
"file",
type=argparse.FileType("r", encoding="utf-8"),
help="The path of the Doxygen XML file to convert",
)
parser.add_argument(
"-o", "--output", help="The path of the output file", default=None
"-o",
"--output",
help="Write to file instead of stdout",
)
parser.add_argument(
"-c",
"--child",
help="Is NOT the root index file",
default=False,
action="store_true",
)

args = parser.parse_args()
filename = args.file
output_filename = args.output
is_child = args.child
if filename:
xmldir = os.path.dirname(filename)
with open(filename, encoding="utf-8") as xml:
if is_child:
result = Node(
BeautifulSoup(xml, "xml").doxygen, xmldir=xmldir
).to_asciidoc(depth=1)
else:
result = DoxygenindexNode(
BeautifulSoup(xml, "xml").doxygenindex, xmldir=xmldir
).to_asciidoc(depth=2)
if output_filename is not None:
with open(output_filename, "w", encoding="utf-8") as output:

with args.file as file:
xmldir = os.path.dirname(file.name)

if args.child:
result = Node(
BeautifulSoup(file, "xml").doxygen, xmldir=xmldir
).to_asciidoc(depth=1)
else:
result = DoxygenindexNode(
BeautifulSoup(file, "xml").doxygenindex, xmldir=xmldir
).to_asciidoc(depth=2)

if args.output:
with open(args.output, "w", encoding="utf-8") as output:
output.write(result)
else:
print(result)

else:
sys.exit(1)
52 changes: 37 additions & 15 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,41 @@ def sanitize(identifier):


def title(text, level, attributes=None):
"""Return text formatted as a title with the given level."""
"""Return text formatted as a title with the given level and attributes."""
if attributes is None:
attributes = {}

attrlist = []

if "id" in attributes:
attrlist.append(f"#{sanitize(attributes.pop('id'))}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, I was about to suggest that it might be worth making attrlist a dict instead of a list, but then I spotted that this first item isn't of the form "xxx=yyy".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can break the dependency on matching the role=h6 contextspecific substring, we can adopt a more concise syntax here and use the AsciiDoc shorthand:

[#group_foo.h6.contextspecific,tag=PICO_2040,type=PICO_2040]


roles = []

if level > 5:
if attributes is not None:
if re.search(r"([,\s]role=)", attributes) is not None:
attributes = re.sub(
r"([,\s]role=)(.*?[,\s]?$)", "\\1h6 \\2", attributes
)
else:
attributes += ",role=h6"
return f"[{attributes}]\n*{escape_text(text)}*"
return f"[.h6]\n*{escape_text(text)}*"

marker = "=" * (level + 1)
if attributes is not None:
return f"[{attributes}]\n{marker} {escape_text(text)}"
return f"{marker} {escape_text(text)}"
roles.append("h6")
if "role" in attributes:
roles.append(attributes.pop("role"))

if roles:
attrlist.append(f"role={' '.join(roles)}")

if "tag" in attributes:
attrlist.append(f"tag={escape_text(attributes.pop('tag'))}")
if "type" in attributes:
attrlist.append(f"type={escape_text(attributes.pop('type'))}")
for key, value in attributes.items():
attrlist.append(f'{escape_text(key)}="{escape_text(value)}"')

output = []

if attrlist:
output.append(f"[{','.join(attrlist)}]")

if level > 5:
output.append(f"*{escape_text(text)}*")
else:
marker = "=" * (level + 1)
output.append(f"{marker} {escape_text(text)}")

return "\n".join(output)
Loading