1 31 package org.blojsom.plugin.footnote; 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.BlojsomUtils; 40 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.text.MessageFormat ; 44 import java.util.Iterator ; 45 import java.util.Map ; 46 import java.util.TreeMap ; 47 import java.util.regex.Matcher ; 48 import java.util.regex.Pattern ; 49 50 58 public class FootnotePlugin implements Plugin { 59 60 private static final String FOOTNOTE_METADATA = "footnote"; 61 private static final String REGEX_FOOTNOTE = "\\[(\\d+)\\]"; 62 private static final String FOOTNOTE_LINKAGE_FORMAT = "[{0}] {1}"; 63 private static final String FOOTNOTES_PROCESSED_METADATA = "footnotes-processed"; 64 65 private Log _logger = LogFactory.getLog(FootnotePlugin.class); 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 Pattern footnotePattern = Pattern.compile(REGEX_FOOTNOTE); 89 90 for (int i = 0; i < entries.length; i++) { 91 Entry entry = entries[i]; 92 String content = entry.getDescription(); 93 Matcher matcher = footnotePattern.matcher(content); 94 StringBuffer modifiedContent = new StringBuffer (); 95 Map footnotes = new TreeMap (); 96 97 while (matcher.find()) { 98 int footnoteIndex; 99 100 try { 101 footnoteIndex = Integer.parseInt(matcher.group(1)); 102 if (BlojsomUtils.checkMapForKey(entry.getMetaData(), FOOTNOTE_METADATA + "-" + footnoteIndex)) { 103 footnotes.put(Integer.toString(footnoteIndex), entry.getMetaData().get(FOOTNOTE_METADATA + "-" + footnoteIndex)); 104 } 105 106 matcher.appendReplacement(modifiedContent, "<sup id=\"footnoteref-" + footnoteIndex + "\"><a HREF=\"#footnote-" + footnoteIndex + "\">" + footnoteIndex + "</a></sup>"); 107 } catch (NumberFormatException e) { 108 if (_logger.isErrorEnabled()) { 109 _logger.error("Footnote index in post is not a valid integer [" + matcher.group(1) + "]"); 110 } 111 } 112 } 113 114 matcher.appendTail(modifiedContent); 115 116 if (!footnotes.isEmpty() && !BlojsomUtils.checkMapForKey(entry.getMetaData(), FOOTNOTES_PROCESSED_METADATA)) { 117 modifiedContent.append("<br/><br/>"); 118 modifiedContent.append("<div class=\"footnote\">"); 119 modifiedContent.append("<hr/>"); 120 modifiedContent.append("<ol>"); 121 Iterator footnotesIterator = footnotes.keySet().iterator(); 122 123 while (footnotesIterator.hasNext()) { 124 String footnoteIndex = (String ) footnotesIterator.next(); 125 modifiedContent.append("<li id=\"footnote-").append(footnoteIndex).append("\"><p>"); 126 modifiedContent.append(MessageFormat.format(FOOTNOTE_LINKAGE_FORMAT, new Object [] {footnoteIndex, entry.getMetaData().get(FOOTNOTE_METADATA + "-" + footnoteIndex)})); 127 modifiedContent.append("<a HREF=\"#footnoteref-").append(footnoteIndex).append("\" class=\"footnoteBackLink\" title=\"Jump back to footnote ").append(footnoteIndex).append(" in the text.\">"); 128 modifiedContent.append("↩</a></p></li>"); 129 } 130 131 modifiedContent.append("</ol>"); 132 modifiedContent.append("</div>"); 133 134 entry.getMetaData().put(FOOTNOTES_PROCESSED_METADATA, "true"); 135 } 136 137 entry.setDescription(modifiedContent.toString()); 138 } 139 140 return entries; 141 } 142 143 149 public void cleanup() throws PluginException { 150 } 151 152 158 public void destroy() throws PluginException { 159 } 160 161 } 162 | Popular Tags |