sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.44k stars 2.1k forks source link

Flexible styling of declarations #4289

Open jakobandersen opened 6 years ago

jakobandersen commented 6 years ago

Currently there are very few options for styling declarations, essentially only a few desc_* nodes in sphinx/addnodes.py. For some languages (e.g., C++) declarations may contain many keywords and built-in types that should be coloured to help readability. It would be cool if themes could define custom styles.

I'm not sure what the best way to go about this is, but how about the following. From Pygments (pygments.tokens) I found the following types of tokens: 'Comment', 'Error', 'Escape', 'Generic', 'Keyword', 'Literal', 'Name', 'Number', 'Operator', 'Other', 'Punctuation', 'STANDARDTYPES', 'String', 'Text', 'Token', 'Whitespace' To addnodes.py we could add additional ``desc*`` nodes for those we don't have yet, and that makes sense. Each should be constructible with a domain name. In terms of CSS each of them should emit two classes; a generic one and a domain-specific one (if a domain is specified).

jakobandersen commented 6 years ago

In https://github.com/jakobandersen/sphinx/tree/4289_decl_styling there is a proof-of-concept implementation, based on the ideas outlined above (The current changes simply colours the C++ keyword template blue in the HTML output):

Modifying a domain to use this new node type is a lot of work, so before making a larger pull-request: are people ok with this change?

jfbu commented 6 years ago

Maybe @jfbu already have some thoughts of how to do this best?

@jakobandersen can you check https://github.com/jfbu/sphinx/tree/4289_decl_styling?

It is not "how to do it best" but rather "how to do it fast" ;-)

(I have amended the commit message)

edit: in my commit I do a manual definition of LaTeX styling macro. But from brief look at Pygments, the Formatters have get_style_defs() methods which produce automatically the styling (css for html, latex commands for latex), starting from a Style.styles. I have not checked if Pygments offer a user interface to extend the list of STANDARD_TYPES (in pygments.token), but this can surely be done. Thus producing CSS style and LatTeX macros can be obtained from Pygments itself. Currently Sphinx LaTeX Builder uses Pygments on the fly to produce again on each build the highlighting macros. It should not be too involved to get this to include the added highlighting macros for cpp domain.

jfbu commented 6 years ago

The commit message at https://github.com/jfbu/sphinx/commit/441c3def2dd4ef22b1f0f163842607ac0cea5798 makes a mention of + character to be avoided but anyhow it is not legit (without escaping) in CSS class names or selectors, so this is (mainly) not an issue.

(I have again amended the commit message)

edit: the commit message at https://github.com/jfbu/sphinx/commit/cc632bae52fbb044389a8231772733ec5d56f599 discussed some issue of usage of \sphinxcode LaTeX macro. I have pushed https://github.com/jfbu/sphinx/commit/88cf9bebf823a7d05346677b958893ec8908871a to address that.

jakobandersen commented 6 years ago

@jfbu, thanks, looks quite nice. I will take a more thorough look when I find more time. A few thoughts:

jfbu commented 6 years ago

(briefly to say that for the LaTeX aspects, I have merged current master in my forked 4289_decl_styling branch because master now includes #4370 which was motivated by needs of this discussion)