Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/nbconvert/preprocessors/regexremove.py: 60%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

15 statements  

1""" 

2Module containing a preprocessor that removes cells if they match 

3one or more regular expression. 

4""" 

5 

6# Copyright (c) IPython Development Team. 

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

8from __future__ import annotations 

9 

10import re 

11 

12from traitlets import List, Unicode 

13 

14from .base import Preprocessor 

15 

16 

17class RegexRemovePreprocessor(Preprocessor): 

18 """ 

19 Removes cells from a notebook that match one or more regular expression. 

20 

21 For each cell, the preprocessor checks whether its contents match 

22 the regular expressions in the ``patterns`` traitlet which is a list 

23 of unicode strings. If the contents match any of the patterns, the cell 

24 is removed from the notebook. 

25 

26 To modify the list of matched patterns, 

27 modify the patterns traitlet. For example, execute the following command 

28 to convert a notebook to html and remove cells containing only whitespace:: 

29 

30 jupyter nbconvert --RegexRemovePreprocessor.patterns="['\\s*\\Z']" mynotebook.ipynb 

31 

32 The command line argument 

33 sets the list of patterns to ``'\\s*\\Z'`` which matches an arbitrary number 

34 of whitespace characters followed by the end of the string. 

35 

36 See https://regex101.com/ for an interactive guide to regular expressions 

37 (make sure to select the python flavor). See 

38 https://docs.python.org/library/re.html for the official regular expression 

39 documentation in python. 

40 """ 

41 

42 patterns = List(Unicode()).tag(config=True) 

43 

44 def check_conditions(self, cell): 

45 """ 

46 Checks that a cell matches the pattern. 

47 

48 Returns: Boolean. 

49 True means cell should *not* be removed. 

50 """ 

51 

52 # Compile all the patterns into one: each pattern is first wrapped 

53 # by a non-capturing group to ensure the correct order of precedence 

54 # and the patterns are joined with a logical or 

55 pattern = re.compile("|".join("(?:%s)" % pattern for pattern in self.patterns)) 

56 

57 # Filter out cells that meet the pattern and have no outputs 

58 return not pattern.match(cell.source) 

59 

60 def preprocess(self, nb, resources): 

61 """ 

62 Preprocessing to apply to each notebook. See base.py for details. 

63 """ 

64 # Skip preprocessing if the list of patterns is empty 

65 if not self.patterns: 

66 return nb, resources 

67 

68 # Filter out cells that meet the conditions 

69 nb.cells = [cell for cell in nb.cells if self.check_conditions(cell)] 

70 

71 return nb, resources