Uses 1059K of libraries. Click here for Pure Java version (4199L/22K).
1 | !7 |
2 | |
3 | sclass SmoothHTMLTemplater { |
4 | // input |
5 | S template; |
6 | bool verbose = true; |
7 | Map<S, LL<Replacement>> entriesByType = ciMap(); |
8 | |
9 | // internal |
10 | LS tok; |
11 | L<IntRange> commentRanges; |
12 | LS comments; |
13 | |
14 | *() {} |
15 | *(S *template) {} |
16 | |
17 | srecord Replacement(S exampleValue, S actualValue) { |
18 | S replaceIn(S text) { |
19 | ret replaceIC(text, exampleValue, actualValue); |
20 | } |
21 | } |
22 | |
23 | L<Replacement> mapToEntry(MapSO map) { |
24 | ret map(map, (key, value) -> new Replacement(key, str(value))); |
25 | } |
26 | |
27 | S render() { |
28 | tok = htmlTok(template); |
29 | commentRanges = charRangesOfHTMLComments(template); |
30 | comments = map(commentRanges, r -> htmlCommentContents(substring(template, r))); |
31 | new Matches m; |
32 | new LPair<IntRange, S> replacements; |
33 | for i over comments: { |
34 | S comment = comments.get(i); |
35 | if (match("start * block", comment, m)) { |
36 | S type = $1; |
37 | S endPat = format3("end * block", type); |
38 | if (verbose) print("Start of block found: " + i + " / " + comment); |
39 | int j = indexOfMatches(endPat, comments, i+1); |
40 | int jEndOfList = indexOfMatches(format3("end * list", type), comments, i+1); |
41 | |
42 | if (j < 0) continue with warn("End of block comment not found: " + endPat); |
43 | if (verbose) print("End of block found: " + j + " / " + comments.get(j)); |
44 | if (!isEntity(type)) continue with print("Not an entity: " + type); |
45 | LL<Replacement> entries = getEntries(type); |
46 | if (verbose) print(nEntries(entries)); |
47 | IntRange r1 = commentRanges.get(i), r2 = commentRanges.get(j); |
48 | IntRange rEndOfList = commentRanges.get(jEndOfList >= 0 ? jEndOfList : j); |
49 | |
50 | S contents = substring(template, r1.end, r2.start); |
51 | addPair(replacements, intRange(r1.end, rEndOfList.start), |
52 | lines(map(entries, entry -> rewriteEntry(contents, entry)))); |
53 | i = j; |
54 | } |
55 | } |
56 | if (verbose) pnlStruct("Replacement", replacements); |
57 | ret replaceCharRanges(template, replacements); |
58 | } |
59 | |
60 | bool isEntity(S type) { true; } |
61 | |
62 | // TODO: avoid multi-replacements |
63 | S rewriteEntry(S text, L<Replacement> entry) { |
64 | fOr (Replacement r : entry) |
65 | text = r.replaceIn(text); |
66 | ret text; |
67 | } |
68 | |
69 | LL<Replacement> getEntries(S type) { |
70 | ret entriesByType.get(type); |
71 | } |
72 | } |
73 | |
74 | cmodule2 TemplaterTest > DynPrintLogAndWeb { |
75 | set flag NoNanoHTTPD. |
76 | !include #1029545 // API for Eleu |
77 | |
78 | S html(IWebRequest req) { |
79 | S template = loadPage("https://pays5.com/text/2559"); |
80 | SmoothHTMLTemplater templater = new(template); |
81 | templater.entriesByType.put("company", map(x -> templater.mapToEntry(x), ll( |
82 | litcimap( |
83 | "Wash pebbles" := "Product name 1", |
84 | "spherical hydrogel sanitiser" := "Description 1", |
85 | "$ 435,000" := "Funding 1", |
86 | "% 1.245" := "Raised 1", |
87 | "6,432" := "Payzones subscribed 1", |
88 | "34 days" := "Ends 1" |
89 | ), |
90 | litcimap( |
91 | "Wash pebbles" := "Product name 2", |
92 | "spherical hydrogel sanitiser" := "Description 2", |
93 | "$ 435,000" := "Funding 2", |
94 | "% 1.245" := "Raised 2", |
95 | "6,432" := "Payzones subscribed 2", |
96 | "34 days" := "Ends 2" |
97 | ) |
98 | ))); |
99 | ret templater.render(); |
100 | } |
101 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030487 |
Snippet name: | SmoothHTMLTemplater Test [dev.] |
Eternal ID of this version: | #1030487/20 |
Text MD5: | a233db0e54ec5f700a8ca4a4697a23fb |
Transpilation MD5: | 3ea3f09e27a5e3e327fb7cc1b3d9fb8d |
Author: | stefan |
Category: | javax |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-12-28 01:45:19 |
Source code size: | 3181 bytes / 101 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 182 / 15277 |
Version history: | 19 change(s) |
Referenced in: | [show references] |