/*
* Thanks to Bent Andre Solheim for the improved header rendering.
*/
package net.sourceforge.phpeclipse.wiki.renderer;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;
import net.sourceforge.phpeclipse.wiki.builder.BuilderRenderEngine;
import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
import net.sourceforge.phpeclipse.wiki.preferences.Util;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.radeox.api.engine.context.InitialRenderContext;
import org.radeox.engine.context.BaseInitialRenderContext;
import org.radeox.macro.MacroRepository;
public final class WikipediaContentRenderer extends AbstractContentRenderer {
public static final String BULLET_MARKUP = "*";
public static final String ALT_BULLET_MARKUP = "-";
protected InitialRenderContext context;
protected BuilderRenderEngine engine;
protected void initialise() {
}
// protected void appendHeader(String line) {
// String header = getHeaderText(line);
// String headerStartTag = "
";
// getBuffer().append(headerStartTag);
// getBuffer().append(header);
// getBuffer().append("
");
// }
// private int getHeaderSize(String line) {
// int size = 0;
// int index = 0;
// while (index < line.length() && line.charAt(index) != ' ') {
// if (line.charAt(index) == '1') {
// size++;
// }
// index++;
// }
// return size;
// }
//
// private String getHeaderCss(int size) {
// int max = Math.min(3, size);
// StringBuffer css = new StringBuffer();
// css.append("class=\"").append("heading");
// for (int i = 0; i < max; i++) {
// css.append("-1");
// }
// css.append("\"");
// return css.toString();
// }
// protected String getHeaderText(String line) {
// return line.substring(line.indexOf(' ')).trim();
// }
protected int getListDepth(String line) {
char bullet = getBullet(line);
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) != bullet) {
return i;
}
}
return line.length();
}
private char getBullet(String line) {
char bullet = line.startsWith(WikipediaContentRenderer.BULLET_MARKUP) ? WikipediaContentRenderer.BULLET_MARKUP.charAt(0)
: WikipediaContentRenderer.ALT_BULLET_MARKUP.charAt(0);
return bullet;
}
protected boolean isHeader(String line) {
return line.startsWith("1") && line.indexOf(' ') > 0;
}
protected boolean isList(String line) {
return !line.trim().equals("----")
&& (line.startsWith(WikipediaContentRenderer.BULLET_MARKUP) || line.startsWith(WikipediaContentRenderer.ALT_BULLET_MARKUP));
}
protected boolean process(String line) {
if (line.trim().matches("^----*$")) {
appendHR();
return true;
}
return false;
}
protected String processTags(String line) {
line = line.replaceAll("\\\\", "
");
line = extractMacroMarkup(line);
line = replacePair(line, "__", "", "");
line = replacePair(line, "~~", "", "");
line = replacePair(line, "--", "", "");
return line;
}
private String extractMacroMarkup(String line) {
return line.replaceAll("\\{.*?\\}", "");
}
protected String getListtext(String line) {
return line.substring(getListDepth(line)).trim();
}
protected String getTableTag() {
return "";
}
public final void render(String templateFileName, String content, StringBuffer buf, int level, boolean completeHTMLPage) {
try {
if (templateFileName == null||templateFileName.equals("")) {
if (completeHTMLPage) {
appendHeader(buf);
}
StringWriter writer = new StringWriter();
try {
engine.setLevel(level);
engine.render(writer, content, context);
buf.append(writer);
} catch (IOException e) {
// never reach
}
if (completeHTMLPage) {
appendFooter(buf);
}
} else {
StringWriter writer = new StringWriter();
engine.setLevel(level);
engine.render(writer, content, context);
int index = 0;
IPath path = new Path(templateFileName);
String fileName = path.toString();
String pathName = fileName;
index = fileName.lastIndexOf('/');
if (index>=0) {
pathName = new Path(fileName.substring(0,index)).toOSString();
fileName = fileName.substring(index+1);
}
Properties p = new Properties();
p.setProperty("resource.loader", "file");
p.setProperty("file.resource.loader.description", "Default Wikipedia Plugin Loader");
p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
p.setProperty("file.resource.loader.path", pathName);
p.setProperty("file.resource.loader.cache", "false");
p.setProperty("file.resource.loader.modificationCheckInterval", "0");
VelocityEngine ve = new VelocityEngine();
ve.init(p);
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("content", String.valueOf(writer) );
String localCssUrl = Util.getLocalCssUrl(fProject);
velocityContext.put("localcss", localCssUrl );
String exportCssUrl = Util.getExportCssUrl(fProject);
velocityContext.put("exportcss", exportCssUrl );
Template template = ve.getTemplate(fileName);
StringWriter sw = new StringWriter();
template.merge(velocityContext, sw);
buf.append(sw);
}
} catch (Exception e) {
WikiEditorPlugin.getDefault().log(buf.toString());
WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
// return "" + e.getLocalizedMessage() + "
";
}
}
/*
* (non-Javadoc)
*
* @see net.sourceforge.phpeclipse.wiki.renderer.IContentRenderer#setProject(org.eclipse.core.resources.IProject)
*/
public void setProject(IProject project) {
super.setProject(project);
context = new BaseInitialRenderContext();
engine = new BuilderRenderEngine(project, context);
context.setRenderEngine(engine);
MacroRepository repository = MacroRepository.getInstance();
repository.setInitialContext(context);
}
}