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
make css highlight style configurable #3516
Conversation
If you want to make it configurable, it should be a trait with |
I'm not entirely sure what that means... could you provide an example? |
from IPython.utils.traitlets import Unicode
class CSSHtmlHeaderTransformer(ActivatableTransformer):
highlight_class = Unicode('default value', config=True, help='blahbah')
# help text could be printed by --help-all
def method(self):
print(self.highlight_class) # will be the value of c.CSSHtmlHeaderTransformer.highlight_class if set. (will develop on the trait class later when more time) |
Thanks! I made the appropriate changes, and will also edit the code snippet in the description. |
HTML has been uppercased a few hours ago, and you are also not obliged to subclass, cf the following : from IPython.nbconvert.filters.highlight import _pygment_highlight
from pygments.formatters import HtmlFormatter
from IPython.nbconvert.exporters import FullHTMLExporter
from IPython.config import Config
from IPython.nbformat import current as nbformat
def my_highlight(source, language='ipython'):
formatter = HtmlFormatter(cssclass='highlight-ipynb')
return _pygment_highlight(source, formatter, language)
c = Config({'CSSHtmlHeaderTransformer':
{'enabled':True, 'highlight_class':'highlight-ipynb'}})
exportHtml = FullHTMLExporter( config=c , filters={'highlight': my_highlight} )
(body,resources) = exportHtml.from_notebook_node(jake_notebook) |
@Carreau - thanks, that makes sense! Aside from HTML capitalization, do you think this modification of |
No problem you are probably one of the person outside of IPython core team that sell the notebook the best, that the least we can do.
Yes, I think it make sens as nbconvert will probably be used in many pelican blog :-) |
@@ -97,7 +102,8 @@ def _regen_header(self): | |||
pass | |||
|
|||
#Add pygments CSS | |||
pygments_css = HtmlFormatter().get_style_defs('.highlight') | |||
formatter = HtmlFormatter() | |||
pygments_css = formatter.get_style_defs('.' + self.highlight_class) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would put the '.'
in the default value, this could allow people to add more 'complex' style defs like div.foo ipynb-render
without prepending a dot. Thought ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call
make css highlight style configurable
Thanks, merged. |
make css highlight style configurable
For blogging with ipython notebook, it's important to be able to control the CSS tag used for pygments highlighting (the style name
"highlight"
can conflict with the default settings on some blogging platforms). Currently this is configurable for the cell rendering using a custom filter, but for the css the value hard-coded inCSSHtmlHeaderTransformer
. This PR makes the value configurable there as well.This may not be the best way to go about this, but here's an example script for how the highlight tag can be configured using the changes in this PR. Note this also needs #3512 in order to properly register the custom filter.
There may well be a better way to accomplish this using the
config
object, or perhaps even adding a configurable value to theHtmlExporter
class. I'm not entirely familiar with the IPython configurables & traitlet system, and would appreciate some feedback.