Skip to content

♻️ REFACTOR: package code #55

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

Merged
merged 19 commits into from
May 4, 2021
Merged

♻️ REFACTOR: package code #55

merged 19 commits into from
May 4, 2021

Conversation

chrisjsewell
Copy link
Member

@chrisjsewell chrisjsewell commented Apr 20, 2021

Essentially re-writes the most of the code, to implement a number of improvements and bug fixes, but the output LaTeX is shown (via tests) to be essentially the same.

  • move event functions to separate file (and rename)
  • add sphinx based tests, which provide comparisons of the doctrees create with/without this extension, before/after post-transforms have been applied. This also shows how the package can be used as a "standalone" sphinx extension.
  • use importlib.resources for accessing jupyterBook.cls
  • Remove enforcement of myst-parser amsmath extension
  • make myst_nb requirement optional, and check for compatibility
  • convert class/function nameing from pascalCase to camel_case (and enforce with pep8-naming linter):
    • replace getFilenameWithSubpath with is_root_document
    • replace removeExtension with remove_suffix
  • replace H2Node and H3Node by RootHeader node, which applies to any sub-section of the root file (i.e. also H4 etc)
  • added app.add_config_value("jblatex_load_imgconverter", True, "env") to enable loading of sphinx.ext.imgconverter to be turned off
  • remove reliance on _toc.yml, instead adding app.add_config_value("jblatex_captions_to_parts", None, "env", (type(None), bool))
    • If None and app.env.external_site_map is present (from sphinx-external-toc), then this will be used to "infer" latex_toplevel_sectioning and whether to convert captions to parts.
  • Move all doctree restructuring to the post-transform, so that the stored doctrees are builder agnostic (as should be the case). The transform (which is now called for all builders) only adds extra node attributes for use by the post-transform.
  • a lot more code documentation and added to README

@chrisjsewell
Copy link
Member Author

chrisjsewell commented Apr 20, 2021

Not finished, but publishing so @mmcky / @AakashGfude don't do any conflicting work

TODO: rewrite LatexMasterDocTransforms, ToctreeTransforms, handleSubSections, add more testing

@codecov
Copy link

codecov bot commented Apr 20, 2021

Codecov Report

Merging #55 (1e3485c) into master (c424447) will decrease coverage by 6.32%.
The diff coverage is 89.16%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #55      +/-   ##
==========================================
- Coverage   96.32%   90.00%   -6.33%     
==========================================
  Files           4        4              
  Lines         245      240       -5     
==========================================
- Hits          236      216      -20     
- Misses          9       24      +15     
Flag Coverage Δ
pytests 90.00% <89.16%> (-6.33%) ⬇️

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

Impacted Files Coverage Δ
jupyterbook_latex/events.py 86.66% <86.66%> (ø)
jupyterbook_latex/transforms.py 88.05% <88.23%> (-7.63%) ⬇️
jupyterbook_latex/__init__.py 93.33% <93.33%> (-2.13%) ⬇️
jupyterbook_latex/nodes.py 97.82% <95.83%> (-2.18%) ⬇️

@mmcky
Copy link
Member

mmcky commented Apr 21, 2021

@AakashGfude there has been a lot of discussion over the past few days re: sphinx-external-toc so let's catchup this morning on zoom and I will bring you up to speed.

- move event functions to separate file (and rename)
- use importlib.resources for accessing jupyterBook.cls
- Update myst-parser amsmath extension addition method (although I question why this is enforced)
- make myst_nb requirement optional, and check for compatibility
- replace `getFilenameWithSubpath` with `is_root_document`
- replace `removeExtension`  with `remove_suffix`
Also fix bug with `hNode` not being reset in `LatexMasterDocTransforms`
@chrisjsewell chrisjsewell marked this pull request as ready for review April 22, 2021 22:33
@chrisjsewell
Copy link
Member Author

chrisjsewell commented Apr 22, 2021

ok this is ready to review, one TODO to consider in the initial comment

@mmcky
Copy link
Member

mmcky commented Apr 23, 2021

thanks @chrisjsewell -- we will review this today.

setup.cfg Outdated
@@ -41,6 +42,7 @@ testing =
pytest>=3.6,<4
pytest-cov~=2.8
pytest-regressions
sphinx-external-toc==0.1.0a8
Copy link
Member

Choose a reason for hiding this comment

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

@AakashGfude this PR should be reviewed against this version of sphinx-external-toc

Copy link
Member

@mmcky mmcky left a comment

Choose a reason for hiding this comment

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

Thanks @chrisjsewell on your work on this refactor. It is a big improvement. We will apply some of these changes (in style etc.) to sphinx-multitoc-numbering as well.

I re-ran the full tests of building our lecture sites for both html and pdf (using this refactor):

