Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/filter.py: 43%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1""" 

2 pygments.filter 

3 ~~~~~~~~~~~~~~~ 

4 

5 Module that implements the default filter. 

6 

7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11 

12def apply_filters(stream, filters, lexer=None): 

13 """ 

14 Use this method to apply an iterable of filters to 

15 a stream. If lexer is given it's forwarded to the 

16 filter, otherwise the filter receives `None`. 

17 """ 

18 def _apply(filter_, stream): 

19 yield from filter_.filter(lexer, stream) 

20 for filter_ in filters: 

21 stream = _apply(filter_, stream) 

22 return stream 

23 

24 

25def simplefilter(f): 

26 """ 

27 Decorator that converts a function into a filter:: 

28 

29 @simplefilter 

30 def lowercase(self, lexer, stream, options): 

31 for ttype, value in stream: 

32 yield ttype, value.lower() 

33 """ 

34 return type(f.__name__, (FunctionFilter,), { 

35 '__module__': getattr(f, '__module__'), 

36 '__doc__': f.__doc__, 

37 'function': f, 

38 }) 

39 

40 

41class Filter: 

42 """ 

43 Default filter. Subclass this class or use the `simplefilter` 

44 decorator to create own filters. 

45 """ 

46 

47 def __init__(self, **options): 

48 self.options = options 

49 

50 def filter(self, lexer, stream): 

51 raise NotImplementedError() 

52 

53 

54class FunctionFilter(Filter): 

55 """ 

56 Abstract class used by `simplefilter` to create simple 

57 function filters on the fly. The `simplefilter` decorator 

58 automatically creates subclasses of this class for 

59 functions passed to it. 

60 """ 

61 function = None 

62 

63 def __init__(self, **options): 

64 if not hasattr(self, 'function'): 

65 raise TypeError('%r used without bound function' % 

66 self.__class__.__name__) 

67 Filter.__init__(self, **options) 

68 

69 def filter(self, lexer, stream): 

70 # pylint: disable=not-callable 

71 yield from self.function(lexer, stream, self.options)