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

136
LINES

< > BotCompany Repo | #1028433 // WordDocumentTemplateProcessor

JavaX fragment (include) [tags: use-pretranspiled]

Uses 16250K of libraries. Click here for Pure Java version (3763L/24K).

sclass WordDocumentTemplateProcessor {
  File inFile, outFile;
  new WordDocumentTextReplacer2 replacer;
  
  swappable S getValue(S key) { null; }
  
  run {
    replacer.inFile = inFile;
    replacer.outFile = outFile;
    S optSpace = regexp_spaceIncludingNbsp() + "*";
    S space = regexp_spaceIncludingNbsp() + "+";
    replacer.processParagraph = runs -> {
      // replace content
      replacer.regexpReplacement(runs,
        "\\[" + optSpace
          + "(\\d+[a-z]*)" // key
          + regexp_negativeLookahead(space + "if" + space)
          + "[:.]" // separator
          + optSpace
          + "(.*?)" // contents
          + "\\]",
        (text, groups) -> {
          new Matches m;
          S key = first(groups);
          S value = getValue(key);
          ret or(value, "undefined");
        });
  
      // conditional paragraph parts
      replacer.regexpReplacement(runs,
        "\\[" + optSpace
          + "(\\d+[a-z]*)" // key
          + space + "if" + space
          + "(.*?)" // condition
          + "[:.]"  // end of condition
          + optSpace
          + "(.*?)" // contents
          + "\\]",
        (text, groups) -> {
          S key = first(groups);
          S condition = regexpReplace_direct(second(groups), space, " ");
          S contents = third(groups);
          
          bool result = evaluateFormCondition(key, condition);
          ret result ? contents : "";
        });
      
      // remove double commas
      replacer.regexpReplacement(runs, "," + regexp_spaceIncludingNbsp() + "*(?=,)", (text, groups) -> "");
    };
    
    replacer.postprocess = r {
      // bullet points
      for (int i = 0; i < l(replacer.outParagraphs); i++) {
        L<WordDocumentTextReplacer2.OutRun> para = replacer.outParagraphs.get(i);
        S text = replacer.fullText(para);
        LS groups = regexpMatchGroupsIC(optSpace
          + "\\["
          + "(\\d+[a-z]*)" // key
          + space + "bullet" + space + "points"
          + "\\]" + optSpace,
          text);
        if (groups != null) {
          S key = first(groups);
          S value = getValue(key);
          replacer.outParagraphs.remove(i);
          LS items = trimAll(splitAt(value, "|"));
          printVars_str("bullet points", +value);
          for (S item : items) {
            print("Adding item at " + i + ": " + item);
            replacer.outParagraphs.add(i++, ll(new WordDocumentTextReplacer2.OutRun(first(para).run, unicode_bullet() + " " + item)));
          }
          --i;
        }
      }
      
      // process conditional paragraphs
      for (int i = l(replacer.outParagraphs)-1; i >= 0; i--) {
        L<WordDocumentTextReplacer2.OutRun> para = replacer.outParagraphs.get(i);
        S text = replacer.fullText(para);
        LS groups = regexpMatchGroupsIC(optSpace
          + "\\[START" + space
          + "(\\d+[a-z]*)" // key
          + space + "if" + space
          + "(.*?)" // condition
          + "\\]" + optSpace,
          text);
        if (groups != null) {
          int j = i+1;
          while (j < l(replacer.outParagraphs) && !cic(
            replacer.fullText(replacer.outParagraphs.get(j)),
            "[END]")) ++j;
          if (j >= l(replacer.outParagraphs)) continue with print("warn: no [END] found for " + text);
          S key = first(groups);
          S condition = second(groups);
          bool result = evaluateFormCondition(key, condition);
          if (result) {
            replacer.outParagraphs.remove(j);
            replacer.outParagraphs.remove(i);
          } else
            removeSubList(replacer.outParagraphs, i, j+1);
          --i;
        }
      }
      
      // take out runs of empty paragraphs
      for (int i = 0; i < l(replacer.outParagraphs); i++) {
        int j = i+1;
        while (j < l(replacer.outParagraphs) && regexpMatchesIC(
          optSpace,
          replacer.fullText(replacer.outParagraphs.get(j)))) ++j;
        
        if (j >= i+2) {
          print("Dropping empty paragraphs at " + i);
          removeSubList(replacer.outParagraphs, i+2, j);
        }
      }
    };
    
    replacer.run();
  }
  
  bool evaluateFormCondition(S key, S condition) {
    S value = getValue(key);
    bool result;
    new Matches m;
    if (swic(condition, "contains ", m))
      result = cic(value, m.rest());
    else if (eqic(condition, "not empty"))
      result = nempty(value);
    else
      result = eqic(value, condition);
    printVars_str(+key, +condition, +value, +result);
    ret result;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028433
Snippet name: WordDocumentTemplateProcessor
Eternal ID of this version: #1028433/4
Text MD5: 9ed2c45e65ba9c385d78f724783411e8
Transpilation MD5: d30626bbdea246eed06398ddafec0309
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-20 11:22:42
Source code size: 4668 bytes / 136 lines
Pitched / IR pitched: No / No
Views / Downloads: 198 / 499
Version history: 3 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)