diff --git a/README.md b/README.md index 8c081e1..2a80009 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ ReadingTime( wpm = 275, postfix = "min read", plural = "min read", - excludeImages = false + excludeImages = false, + extra = 0 ) ``` @@ -46,6 +47,7 @@ Property | Description `postfix` | The value to be appended to the reading time. `plural` | The value to be appended if the reading time is more than 1 minute. `excludeImages` | Images are excluded from the reading time when set. +`extra` | Additional seconds to be added to the total reading time. ### Functions @@ -66,7 +68,8 @@ A JSP tag is also available for easy incorporation into web applications: wpm="275" postfix="min read" plural="min read" - excludeImages="false">some_text + excludeImages="false" + extra="0">some_text ``` None of the attributes are required. diff --git a/src/main/kotlin/net/thauvin/erik/readingtime/ReadingTime.kt b/src/main/kotlin/net/thauvin/erik/readingtime/ReadingTime.kt index d4c0043..5787f0a 100644 --- a/src/main/kotlin/net/thauvin/erik/readingtime/ReadingTime.kt +++ b/src/main/kotlin/net/thauvin/erik/readingtime/ReadingTime.kt @@ -46,13 +46,15 @@ import java.math.RoundingMode * @param postfix The value to be appended to the reading time. * @param plural The value to be appended if the reading time is more than 1 minute. * @param excludeImages Images are excluded from the reading time when set. + * @param extra Additional seconds to be added to the total reading time. */ class ReadingTime @JvmOverloads constructor( text: String, wpm: Int = 275, var postfix: String = "min read", var plural: String = "min read", - excludeImages: Boolean = false + excludeImages: Boolean = false, + extra: Int = 0 ) { companion object { private const val INVALID: Double = -1.0 @@ -97,6 +99,12 @@ class ReadingTime @JvmOverloads constructor( field = value } + var extra: Int = extra + set(value) { + reset(value != extra) + field = value + } + /** * Calculates and returns the reading time in seconds. */ @@ -106,7 +114,7 @@ class ReadingTime @JvmOverloads constructor( readTime += wordCount(text) / (wpm / 60.0) } - return readTime + return readTime + extra } /** diff --git a/src/main/resources/META-INF/tags/readingtime.tag b/src/main/resources/META-INF/tags/readingtime.tag index ce12add..65f026f 100644 --- a/src/main/resources/META-INF/tags/readingtime.tag +++ b/src/main/resources/META-INF/tags/readingtime.tag @@ -7,6 +7,7 @@ <%@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="extra" type="java.lang.Integer"%> <%@attribute name="plural"%> <%@attribute name="postfix"%> <%@attribute name="wpm" type="java.lang.Integer"%> @@ -14,6 +15,7 @@ <% final Boolean debug = (Boolean) getJspContext().getAttribute("debug"); final Boolean excludeImages = (Boolean) getJspContext().getAttribute("excludeImages"); + final Integer extra = (Integer) getJspContext().getAttribute("extra"); final Integer wpm = (Integer) getJspContext().getAttribute("wpm"); final String body = (String) getJspContext().getAttribute("body");; final String plural = (String) getJspContext().getAttribute("plural"); @@ -22,6 +24,7 @@ if (body != null) { final ReadingTime rt = new ReadingTime(body); if (excludeImages != null) rt.setExcludeImages(excludeImages); + if (extra != null) rt.setExtra(extra); if (plural != null) rt.setPlural(plural); if (postfix != null) rt.setPostfix(postfix); if (wpm != null) rt.setWpm(wpm); @@ -31,7 +34,8 @@ 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() + ")\n-->"); + out.write("excludeImages: " + excludeImages + " (" + rt.getExcludeImages() + ")\n"); + out.write("extra: " + extra + " (" + rt.getExtra() + ")\n-->"); } } %> diff --git a/src/test/kotlin/net/thauvin/erik/readingtime/ReadingTimeTest.kt b/src/test/kotlin/net/thauvin/erik/readingtime/ReadingTimeTest.kt index 1a7eed9..864bed3 100644 --- a/src/test/kotlin/net/thauvin/erik/readingtime/ReadingTimeTest.kt +++ b/src/test/kotlin/net/thauvin/erik/readingtime/ReadingTimeTest.kt @@ -92,6 +92,9 @@ class ReadingTimeTest { rt.excludeImages = true assertEquals(calcReadingTime(rt.text, rt.wpm), rt.calcReadingTimeInSec()) + rt.extra = 60 + assertEquals(calcReadingTime(rt.text, rt.wpm) + 60L, rt.calcReadingTimeInSec()) + rt.extra = 0 rt.excludeImages = false rt.text = mediumPost