html.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. """HTML Exporter class"""
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. import os
  6. from traitlets import default, Unicode
  7. from traitlets.config import Config
  8. from jupyter_core.paths import jupyter_path
  9. from jinja2 import contextfilter
  10. from nbconvert.filters.highlight import Highlight2HTML
  11. from nbconvert.filters.markdown_mistune import IPythonRenderer, MarkdownWithMath
  12. from .templateexporter import TemplateExporter
  13. class HTMLExporter(TemplateExporter):
  14. """
  15. Exports a basic HTML document. This exporter assists with the export of
  16. HTML. Inherit from it if you are writing your own HTML template and need
  17. custom preprocessors/filters. If you don't need custom preprocessors/
  18. filters, just change the 'template_file' config option.
  19. """
  20. export_from_notebook = "HTML"
  21. anchor_link_text = Unicode(u'¶',
  22. help="The text used as the text for anchor links.").tag(config=True)
  23. @default('file_extension')
  24. def _file_extension_default(self):
  25. return '.html'
  26. @default('default_template_path')
  27. def _default_template_path_default(self):
  28. return os.path.join("..", "templates", "html")
  29. @default('template_data_paths')
  30. def _template_data_paths_default(self):
  31. return jupyter_path("nbconvert", "templates", "html")
  32. @default('template_file')
  33. def _template_file_default(self):
  34. return 'full.tpl'
  35. output_mimetype = 'text/html'
  36. @property
  37. def default_config(self):
  38. c = Config({
  39. 'NbConvertBase': {
  40. 'display_data_priority' : ['application/vnd.jupyter.widget-state+json',
  41. 'application/vnd.jupyter.widget-view+json',
  42. 'application/javascript',
  43. 'text/html',
  44. 'text/markdown',
  45. 'image/svg+xml',
  46. 'text/latex',
  47. 'image/png',
  48. 'image/jpeg',
  49. 'text/plain'
  50. ]
  51. },
  52. 'CSSHTMLHeaderPreprocessor':{
  53. 'enabled':True
  54. },
  55. 'HighlightMagicsPreprocessor': {
  56. 'enabled':True
  57. }
  58. })
  59. c.merge(super(HTMLExporter,self).default_config)
  60. return c
  61. @contextfilter
  62. def markdown2html(self, context, source):
  63. """Markdown to HTML filter respecting the anchor_link_text setting"""
  64. cell = context.get('cell', {})
  65. attachments = cell.get('attachments', {})
  66. renderer = IPythonRenderer(escape=False, attachments=attachments,
  67. anchor_link_text=self.anchor_link_text)
  68. return MarkdownWithMath(renderer=renderer).render(source)
  69. def default_filters(self):
  70. for pair in super(HTMLExporter, self).default_filters():
  71. yield pair
  72. yield ('markdown2html', self.markdown2html)
  73. def from_notebook_node(self, nb, resources=None, **kw):
  74. langinfo = nb.metadata.get('language_info', {})
  75. lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
  76. highlight_code = self.filters.get('highlight_code', Highlight2HTML(pygments_lexer=lexer, parent=self))
  77. self.register_filter('highlight_code', highlight_code)
  78. return super(HTMLExporter, self).from_notebook_node(nb, resources, **kw)