Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbconvert/preprocessors/latex.py: 42%

19 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1"""Module that allows latex output notebooks to be conditioned before 

2they are converted. 

3""" 

4# ----------------------------------------------------------------------------- 

5# Copyright (c) 2013, the IPython Development Team. 

6# 

7# Distributed under the terms of the Modified BSD License. 

8# 

9# The full license is in the file COPYING.txt, distributed with this software. 

10# ----------------------------------------------------------------------------- 

11 

12# ----------------------------------------------------------------------------- 

13# Imports 

14# ----------------------------------------------------------------------------- 

15 

16 

17from traitlets import List, Unicode 

18 

19from .base import Preprocessor 

20 

21# ----------------------------------------------------------------------------- 

22# Classes 

23# ----------------------------------------------------------------------------- 

24 

25 

26class LatexPreprocessor(Preprocessor): 

27 """Preprocessor for latex destined documents. 

28 

29 Populates the ``latex`` key in the resources dict, 

30 adding definitions for pygments highlight styles. 

31 

32 Sets the authors, date and title of the latex document, 

33 overriding the values given in the metadata. 

34 """ 

35 

36 date = Unicode( 

37 None, 

38 help=("Date of the LaTeX document"), 

39 allow_none=True, 

40 ).tag(config=True) 

41 

42 title = Unicode(None, help=("Title of the LaTeX document"), allow_none=True).tag(config=True) 

43 

44 author_names = List( 

45 Unicode(), 

46 default_value=None, 

47 help=("Author names to list in the LaTeX document"), 

48 allow_none=True, 

49 ).tag(config=True) 

50 

51 style = Unicode("default", help="Name of the pygments style to use").tag(config=True) 

52 

53 def preprocess(self, nb, resources): 

54 """Preprocessing to apply on each notebook. 

55 

56 Parameters 

57 ---------- 

58 nb : NotebookNode 

59 Notebook being converted 

60 resources : dictionary 

61 Additional resources used in the conversion process. Allows 

62 preprocessors to pass variables into the Jinja engine. 

63 """ 

64 # Generate Pygments definitions for Latex 

65 from pygments.formatters import LatexFormatter 

66 

67 resources.setdefault("latex", {}) 

68 resources["latex"].setdefault( 

69 "pygments_definitions", LatexFormatter(style=self.style).get_style_defs() 

70 ) 

71 resources["latex"].setdefault("pygments_style_name", self.style) 

72 

73 if self.author_names is not None: 

74 nb.metadata["authors"] = [{"name": author} for author in self.author_names] 

75 

76 if self.date is not None: 

77 nb.metadata["date"] = self.date 

78 

79 if self.title is not None: 

80 nb.metadata["title"] = self.title 

81 

82 return nb, resources