2017 © Pedro Peláez
 

craft-plugin craft-textstatistics

Text Statistics plugin for Craft CMS

image

carlcs/craft-textstatistics

Text Statistics plugin for Craft CMS

  • Thursday, May 26, 2016
  • by carlcs
  • Repository
  • 1 Watchers
  • 13 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Text Statistics plugin for Craft CMS

This Craft plugin provides Twig functions to display information about your texts. It allows you to measure the readability of your content, calculate the average reading time or access other text statistics like sentence count., (*1)

Installation

The plugin is available on Packagist and can be installed using Composer. You can also download the latest release and copy the files into craft/plugins/textstatistics/., (*2)

$ composer require carlcs/craft-textstatistics

Settings

The plugin can be configured with a craft/config/textstatistics.php config file to set the default template used to render the reading time string and to set a default words per minute value., (*3)

TextStatisticsModel

When you generate text statistics using the getTextStatistics function the plugin returns a TextStatisticsModel with the following properties populated. Most of the data is calculated with the excellent Text Statistics library by Dave Child. For information about the provided readability scores have a look these Wikipedia articles:, (*4)

Properties

  • textLength
  • letterCount
  • syllableCount
  • wordCount
  • sentenceCount
  • readingTime
  • readingTimeMinutes
  • readingTimeString
  • gunningFog
  • fleschKincaid

Templating Examples

A single article

{% do addToTextStatistics(entry.text) %}

{% set statistics = getTextStatistics() %}
{{ statistics.readingTimeString }}

{# outputs "21 minutes" #}

Adjust WPM according to text language

{% set wpm = {
    'en': 200,
    'fr': 180,
} %}

{% set statistics = getTextStatistics(null, wpm[craft.locale]) %}
{{ statistics.readingTimeString }}

{# outputs "moins de 5 minutes" #}

Usage with a Matrix field

{% for block in entry.article %}

    {% if block.type == 'text' %}
        {% do addToTextStatistics(block.text) %}
        {{ block.text }}
    {% endif %}

    {% if block.type == 'image' %}
        {% set image = block.image.first() %}
        {% if image %}
            {% do addToReadingTime(10) %}
            <img src="{{ image.getUrl() }}" alt="{{ image.title }}">
        {% endif %}
    {% endif %}

{% endfor %}

{% set statistics = getTextStatistics() %}

Twig functions

addToTextStatistics( text, articleId )

Adds text to the data storage so you can later perform text statistics calculations from it., (*5)

  • text (required) – The text to add to the data storage for text statistics. It can contain HTML tags, the plugin removes them before doing its calculations.
  • articleId (default '') – An ID to identify the article. Only required if you have multiple articles for text statistics on the same page.
{% do addToTextStatistics(entry.myRichTextField) %}

Alternativly you can use the Twig filter., (*6)

{{ entry.myRichTextField|addToTextStatistics }}

addToReadingTime( seconds, articleId )

Adds time to the data storage to further influence the reading time returned with the text statistics., (*7)

  • seconds (required) – Time in seconds to add to the data storage for text statistics.
  • articleId (default '') – An ID to identify the article. Only required if you have multiple articles for text statistics on the same page.
{% do addToReadingTime(15) %}

getTextStatistics( articleId, wpm, readingTimeTemplate )

Performs the text statistics calculations for an article and returns the result as a TextStatisticsModel., (*8)

  • articleId (default '') – An ID to identify the article. Only required if you have multiple articles for text statistics on the same page.
  • wpm (default 200) – The average reading speed used for the reading time calculations in words per minute units.
  • readingTimeTemplate (default ) – The Twig template code used to render for the reading time string.
{% set statistics = getTextStatistics() %}
{{ statistics.gunningFog }}

{# outputs "12.1" #}

Requirements

  • PHP 5.4+

The Versions

26/05 2016

dev-master

9999999-dev

Text Statistics plugin for Craft CMS

  Sources   Download

The Requires

 

26/05 2016

v1.0.1

1.0.1.0

Text Statistics plugin for Craft CMS

  Sources   Download

The Requires

 

24/05 2016

v1.0.0

1.0.0.0

Text Statistics plugin for Craft CMS

  Sources   Download

The Requires