| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
package net.sourceforge.pebble.decorator; |
| 34 | |
|
| 35 | |
import net.sourceforge.pebble.domain.BlogEntry; |
| 36 | |
import net.sourceforge.pebble.domain.Blog; |
| 37 | |
import net.sourceforge.pebble.domain.StaticPage; |
| 38 | |
import net.sourceforge.pebble.util.UrlRewriter; |
| 39 | |
import net.sourceforge.pebble.api.decorator.ContentDecoratorContext; |
| 40 | |
import org.radeox.api.engine.RenderEngine; |
| 41 | |
import org.radeox.api.engine.WikiRenderEngine; |
| 42 | |
import org.radeox.api.engine.context.InitialRenderContext; |
| 43 | |
import org.radeox.api.engine.context.RenderContext; |
| 44 | |
import org.radeox.engine.BaseRenderEngine; |
| 45 | |
import org.radeox.engine.context.BaseInitialRenderContext; |
| 46 | |
|
| 47 | |
import java.util.regex.Matcher; |
| 48 | |
import java.util.regex.Pattern; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 2720 | public class RadeoxDecorator extends ContentDecoratorSupport { |
| 57 | |
|
| 58 | |
private static final String WIKI_START_TAG = "<wiki>"; |
| 59 | |
private static final String WIKI_END_TAG = "</wiki>"; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) { |
| 68 | 28 | InitialRenderContext initialContext = new BaseInitialRenderContext(); |
| 69 | 28 | initialContext.set(RenderContext.INPUT_LOCALE, getBlog().getLocale()); |
| 70 | 28 | RenderEngine engineWithContext = new RadeoxWikiRenderEngine(initialContext, getBlog()); |
| 71 | |
|
| 72 | 28 | blogEntry.setExcerpt(wikify(blogEntry.getExcerpt(), engineWithContext, initialContext)); |
| 73 | 28 | blogEntry.setBody(wikify(blogEntry.getBody(), engineWithContext, initialContext)); |
| 74 | 28 | } |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public void decorate(ContentDecoratorContext context, StaticPage staticPage) { |
| 83 | 0 | InitialRenderContext initialContext = new BaseInitialRenderContext(); |
| 84 | 0 | initialContext.set(RenderContext.INPUT_LOCALE, getBlog().getLocale()); |
| 85 | 0 | RenderEngine engineWithContext = new RadeoxWikiRenderEngine(initialContext, getBlog()); |
| 86 | |
|
| 87 | 0 | staticPage.setBody(wikify(staticPage.getBody(), engineWithContext, initialContext)); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
private String wikify(String content, RenderEngine renderEngine, InitialRenderContext renderContext) { |
| 91 | |
|
| 92 | 56 | if (content == null || content.length() == 0) { |
| 93 | 36 | return ""; |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | 20 | Pattern p = Pattern.compile(WIKI_START_TAG + ".+?" + WIKI_END_TAG, |
| 101 | |
Pattern.CASE_INSENSITIVE | Pattern.DOTALL); |
| 102 | 20 | Matcher m = p.matcher(content); |
| 103 | |
|
| 104 | |
|
| 105 | 20 | while (m.find()) { |
| 106 | 0 | int start = m.start(); |
| 107 | 0 | int end = m.end(); |
| 108 | |
|
| 109 | |
|
| 110 | 0 | String textToWikify = content.substring(start, end); |
| 111 | 0 | textToWikify = textToWikify.substring(WIKI_START_TAG.length(), textToWikify.length() - WIKI_END_TAG.length()); |
| 112 | 0 | textToWikify = renderEngine.render(textToWikify, renderContext); |
| 113 | |
|
| 114 | |
|
| 115 | 0 | content = content.substring(0, start) + textToWikify + content.substring(end, content.length()); |
| 116 | 0 | m = p.matcher(content); |
| 117 | 0 | } |
| 118 | |
|
| 119 | 20 | return content; |
| 120 | |
} |
| 121 | |
|
| 122 | |
} |
| 123 | |
|
| 124 | |
class RadeoxWikiRenderEngine extends BaseRenderEngine implements WikiRenderEngine { |
| 125 | |
|
| 126 | |
private Blog blog; |
| 127 | |
|
| 128 | |
public RadeoxWikiRenderEngine(InitialRenderContext context, Blog blog) { |
| 129 | 28 | super(context); |
| 130 | 28 | context.setRenderEngine(this); |
| 131 | 28 | this.blog = blog; |
| 132 | 28 | } |
| 133 | |
|
| 134 | |
public boolean exists(String name) { |
| 135 | 0 | return blog.getStaticPageIndex().contains(name); |
| 136 | |
} |
| 137 | |
|
| 138 | |
public boolean showCreate() { |
| 139 | 0 | return true; |
| 140 | |
} |
| 141 | |
|
| 142 | |
public void appendLink(StringBuffer buffer, String name, String view) { |
| 143 | 0 | appendLink(buffer, name, view, null); |
| 144 | 0 | } |
| 145 | |
|
| 146 | |
public void appendLink(StringBuffer buffer, String name, String view, String anchor) { |
| 147 | 0 | buffer.append("<a href=\""); |
| 148 | 0 | StringBuffer url = new StringBuffer(); |
| 149 | 0 | url.append(blog.getUrl()).append("pages/").append(name).append(".html"); |
| 150 | 0 | if (anchor != null && anchor.trim().length() > 0) { |
| 151 | 0 | url.append("#"); |
| 152 | 0 | url.append(anchor); |
| 153 | |
} |
| 154 | 0 | buffer.append(UrlRewriter.doRewrite(url.toString())); |
| 155 | 0 | buffer.append("\">"); |
| 156 | 0 | buffer.append(view); |
| 157 | 0 | buffer.append("</a>"); |
| 158 | 0 | } |
| 159 | |
|
| 160 | |
public void appendCreateLink(StringBuffer buffer, String name, String view) { |
| 161 | 0 | buffer.append("<a href=\"addStaticPage.secureaction?name="); |
| 162 | 0 | buffer.append(name); |
| 163 | 0 | buffer.append("\">"); |
| 164 | 0 | buffer.append(view); |
| 165 | 0 | buffer.append("</a><sup>?</sup>"); |
| 166 | 0 | } |
| 167 | |
} |