1 | sclass SmoothHTMLTemplater { |
2 | // input |
3 | S template; |
4 | bool verbose; |
5 | Map<S, LL<Replacement>> entriesByType = ciMap(); |
6 | |
7 | // internal |
8 | LS tok; |
9 | L<IntRange> commentRanges; |
10 | LS comments; |
11 | |
12 | *() {} |
13 | *(S *template) {} |
14 | |
15 | srecord Replacement(S exampleValue, S actualValue) { |
16 | S replaceIn(S text) { |
17 | ret replaceIC(text, exampleValue, actualValue); |
18 | } |
19 | } |
20 | |
21 | L<Replacement> mapToEntry(MapSO map) { |
22 | ret map(map, (key, value) -> new Replacement(key, str(value))); |
23 | } |
24 | |
25 | S render() { |
26 | tok = htmlTok(template); |
27 | commentRanges = charRangesOfHTMLComments(template); |
28 | comments = map(commentRanges, r -> htmlCommentContents(substring(template, r))); |
29 | new Matches m; |
30 | new LPair<IntRange, S> replacements; |
31 | for i over comments: { |
32 | S comment = comments.get(i); |
33 | if (match("start * block", comment, m)) { |
34 | S type = $1; |
35 | S endPat = format3("end * block", type); |
36 | if (verbose) print("Start of block found: " + i + " / " + comment); |
37 | int j = indexOfMatches(endPat, comments, i+1); |
38 | int jEndOfList = indexOfMatches(format3("end * list", type), comments, i+1); |
39 | |
40 | if (j < 0) continue with warn("End of block comment not found: " + endPat); |
41 | if (verbose) print("End of block found: " + j + " / " + comments.get(j)); |
42 | if (!isEntity(type)) continue with print("Not an entity: " + type); |
43 | LL<Replacement> entries = getEntries(type); |
44 | if (verbose) print(nEntries(entries)); |
45 | IntRange r1 = commentRanges.get(i), r2 = commentRanges.get(j); |
46 | IntRange rEndOfList = commentRanges.get(jEndOfList >= 0 ? jEndOfList : j); |
47 | |
48 | S contents = substring(template, r1.end, r2.start); |
49 | addPair(replacements, intRange(r1.end, rEndOfList.start), |
50 | lines(map(entries, entry -> rewriteEntry(contents, entry)))); |
51 | i = j; |
52 | } |
53 | } |
54 | if (verbose) pnlStruct("Replacement", replacements); |
55 | ret replaceCharRanges(template, replacements); |
56 | } |
57 | |
58 | bool isEntity(S type) { true; } |
59 | |
60 | // TODO: avoid multi-replacements |
61 | S rewriteEntry(S text, L<Replacement> entry) { |
62 | fOr (Replacement r : entry) |
63 | text = r.replaceIn(text); |
64 | ret text; |
65 | } |
66 | |
67 | LL<Replacement> getEntries(S type) { |
68 | ret entriesByType.get(type); |
69 | } |
70 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030496 |
Snippet name: | SmoothHTMLTemplater |
Eternal ID of this version: | #1030496/1 |
Text MD5: | b77fc9a814374383b5d093b33e6dad77 |
Author: | stefan |
Category: | javax / html |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-12-28 03:45:32 |
Source code size: | 2330 bytes / 70 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 211 / 385 |
Referenced in: | [show references] |