1 31 package org.blojsom.plugin.markdown; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.blog.Blog; 36 import org.blojsom.blog.Entry; 37 import org.blojsom.plugin.Plugin; 38 import org.blojsom.plugin.PluginException; 39 import org.blojsom.util.BlojsomConstants; 40 import org.blojsom.util.BlojsomUtils; 41 42 import javax.servlet.ServletConfig ; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.io.*; 46 import java.util.Map ; 47 48 58 public class MarkdownPlugin implements Plugin { 59 60 private Log _logger = LogFactory.getLog(MarkdownPlugin.class); 61 62 65 private static final String METADATA_RUN_MARKDOWN = "run-markdown"; 66 67 70 private static final String MARKDOWN_EXTENSION = ".markdown"; 71 72 75 private static final String PLUGIN_MARKDOWN_EXECUTION_IP = "plugin-markdown-execution"; 76 77 private String _markdownExecution; 78 private ServletConfig _servletConfig; 79 80 85 public void init() throws PluginException { 86 _markdownExecution = _servletConfig.getInitParameter(PLUGIN_MARKDOWN_EXECUTION_IP); 87 88 if (BlojsomUtils.checkNullOrBlank(_markdownExecution)) { 89 if (_logger.isErrorEnabled()) { 90 _logger.error("No Markdown execution string provided. Use initialization parameter: " + PLUGIN_MARKDOWN_EXECUTION_IP); 91 } 92 } 93 } 94 95 100 public void setServletConfig(ServletConfig servletConfig) { 101 _servletConfig = servletConfig; 102 } 103 104 115 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 116 if (!BlojsomUtils.checkNullOrBlank(_markdownExecution)) { 117 for (int i = 0; i < entries.length; i++) { 118 Entry entry = entries[i]; 119 120 if ((entry.getPostSlug().endsWith(MARKDOWN_EXTENSION) || BlojsomUtils.checkMapForKey(entry.getMetaData(), METADATA_RUN_MARKDOWN))) 121 { 122 try { 123 Process process = Runtime.getRuntime().exec(_markdownExecution); 124 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), BlojsomConstants.UTF8)); 125 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), BlojsomConstants.UTF8)); 126 bw.write(entry.getDescription()); 127 bw.close(); 128 String input; 129 StringBuffer collectedDescription = new StringBuffer (); 130 131 while ((input = br.readLine()) != null) { 132 collectedDescription.append(input).append(BlojsomConstants.LINE_SEPARATOR); 133 } 134 135 entry.setDescription(collectedDescription.toString()); 136 br.close(); 137 } catch (IOException e) { 138 if (_logger.isErrorEnabled()) { 139 _logger.error(e); 140 } 141 } 142 } 143 } 144 } 145 146 return entries; 147 } 148 149 154 public void cleanup() throws PluginException { 155 } 156 157 162 public void destroy() throws PluginException { 163 } 164 } | Popular Tags |