Display a reading duration

Post here questions and problems related to editing and publishing DITA content.
gbv34
Posts: 66
Joined: Thu Jan 20, 2022 12:36 pm

Display a reading duration

Post by gbv34 »

Hello!
I'm looking a way to display the reading duration for a topic.
Currently, my best lead is an XSLT shared in this forum that displays the number of words contained in a map.

Code: Select all

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes"/>

    <xsl:template match="/">

        <counts>

            <xsl:apply-templates/>

        </counts>

    </xsl:template>

    <xsl:template match="text()"/>

    <xsl:template match="*[contains(@class, 'map/map')]">

        <xsl:variable name="text">

            <xsl:apply-templates mode="getText" select="node()"/>

        </xsl:variable>

        <count>

            <xsl:value-of
                select="count(tokenize(lower-case($text), '(\s|[,.!:;]|[n][b][s][p][;])+')[string(.)])"/>

        </count>

        <xsl:apply-templates/>

    </xsl:template>

    <xsl:template match="*[contains(@class, 'map/map')]" mode="getText"/>

</xsl:stylesheet>
However, what I don't get is how I could insert the results in the first topic of the map, as a kind of infoblock.
Also, having the number of words is a good basis, but it should be necessary to proceed some calculation. I found this nice little js function which provides a rough estimation based on a word per minute.

Code: Select all

function readingTime() {
  const text = document.getElementById("article").innerText;
  const wpm = 225;
  const words = text.trim().split(/\s+/).length;
  const time = Math.ceil(words / wpm);
  document.getElementById("time").innerText = time;
}
readingTime();
Is there any way to apply such a function with xslt?
Has someone in the forum already met this need?

Any feedback is welcome. I recognize I was surprise nit seeing any plugin developed for that.
chrispitude
Posts: 529
Joined: Thu May 02, 2019 2:32 pm

Re: Display a reading duration

Post by chrispitude »

Hi gbv34,

Do you want this for PDF or HTML5 output, or both? This might influence how the implementation would work.
gbv34
Posts: 66
Joined: Thu Jan 20, 2022 12:36 pm

Re: Display a reading duration

Post by gbv34 »

Hi Chris,
I intend to achieve this feature for HTML files.
chrispitude
Posts: 529
Joined: Thu May 02, 2019 2:32 pm

Re: Display a reading duration

Post by chrispitude »

Hi gbv34,

How would you want it annotated to the HTML output? Is there a specific HTML structure you would want?

Maybe just as a proof of concept, I could put the information in an HTML <abstract> element?

- Chris
gbv34
Posts: 66
Joined: Thu Jan 20, 2022 12:36 pm

Re: Display a reading duration

Post by gbv34 »

Hi again :)
Actually, I don't have a specific requirement for that but I thought it would be more relevant to place it close to the main heading in a topic, right after the opening of the <body> element.
Post Reply