Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbconvert/preprocessors/csshtmlheader.py: 42%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""Module that pre-processes the notebook for export to HTML.
2"""
4# Copyright (c) Jupyter Development Team.
5# Distributed under the terms of the Modified BSD License.
7import hashlib
8import os
10from jupyterlab_pygments import JupyterStyle # type:ignore
11from pygments.style import Style
12from traitlets import Type, Unicode, Union
14from .base import Preprocessor
16try:
17 from notebook import DEFAULT_STATIC_FILES_PATH # type:ignore
18except ImportError:
19 DEFAULT_STATIC_FILES_PATH = None
22class CSSHTMLHeaderPreprocessor(Preprocessor):
23 """
24 Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook
25 front-end CSS and Pygments CSS to HTML output.
26 """
28 highlight_class = Unicode(".highlight", help="CSS highlight class identifier").tag(config=True)
30 style = Union(
31 [Unicode("default"), Type(klass=Style)],
32 help="Name of the pygments style to use",
33 default_value=JupyterStyle,
34 ).tag(config=True)
36 def __init__(self, *pargs, **kwargs):
37 """Initialize the preprocessor."""
38 Preprocessor.__init__(self, *pargs, **kwargs)
39 self._default_css_hash = None
41 def preprocess(self, nb, resources):
42 """Fetch and add CSS to the resource dictionary
44 Fetch CSS from IPython and Pygments to add at the beginning
45 of the html files. Add this css in resources in the
46 "inlining.css" key
48 Parameters
49 ----------
50 nb : NotebookNode
51 Notebook being converted
52 resources : dictionary
53 Additional resources used in the conversion process. Allows
54 preprocessors to pass variables into the Jinja engine.
55 """
56 resources["inlining"] = {}
57 resources["inlining"]["css"] = self._generate_header(resources)
58 return nb, resources
60 def _generate_header(self, resources):
61 """
62 Fills self.header with lines of CSS extracted from IPython
63 and Pygments.
64 """
65 from pygments.formatters import HtmlFormatter
67 header = []
69 formatter = HtmlFormatter(style=self.style)
70 pygments_css = formatter.get_style_defs(self.highlight_class)
71 header.append(pygments_css)
73 # Load the user's custom CSS and IPython's default custom CSS. If they
74 # differ, assume the user has made modifications to his/her custom CSS
75 # and that we should inline it in the nbconvert output.
76 config_dir = resources["config_dir"]
77 custom_css_filename = os.path.join(config_dir, "custom", "custom.css")
78 if os.path.isfile(custom_css_filename):
79 if DEFAULT_STATIC_FILES_PATH and self._default_css_hash is None:
80 self._default_css_hash = self._hash(
81 os.path.join(DEFAULT_STATIC_FILES_PATH, "custom", "custom.css")
82 )
83 if self._hash(custom_css_filename) != self._default_css_hash:
84 with open(custom_css_filename, encoding="utf-8") as f:
85 header.append(f.read())
86 return header
88 def _hash(self, filename):
89 """Compute the hash of a file."""
90 md5 = hashlib.md5() # noqa
91 with open(filename, "rb") as f:
92 md5.update(f.read())
93 return md5.digest()