Skip to content

Commit

Permalink
Use JupyterLab pygments
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainCorlay committed May 25, 2019
1 parent 1d7b66e commit 7163caf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 61 deletions.
74 changes: 17 additions & 57 deletions nbconvert/preprocessors/csshtmlheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 7163caf

Please sign in to comment.