diff --git a/nbconvert/preprocessors/csshtmlheader.py b/nbconvert/preprocessors/csshtmlheader.py index c9dced814..a557a9499 100755 --- a/nbconvert/preprocessors/csshtmlheader.py +++ b/nbconvert/preprocessors/csshtmlheader.py @@ -9,9 +9,11 @@ import hashlib import nbconvert.resources -from traitlets import Unicode -from .base import Preprocessor +from traitlets import Unicode, Union, Type +from pygments.style import Style +from jupyterlab_pygments import JupyterStyle +from .base import Preprocessor try: from notebook import DEFAULT_STATIC_FILES_PATH @@ -28,12 +30,13 @@ class CSSHTMLHeaderPreprocessor(Preprocessor): help="CSS highlight class identifier" ).tag(config=True) - style = Unicode('default', - help='Name of the pygments style to use' + style = Union([Unicode('default'), Type(klass=Style)], + help='Name of the pygments style to use', + default_value=JupyterStyle ).tag(config=True) theme = Unicode('light', - help='JupyterLab CSS theme' + help='JupyterLab CSS theme' ).tag(config=True) def __init__(self, *pargs, **kwargs): @@ -44,9 +47,9 @@ def preprocess(self, nb, resources): """Fetch and add CSS to the resource dictionary Fetch CSS from IPython and Pygments to add at the beginning - of the html files. Add this css in resources in the + of the html files. Add this css in resources in the "inlining.css" key - + Parameters ---------- nb : NotebookNode @@ -60,19 +63,19 @@ def preprocess(self, nb, resources): return nb, resources def _generate_header(self, resources): - """ - Fills self.header with lines of CSS extracted from IPython + """ + Fills self.header with lines of CSS extracted from IPython and Pygments. """ from pygments.formatters import HtmlFormatter header = [] - + # Construct path to Jupyter CSS sheet_filename = os.path.join( os.path.dirname(nbconvert.resources.__file__), 'index.css', ) - + # Load style CSS file. with io.open(sheet_filename, encoding='utf-8') as f: header.append(f.read()) @@ -82,57 +85,14 @@ def _generate_header(self, resources): os.path.dirname(nbconvert.resources.__file__), 'theme-%s.css' % self.theme, ) - + # Load style theme file. with io.open(theme_filename, encoding='utf-8') as f: header.append(f.read()) # Add pygments CSS - # formatter = HtmlFormatter(style=self.style) - # pygments_css = formatter.get_style_defs(self.highlight_class) - - pygments_css = ''' -.highlight .hll { background-color: var(--jp-cell-editor-active-background) } -.highlight { background: var(--jp-cell-editor-background); } -.highlight .c { color: var(--jp-mirror-editor-comment-color) } /* Comment */ -.highlight .err { color: var(--jp-mirror-editor-error-color) } /* Error */ -.highlight .k { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword */ -.highlight .o { color: var(--jp-mirror-editor-operator-color) } /* Operator */ -.highlight .ch { color: var(--jp-mirror-editor-comment-color) } /* Comment.Hashbang */ -.highlight .cm { color: var(--jp-mirror-editor-comment-color) } /* Comment.Multiline */ -.highlight .cp { color: var(--jp-mirror-editor-comment-color) } /* Comment.Preproc */ -.highlight .cpf { color: var(--jp-mirror-editor-comment-color) } /* Comment.PreprocFile */ -.highlight .c1 { color: var(--jp-mirror-editor-comment-color) } /* Comment.Single */ -.highlight .cs { color: var(--jp-mirror-editor-comment-color) } /* Comment.Special */ -.highlight .kc { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Constant */ -.highlight .kd { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Declaration */ -.highlight .kn { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Namespace */ -.highlight .kp { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Pseudo */ -.highlight .kr { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Reserved */ -.highlight .kt { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Type */ -.highlight .m { color: var(--jp-mirror-editor-number-color); font-weight: bold } /* Literal.Number */ -.highlight .s { color: var(--jp-mirror-editor-string-color) } /* Literal.String */ -.highlight .ow { color: var(--jp-mirror-editor-operator-color) } /* Operator.Word */ -.highlight .mb { color: var(--jp-mirror-editor-number-color); font-weight: bold } /* Literal.Number.Bin */ -.highlight .mf { color: var(--jp-mirror-editor-number-color); font-weight: bold } /* Literal.Number.Float */ -.highlight .mh { color: var(--jp-mirror-editor-number-color); font-weight: bold } /* Literal.Number.Hex */ -.highlight .mi { color: var(--jp-mirror-editor-number-color); font-weight: bold } /* Literal.Number.Integer */ -.highlight .mo { color: var(--jp-mirror-editor-number-color); font-weight: bold } /* Literal.Number.Oct */ -.highlight .sa { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Affix */ -.highlight .sb { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Backtick */ -.highlight .sc { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Char */ -.highlight .dl { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Delimiter */ -.highlight .sd { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Doc */ -.highlight .s2 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Double */ -.highlight .se { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Escape */ -.highlight .sh { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Heredoc */ -.highlight .si { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Interpol */ -.highlight .sx { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Other */ -.highlight .sr { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Regex */ -.highlight .s1 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Single */ -.highlight .ss { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Symbol */ -.highlight .il { color: var(--jp-mirror-editor-number-color); font-weight: bold } /* Literal.Number.Integer.Long */ -''' + formatter = HtmlFormatter(style=self.style) + pygments_css = formatter.get_style_defs(self.highlight_class) header.append(pygments_css) # Load the user's custom CSS and IPython's default custom CSS. If they diff --git a/setup.py b/setup.py index 3edddb546..41593791e 100644 --- a/setup.py +++ b/setup.py @@ -65,7 +65,6 @@ ], } - jupyterlab_css_version = '0.1.0' css_url = "https://unpkg.com/@jupyterlab/nbconvert-css@%s/style/index.css" % jupyterlab_css_version @@ -77,7 +76,7 @@ class FetchCSS(Command): - description = "Fetch Notebook CSS from CDN" + description = "Fetch JupyterLab CSS from CDN" user_options = [] def initialize_options(self): @@ -126,7 +125,7 @@ def run(self): if os.path.exists(css_dest) and os.path.exists(theme_light_dest) and os.path.exists(theme_dark_dest): print("Already have CSS, moving on.") else: - raise OSError("Need Notebook CSS to proceed.") + raise OSError("Need JupyterLab CSS to proceed.") return with open(css_dest, 'wb') as f: @@ -210,7 +209,8 @@ def run(self): setup_args['install_requires'] = [ 'mistune>=0.8.1', 'jinja2>=2.4', - 'pygments', + 'pygments>=2.4.1', + 'jupyterlab_pygments', 'traitlets>=4.2', 'jupyter_core', 'nbformat>=4.4',