test_latex.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. """Tests for Latex exporter"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import os.path
  5. import textwrap
  6. import re
  7. from .base import ExportersTestsBase
  8. from ..latex import LatexExporter
  9. from ...tests.utils import onlyif_cmds_exist
  10. from traitlets.config import Config
  11. from nbformat import write
  12. from nbformat import v4
  13. from testpath.tempdir import TemporaryDirectory
  14. from jinja2 import DictLoader
  15. current_dir = os.path.dirname(__file__)
  16. class TestLatexExporter(ExportersTestsBase):
  17. """Contains test functions for latex.py"""
  18. exporter_class = LatexExporter
  19. should_include_raw = ['latex']
  20. def test_constructor(self):
  21. """
  22. Can a LatexExporter be constructed?
  23. """
  24. LatexExporter()
  25. @onlyif_cmds_exist('pandoc')
  26. def test_export(self):
  27. """
  28. Can a LatexExporter export something?
  29. """
  30. (output, resources) = LatexExporter().from_filename(self._get_notebook())
  31. assert len(output) > 0
  32. @onlyif_cmds_exist('pandoc')
  33. def test_export_book(self):
  34. """
  35. Can a LatexExporter export using 'report' template?
  36. """
  37. (output, resources) = LatexExporter(template_file='report').from_filename(self._get_notebook())
  38. assert len(output) > 0
  39. @onlyif_cmds_exist('pandoc')
  40. def test_export_basic(self):
  41. """
  42. Can a LatexExporter export using 'article' template?
  43. """
  44. (output, resources) = LatexExporter(template_file='article').from_filename(self._get_notebook())
  45. assert len(output) > 0
  46. @onlyif_cmds_exist('pandoc')
  47. def test_export_article(self):
  48. """
  49. Can a LatexExporter export using 'article' template?
  50. """
  51. (output, resources) = LatexExporter(template_file='article').from_filename(self._get_notebook())
  52. assert len(output) > 0
  53. @onlyif_cmds_exist('pandoc')
  54. def test_very_long_cells(self):
  55. """
  56. Torture test that long cells do not cause issues
  57. """
  58. lorem_ipsum_text = textwrap.dedent("""\
  59. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
  60. dignissim, ipsum non facilisis tempus, dui felis tincidunt metus,
  61. nec pulvinar neque odio eget risus. Nulla nisi lectus, cursus
  62. suscipit interdum at, ultrices sit amet orci. Mauris facilisis
  63. imperdiet elit, vitae scelerisque ipsum dignissim non. Integer
  64. consequat malesuada neque sit amet pulvinar. Curabitur pretium
  65. ut turpis eget aliquet. Maecenas sagittis lacus sed lectus
  66. volutpat, eu adipiscing purus pulvinar. Maecenas consequat
  67. luctus urna, eget cursus quam mollis a. Aliquam vitae ornare
  68. erat, non hendrerit urna. Sed eu diam nec massa egestas pharetra
  69. at nec tellus. Fusce feugiat lacus quis urna sollicitudin volutpat.
  70. Quisque at sapien non nibh feugiat tempus ac ultricies purus.
  71. """)
  72. lorem_ipsum_text = lorem_ipsum_text.replace("\n"," ") + "\n\n"
  73. large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000)
  74. notebook_name = "lorem_ipsum_long.ipynb"
  75. nb = v4.new_notebook(
  76. cells=[
  77. v4.new_markdown_cell(source=large_lorem_ipsum_text)
  78. ]
  79. )
  80. with TemporaryDirectory() as td:
  81. nbfile = os.path.join(td, notebook_name)
  82. with open(nbfile, 'w') as f:
  83. write(nb, f, 4)
  84. (output, resources) = LatexExporter(template_file='article').from_filename(nbfile)
  85. assert len(output) > 0
  86. @onlyif_cmds_exist('pandoc')
  87. def test_prompt_number_color(self):
  88. """
  89. Does LatexExporter properly format input and output prompts in color?
  90. """
  91. (output, resources) = LatexExporter().from_filename(
  92. self._get_notebook(nb_name="prompt_numbers.ipynb"))
  93. in_regex = r"\\prompt\{In\}\{incolor\}\{(\d+|\s*)\}"
  94. out_regex = r"\\prompt\{Out\}\{outcolor\}\{(\d+|\s*)\}"
  95. ins = ["2", "10", " ", " ", "0"]
  96. outs = ["10"]
  97. assert re.findall(in_regex, output) == ins
  98. assert re.findall(out_regex, output) == outs
  99. @onlyif_cmds_exist('pandoc')
  100. def test_prompt_number_color_ipython(self):
  101. """
  102. Does LatexExporter properly format input and output prompts in color?
  103. Uses an in memory latex template to load style_ipython as the cell style.
  104. """
  105. my_loader_tplx = DictLoader({'my_template':
  106. """
  107. ((* extends 'style_ipython.tplx' *))
  108. ((* block docclass *))
  109. \documentclass[11pt]{article}
  110. ((* endblock docclass *))
  111. """})
  112. class MyExporter(LatexExporter):
  113. template_file = 'my_template'
  114. (output, resources) = MyExporter(extra_loaders=[my_loader_tplx]).from_filename(
  115. self._get_notebook(nb_name="prompt_numbers.ipynb"))
  116. in_regex = r"In \[\{\\color\{incolor\}(.*)\}\]:"
  117. out_regex = r"Out\[\{\\color\{outcolor\}(.*)\}\]:"
  118. ins = ["2", "10", " ", " ", "0"]
  119. outs = ["10"]
  120. assert re.findall(in_regex, output) == ins
  121. assert re.findall(out_regex, output) == outs
  122. @onlyif_cmds_exist('pandoc')
  123. def test_no_prompt_yes_input(self):
  124. no_prompt = {
  125. "TemplateExporter":{
  126. "exclude_input_prompt": True,
  127. "exclude_output_prompt": True,
  128. }
  129. }
  130. c_no_prompt = Config(no_prompt)
  131. exporter = LatexExporter(config=c_no_prompt)
  132. (output, resources) = exporter.from_filename(
  133. self._get_notebook(nb_name="prompt_numbers.ipynb"))
  134. assert "shape" in output
  135. assert "evs" in output
  136. @onlyif_cmds_exist('pandoc', 'inkscape')
  137. def test_svg(self):
  138. """
  139. Can a LatexExporter export when it recieves raw binary strings form svg?
  140. """
  141. filename = os.path.join(current_dir, 'files', 'svg.ipynb')
  142. (output, resources) = LatexExporter().from_filename(filename)
  143. assert len(output) > 0
  144. def test_in_memory_template_tplx(self):
  145. # Loads in an in memory latex template (.tplx) using jinja2.DictLoader
  146. # creates a class that uses this template with the template_file argument
  147. # converts an empty notebook using this mechanism
  148. my_loader_tplx = DictLoader({'my_template': "{%- extends 'article.tplx' -%}"})
  149. class MyExporter(LatexExporter):
  150. template_file = 'my_template'
  151. exporter = MyExporter(extra_loaders=[my_loader_tplx])
  152. nb = v4.new_notebook()
  153. out, resources = exporter.from_notebook_node(nb)
  154. def test_custom_filter_highlight_code(self):
  155. # Overwriting filters takes place at: Exporter.from_notebook_node
  156. nb = v4.new_notebook()
  157. nb.cells.append(v4.new_code_cell("some_text"))
  158. def custom_highlight_code(source, language="python", metadata=None, strip_verbatim=False):
  159. return source + " ADDED_TEXT"
  160. filters = {
  161. "highlight_code": custom_highlight_code
  162. }
  163. (output, resources) = LatexExporter(filters=filters).from_notebook_node(nb)
  164. self.assertTrue("ADDED_TEXT" in output)