Added JSP tag.

This commit is contained in:
Erik C. Thauvin 2020-07-27 15:38:15 -07:00
parent ae134efac4
commit 6bd908ba1f
3 changed files with 64 additions and 1 deletions

View file

@ -11,7 +11,7 @@ A simple Kotlin/Java implementation of [Medium's Read Time calculation](https://
```kotlin
import net.thauvin.erik.readingtime.ReadingTime
...
// ...
val rt = ReadingTime(htmlText)
println(rt.calcEstimatedReadTime()) // eg: 2 min read
@ -56,6 +56,19 @@ ReadingTime.wordCount(htmlText) // Returns the count of words. (HTML stripped)
ReadingTime.imgCount(htmlText) // Returns the count of images. (HTML img tags)
```
### JSP
A JSP tag is also available for easy incorporation into web applications:
```jsp
<%@taglib uri="https://erik.thauvin.net/taglibs/readingtime" prefix="t"%>
<t:readingtime postfix="min read" plural="min read" excludeImages="false" wpm="275">some_text</t:readingtime>
```
None of the attributes are required.
Just drop the jar into your `WEB-INF/lib` directory.
### Gradle
To use with [Gradle](https://gradle.org/), include the following dependency in your [build](https://github.com/ethauvin/readingtime/blob/master/examples/build.gradle.kts) file:

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<short-name>ReadingTime</short-name>
<uri>https://erik.thauvin.net/taglibs/readingtime</uri>
<tag-file>
<name>readingtime</name>
<path>/META-INF/tags/readingtime.tag</path>
</tag-file>
</taglib>

View file

@ -0,0 +1,37 @@
<%--
Copyright (c) 2020, Erik C. Thauvin (erik@thauvin.net)
All rights reserved.
See https://opensource.org/licenses/BSD-3-Clause
--%>
<%@tag body-content="scriptless" import="net.thauvin.erik.readingtime.ReadingTime" trimDirectiveWhitespaces="true"%>
<%@attribute name="debug" type="java.lang.Boolean"%>
<%@attribute name="excludeImages" type="java.lang.Boolean"%>
<%@attribute name="plural"%>
<%@attribute name="postfix"%>
<%@attribute name="wpm" type="java.lang.Integer"%>
<jsp:doBody var="body" scope="page"/>
<%
final Boolean debug = (Boolean) getJspContext().getAttribute("debug");
final Boolean excludeImages = (Boolean) getJspContext().getAttribute("excludeImages");
final Integer wpm = (Integer) getJspContext().getAttribute("wpm");
final String body = (String) getJspContext().getAttribute("body");;
final String plural = (String) getJspContext().getAttribute("plural");
final String postfix = (String) getJspContext().getAttribute("postfix");
if (body != null) {
final ReadingTime rt = new ReadingTime(body);
if (excludeImages != null) rt.setExcludeImages(excludeImages);
if (plural != null) rt.setPlural(plural);
if (postfix != null) rt.setPostfix(postfix);
if (wpm != null) rt.setWpm(wpm);
out.write(rt.calcReadingTime());
if (debug != null && debug) {
out.write("<!--\n" + "body: " + body + "\n");
out.write("wpm: " + wpm + " (" + rt.getWpm() + ")\n");
out.write("postfix: " + postfix + " (" + rt.getPostfix() + ")\n");
out.write("plural: " + plural + " (" + rt.getPlural() + ")\n");
out.write("excludeImages: " + excludeImages + " (" + rt.getExcludeImages() + ") -->");
}
}
%>