18 package org.turro.elephant.impl.util;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import javax.servlet.ServletException;
27 import org.turro.string.Strings;
28 import org.jsoup.Jsoup;
29 import org.turro.annotation.ExternalParser;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.html.HtmlWhitelist;
33 import org.turro.i18n.I_;
34 import org.turro.parser.IParser;
35 import org.turro.parser.processor.MacroProcessors;
36 import org.turro.reflection.Instances;
37 import org.turro.syntax.parser.ExternalResolver;
38 import org.turro.syntax.parser.HtmlParser;
39 import org.turro.syntax.wiki.WikiDef;
45 public class Parser implements ExternalResolver {
48 private StringBuffer buffer;
49 private ExternalResolver resolver;
60 public Parser(String text, String end) {
63 p = text.indexOf(end);
66 buffer =
new StringBuffer(text.substring(0, p));
69 buffer =
new StringBuffer(text);
72 if(text.startsWith(
"$wiki\n * ") || text.startsWith(
"$wiki\n 1 ")) {
73 buffer.insert(6,
"\n");
77 public static String
toWiki(String text) {
78 text = Jsoup.clean(text, HtmlWhitelist.wikiSafe());
79 return new HtmlParser(text).parse();
83 this.resolver = resolver;
91 this.constructor = constructor;
92 WikiDef wikiDef =
new WikiDef();
93 if(resolver !=
null) wikiDef.addResolver(resolver);
94 wikiDef.addResolver(
this);
95 return wikiDef.parseBuffer(buffer);
99 WikiDef wikiDef =
new WikiDef();
100 return wikiDef.parseBuffer(buffer);
104 if(constructor ==
null || line.indexOf(
"[thumb") == -1)
return line;
105 Pattern pat = Pattern.compile(
"\\[thumb (.+?)\\:([0-9]+?)(\\:[RL])?\\]");
106 Matcher mat = pat.matcher(line);
107 StringBuffer result =
new StringBuffer();
111 dim = Integer.parseInt(mat.group(2));
112 }
catch(Exception ex) {}
113 mat.appendReplacement(
115 "<a target=\"_blank\" href=\"" + mat.group(1) +
"\">" +
116 "<img border=\"0\" src=\"" +
128 mat.appendTail(result);
129 return result.toString();
133 if(constructor ==
null || line.indexOf(
"macro") == -1)
return line;
134 Pattern pat = Pattern.compile(
"macro ?\\((.*?)\\)");
135 Matcher mat = pat.matcher(line);
136 StringBuffer result =
new StringBuffer();
139 tmp =
I_.
get(mat.group(1));
140 if(tmp ==
null) tmp = mat.group(1);
141 mat.appendReplacement(result, tmp);
143 mat.appendTail(result);
144 return result.toString();
149 if(!constructor.
isInRole(
"inc:new")) {
150 text = text.replaceAll(
"\\{\\@inc",
"{@---");
152 text = text.replaceAll(
153 "grepository ?\\((.*?)\\)",
155 "/_internal/repository$1"
158 text = text.replaceAll(
159 "repository ?\\((.*?)\\)",
162 "/_internal/repository$1"
165 text = text.replaceAll(
166 "pubimage ?\\((.*?)\\)",
168 "/_internal/UserFiles/Image$1"
170 text = text.replaceAll(
171 "pubflash ?\\((.*?)\\)",
173 "/_internal/UserFiles/Flash$1"
175 text = text.replaceAll(
176 "download ?\\((.*?)\\)",
179 text = text.replaceAll(
"<verbatim>",
"<pre>");
180 text = text.replaceAll(
"</verbatim>",
"</pre>");
183 text = text.replaceAll(
"\\[\\[(.*?)\\]\\[(.*?)\\]\\]",
"<a href='$1'>$2</a>");
184 text = text.replaceAll(
"\\[new\\[(.*?)\\]\\[(.*?)\\]\\]",
"<a class='newwindow' target='_blank' href='$1'>$2</a>");
185 text = text.replaceAll(
"\\[ext\\[(.*?)\\]\\[(.*?)\\]\\]",
"<a class='externallink' target='_blank' href='$1'>$2</a>");
186 text = text.replaceAll(
"\\[\\[(.*?)\\]\\]",
"<a href='$1'>$1</a>");
187 text = text.replaceAll(
"\\[\\[(.*?)\\](.*?)\\]",
"<a href='$1'>$2</a>");
188 text = text.replaceAll(
"\\[new\\[(.*?)\\](.*?)\\]",
"<a class='newwindow' target='_blank' href='$1'>$2</a>");
189 text = text.replaceAll(
"\\[new\\[(.*?)\\]\\]",
"<a class='newwindow' target='_blank' href='$1'>$1</a>");
190 text = text.replaceAll(
"\\[ext\\[(.*?)\\](.*?)\\]",
"<a class='externallink' target='_blank' href='$1'>$2</a>");
191 text = text.replaceAll(
"\\[ext\\[(.*?)\\]\\]",
"<a class='externallink' target='_blank' href='$1'>$1</a>");
192 text = text.replaceAll(
"\\[color\\((.*?)\\) (.*?)\\]",
"<span style='color:$1'>$2</span>");
194 text = text.replaceAll(
"\\[frame ([^,]*?),([^,]*?),(.*?)\\]",
"<iframe src='$1' style='width:$2;height:$3;border:0px solid gray'></iframe>");
195 text = text.replaceAll(
"\\%BR\\%",
"<br/>\n");
202 if(text.indexOf(
"{@") > -1) {
203 text = processNestedMacros(constructor, text);
206 while((p = text.indexOf(
"{@")) > -1) {
207 e = text.indexOf(
'}', p + 2);
209 tmp = text.substring(p + 2, e);
210 out.write(text.substring(0, p));
212 text = text.substring(e + 1);
221 private static String processNestedMacros(
IConstructor constructor, String text)
throws ServletException, IOException {
222 List<String> l = Strings.extractAll(text,
"\\{\\@[^\\{\\@]*(\\{\\@[^\\}]*\\})");
224 text = text.replaceAll(Strings.literal(s),
processMacroString(constructor, s.substring(2, s.length() - 1)));
230 StringWriter sw =
new StringWriter();
231 PrintWriter pw =
new PrintWriter(sw);
233 return sw.toString();
237 String tokens[] = macro.split(
"(?<!\\/)(\\:)");
242 if(sequence ==
null || !sequence.contains(
"$"))
return sequence;
243 StringBuilder sb =
new StringBuilder(sequence);
245 while((p = sb.indexOf(
"$", start)) > -1) {
249 return sb.toString();
256 if(iParser.execute(constructor, out, tokens)) {
static String getRootResourcePath()
static String getRootWebPath()
static String replaceEmoticons(IConstructor constructor, String text)
static void processMacros(IConstructor constructor, PrintWriter out, String text)
static String toWiki(String text)
static String escapeGrouping(String sequence)
Parser(String text, String end)
String parseMacros(String line)
String resolve(String text)
String parse(IConstructor constructor)
static void processMacro(IConstructor constructor, PrintWriter out, String macro)
static String processMacroString(IConstructor constructor, String macro)
static void executeExternalParsers(IConstructor constructor, PrintWriter out, String[] tokens)
String parseThumb(String line)
void setResolver(ExternalResolver resolver)
String getThumb(String file, int maxDim, String rotate)
static String get(String msg)
static MacroProcessors full()
HttpServletRequest getRequest()
String getParameter(String param)
boolean isInRole(String role)