lektor_ng.types.flow

src/lektor_ng/types/flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import re

from jinja2 import TemplateNotFound, is_undefined
from markupsafe import Markup

from lektor_ng.constants import PRIMARY_ALT
from lektor_ng.context import get_ctx
from lektor_ng.metaformat import tokenize
from lektor_ng.types.base import Type

_block_re = re.compile(r"^####\s*([^#]*?)\s*####\s*$")
_line_unescape_re = re.compile(r"^#####(.*?)#####(\s*)$")


def discover_relevant_flowblock_models(flow, pad, record, alt):
    """Returns a dictionary of all relevant flow blocks.  If no list of
    flow block names is provided all flow blocks are returned.  Otherwise
    only flow blocks that are in the list or are children of flowblocks
    in the list are returned.
    """
    flow_blocks = flow.flow_blocks

    all_blocks = pad.db.flowblocks
    if flow_blocks is None:
        return {k: v.to_json(pad, record, alt) for k, v in all_blocks.items()}

    wanted_blocks = set()
    to_process = flow_blocks[:]

    while to_process:
        block_name = to_process.pop()
        flowblock = all_blocks.get(block_name)
        if block_name in wanted_blocks or flowblock is None:
            continue
        wanted_blocks.add(block_name)
        for field in flowblock.fields:
            if isinstance(field.type, FlowType):
                if field.type.flow_blocks is None:
                    raise RuntimeError("Nested flow-blocks require explicit list of involved blocks.")
                to_process.extend(field.type.flow_blocks)

    rv = {}
    for block_name in wanted_blocks:
        rv[block_name] = all_blocks[block_name].to_json(pad, record, alt)

    return rv


class BadFlowBlock(Exception):
    pass


class FlowBlock:
    """Represents a flowblock for the template."""

    def __init__(self, data, pad, record):
        self._data = data
        self._bound_data = {}
        self.pad = pad
        self.record = record

    @property
    def flowblockmodel(self):
        """The flowblock model that created this flow block."""
        return self.pad.db.flowblocks[self._data["_flowblock"]]

    def __contains__(self, name):
        return name in self._data and not is_undefined(self._data[name])

    def __getitem__(self, name):
        # If any data of a flowblock is accessed, we record that we need
        # this dependency.
        ctx = get_ctx()
        if ctx is not None:
            ctx.record_dependency(self.flowblockmodel.filename)

        rv = self._bound_data.get(name, Ellipsis)
        if rv is not Ellipsis:
            return rv
        rv = self._data[name]
        if hasattr(rv, "__get__"):
            rv = rv.__get__(self.record)
            self._bound_data[name] = rv
        return rv

    def __html__(self):
        ctx = get_ctx()

        # If we're in a nested render, we disable the rendering here or we
        # risk a recursion error.
        if ctx is None or self in ctx.flow_block_render_stack:
            return Markup.escape(repr(self))

        ctx.flow_block_render_stack.append(self)
        try:
            try:
                return self.pad.db.env.render_template(
                    [
                        "blocks/{}.html".format(self._data["_flowblock"]),
                        "blocks/default.html",
                    ],
                    pad=self.pad,
                    this=self,
                    alt=self.record.alt,
                    values={"record": self.record},
                )
            except TemplateNotFound:
                return Markup("[could not find snippet template]")
        finally:
            ctx.flow_block_render_stack.pop()

    def __repr__(self):
        return f"<{self.__class__.__name__} {self['_flowblock']!r}>"


class Flow:
    def __init__(self, blocks, record):
        self.blocks = blocks
        self.record = record

    def __html__(self):
        return Markup("\n\n".join(x.__html__() for x in self.blocks))

    def __bool__(self):
        return bool(self.blocks)

    __nonzero__ = __bool__

    def __repr__(self):
        return f"<{self.__class__.__name__} {self.blocks!r}>"


class FlowDescriptor:
    def __init__(self, blocks, pad):
        self._blocks = blocks
        self._pad = pad

    def __get__(self, obj, type=None):
        if obj is None:
            return self
        return Flow([FlowBlock(data, self._pad, obj) for data in self._blocks], obj)


def process_flowblock_data(raw_value):
    lineiter = iter(raw_value.splitlines(True))
    block = None
    buf = []
    blocks = []

    for line in lineiter:
        # Until we found the first block, we ignore leading whitespace.
        if block is None and not line.strip():
            continue

        # Find a new block start
        block_start = _block_re.match(line)
        if block_start is None:
            if block is None:
                raise BadFlowBlock("Did not find beginning of flow block")
        else:
            if block is not None:
                blocks.append((block, buf))
                buf = []
            block = block_start.group(1)
            continue
        buf.append(_line_unescape_re.sub("####\\1####\\2", line))

    if block is not None:
        blocks.append((block, buf))

    return blocks


class FlowType(Type):
    widget = "flow"

    def __init__(self, env, options):
        Type.__init__(self, env, options)
        self.flow_blocks = [x.strip() for x in options.get("flow_blocks", "").split(",") if x.strip()] or None

    def value_from_raw(self, raw):
        if raw.value is None:
            return raw.missing_value("Missing flow")
        if raw.pad is None:
            return raw.missing_value("Flow value was technically present but used in a place where it cannot be used.")

        db = raw.pad.db
        rv = []

        try:
            for block, block_lines in process_flowblock_data(raw.value):
                # Unknown flow blocks are skipped for the moment
                if self.flow_blocks is not None and block not in self.flow_blocks:
                    continue
                flowblock = db.flowblocks.get(block)
                if flowblock is None:
                    continue

                d = {}
                for key, lines in tokenize(block_lines):
                    d[key] = "".join(lines)
                rv.append(flowblock.process_raw_data(d, pad=raw.pad))
        except BadFlowBlock as e:
            return raw.bad_value(str(e))

        return FlowDescriptor(rv, raw.pad)

    def to_json(self, pad, record=None, alt=PRIMARY_ALT):
        rv = Type.to_json(self, pad, record, alt)

        rv["flowblocks"] = discover_relevant_flowblock_models(self, pad, record, alt)

        block_order = self.flow_blocks
        if block_order is None:
            block_order = [k for k, v in sorted(pad.db.flowblocks.items(), key=lambda x: x[1].order)]
        rv["flowblock_order"] = block_order

        return rv