2 * Thanks to Bent Andre Solheim for the improved header rendering.
4 package net.sourceforge.phpeclipse.wiki.renderer;
6 import java.io.IOException;
7 import java.io.StringWriter;
8 import java.util.Properties;
10 import net.sourceforge.phpeclipse.wiki.builder.BuilderRenderEngine;
11 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
12 import net.sourceforge.phpeclipse.wiki.preferences.Util;
14 import org.apache.velocity.Template;
15 import org.apache.velocity.VelocityContext;
16 import org.apache.velocity.app.VelocityEngine;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.radeox.api.engine.context.InitialRenderContext;
21 import org.radeox.engine.context.BaseInitialRenderContext;
22 import org.radeox.macro.MacroRepository;
24 public final class WikipediaContentRenderer extends AbstractContentRenderer {
25 public static final String BULLET_MARKUP = "*";
27 public static final String ALT_BULLET_MARKUP = "-";
29 protected InitialRenderContext context;
31 protected BuilderRenderEngine engine;
33 protected void initialise() {
37 // protected void appendHeader(String line) {
38 // String header = getHeaderText(line);
39 // String headerStartTag = "<h1 " + getHeaderCss(getHeaderSize(line)) + ">";
40 // getBuffer().append(headerStartTag);
41 // getBuffer().append(header);
42 // getBuffer().append("</h1>");
45 // private int getHeaderSize(String line) {
48 // while (index < line.length() && line.charAt(index) != ' ') {
49 // if (line.charAt(index) == '1') {
57 // private String getHeaderCss(int size) {
58 // int max = Math.min(3, size);
59 // StringBuffer css = new StringBuffer();
60 // css.append("class=\"").append("heading");
61 // for (int i = 0; i < max; i++) {
65 // return css.toString();
68 // protected String getHeaderText(String line) {
69 // return line.substring(line.indexOf(' ')).trim();
72 protected int getListDepth(String line) {
73 char bullet = getBullet(line);
74 for (int i = 0; i < line.length(); i++) {
75 if (line.charAt(i) != bullet) {
82 private char getBullet(String line) {
83 char bullet = line.startsWith(WikipediaContentRenderer.BULLET_MARKUP) ? WikipediaContentRenderer.BULLET_MARKUP.charAt(0)
84 : WikipediaContentRenderer.ALT_BULLET_MARKUP.charAt(0);
88 protected boolean isHeader(String line) {
89 return line.startsWith("1") && line.indexOf(' ') > 0;
92 protected boolean isList(String line) {
93 return !line.trim().equals("----")
94 && (line.startsWith(WikipediaContentRenderer.BULLET_MARKUP) || line.startsWith(WikipediaContentRenderer.ALT_BULLET_MARKUP));
97 protected boolean process(String line) {
98 if (line.trim().matches("^----*$")) {
105 protected String processTags(String line) {
106 line = line.replaceAll("\\\\", "<br>");
107 line = extractMacroMarkup(line);
108 line = replacePair(line, "__", "<b>", "</b>");
109 line = replacePair(line, "~~", "<i>", "</i>");
110 line = replacePair(line, "--", "<strike>", "</strike>");
114 private String extractMacroMarkup(String line) {
115 return line.replaceAll("\\{.*?\\}", "");
118 protected String getListtext(String line) {
119 return line.substring(getListDepth(line)).trim();
122 protected String getTableTag() {
123 return "<table class=\"wiki-table\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
126 public final void render(String templateFileName, String content, StringBuffer buf, int level, boolean completeHTMLPage) {
128 if (templateFileName == null||templateFileName.equals("")) {
129 if (completeHTMLPage) {
132 StringWriter writer = new StringWriter();
134 engine.setLevel(level);
135 engine.render(writer, content, context);
137 } catch (IOException e) {
140 if (completeHTMLPage) {
144 StringWriter writer = new StringWriter();
145 engine.setLevel(level);
146 engine.render(writer, content, context);
149 IPath path = new Path(templateFileName);
150 String fileName = path.toString();
151 String pathName = fileName;
152 index = fileName.lastIndexOf('/');
154 pathName = new Path(fileName.substring(0,index)).toOSString();
155 fileName = fileName.substring(index+1);
157 Properties p = new Properties();
158 p.setProperty("resource.loader", "file");
159 p.setProperty("file.resource.loader.description", "Default Wikipedia Plugin Loader");
160 p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
161 p.setProperty("file.resource.loader.path", pathName);
162 p.setProperty("file.resource.loader.cache", "false");
163 p.setProperty("file.resource.loader.modificationCheckInterval", "0");
164 VelocityEngine ve = new VelocityEngine();
167 VelocityContext velocityContext = new VelocityContext();
168 velocityContext.put("content", String.valueOf(writer) );
169 String localCssUrl = Util.getLocalCssUrl(fProject);
170 velocityContext.put("localcss", localCssUrl );
171 String exportCssUrl = Util.getExportCssUrl(fProject);
172 velocityContext.put("exportcss", exportCssUrl );
173 Template template = ve.getTemplate(fileName);
175 StringWriter sw = new StringWriter();
176 template.merge(velocityContext, sw);
179 } catch (Exception e) {
180 WikiEditorPlugin.getDefault().log(buf.toString());
181 WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
182 // return "<html><body><p>" + e.getLocalizedMessage() + "</p></body></html>";
189 * @see net.sourceforge.phpeclipse.wiki.renderer.IContentRenderer#setProject(org.eclipse.core.resources.IProject)
191 public void setProject(IProject project) {
192 super.setProject(project);
193 context = new BaseInitialRenderContext();
194 engine = new BuilderRenderEngine(project, context);
195 context.setRenderEngine(engine);
196 MacroRepository repository = MacroRepository.getInstance();
197 repository.setInitialContext(context);