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

✨ NEW: Add Docutils MyST config and CLI #426

Merged
merged 17 commits into from
Dec 11, 2021

Conversation

cpitclaudel
Copy link
Contributor

This is a follow-up to my previous PR (GH-418), part of an effort (GH-420) to make myst_parser integrate better with Docutils.
This exposes (almost) all MyST configuration settings in docutils.conf, making it possible to configure myst_parser for use without Sphinx.

I've added a simple entry point to make it easier to experiment (the second commit on this branch). Here's a quick demo:

# We have access to the standard docutils command line interface:
$ python3 -m myst_parser.docutils_ -h
...
Usage
=====
  docutils_.py [options] [<source> [<destination>]]

Generates (X)HTML documents from standalone MyST sources.  Reads from <source>
(default is stdin) and writes to <destination> (default is stdout).  See
<http://docutils.sf.net/docs/user/config.html> for the full reference.

Options
=======
General Docutils Options
------------------------
...

MyST options
------------
--myst-commonmark-only=MYST_COMMONMARK_ONLY
--myst-enable-extensions=MYST_ENABLE_EXTENSIONS
--myst-dmath-allow-labels=MYST_DMATH_ALLOW_LABELS
--myst-dmath-allow-space=MYST_DMATH_ALLOW_SPACE
--myst-dmath-allow-digits=MYST_DMATH_ALLOW_DIGITS
--myst-dmath-double-inline=MYST_DMATH_DOUBLE_INLINE
--myst-update-mathjax=MYST_UPDATE_MATHJAX
--myst-mathjax-classes=MYST_MATHJAX_CLASSES
--myst-disable-syntax=MYST_DISABLE_SYNTAX
--myst-url-schemes=MYST_URL_SCHEMES
--myst-heading-anchors=MYST_HEADING_ANCHORS
--myst-footnote-transition=MYST_FOOTNOTE_TRANSITION
--myst-sub-delimiters=MYST_SUB_DELIMITERS
--myst-words-per-minute=MYST_WORDS_PER_MINUTE
...

# We can compile markdown documents directly:
$ printf "# Title\n:::{note}\nIt works!\n:::" > test.md
$ python3 -m myst_parser.docutils_ --myst-enable-extensions=colon_fence test.md 
...
<body>
<div class="tex2jax_ignore mathjax_ignore document" id="title">
<h1 class="title">Title</h1>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">It works!</p>
</div>
</div>
</body>
</html>

Remaining issues:

  • Math doesn't work, because of [draft] Remove option mathjax_classes #406. I think it would be reasonable to include the fix from that PR (add a transform that adds mathjax_process where relevant), but I'm happy to consider alternatives.
  • There's no documentation for CLI options, because the documentation is on the web but not in the code.
  • I need help with writing tests, as I don't have time to understand how the tests work at the moment :/

cpitclaudel and others added 3 commits September 16, 2021 23:18
This is part of executablebooksGH-420, an effort to make myst_parser integrate better with
Docutils.

* myst_parser/docutils_.py (Parser): Use new _myst_docutils_setting_tuples
function to generate a settings_spec.
(Parser.parse): Use new create_myst_config function to reconstruct a
MdParserConfig object from a Docutils configuration object.
@chrisjsewell
Copy link
Member

cheers, I’ll give a proper look soon

There's no documentation for CLI options, because the documentation is on the web but not in the code.

It would be very possible to self document the MDConfig attr class via metadata. I’ve already done it in other projects: https://github.com/chrisjsewell/ase-notebook/blob/eb12723f0aed4e774484f0e8bce10b3785c0a2e5/ase_notebook/configuration.py#L114

I need help with writing tests, as I don't have time to understand how the tests work at the moment :/

is it ok if I push directly to your branch?

@cpitclaudel
Copy link
Contributor Author

is it ok if I push directly to your branch?

Please do, thanks a lot!

@cpitclaudel
Copy link
Contributor Author

