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

Conversation

mudge
Copy link
Contributor

@mudge mudge commented Aug 21, 2024

  • Remove redundant stripping of whitespace
  • Expand usage of argparse to handle CLI options
  • Refactor attribute handling

Comparing the resulting AsciiDoc with and without this extra stripping
of trailing whitespace on the current Raspberry Pi Pico C/C++ SDK
databook results in no change in output.
@mudge mudge force-pushed the refactoring branch 2 times, most recently from f048b6f to 372d99b Compare August 21, 2024 11:30
cli.py Outdated Show resolved Hide resolved
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]

helpers.py Outdated Show resolved Hide resolved
nodes.py Outdated Show resolved Hide resolved
@lurch
Copy link
Contributor

lurch commented Aug 21, 2024

👍 to keeping things as dicts/lists for as long as possible, rather than prematurely converting to strings.

mudge added 4 commits August 21, 2024 14:00
Rather than using optional string arguments for both input and output files,
use more of Python's argparse library to handle a single, mandatory,
positional file argument (which now returns a handle to read the file).

This is a breaking change as the "-f" argument is now a positional one
so any downstream usage will need to be updated.

Note we don't use `argparse.FileType("w")` for the output file as that
would cause the given file to be created or truncated as soon as the
program runs when we only want it to produce output if successful.
Rather than passing around an AsciiDoc string of attributes, pass around
a dictionary that is only converted to AsciiDoc at the very end when
passed to the `title` helper.

Note I considered switching to using the role shorthand for multiple
roles (e.g. `.h6.contextspecific` rather than `role=h6 contextspecific`)
but there is downstream code that relies on matching the existing
format. Similarly, the order of the `tag` and `type` attributes is
currently relied upon by client code so these have to be special cased
for now.

This also removes the dedicated Title Node class in favour of handling
title rendering in the parent element (in this case, the Sect Node).
Rather than joining tuples on the empty string, use f-strings to build
up simple strings with interpolated expressions.
@mudge mudge requested a review from lurch August 21, 2024 13:19
nodes.py Outdated


class SimplesectNode(Node):
def to_asciidoc(self, **kwargs):
previous_node = self.previous_node()
next_node = self.next_node()
if self.node.get("kind", "") == "see":
if self.node.get("kind") == "see":
Copy link
Contributor

@lurch lurch Aug 21, 2024

Choose a reason for hiding this comment

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

Looks like lines 665 and 668 could be converted from if to elif ? And given that they're all doing self.node.get("kind") perhaps its worth doing kind = self.node.get("kind") above line 648?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As there is an early return on line 666 😈, pylint will complain if you switch to an elif:

Unnecessary "elif" after "return", remove the leading "el" from "elif"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Memoized self.node.get("kind") in 06370ed

Copy link
Contributor

@lurch lurch left a comment

Choose a reason for hiding this comment

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

One small nitpick but otherwise LGTM!

Rather than repeatedly loading the kind from the node's attributes,
fetch it once and then switch on the resulting value multiple times.
@mudge mudge merged commit 09e14b6 into main Aug 21, 2024
1 check passed
@mudge mudge deleted the refactoring branch August 21, 2024 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants