ftml 1.41.0

Foundation Text Markup Language - a library to render Wikidot text as HTML
Documentation
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/python3

import os
import re
import sys
from collections import defaultdict

import inflection
import tomllib

EXCLUDE_BLOCKS = ["later"]
EXCLUDE_MODULES = []

BLOCK_DIRECTORY = "src/parsing/rule/impls/block/blocks"
MODULE_DIRECTORY = "src/parsing/rule/impls/block/blocks/module/modules"

# Regular expressions to extract rules from source
BLOCK_RULE_REGEX = re.compile(
    r"""pub const BLOCK_\w+: BlockRule = BlockRule \{
    name: "block-([\w\-]+)",
    accepts_names: &(\[(?:"[^"]+"(?:, )?)+\]),
    accepts_star: (true|false),
    accepts_score: (true|false),
    accepts_newlines: (true|false),
    parse_fn.*
\};"""
)

MODULE_RULE_REGEX = re.compile(
    r"""pub const MODULE_\w+: ModuleRule = ModuleRule \{
    name: "module-([\w\-]+)",
    accepts_names: &(\[(?:"[^"]+"(?:, )?)+\]),
    parse_fn.*
\};"""
)

MODULE_EXAMPLE_REGEX = re.compile(r"\[\[module (\w+).*\]\]")

# Converting bool string to a Python bool
BOOL_VALUES = {
    "true": True,
    "false": False,
}

# Aliases which have one of these prefixes should be ignored because they
# exist because of Wikidot's weird alignment naming or jank.
BLOCK_NAME_IGNORE_PREFIXES = [
    "<",
    "=",
    ">",
    "f<",
    "f>",
    "module654",
]


def check_block_alias_in_doc(alias):
    for prefix in BLOCK_NAME_IGNORE_PREFIXES:
        if alias.startswith(prefix):
            return False
    return True


# Container for primitives which we want by reference
class Container:
    __slots__ = ("data",)

    def __init__(self, data):
        self.data = data

    def get(self):
        return self.data

    def set(self, data):
        self.data = data


# Improve readability of some objects, like sets
def format_check_value(value):
    if isinstance(value, (set, frozenset)):
        return str(list(map(format_check_value, value)))
    elif isinstance(value, bool):
        return str(value).casefold()

    return str(value)


# Case-insensitivity and converting from kebab-case
def convert_block_name(value):
    return value.casefold()


def convert_module_name(value):
    value = inflection.underscore(value)
    value = inflection.camelize(value)
    return value.casefold()


# Find all Rust files that might have rule definitions
def get_submodule_paths(directory):
    def process(path):
        if not path.endswith(".rs"):
            path = os.path.join(path, "rule.rs")

        return os.path.join(directory, path)

    return map(process, os.listdir(directory))


def load_block_data(root_dir):
    blocks_path = os.path.join(root_dir, "conf/blocks.toml")
    block_rules_path = os.path.join(root_dir, BLOCK_DIRECTORY)

    # Load config
    with open(blocks_path, "rb") as file:
        blocks = tomllib.load(file)

        # Normalize module keys for case-insensitive access
        blocks = {convert_block_name(name): value for name, value in blocks.items()}

    # Adjust configuration
    # Make implicit fields explicit, etc.
    for name, block in blocks.items():
        # Aliases
        # We use sets so alias order doesn't matter
        aliases = block.get("aliases", [])

        if not block.get("exclude-name", False):
            aliases.append(name)

        block["aliases"] = frozenset(aliases)

        # Flags
        if "accepts-star" not in block:
            block["accepts-star"] = False

        if "accepts-score" not in block:
            block["accepts-score"] = False

        if "accepts-newlines" not in block:
            block["accepts-newlines"] = False

    # Load rules
    block_rules = {}
    for path in get_submodule_paths(block_rules_path):
        with open(path) as file:
            contents = file.read()

        for match in BLOCK_RULE_REGEX.finditer(contents):
            name = convert_block_name(match[1])

            if name in EXCLUDE_BLOCKS:
                continue

            block_rules[name] = {
                "aliases": frozenset(s.casefold() for s in eval(match[2])),
                "accepts-star": BOOL_VALUES[match[3]],
                "accepts-score": BOOL_VALUES[match[4]],
                "accepts-newlines": BOOL_VALUES[match[5]],
            }

    return blocks, block_rules


def load_module_data(root_dir):
    modules_path = os.path.join(root_dir, "conf/modules.toml")
    module_rules_path = os.path.join(root_dir, MODULE_DIRECTORY)

    # Load blocks
    with open(modules_path, "rb") as file:
        modules = tomllib.load(file)

        # Normalize module keys for case-insensitive access
        modules = {convert_module_name(name): value for name, value in modules.items()}

    # Adjust configuration
    # Make implicit fields explicit, etc.
    for name, module in modules.items():
        # Aliases
        # We use sets so alias order doesn't matter
        aliases = module.get("aliases", [])
        aliases.append(name)
        module["aliases"] = frozenset(aliases)

    # Load rules
    module_rules = {}
    for path in get_submodule_paths(module_rules_path):
        with open(path) as file:
            contents = file.read()

        for match in MODULE_RULE_REGEX.finditer(contents):
            name = convert_module_name(match[1])

            if name in EXCLUDE_MODULES:
                continue

            module_rules[name] = {
                "aliases": frozenset(s.casefold() for s in eval(match[2])),
            }

    return modules, module_rules


# Load documentation files
def load_block_docs(root_dir):
    blocks_path = os.path.join(root_dir, "docs/Blocks.md")

    with open(blocks_path) as file:
        return file.read()


def load_module_docs(root_dir):
    blocks_path = os.path.join(root_dir, "docs/Modules.md")

    with open(blocks_path) as file:
        return file.read()


# Compare extracted data
def compare_block_data(block_conf, block_rules):
    success = Container(True)

    # Check for new or removed blocks
    block_conf_names = frozenset(map(convert_block_name, block_conf.keys()))
    block_rule_names = frozenset(map(convert_block_name, block_rules.keys()))

    added = block_rule_names - block_conf_names
    deleted = block_conf_names - block_rule_names

    if added:
        print("!! Added blocks !!")

        for name in added:
            print(f"- {name}")

        print()
        success.set(False)

    if deleted:
        print("!! Deleted blocks !!")

        for name in deleted:
            print(f"- {name}")

        print()
        success.set(False)

    # Check contents of each block
    print("Checking blocks:")
    for name in sorted(block_rules.keys()):
        if name not in block_conf:
            print(f"! {name} (MISSING)")
            continue

        # Check block
        print(f"+ {name}")
        conf = block_conf[name]
        rule = block_rules[name]

        def check(key):
            if rule[key] != conf[key]:
                print(f"  Key {key} differs!")
                print(f"    Code:   {format_check_value(rule[key])}")
                print(f"    Config: {format_check_value(conf[key])}")
                success.set(False)

        check("aliases")
        check("accepts-star")
        check("accepts-score")
        check("accepts-newlines")

    print()
    return success.get()


def compare_module_data(module_conf, module_rules):
    success = True

    # Check for new or removed modules
    module_conf_names = frozenset(module_conf.keys())
    module_rule_names = frozenset(module_rules.keys())

    added = module_rule_names - module_conf_names
    deleted = module_conf_names - module_rule_names

    if added:
        print("!! Added modules !!")

        for name in sorted(added):
            print(f"- {name}")

        print()
        success = False

    if deleted:
        print("!! Deleted modules !!")

        for name in sorted(deleted):
            print(f"- {name}")

        print()
        success = False

    # Check contents of each module
    print("Checking modules:")
    for name in sorted(module_rules.keys()):
        if name not in module_conf:
            print(f"! {name} (MISSING)")
            continue

        # Check module
        print(f"+ {name}")
        conf = module_conf[name]
        rule = module_rules[name]

        def check(key):
            if rule[key] != conf[key]:
                print(f"  Key {key}:")
                print(f"    Code:   {format_check_value(rule[key])}")
                print(f"    Config: {format_check_value(conf[key])}")
                success = False

        check("aliases")

    print()
    return success


# Check documentation files
#
# I am not writing a full markdown scraper, besides
# documentation is loose to explain things and can't
# capture everything well anyways.
#
# Instead, this will look for the presence of the blocks'
# aliases, and if any are missing, the human knows to
# go through and ensure everything is present.
def check_block_docs(block_conf, block_docs):
    missing_aliases = defaultdict(list)

    for name, block in block_conf.items():
        for alias in filter(check_block_alias_in_doc, block["aliases"]):
            if f"`{alias}`" not in block_docs:
                missing_aliases[name].append(alias)

    if missing_aliases:
        print("!! Missing documentation for blocks !!")

        for name in sorted(missing_aliases.keys()):
            aliases = missing_aliases[name]
            aliases.sort()
            for alias in aliases:
                print(f"- {alias}")

        print()

    return not missing_aliases


def check_module_docs(module_conf, module_docs):
    def case_insensitive_set(collection):
        return frozenset(s.casefold() for s in collection)

    success = True

    modules_found = case_insensitive_set(MODULE_EXAMPLE_REGEX.findall(module_docs))
    modules_expected = case_insensitive_set(module_conf.keys())

    added = modules_found - modules_expected
    deleted = modules_expected - modules_found

    if added:
        print("!! Added module documentation !!")

        for name in added:
            print(f"- {name}")

        print()
        success = False

    if deleted:
        print("!! Deleted module documentation !!")

        for name in deleted:
            print(f"- {name}")

        print()
        success = False

    return success


if __name__ == "__main__":
    if len(sys.argv) >= 3:
        print("Usage: check_blocks.py <ftml-root-dir>")
        sys.exit(-1)

    success = True
    if len(sys.argv) == 2:
        root_dir = sys.argv[1]
    else:
        root_dir = "."

    blocks, block_rules = load_block_data(root_dir)
    block_docs = load_block_docs(root_dir)

    success &= compare_block_data(blocks, block_rules)
    success &= check_block_docs(blocks, block_docs)

    modules, module_rules = load_module_data(root_dir)
    module_docs = load_module_docs(root_dir)

    success &= compare_module_data(modules, module_rules)
    success &= check_module_docs(modules, module_docs)

    if success:
        print("FTML configuration check passed.")
        sys.exit(0)
    else:
        print("FTML configuration check failed!")
        sys.exit(1)