Repository navigation
|
Hi, I wanted to use the atsphinx.color_text package to use ":color:" syntax in reStructuredText for keywords. Do I missed something or is there another way to perform such functionality ? Thanks |
Replies: 7 comments 1 reply
|
Sphinx extensions need to implement support for each builder. The atsphinx-color-text extension only supports HTML output. You can however hack in support for rinohtype by adding the following to tour conf.py: # -- rinohtype implementation for atsphinx-color-text ---------------------
from rinoh.color import HexColor
from rinoh.frontend.rst.nodes import Inline
from rinoh.text import TextStyle
class RinohColorText(Inline):
node_name = 'ColorText'
def build_styled_text(self):
_, color = self.attributes['style'].split(': #')
return self.process_content(style=TextStyle(font_color=HexColor(color)))Often it is possible to make Sphinx extensions independent of the builder, but extension authors are typically not aware of this. See #201 (comment) (and the comments following it) for more information on that. This particular extensions would still need adding styles to the style sheet, however. Note that you can also color text by declaring custom roles as proposed in this StackOverflow item: https://stackoverflow.com/questions/3702865/sphinx-restructuredtext-set-color-for-a-single-word. For rinohtype, you would need to define extra styles in the style sheet to handle these roles. Let me know if you want more info. Finally, I would recommend against explicitly coloring text. reStructuredText is designed to be describe semantics, not representation. For example, you have the built-in |
|
Thanks for your answer and advices. I confess, i didn't spent enough time to look for a proper solution to my request. I also found and tried :
Then I tried your solution but failed to get expected result (don't you have example with result ?) Regarding the coloring text process, I completly agree with your standpoint of dissociating semantic from representation. I'had a look on the Sphinx doc for adding new role. I'm now exploring how to set specific style for the keyword role. Any comment are welcome ;-) |
|
No need to apologize! Of course, the easiest solution would be if plugins like atsphinx.color_text and sphinxcontrib-text-styles would support rinohtype. You could kindly ask their authors if they would be interested in adding support for the rinoh builder. I could do it, but I'm already lacking the time to work on rinohtype itself, so I better not. That said, using atsphinx.color_text should work if you add the code I provided to your conf.py. Without it, rendering the document Sphinx aborts with an exception when rendering the PDF. What happens at your side? Do you get to see an error? As for an approach using text roles, you can define and use a new role in a reStructuredText file (keyword.rst): .. role:: keyword
This is a :keyword:`schnitzel`.A role will add a class to the corresponding element in the document tree. You can use this class to define a style in the stylesheet (keyword.rts) with a rule that matches the element: [STYLESHEET]
base=sphinx
[table of contents section]
hide = true
[keyword : StyledText(has_class='keyword')]
font_slant = italic
font_weight = bold
font_color = #b33Note that adding a style You can render this example from the command line: rinoh --stylesheet keyword.rts keyword.rstThis is not yet documented in the rinohtype manual, so it would be difficult to figure out. Custom selectors are documented, but the BTW, you can get a view on the internals:
|
|
I finally succeed to get get colored text with the solution you proposed associated with atsphinx.color_text package. Then I tried to extend what you provided for TextStyle to add bold and italic style to colored text : Unfluckily failed and got following Traceback : I didn't explore deeper text role approach for now because I don't have enough knowledge on sphinx / rinohtype principles and working. |
|
The font_slant and font_weight Text style arguments expect FontSlant and FontWeight instances instead of strings. In that sense, it is easier to map the atsphinx styles (hex color codes) to rinohtype styles. To do that, assign the [color-text-red : StyledText('ff0000')]
...Typing this on my phone, so it's a bit basic. But I hope it's clear. |
|
Here's a more complete implementation of this suggestion. Add this to your conf.py: # -- rinohtype implementation for atsphinx-color-text ---------------------
from atsphinx.color_text import COLORS
from rinoh.frontend.rst.nodes import Inline
from rinoh.text import StyledText
from rinoh.stylesheets import matcher
class RinohColorText(Inline):
node_name = 'ColorText'
def build_styled_text(self):
# set style on the StyledText, so that matchers can pick it up
_, hex_color = self.attributes['style'].split(': #')
return self.process_content(style=f'color-text-{hex_color}')
# define styles for all colors
for name, hex_color in COLORS.items():
matcher[f'color-text-{name}'] = StyledText.like(style=f'color-text-{hex_color[1:]}')stylesheet.rst: [color-text-black]
font_color=#000000
[color-text-silver]
font_color=#c0c0c0
[color-text-gray]
font_color=#808080
[color-text-white]
font_color=#ffffff
[color-text-maroon]
font_color=#800000
[color-text-red]
font_color=#ff0000
[color-text-purple]
font_color=#800080
[color-text-fuchsia]
font_color=#ff00ff
[color-text-green]
font_color=#008000
[color-text-lime]
font_color=#00ff00
[color-text-olive]
font_color=#808000
[color-text-yellow]
font_color=#ffff00
[color-text-navy]
font_color=#000080
[color-text-blue]
font_color=#0000ff
[color-text-teal]
font_color=#008080
[color-text-aqua]
font_color=#00ffffYou need to configure Sphinx to find your stylesheet.rts. In conf.py: rinoh_documents = [{
'doc': master_doc, # top-level file (index.rst)
'target': 'manual', # output (manual.pdf)
'title': project, # document title
'author': author, # document author
'template': 'template.rtt' # document template (references stylesheet.rts)
}]template.rtt: [TEMPLATE_CONFIGURATION]
name=my-template
template=book
stylesheet=stylesheet.rtsYou can now adjust stylesheet.rts to your liking. |


Ah, indeed, the
:keyword:role is already defined by Sphinx for cross-referencing Python keywords. It would be nice if Sphinx would warn the user when they redefine the role.In case there is confusion here: the rinoh command line tool is independent of Sphinx. It does not require conf.py or use it when present. This also means that when rendering a document using rinoh, no …