You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2019/08/08 22:30:55 UTC
[jspwiki] 06/07: JSPWIKI-1114: Show only part of Weblog entry on
the overview page. Contributed by Ulf Dittmer - thanks!
This is an automated email from the ASF dual-hosted git repository.
juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 17e5e21abf25b4b34300e5fee2c157a29f05ddda
Author: juanpablo <ju...@apache.org>
AuthorDate: Fri Aug 9 00:28:47 2019 +0200
JSPWIKI-1114: Show only part of Weblog entry on the overview page. Contributed by Ulf Dittmer - thanks!
---
.../java/org/apache/wiki/plugin/WeblogPlugin.java | 82 ++++++++++++++++------
.../resources/plugin/PluginResources.properties | 1 +
.../resources/plugin/PluginResources_de.properties | 3 +
.../resources/plugin/PluginResources_es.properties | 7 +-
.../org/apache/wiki/plugin/WeblogPluginTest.java | 76 ++++++++++++++++++++
5 files changed, 144 insertions(+), 25 deletions(-)
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/plugin/WeblogPlugin.java b/jspwiki-main/src/main/java/org/apache/wiki/plugin/WeblogPlugin.java
index d9dd0e8..ae5e712 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/plugin/WeblogPlugin.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/plugin/WeblogPlugin.java
@@ -18,23 +18,6 @@
*/
package org.apache.wiki.plugin;
-import java.text.DateFormat;
-import java.text.MessageFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.ResourceBundle;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
import org.apache.log4j.Logger;
import org.apache.wiki.WikiContext;
import org.apache.wiki.WikiEngine;
@@ -52,6 +35,23 @@ import org.apache.wiki.preferences.Preferences;
import org.apache.wiki.preferences.Preferences.TimeFormat;
import org.apache.wiki.util.TextUtil;
+import java.text.DateFormat;
+import java.text.MessageFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.ResourceBundle;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
/**
* <p>Builds a simple weblog.
* The pageformat can use the following params:</p>
@@ -69,6 +69,7 @@ import org.apache.wiki.util.TextUtil;
* <li><b>pageformat</b> - What the entry pages should look like.</li>
* <li><b>startDate</b> - Date when to start. Format is "ddMMyy."</li>
* <li><b>maxEntries</b> - How many entries to show at most.</li>
+ * <li><b>preview</b> - How many characters of the text to show on the preview page.</li>
* </ul>
* <p>The "days" and "startDate" can also be sent in HTTP parameters,
* and the names are "weblog.days" and "weblog.startDate", respectively.</p>
@@ -106,6 +107,8 @@ public class WeblogPlugin
public static final String PARAM_MAXENTRIES = "maxEntries";
/** Parameter name for the page. Value is <tt>{@value}</tt>. */
public static final String PARAM_PAGE = "page";
+ /** Parameter name for the preview. Value is <tt>{@value}</tt>. */
+ public static final String PARAM_PREVIEW = "preview";
/** The attribute which is stashed to the WikiPage attributes to check if a page
* is a weblog or not. You may check for its presence.
@@ -283,7 +286,7 @@ public class WeblogPlugin
if( mgr.checkPermission( context.getWikiSession(),
new PagePermission(p, PagePermission.VIEW_ACTION) ) )
{
- addEntryHTML(context, entryFormat, hasComments, sb, p);
+ addEntryHTML(context, entryFormat, hasComments, sb, p, params);
}
}
@@ -308,8 +311,9 @@ public class WeblogPlugin
* @param entry
* @throws ProviderException
*/
- private void addEntryHTML(WikiContext context, DateFormat entryFormat, boolean hasComments, StringBuilder buffer, WikiPage entry)
- throws ProviderException
+ private void addEntryHTML(WikiContext context, DateFormat entryFormat, boolean hasComments,
+ StringBuilder buffer, WikiPage entry, Map<String, String> params)
+ throws ProviderException
{
WikiEngine engine = context.getEngine();
ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
@@ -352,7 +356,43 @@ public class WeblogPlugin
buffer.append("</div>\n");
buffer.append("<div class=\"weblogentrybody\">\n");
- buffer.append( html );
+ int preview = TextUtil.parseIntParameter(params.get(PARAM_PREVIEW), 0);
+ if (preview > 0)
+ {
+ //
+ // We start with the first 'preview' number of characters from the text,
+ // and then add characters to it until we get to a linebreak or a period.
+ // The idea is that cutting off at a linebreak is less likely
+ // to disturb the HTML and leave us with garbled output.
+ //
+ boolean hasBeenCutOff = false;
+ int cutoff = Math.min(preview, html.length());
+ while (cutoff < html.length())
+ {
+ if (html.charAt(cutoff) == '\r' || html.charAt(cutoff) == '\n')
+ {
+ hasBeenCutOff = true;
+ break;
+ }
+ else if (html.charAt(cutoff) == '.')
+ {
+ // we do want the period
+ cutoff++;
+ hasBeenCutOff = true;
+ break;
+ }
+ cutoff++;
+ }
+ buffer.append(html.substring(0, cutoff));
+ if (hasBeenCutOff)
+ {
+ buffer.append(" <a href=\""+entryCtx.getURL(WikiContext.VIEW, entry.getName())+"\">"+rb.getString("weblogentryplugin.more")+"</a>\n");
+ }
+ }
+ else
+ {
+ buffer.append(html);
+ }
buffer.append("</div>\n");
//
diff --git a/jspwiki-main/src/main/resources/plugin/PluginResources.properties b/jspwiki-main/src/main/resources/plugin/PluginResources.properties
index f953262..741110a 100644
--- a/jspwiki-main/src/main/resources/plugin/PluginResources.properties
+++ b/jspwiki-main/src/main/resources/plugin/PluginResources.properties
@@ -49,6 +49,7 @@ weblogentryplugin.newentry = <span class="icon-plus"></span> New entry
weblogentryplugin.postedby = Posted by {0}
weblogentryplugin.permalink = Permalink
weblogentryplugin.addcomment = <span class="icon-plus"></span> Add new comment ({0})
+weblogentryplugin.more = (more)
# InserPagePlugin
insertpage.more=More...
diff --git a/jspwiki-main/src/main/resources/plugin/PluginResources_de.properties b/jspwiki-main/src/main/resources/plugin/PluginResources_de.properties
index 486ef25..4e97379 100644
--- a/jspwiki-main/src/main/resources/plugin/PluginResources_de.properties
+++ b/jspwiki-main/src/main/resources/plugin/PluginResources_de.properties
@@ -62,6 +62,9 @@ currenttimeplugin.badformat=Du hast ein ung
# WeblogEntryPlugin
weblogentryplugin.newentry=Neuer Eintrag
+weblogentryplugin.postedby = Von {0}
+weblogentryplugin.addcomment = <span class="icon-plus"></span> Kommentieren ({0})
+weblogentryplugin.more = (mehr)
# ReferringPagesPlugin
diff --git a/jspwiki-main/src/main/resources/plugin/PluginResources_es.properties b/jspwiki-main/src/main/resources/plugin/PluginResources_es.properties
index e3d0e06..23d0d58 100644
--- a/jspwiki-main/src/main/resources/plugin/PluginResources_es.properties
+++ b/jspwiki-main/src/main/resources/plugin/PluginResources_es.properties
@@ -47,8 +47,10 @@ currenttimeplugin.badformat = Especificaste un formato err
# WeblogEntryPlugin
weblogentryplugin.newentry = Nueva entrada
+weblogentryplugin.postedby = Publicado por {0}
weblogentryplugin.permalink = Enlace permanente
weblogentryplugin.addcomment = <span class="icon-plus"></span> A�adir nuevo comentario ({0})
+weblogentryplugin.more = (más)
# InserPagePlugin
insertpage.more=Mu00e1s...
@@ -79,7 +81,4 @@ plugin.listlocks.expires=Expira
plugin.listlocks.no.locks.exist=No existen bloqueos actualmente.
# ReferringUndefinedPagesPlugin
-referringundefinedpagesplugin.more = ... y {0} m�s
-
-# WeblogEntryPlugin
-weblogentryplugin.postedby = Publicado por {0}
+referringundefinedpagesplugin.more = ... y {0} m�s
\ No newline at end of file
diff --git a/jspwiki-main/src/test/java/org/apache/wiki/plugin/WeblogPluginTest.java b/jspwiki-main/src/test/java/org/apache/wiki/plugin/WeblogPluginTest.java
new file mode 100644
index 0000000..0bf7d9f
--- /dev/null
+++ b/jspwiki-main/src/test/java/org/apache/wiki/plugin/WeblogPluginTest.java
@@ -0,0 +1,76 @@
+package org.apache.wiki.plugin;
+
+import org.apache.wiki.TestEngine;
+import org.apache.wiki.api.exceptions.WikiException;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Properties;
+
+
+public class WeblogPluginTest {
+
+ TestEngine testEngine;
+
+ @BeforeEach
+ public void setUp() throws Exception {
+ final Properties props = TestEngine.getTestProperties();
+ testEngine = new TestEngine( props );
+ }
+
+ @AfterEach
+ public void tearDown() throws Exception {
+ testEngine.deletePage( "Test" );
+ }
+
+ @Test
+ public void testWeblogEmpty() throws WikiException {
+ final String src="[{WeblogPlugin days='90' allowComments='true'}]";
+ testEngine.saveText( "Test1", src );
+
+ final String res = testEngine.getI18nHTML( "Test1" );
+ Assertions.assertEquals( "<div class=\"weblog\">\n</div>\n\n", res );
+ }
+
+ @Test
+ public void testWeblogEntries() throws WikiException {
+ final WeblogEntryPlugin wep = new WeblogEntryPlugin();
+ testEngine.saveText( wep.getNewEntryPage( testEngine, "Test2" ), "My first blog entry, W00t!" );
+ final String src="[{WeblogPlugin days='90' allowComments='true'}]";
+ testEngine.saveText( "Test2", src );
+
+ final String res = testEngine.getI18nHTML( "Test2" );
+ Assertions.assertTrue( res.startsWith( "<div class=\"weblog\">\n<div class=\"weblogentry\">\n<div class=\"weblogentryheading\">\n" ), res );
+ Assertions.assertTrue( res.contains( "<div class=\"weblogentrybody\">\nMy first blog entry, W00t!\n</div>\n" ), res );
+ }
+
+ @Test
+ public void testWeblogEntriesWithPreviewAndNoTrimming() throws WikiException {
+ final WeblogEntryPlugin wep = new WeblogEntryPlugin();
+ // preview mantains complete paragraphs, as odds are that should help rendering a more well-formed html
+ final String blogEntryPage = wep.getNewEntryPage( testEngine, "Test3" );
+ testEngine.saveText( blogEntryPage, "My first blog entry, W00t!" );
+ final String src="[{WeblogPlugin days='90' allowComments='true' preview='5'}]";
+ testEngine.saveText( "Test3", src );
+
+ final String res = testEngine.getI18nHTML( "Test3" );
+ Assertions.assertTrue( res.startsWith( "<div class=\"weblog\">\n<div class=\"weblogentry\">\n<div class=\"weblogentryheading\">\n" ), res );
+ Assertions.assertTrue( res.contains( "<div class=\"weblogentrybody\">\nMy first blog entry, W00t! <a href=\"/test/Wiki.jsp?page="+ blogEntryPage + "\">(more)</a>\n</div>\n" ), res );
+ }
+
+ @Test
+ public void testWeblogEntriesWithPreviewAndTrimming() throws WikiException {
+ final WeblogEntryPlugin wep = new WeblogEntryPlugin();
+ final String blogEntryPage = wep.getNewEntryPage( testEngine, "Test4" );
+ testEngine.saveText( blogEntryPage, "Another blog entry \n \n this time about some serious stuff!" );
+ final String src="[{WeblogPlugin days='90' allowComments='true' preview='5'}]";
+ testEngine.saveText( "Test4", src );
+
+ final String res = testEngine.getI18nHTML( "Test4" );
+ Assertions.assertTrue( res.startsWith( "<div class=\"weblog\">\n<div class=\"weblogentry\">\n<div class=\"weblogentryheading\">\n" ), res );
+ Assertions.assertTrue( res.contains( "<div class=\"weblogentrybody\">\nAnother blog entry <a href=\"/test/Wiki.jsp?page="+ blogEntryPage + "\">(more)</a>\n</div>\n" ), res );
+ }
+
+}