Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

101
LINES

< > BotCompany Repo | #1030487 // SmoothHTMLTemplater Test [dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1059K of libraries. Click here for Pure Java version (4199L/22K).

!7

sclass SmoothHTMLTemplater {
  // input
  S template;
  bool verbose = true;
  Map<S, LL<Replacement>> entriesByType = ciMap();

  // internal
  LS tok;
  L<IntRange> commentRanges;
  LS comments;
  
  *() {}
  *(S *template) {}
  
  srecord Replacement(S exampleValue, S actualValue) {
    S replaceIn(S text) {
      ret replaceIC(text, exampleValue, actualValue);
    }
  }

  L<Replacement> mapToEntry(MapSO map) {
    ret map(map, (key, value) -> new Replacement(key, str(value)));
  }
  
  S render() {
    tok = htmlTok(template);
    commentRanges = charRangesOfHTMLComments(template);
    comments = map(commentRanges, r -> htmlCommentContents(substring(template, r)));
    new Matches m;
    new LPair<IntRange, S> replacements;
    for i over comments: {
      S comment = comments.get(i);
      if (match("start * block", comment, m)) {
        S type = $1;
        S endPat = format3("end * block", type);
        if (verbose) print("Start of block found: " + i + " / " + comment);
        int j = indexOfMatches(endPat, comments, i+1);
        int jEndOfList = indexOfMatches(format3("end * list", type), comments, i+1);

        if (j < 0) continue with warn("End of block comment not found: " + endPat);
        if (verbose) print("End of block found: " + j + " / " + comments.get(j));
        if (!isEntity(type)) continue with print("Not an entity: " + type);
        LL<Replacement> entries = getEntries(type);
        if (verbose) print(nEntries(entries));
        IntRange r1 = commentRanges.get(i), r2 = commentRanges.get(j);
        IntRange rEndOfList = commentRanges.get(jEndOfList >= 0 ? jEndOfList : j);
        
        S contents = substring(template, r1.end, r2.start);
        addPair(replacements, intRange(r1.end, rEndOfList.start),
          lines(map(entries, entry -> rewriteEntry(contents, entry))));
        i = j;
      }
    }
    if (verbose) pnlStruct("Replacement", replacements);
    ret replaceCharRanges(template, replacements);
  }

  bool isEntity(S type) { true; }
  
  // TODO: avoid multi-replacements
  S rewriteEntry(S text, L<Replacement> entry) {
    fOr (Replacement r : entry)
      text = r.replaceIn(text);
    ret text;
  }
  
  LL<Replacement> getEntries(S type) {
    ret entriesByType.get(type);
  }
}

cmodule2 TemplaterTest > DynPrintLogAndWeb {
  set flag NoNanoHTTPD.
  !include #1029545 // API for Eleu

  S html(IWebRequest req) {
    S template = loadPage("https://pays5.com/text/2559");
    SmoothHTMLTemplater templater = new(template);
    templater.entriesByType.put("company", map(x -> templater.mapToEntry(x), ll(
      litcimap(
        "Wash  pebbles" := "Product name 1",
        "spherical hydrogel sanitiser" := "Description 1",
        "$ 435,000" := "Funding 1",
        "% 1.245" := "Raised 1",
        "6,432" := "Payzones subscribed 1",
        "34 days" := "Ends 1"
        ),
      litcimap(
        "Wash  pebbles" := "Product name 2",
        "spherical hydrogel sanitiser" := "Description 2",
        "$ 435,000" := "Funding 2",
        "% 1.245" := "Raised 2",
        "6,432" := "Payzones subscribed 2",
        "34 days" := "Ends 2"
      )
    )));
    ret templater.render();
  }
}

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: 113 / 15183
Version history: 19 change(s)
Referenced in: [show references]