It would be very possible to self document the MDConfig attr class via metadata.

Yep, that would be ideal. Ideally that would also be used to generate the relevant section of the docs, to avoid having two sources of truth. I just didn't find the time to do it ^^

@codecov
Copy link

codecov bot commented Dec 5, 2021

Codecov Report

Merging #426 (2782088) into master (86bf84d) will decrease coverage by 0.51%.
The diff coverage is 79.34%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #426      +/-   ##
==========================================
- Coverage   90.84%   90.32%   -0.52%     
==========================================
  Files          16       16              
  Lines        1912     1985      +73     
==========================================
+ Hits         1737     1793      +56     
- Misses        175      192      +17     
Flag Coverage Δ
pytests 90.32% <79.34%> (-0.52%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
myst_parser/sphinx_.py 100.00% <ø> (+100.00%) ⬆️
myst_parser/docutils_.py 78.88% <75.00%> (-21.12%) ⬇️
myst_parser/docutils_renderer.py 92.83% <100.00%> (+0.03%) ⬆️
myst_parser/main.py 88.61% <100.00%> (+0.81%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 86bf84d...2782088. Read the comment docs.

@chrisjsewell chrisjsewell linked an issue Dec 5, 2021 that may be closed by this pull request
@chrisjsewell
Copy link
Member

chrisjsewell commented Dec 5, 2021

Ok @cpitclaudel, how's this!?

Now you have separate CLI commands for writers: myst-docutils-html myst-docutils-html5 myst-docutils-latex myst-docutils-pseudoxml myst-docutils-xml

and full doc-strings:

$ myst-docutils-xml --help
Usage
=====
  myst-docutils-xml [options] [<source> [<destination>]]

Generates Docutils-native XML from standalone MyST sources. Reads from
<source> (default is stdin) and writes to <destination> (default is stdout).
See <http://docutils.sf.net/docs/user/config.html> for the full reference.

Options
=======
...
MyST options
------------
--myst-commonmark-only=MYST_COMMONMARK_ONLY
                        Use strict CommonMark parser (type: bool, default:
                        False)
--myst-enable-extensions=MYST_ENABLE_EXTENSIONS
                        Enable extensions (type: comma-delimited, default:
                        'dollarmath')
--myst-dmath-allow-labels=MYST_DMATH_ALLOW_LABELS
                        Parse `$$...$$ (label)` (type: bool, default: True)
--myst-dmath-allow-space=MYST_DMATH_ALLOW_SPACE
                        dollarmath: allow initial/final spaces in `$ ... $`
                        (type: bool, default: True)
--myst-dmath-allow-digits=MYST_DMATH_ALLOW_DIGITS
                        dollarmath: allow initial/final digits `1$ ...$2`
                        (type: bool, default: True)
--myst-dmath-double-inline=MYST_DMATH_DOUBLE_INLINE
                        dollarmath: parse inline `$$ ... $$` (type: bool,
                        default: False)
--myst-disable-syntax=MYST_DISABLE_SYNTAX
                        Disable syntax elements (type: comma-delimited,
                        default: '')
--myst-url-schemes=MYST_URL_SCHEMES
                        URL schemes to allow in links (type: comma-delimited,
                        default: 'http,https,mailto,ftp')
--myst-footnote-transition=MYST_FOOTNOTE_TRANSITION
                        Place a transition before any footnotes (type: bool,
                        default: True)
--myst-words-per-minute=MYST_WORDS_PER_MINUTE
                        For reading speed calculations (type: int, default:
                        200)

I removed the addition of the section level mathjax classes, when using docutils (since this is only useful with sphinx.ext.mathjax)

I also removed the heading_anchors option, since heading anchor slugs are currently only handled by the sphinx renderer.
It might be nice to eventually allow this functionality for docutils

still to add some extra testing and documentation...

@cpitclaudel
Copy link
Contributor Author

😍

@cpitclaudel
Copy link
Contributor Author

I forget if the heading_anchors option is related to this recently implemented feature in docutils: https://sourceforge.net/p/docutils/feature-requests/28/

@cpitclaudel
Copy link
Contributor Author

cpitclaudel commented Dec 5, 2021

On the topic of script names, since Docutils uses rst2XXX.py, maybe we should have myst2xxx.py for consistency?
Although see https://sourceforge.net/p/docutils/feature-requests/82/:

Docutils 0.17 comes with tools/docutils-cli.py:
New generic command line front end that allows the free selection of
reader, parser, and writer components.

@chrisjsewell
Copy link
Member

chrisjsewell commented Dec 5, 2021

maybe we should have myst2xxx.py for consistency

Yeh, I was hesitant to have this, since it does not make it clear that it will only parse using docutils and not sphinx, e.g. you can't properly parse any files that use sphinx/sphinx-extension roles/directives

@chrisjsewell
Copy link
Member

Although see https://sourceforge.net/p/docutils/feature-requests/82/

I very much agree with this issue though; it's a shame there is currently no easy "middle ground" between the single document docutils API/CLI, lacking any support for sphinx features, and the sphinx-build CLI, requiring use of the full, complex, project build system

setting = f"myst_{attribute.name}"
val = getattr(settings, setting, DOCUTILS_UNSET)
with suppress(AttributeError):
delattr(settings, setting)
Copy link
Member

Choose a reason for hiding this comment

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

@cpitclaudel why do you need to delete the setting here?

Copy link
Member

Choose a reason for hiding this comment

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

(note here I made the getting/deleting optional, since if you include a MyST file from an RST file, the MyST settings will not have been loaded)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I forget the details :/ I think it was to avoid namespace pollution? I'm guessing I was worried about having the settings both directly in the document under myst_* and in a config object, so I was planning to remove all the myst_* settings from document.settings and instead have a single object document.settings.myst_config.

Of course, this explanation would make more sense if I had then actually stored the config object as document.settings.myst_config, but the code doesn't seem to do that, so either I forgot or my guess above is wrong ^^

Copy link
Member

Choose a reason for hiding this comment

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

I think I'll remove it for now then, as it doesn't appear to break anything

@chrisjsewell
Copy link
Member

I forget if the heading_anchors option is related to this recently implemented feature in docutils: sourceforge.net/p/docutils/feature-requests/28

If you want to open this as a future improvement issue

@chrisjsewell
Copy link
Member

@chrisjsewell
Copy link
Member

Ok I think this is good to go! @cpitclaudel do you want to review your own PR lol, and let me know if you are happy for it to be merged

@chrisjsewell chrisjsewell changed the title 👌 IMPROVE: Support configuration through docutils.conf ✨ NEW: Add Docutils MyST configuration and CLI Dec 5, 2021
@chrisjsewell chrisjsewell changed the title ✨ NEW: Add Docutils MyST configuration and CLI ✨ NEW: Add Docutils MyST config and CLI Dec 5, 2021
@chrisjsewell
Copy link
Member

@cpitclaudel let me know if you happy for this to be merged cheers, eager to get out version 0.16.0 😄 (#426 -> #456 -> #457 -> 🚀)

@chrisjsewell
Copy link
Member

Ok times up 😛 but I'm sure its all fine

@chrisjsewell chrisjsewell merged commit 560f641 into executablebooks:master Dec 11, 2021
@cpitclaudel
Copy link
Contributor Author

Ok times up but I'm sure its all fine

Yep! Thanks a lot for moving ahead, I'm terrible at email and super busy these days. Very excited to see things moving in this direction 👍

@cpitclaudel cpitclaudel deleted the docutils_conf branch December 11, 2021 23:51
@chrisjsewell chrisjsewell mentioned this pull request Dec 17, 2021
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.

Build single pages with docutils (instead of Sphinx)
2 participants