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;
9 import net.sourceforge.phpeclipse.wiki.builder.BuilderRenderEngine;
10 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
12 import org.eclipse.core.resources.IProject;
13 import org.radeox.api.engine.RenderEngine;
14 import org.radeox.api.engine.context.InitialRenderContext;
15 import org.radeox.engine.context.BaseInitialRenderContext;
16 import org.radeox.macro.MacroRepository;
18 public final class WikipediaContentRenderer extends AbstractContentRenderer {
19 public static final String BULLET_MARKUP = "*";
21 public static final String ALT_BULLET_MARKUP = "-";
23 protected InitialRenderContext context;
25 protected BuilderRenderEngine engine;
27 protected void initialise() {
31 protected void appendHeader(String line) {
32 String header = getHeaderText(line);
33 String headerStartTag = "<h1 " + getHeaderCss(getHeaderSize(line)) + ">";
34 getBuffer().append(headerStartTag);
35 getBuffer().append(header);
36 getBuffer().append("</h1>");
39 private int getHeaderSize(String line) {
42 while (index < line.length() && line.charAt(index) != ' ') {
43 if (line.charAt(index) == '1') {
51 private String getHeaderCss(int size) {
52 int max = Math.min(3, size);
53 StringBuffer css = new StringBuffer();
54 css.append("class=\"").append("heading");
55 for (int i = 0; i < max; i++) {
59 return css.toString();
62 protected String getHeaderText(String line) {
63 return line.substring(line.indexOf(' ')).trim();
66 protected int getListDepth(String line) {
67 char bullet = getBullet(line);
68 for (int i = 0; i < line.length(); i++) {
69 if (line.charAt(i) != bullet) {
76 private char getBullet(String line) {
77 char bullet = line.startsWith(WikipediaContentRenderer.BULLET_MARKUP) ? WikipediaContentRenderer.BULLET_MARKUP.charAt(0)
78 : WikipediaContentRenderer.ALT_BULLET_MARKUP.charAt(0);
82 protected boolean isHeader(String line) {
83 return line.startsWith("1") && line.indexOf(' ') > 0;
86 protected boolean isList(String line) {
87 return !line.trim().equals("----")
88 && (line.startsWith(WikipediaContentRenderer.BULLET_MARKUP) || line.startsWith(WikipediaContentRenderer.ALT_BULLET_MARKUP));
91 protected boolean process(String line) {
92 if (line.trim().matches("^----*$")) {
99 protected String processTags(String line) {
100 line = line.replaceAll("\\\\", "<br>");
101 line = extractMacroMarkup(line);
102 line = replacePair(line, "__", "<b>", "</b>");
103 line = replacePair(line, "~~", "<i>", "</i>");
104 line = replacePair(line, "--", "<strike>", "</strike>");
108 private String extractMacroMarkup(String line) {
109 return line.replaceAll("\\{.*?\\}", "");
112 protected String getListtext(String line) {
113 return line.substring(getListDepth(line)).trim();
116 protected String getTableTag() {
117 return "<table class=\"wiki-table\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
120 public final void render(String content, StringBuffer buf, int level, boolean completeHTMLPage) {
123 if (completeHTMLPage) {
126 StringWriter writer = new StringWriter();
128 engine.setLevel(level);
129 engine.render(writer, content, context);
130 buffer.append(writer);
131 } catch (IOException e) {
134 if (completeHTMLPage) {
137 // return buffer.toString();
138 } catch (Exception e) {
139 WikiEditorPlugin.getDefault().log(buffer.toString());
140 WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
141 // return "<html><body><p>" + e.getLocalizedMessage() + "</p></body></html>";
148 * @see net.sourceforge.phpeclipse.wiki.renderer.IContentRenderer#setProject(org.eclipse.core.resources.IProject)
150 public void setProject(IProject project) {
151 super.setProject(project);
152 context = new BaseInitialRenderContext();
153 engine = new BuilderRenderEngine(project, context);
154 context.setRenderEngine(engine);
155 MacroRepository repository = MacroRepository.getInstance();
156 repository.setInitialContext(context);