1
"""
2
postprocessors that modify the output stream
3
"""
4 1
import re
5

6 1
from ipypublish.postprocessors.base import IPyPostProcessor
7

8

9 1
class RemoveBlankLines(IPyPostProcessor):
10
    """ remove multiple lines of blank space """
11

12 1
    @property
13 1
    def allowed_mimetypes(self):
14 1
        return (
15
            "text/latex",
16
            "text/restructuredtext",
17
            "text/html",
18
            "text/x-python",
19
            "application/json",
20
            "text/markdown",
21
        )
22

23 1
    @property
24 1
    def requires_path(self):
25 1
        return False
26

27 1
    @property
28 1
    def logger_name(self):
29 0
        return "remove-blank-lines"
30

31 1
    def run_postprocess(self, stream, mimetype, filepath, resources):
32 1
        stream = re.sub(r"\n\s*\n", "\n\n", stream)
33 1
        return stream, filepath, resources
34

35

36 1
class RemoveTrailingSpace(IPyPostProcessor):
37
    """ remove trailing whitespace on each line """
38

39 1
    @property
40 1
    def allowed_mimetypes(self):
41 1
        return (
42
            "text/latex",
43
            "text/restructuredtext",
44
            "text/x-python",
45
            "application/json",
46
            "text/markdown",
47
        )
48

49 1
    @property
50 1
    def requires_path(self):
51 1
        return False
52

53 1
    @property
54 1
    def logger_name(self):
55 1
        return "remove-trailing-space"
56

57 1
    def run_postprocess(self, stream, mimetype, filepath, resources):
58 1
        stream = "\n".join([l.rstrip() for l in stream.splitlines()])
59 1
        return stream, filepath, resources
60

61

62 1
class FilterOutputFiles(IPyPostProcessor):
63
    """ filter internal files in resources['outputs'],
64
    by those that are referenced in the stream """
65

66 1
    @property
67 1
    def allowed_mimetypes(self):
68 1
        return None
69

70 1
    @property
71 1
    def requires_path(self):
72 1
        return False
73

74 1
    @property
75 1
    def logger_name(self):
76 0
        return "filter-output-files"
77

78 1
    def run_postprocess(self, stream, mimetype, filepath, resources):
79

80 1
        if "outputs" in resources:
81 1
            for path in list(resources["outputs"].keys()):
82 1
                if path not in stream:
83 1
                    resources["outputs"].pop(path)
84

85 1
        return stream, filepath, resources
86

87

88 1
class FixSlideReferences(IPyPostProcessor):
89
    """ make sure references refer to correct slides """
90

91 1
    @property
92 1
    def allowed_mimetypes(self):
93 1
        return "text/html"
94

95 1
    @property
96 1
    def requires_path(self):
97 1
        return False
98

99 1
    @property
100 1
    def logger_name(self):
101 1
        return "fix-slide-refs"
102

103 1
    def run_postprocess(self, stream, mimetype, filepath, resources):
104 1
        if resources and "refslide" in resources:
105 1
            for k, (col, row) in resources["refslide"].items():
106 1
                stream = stream.replace(
107
                    "{{id_home_prefix}}{0}".format(k),
108
                    "#/{0}/{1}{2}".format(col, row, k),
109
                )
110 1
        return stream, filepath, resources

Read our documentation on viewing source code .

Loading