BrightSide Workbench Full Report + Source Code
WikiCompiler.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.parser.wiki;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.elephant.context.IConstructor;
26 import org.turro.elephant.impl.util.Parser;
27 import org.turro.wiki.WikiParser;
28 import org.turro.wiki.WikiResult;
29 import org.turro.wiki.parsing.IWikiResolver;
30 
35 public class WikiCompiler {
36 
37  public String html() {
38  String html;
39  if(elWiki) {
40  WikiResult result = WikiParser.instance()
41  .onProcessor(wmacro -> WikiProcessors.instance().constructor(constructor()).resolve(wmacro))
42  .addResolvers(resolvers)
43  .parse(source);
44  html = result.getHtml();
45  } else {
46  Parser parser = new Parser("$wiki\n" + source);
47  parser.setResolver(w -> {
48  for(IWikiResolver resolver : resolvers) {
49  w = resolver.apply(w);
50  }
51  return w;
52  });
53  html = parser.parse(constructor());
54  }
55  return (preview ? styleSheets() : "") + html;
56  }
57 
58  /* ElWiki */
59 
60  private boolean elWiki;
61 
62  public WikiCompiler elWiki() {
63  elWiki = true;
64  return this;
65  }
66 
67  public WikiCompiler legacy() {
68  elWiki = false;
69  return this;
70  }
71 
72  /* Constructor */
73 
74  private IConstructor constructor;
75 
76  public WikiCompiler constructor(IConstructor constructor) {
77  this.constructor = constructor;
78  return this;
79  }
80 
81  private IConstructor constructor() {
82  if(constructor == null) {
83  constructor = Application.getApplication().getConstructor();
84  }
85  return constructor;
86  }
87 
88  /* Resolvers */
89 
90  private final List<IWikiResolver> resolvers = new ArrayList<>();
91 
92  public WikiCompiler resolver(IWikiResolver resolver) {
93  if(resolver != null) resolvers.add(resolver);
94  return this;
95  }
96 
97  /* Style sheets */
98 
99  private String styleSheets() {
100  return "<link rel=\"stylesheet\" type=\"text/css\" href=\"" +
101  ElephantContext.getRootWebPath() + "/_internal/css/elephant-theme.css" +
102  "\"/>\n";
103  }
104 
105  /* Factory */
106 
107  public static WikiCompiler source(String source) {
108  return new WikiCompiler(source, false);
109  }
110 
111  public static WikiCompiler preview(String source) {
112  return new WikiCompiler(source, true);
113  }
114 
115  private final String source;
116  private final boolean preview;
117 
118  private WikiCompiler(String source, boolean preview) {
119  this.source = source;
120  this.preview = preview;
121  this.elWiki = WikiParser.isElWiki(source);
122  }
123 
124 }
void setResolver(ExternalResolver resolver)
Definition: Parser.java:82
WikiCompiler resolver(IWikiResolver resolver)
static WikiCompiler preview(String source)
static WikiCompiler source(String source)
WikiCompiler constructor(IConstructor constructor)