Coverage for pyquickhelper/helpgen/notebook_exporter.py: 98%

47 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 02:21 +0200

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Customer notebook exporters. 

5""" 

6import os 

7from textwrap import indent 

8from traitlets import default 

9from traitlets.config import Config 

10from jinja2 import DictLoader 

11from nbconvert.exporters import RSTExporter 

12from nbconvert.filters.pandoc import convert_pandoc 

13 

14 

15def convert_pandoc_rst(source, from_format, to_format, extra_args=None): 

16 """ 

17 Overwrites `convert_pandoc 

18 <https://github.com/jupyter/nbconvert/blob/master/nbconvert/filters/pandoc.py>`_. 

19 

20 @param source string to convert 

21 @param from_format from format 

22 @param to_format to format 

23 @param extra_args extra arguments 

24 @return results 

25 """ 

26 return convert_pandoc(source, from_format, to_format, extra_args=None) 

27 

28 

29def process_raw_html(source, extra_args=None): 

30 """ 

31 Replaces the output of 

32 `add_menu_notebook 

33 <http://www.xavierdupre.fr/app/jyquickhelper/helpsphinx/jyquickhelper/ 

34 helper_in_notebook.html#jyquickhelper.helper_in_notebook.add_notebook_menu>`_ 

35 by: 

36 

37 :: 

38 

39 .. contents:: 

40 :local: 

41 """ 

42 if source is None: 

43 return source # pragma: no cover 

44 if 'var update_menu = function() {' in source: 

45 return "\n\n.. contents::\n :local:\n\n" 

46 return "\n\n.. raw:: html\n\n" + indent(source, prefix=' ') 

47 

48 

49class UpgradedRSTExporter(RSTExporter): 

50 """ 

51 Exports :epkg:`rst` documents. 

52 Overwrites `RSTExporter <https://github.com/jupyter/ 

53 nbconvert/blob/master/nbconvert/exporters/rst.py>`_. 

54 

55 * It replaces `convert_pandoc <https://github.com/jupyter/ 

56 nbconvert/blob/master/nbconvert/filters/pandoc.py>`_ 

57 by @see fn convert_pandoc_rst. 

58 * It converts :epkg:`svg` into :epkg:`png` if possible, 

59 see @see fn process_raw_html. 

60 * It replaces some known :epkg:`javascript`. The output of function 

61 `add_menu_notebook <http://www.xavierdupre.fr/app/jyquickhelper/helpsphinx/jyquickhelper/ 

62 helper_in_notebook.html#jyquickhelper.helper_in_notebook.add_notebook_menu>`_ 

63 is replaced by ``.. contents::``. 

64 

65 .. index:: notebook export, nbconvert 

66 

67 It extends the template 

68 `rst.tpl <https://github.com/jupyter/nbconvert/blob/master/nbconvert/templates/rst.tpl>`_. 

69 New template is `rst_modified.tpl <https://github.com/sdpython/pyquickhelper/blob/master/ 

70 pyquickhelper/helpgen/rst_modified.tpl>`_. 

71 It follows the hints given at 

72 `Programatically creating templates 

73 <https://nbconvert.readthedocs.io/en/latest/ 

74 nbconvert_library.html#Programatically-creating-templates>`_. 

75 

76 :epkg:`jyquickhelper` should add a string highly recognizable when adding a menu. 

77 """ 

78 

79 def __init__(self, *args, **kwargs): 

80 """ 

81 Overwrites the extra loaders to get the right template. 

82 """ 

83 filename = os.path.join(os.path.dirname(__file__), 'rst_modified.tpl') 

84 with open(filename, 'r', encoding='utf-8') as f: 

85 content = f.read() 

86 filename = os.path.join(os.path.dirname(__file__), 'rst.tpl') 

87 with open(filename, 'r', encoding='utf-8') as f: 

88 content2 = f.read() 

89 

90 dl = DictLoader({'rst_modified.tpl': content, 'rst.tpl': content2}) 

91 kwargs['extra_loaders'] = [dl] 

92 RSTExporter.__init__(self, *args, **kwargs) 

93 

94 def default_filters(self): 

95 """ 

96 Overrides in subclasses to provide extra filters. 

97 

98 This should return an iterable of 2-tuples: (name, class-or-function). 

99 You should call the method on the parent class and include the filters 

100 it provides. 

101 

102 If a name is repeated, the last filter provided wins. Filters from 

103 user-supplied config win over filters provided by classes. 

104 """ 

105 for k, v in RSTExporter.default_filters(self): 

106 yield (k, v) 

107 yield ('convert_pandoc_rst', convert_pandoc_rst) 

108 yield ('process_raw_html', process_raw_html) 

109 

110 output_mimetype = 'text/restructuredtext' 

111 export_from_notebook = "reST" 

112 

113 @default('template_file') 

114 def _template_file_default(self): 

115 return "rst_modified.tpl" 

116 

117 @default('file_extension') 

118 def _file_extension_default(self): 

119 return '.rst' 

120 

121 @default('template_name') 

122 def _template_name_default(self): 

123 return 'rst' 

124 

125 @property 

126 def default_config(self): 

127 c = Config({ 

128 'ExtractOutputPreprocessor': { 

129 'enabled': True, 

130 'output_filename_template': '{unique_key}_{cell_index}_{index}{extension}' 

131 }, 

132 'HighlightMagicsPreprocessor': { 

133 'enabled': True 

134 }, 

135 }) 

136 c.merge(super(UpgradedRSTExporter, self).default_config) 

137 return c