| 1 31 package org.blojsom.plugin.excerpt; 32 33 import org.blojsom.blog.Blog; 34 import org.blojsom.blog.Entry; 35 import org.blojsom.plugin.Plugin; 36 import org.blojsom.plugin.PluginException; 37 import org.blojsom.util.BlojsomUtils; 38 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.util.Map ; 42 import java.util.regex.Matcher ; 43 import java.util.regex.Pattern ; 44 45 53 public class ExcerptPlugin implements Plugin { 54 55 private static final String SHOWME_PARAM = "smm"; 56 57 private static final String EXCERPT_EXPRESSION = "(^|\\s).*<div class=\"excerpt\">(.*)</div>.*"; 58 private static final String SHOWME_START = "$2 <a class=\"smm\" HREF=\""; 59 private static final String SHOWME_FINISH = "&" + SHOWME_PARAM + "=y\">Read More</a>"; 60 61 64 public ExcerptPlugin() { 65 } 66 67 73 public void init() throws PluginException { 74 } 75 76 87 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 88 String showme = BlojsomUtils.getRequestValue(SHOWME_PARAM, httpServletRequest); 89 if (showme == null) { 90 processEntries(blog, entries); 91 } 92 93 return entries; 94 } 95 96 102 private void processEntries(Blog blog, Entry[] entries) { 103 Pattern p = Pattern.compile(EXCERPT_EXPRESSION, Pattern.DOTALL); 104 105 for (int i = 0; i < entries.length; i++) { 106 Entry entry = entries[i]; 107 String originalDescription = entry.getDescription(); 108 Matcher m = p.matcher(originalDescription); 109 if (m.matches()) { 110 StringBuffer newDescription = new StringBuffer (m.replaceAll(SHOWME_START)); 111 newDescription.append(blog.getBlogURL()).append("?permalink=").append(entry.getPostSlug()); 112 newDescription.append(SHOWME_FINISH); 113 entry.setDescription(newDescription.toString()); 114 } 115 } 116 } 117 118 124 public void cleanup() throws PluginException { 125 } 126 127 133 public void destroy() throws PluginException { 134 } 135 } 136 | Popular Tags |