Extension error:
Handler <function validate_config_values at 0x7fb649c8e550> for event 'config-inited' threw an exception (exception: entry_points() got an unexpected keyword argument 'group')
Traceback (most recent call last):
  File "/usr/share/miniconda3/envs/quantecon/lib/python3.8/site-packages/sphinx/events.py", line 110, in emit
    results.append(listener.handler(self.app, *args))
  File "/usr/share/miniconda3/envs/quantecon/lib/python3.8/site-packages/myst_nb/__init__.py", line 256, in validate_config_values
    load_renderer(app.config["nb_render_plugin"])
  File "/usr/share/miniconda3/envs/quantecon/lib/python3.8/site-packages/myst_nb/render_outputs.py", line 80, in load_renderer
    eps = entry_points(group="myst_nb.mime_render", name=name)
TypeError: entry_points() got an unexpected keyword argument 'group'

the build logs are here and the artifact is a complete _build folder (cc: @AakashGfude).

@mmcky
Copy link
Member

mmcky commented Apr 27, 2021

thanks @chrisjsewell I confirm both of our current project tests cases are now building via CI

@mmcky mmcky self-requested a review April 27, 2021 00:13
mmcky
mmcky previously approved these changes Apr 27, 2021
Copy link
Member

@mmcky mmcky left a comment

Choose a reason for hiding this comment

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

@chrisjsewell nice work!

AakashGfude
AakashGfude previously approved these changes Apr 27, 2021
Copy link
Member

@AakashGfude AakashGfude left a comment

Choose a reason for hiding this comment

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

Awesome work @chrisjsewell . The only thing left is to transfer tableofcontents directive here, for which I will create an issue and PR. Thanks so much.

@chrisjsewell
Copy link
Member Author

Thanks, I'm not sure what you mean by transfer the tableofcontents directive though, that's part of sphinx-external-toc now, you wouldn't use it here. If you want those bullet lists of contents (which should be optional, with "pure" latex alternatives also made available) you'd just be looking for non-hidden toctrees

@chrisjsewell
Copy link
Member Author

Also, just to clarify, I'll merge this once I've finished the final point

@AakashGfude
Copy link
Member

Thanks, I'm not sure what you mean by transfer the tableofcontents directive though, that's part of sphinx-external-toc now, you wouldn't use it here. If you want those bullet lists of contents (which should be optional, with "pure" latex alternatives also made available) you'd just be looking for non-hidden toctrees

ooh. I could not see the tableofcontents directive result in the pdf, so assumed it was not handled for latex yet.

A screenshot:
Screen Shot 2021-04-27 at 11 29 36 am

book.pdf

@chrisjsewell
Copy link
Member Author

The tableofcontents now works no different to if you were just using a regular toctree (without sphinx-external-toc).
So the point is you should be thinking of a "generic" solution that would work with any regular sphinx build (as the rest of the package now does)

@AakashGfude
Copy link
Member

The tableofcontents now works no different to if you were just using a regular toctree (without sphinx-external-toc).
So the point is you should be thinking of a "generic" solution that would work with any regular sphinx build (as the rest of the package now does)

Sorry, maybe I did not make my point clear earlier. The problem I faced when using a regular toctree with tableofcontents like the rest of the sphinx build, was that in the case of latex, it literally included the files itself, instead of just links to the file, as was expected. If I wanted to change this behavior, I had to interfere with the sphinx latex writer.

So, while the rest of the builders were using the inline toctree directive of that page, the latex builder needed special handling. I am very much looking forward to any generic solutions you have for the latex builder. But, didn't see any solution in the sphinx-external-toc PR, so was enquiring.

@chrisjsewell chrisjsewell dismissed stale reviews from AakashGfude and mmcky via 175efc0 April 27, 2021 15:43
@mmcky
Copy link
Member

mmcky commented Apr 30, 2021

@AakashGfude if we need special handling of the toc for the latex builder then this package is the best place for that.

I had to interfere with the sphinx latex writer.

Do we need to adjust the writer or the translator?
Could we just override the method that is producing output that we don't want (that has the filenames included)?

To assist with this discussion could you please add some screenshots showing the pdf output that sphinx produces. I remember seeing it (and it didn't look good) and that's when we went with the special handling for displaying local contents.

Update: Ah ok I have re-read the thread. @chrisjsewell the issue is that the generic latex writer doesn't render the toc -- it inlines it when building the latex document for inclusion of the documents but doesn't write a representation of the toc in the document. I guess it leans on latex to generate the table of contents. So @AakashGfude addition generates a simple local contents for latex output. I think the best way forward is to use the simple lists that @AakashGfude has put together and we can open an issue to replace it with a more pure latex implementation using some latex package to generate simple local contents listings. @AakashGfude can you open a PR to add in this support.

@mmcky mmcky requested review from mmcky and AakashGfude April 30, 2021 00:20
@chrisjsewell
Copy link
Member Author

chrisjsewell commented May 4, 2021

I think the best way forward is to use the simple lists that @AakashGfude has put together and we can open an issue to replace it with a more pure latex implementation using some latex package to generate simple local contents listings. @AakashGfude can you open a PR to add in this support.

this should not be that difficult:

  1. The point of this PR, and thus any future work, is that for builder specific extensions you can "gather" information in a transform, but you cannot implement any modifications of the AST until a post-transform.
  2. The list addition must be optional, and should be one option of how to generate an in-page toc.

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.

ENH: Turn this into a Sphinx Extension Document your code Fix RemoveExtensions
3 participants