Coding 2024-02-22
After yesterday's entry, I remembered that I want to handle Leo directives (by ignoring them) and reStructuredText bibliographic fields (which I'm not overly concerned about getting right, by my standards, but I still need to do stuff). I'm right now trying to figure out the right way to handle bibliographic fields. I could try to hack together a small portion of a reStructuredText parser to winnow them out, or I could attempt to use docutils to just get it parsed out for free. I'm currently not sure which option would be more of a pain, and whether there's some better way.
Let's take a look at the latter option, since it's easier to tell what that entails.
- parser = docutils.parsers.rst.Parser()
- Store the input in a variable.
- document = docutils.utils.new_document("programmatic") (not exactly right, but let's see how this trips me up first)
- parser.parse(input, document)
Where the input looks something like
:key: value
Hold on, does something *weird* happen if I have an indented field list inside a code block?
And my hope is that I can extract a mapping of bibliographic fields, and get back the literal text of the rest of it. I do not know if that's a supported usage, so this might be a non-starter. (If it's not, do I have to roundtrip it through xml2rst or something?)
...
AttributeError: 'Values' object has no attribute 'pep_references'
Clearly, I was a fool to trust the builtin documentation.
Okay, maybe the mistake was specifically trusting the default behavior of the settings argument. Okay, that was it, had to get the proper default parser from the frontend module. Now, I have more information than I know what to do with.
After playing around a bit, I see that there are field_list objects, which contain the desired mapping information, several nodes deep. It is possible, then, to pull out those nodes and get the mapping, and somehow convert what remains back to reStructuredText, but...
It's truly impressive to me how this part of the Python ecosystem has managed to convince me that slopping together a rinky-dink little parser would be less trouble than wiring together existing libraries.
...
It is now rather later than I would like, but I have tossed something together that looks plausible. I'll have to try it out later, but it should be at least close. And now, I rest.
Good night.