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

22 statements  

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

1"""This preprocessor detect cells using a different language through 

2magic extensions such as `%%R` or `%%octave`. Cell's metadata is marked 

3so that the appropriate highlighter can be used in the `highlight` 

4filter. 

5""" 

6 

7# Copyright (c) Jupyter Development Team. 

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

9 

10 

11import re 

12 

13from traitlets import Dict 

14 

15from .base import Preprocessor 

16 

17 

18class HighlightMagicsPreprocessor(Preprocessor): 

19 """ 

20 Detects and tags code cells that use a different languages than Python. 

21 """ 

22 

23 # list of magic language extensions and their associated pygment lexers 

24 default_languages = Dict( 

25 { 

26 "%%R": "r", 

27 "%%bash": "bash", 

28 "%%cython": "cython", 

29 "%%javascript": "javascript", 

30 "%%julia": "julia", 

31 "%%latex": "latex", 

32 "%%octave": "octave", 

33 "%%perl": "perl", 

34 "%%ruby": "ruby", 

35 "%%sh": "sh", 

36 "%%sql": "sql", 

37 } 

38 ) 

39 

40 # user defined language extensions 

41 languages = Dict( 

42 help=( 

43 "Syntax highlighting for magic's extension languages. " 

44 "Each item associates a language magic extension such as %%R, " 

45 "with a pygments lexer such as r." 

46 ) 

47 ).tag(config=True) 

48 

49 def __init__(self, config=None, **kw): 

50 """Public constructor""" 

51 

52 super().__init__(config=config, **kw) 

53 

54 # Update the default languages dict with the user configured ones 

55 self.default_languages.update(self.languages) 

56 

57 # build a regular expression to catch language extensions and choose 

58 # an adequate pygments lexer 

59 any_language = "|".join(self.default_languages.keys()) 

60 self.re_magic_language = re.compile(rf"^\s*({any_language})\s+") 

61 

62 def which_magic_language(self, source): 

63 """ 

64 When a cell uses another language through a magic extension, 

65 the other language is returned. 

66 If no language magic is detected, this function returns None. 

67 

68 Parameters 

69 ---------- 

70 source: str 

71 Source code of the cell to highlight 

72 """ 

73 

74 m = self.re_magic_language.match(source) 

75 

76 if m: 

77 # By construction of the re, the matched language must be in the 

78 # languages dictionary 

79 return self.default_languages[m.group(1)] 

80 else: 

81 return None 

82 

83 def preprocess_cell(self, cell, resources, cell_index): 

84 """ 

85 Tags cells using a magic extension language 

86 

87 Parameters 

88 ---------- 

89 cell : NotebookNode cell 

90 Notebook cell being processed 

91 resources : dictionary 

92 Additional resources used in the conversion process. Allows 

93 preprocessors to pass variables into the Jinja engine. 

94 cell_index : int 

95 Index of the cell being processed (see base.py) 

96 """ 

97 

98 # Only tag code cells 

99 if cell.cell_type == "code": 

100 magic_language = self.which_magic_language(cell.source) 

101 if magic_language: 

102 cell["metadata"]["magics_language"] = magic_language 

103 return cell, resources