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).

1  
sclass WordDocumentTemplateProcessor {
2  
  File inFile, outFile;
3  
  new WordDocumentTextReplacer2 replacer;
4  
  
5  
  swappable S getValue(S key) { null; }
6  
  
7  
  run {
8  
    replacer.inFile = inFile;
9  
    replacer.outFile = outFile;
10  
    S optSpace = regexp_spaceIncludingNbsp() + "*";
11  
    S space = regexp_spaceIncludingNbsp() + "+";
12  
    replacer.processParagraph = runs -> {
13  
      // replace content
14  
      replacer.regexpReplacement(runs,
15  
        "\\[" + optSpace
16  
          + "(\\d+[a-z]*)" // key
17  
          + regexp_negativeLookahead(space + "if" + space)
18  
          + "[:.]" // separator
19  
          + optSpace
20  
          + "(.*?)" // contents
21  
          + "\\]",
22  
        (text, groups) -> {
23  
          new Matches m;
24  
          S key = first(groups);
25  
          S value = getValue(key);
26  
          ret or(value, "undefined");
27  
        });
28  
  
29  
      // conditional paragraph parts
30  
      replacer.regexpReplacement(runs,
31  
        "\\[" + optSpace
32  
          + "(\\d+[a-z]*)" // key
33  
          + space + "if" + space
34  
          + "(.*?)" // condition
35  
          + "[:.]"  // end of condition
36  
          + optSpace
37  
          + "(.*?)" // contents
38  
          + "\\]",
39  
        (text, groups) -> {
40  
          S key = first(groups);
41  
          S condition = regexpReplace_direct(second(groups), space, " ");
42  
          S contents = third(groups);
43  
          
44  
          bool result = evaluateFormCondition(key, condition);
45  
          ret result ? contents : "";
46  
        });
47  
      
48  
      // remove double commas
49  
      replacer.regexpReplacement(runs, "," + regexp_spaceIncludingNbsp() + "*(?=,)", (text, groups) -> "");
50  
    };
51  
    
52  
    replacer.postprocess = r {
53  
      // bullet points
54  
      for (int i = 0; i < l(replacer.outParagraphs); i++) {
55  
        L<WordDocumentTextReplacer2.OutRun> para = replacer.outParagraphs.get(i);
56  
        S text = replacer.fullText(para);
57  
        LS groups = regexpMatchGroupsIC(optSpace
58  
          + "\\["
59  
          + "(\\d+[a-z]*)" // key
60  
          + space + "bullet" + space + "points"
61  
          + "\\]" + optSpace,
62  
          text);
63  
        if (groups != null) {
64  
          S key = first(groups);
65  
          S value = getValue(key);
66  
          replacer.outParagraphs.remove(i);
67  
          LS items = trimAll(splitAt(value, "|"));
68  
          printVars_str("bullet points", +value);
69  
          for (S item : items) {
70  
            print("Adding item at " + i + ": " + item);
71  
            replacer.outParagraphs.add(i++, ll(new WordDocumentTextReplacer2.OutRun(first(para).run, unicode_bullet() + " " + item)));
72  
          }
73  
          --i;
74  
        }
75  
      }
76  
      
77  
      // process conditional paragraphs
78  
      for (int i = l(replacer.outParagraphs)-1; i >= 0; i--) {
79  
        L<WordDocumentTextReplacer2.OutRun> para = replacer.outParagraphs.get(i);
80  
        S text = replacer.fullText(para);
81  
        LS groups = regexpMatchGroupsIC(optSpace
82  
          + "\\[START" + space
83  
          + "(\\d+[a-z]*)" // key
84  
          + space + "if" + space
85  
          + "(.*?)" // condition
86  
          + "\\]" + optSpace,
87  
          text);
88  
        if (groups != null) {
89  
          int j = i+1;
90  
          while (j < l(replacer.outParagraphs) && !cic(
91  
            replacer.fullText(replacer.outParagraphs.get(j)),
92  
            "[END]")) ++j;
93  
          if (j >= l(replacer.outParagraphs)) continue with print("warn: no [END] found for " + text);
94  
          S key = first(groups);
95  
          S condition = second(groups);
96  
          bool result = evaluateFormCondition(key, condition);
97  
          if (result) {
98  
            replacer.outParagraphs.remove(j);
99  
            replacer.outParagraphs.remove(i);
100  
          } else
101  
            removeSubList(replacer.outParagraphs, i, j+1);
102  
          --i;
103  
        }
104  
      }
105  
      
106  
      // take out runs of empty paragraphs
107  
      for (int i = 0; i < l(replacer.outParagraphs); i++) {
108  
        int j = i+1;
109  
        while (j < l(replacer.outParagraphs) && regexpMatchesIC(
110  
          optSpace,
111  
          replacer.fullText(replacer.outParagraphs.get(j)))) ++j;
112  
        
113  
        if (j >= i+2) {
114  
          print("Dropping empty paragraphs at " + i);
115  
          removeSubList(replacer.outParagraphs, i+2, j);
116  
        }
117  
      }
118  
    };
119  
    
120  
    replacer.run();
121  
  }
122  
  
123  
  bool evaluateFormCondition(S key, S condition) {
124  
    S value = getValue(key);
125  
    bool result;
126  
    new Matches m;
127  
    if (swic(condition, "contains ", m))
128  
      result = cic(value, m.rest());
129  
    else if (eqic(condition, "not empty"))
130  
      result = nempty(value);
131  
    else
132  
      result = eqic(value, condition);
133  
    printVars_str(+key, +condition, +value, +result);
134  
    ret result;
135  
  }
136  
}

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: 139 / 410
Version history: 3 change(s)
Referenced in: [show references]