Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed May 13, 2020
2 parents df7a596 + 3bb3565 commit fee2bf2
Show file tree
Hide file tree
Showing 18 changed files with 820 additions and 28 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# v1.4.0
## 05/13/2020

1. [](#new)
* Added optional features to include additional reading time for images [#16](https://github.com/getgrav/grav-plugin-readingtime/pull/16)
* Started using composer
1. [](#improved)
* Added hungarian translation [#12](https://github.com/getgrav/grav-plugin-readingtime/pull/12)
* Added dutch translation [#14](https://github.com/getgrav/grav-plugin-readingtime/pull/14)
* PHPStan 7 fixes
1. [](#bugfix)
* Fixed issue with passed in labels [#18](https://github.com/getgrav/grav-plugin-readingtime/pull/18)
* Fixed plugin initialization

# v1.3.0
## 12/10/2018

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,23 @@ Display variables for text labels:
| `{seconds_text}` | Displays the Seconds Text Label (Singular or Plural, Based on Number of Seconds) | `seconds` |

>> NOTE: Any time you are making alterations to a theme's files, you will want to duplicate the theme folder in the `user/themes/` directory, rename it, and set the new name as your active theme. This will ensure that you don't lose your customizations in the event that a theme is updated. Once you have tested the change thoroughly, you can delete or back up that folder elsewhere.
### Include image views
The number of seconds to view images is added to the reading time of text when `include_image_views` is set to true.

Images are identified by `<img>` tags in `page.content()`.

The default values for `seconds_per_image` (shown below) mean that the first image adds `12` seconds to the reading time, the second adds `11` seconds, the third adds `10` seconds, and so on.
Only integers, whitespace, and commas are permitted in the string.

```
seconds_per_image: '12,11,10,9,8,7,6,5,4,3'
```

If there are more images in a page than what is defined in `seconds_per_image` (e.g., more than 10 images in the default shown above) then subsequent images take the last value (`3` seconds in the default shown above).

The example below adds `5` seconds reading time for all images.

```
seconds_per_image: 5
```
23 changes: 22 additions & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Reading Time
version: 1.3.0
version: 1.4.0
description: "Add human readable reading time to your pages."
icon: clock-o
author:
Expand Down Expand Up @@ -49,3 +49,24 @@ form:
options:
seconds: second
minutes: minute

include_image_views:
type: toggle
label: Include time for viewing images
highlight: 1
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool

seconds_per_image:
type: text
label: Seconds to view images
help: 'Comma-separated list of whole numbers where first number is the number of seconds to view the first image, second number for the second image, and so on. Example: 12,10,11'
default: '12,11,10,9,8,7,6,5,4,3'
validate:
required: true
pattern: '(^(\s*\d+\s*))?(,\s*\d+\s*)*'
message: 'Must be a comma-separated list of whole numbers (e.g. 12,11,10) with at least one number'
65 changes: 59 additions & 6 deletions classes/TwigReadingTimeFilters.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php namespace Grav\Common;
<?php

namespace Grav\Plugin\ReadingTime;

use Grav\Common\Grav;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use Twig_Extension;

class TwigReadingTimeFilters extends \Twig_Extension
class TwigReadingTimeFilters extends Twig_Extension
{
private $grav;

Expand All @@ -25,6 +27,20 @@ public function getFilters()
];
}

public function validatePattern($seconds_per_image)
{
// Get regex that is used in the user interface
$pattern = '/' . $this->grav['plugins']->get('readingtime')->blueprints()->schema()->get('seconds_per_image')['validate']['pattern'] . '/';

if (preg_match($pattern, $seconds_per_image, $matches) === false) {
return false;
}

// Note: "$matches[0] will contain the text that matched the full pattern"
// https://www.php.net/manual/en/function.preg-match.php
return strlen($seconds_per_image) === strlen($matches[0]);
}

public function getReadingTime( $content, $params = array() )
{

Expand All @@ -33,12 +49,35 @@ public function getReadingTime( $content, $params = array() )

$options = array_merge($this->grav['config']->get('plugins.readingtime'), $params);

$words = count(preg_split('/\s+/', strip_tags($content)));
$words = count(preg_split('/\s+/', strip_tags($content)) ?: []);
$wpm = $options['words_per_minute'];

$minutes_short_count = floor($words / $wpm);
$seconds_short_count = floor($words % $wpm / ($wpm / 60));

if ($options['include_image_views']) {
$stripped = strip_tags($content, "<img>");
$images_in_content = substr_count($stripped, "<img ");

if ($images_in_content > 0) {
if ($this->validatePattern($options['seconds_per_image'])) {

// assumes string only contains integers, commas, and whitespace
$spi = preg_split('/\D+/', trim($options['seconds_per_image']));
$seconds_images = 0;

for ($i = 0; $i < $images_in_content; ++$i) {
$seconds_images += $i < count($spi) ? $spi[$i] : end($spi);
}

$minutes_short_count += floor($seconds_images / 60);
$seconds_short_count += $seconds_images % 60;
} else {
$this->grav['log']->error("Plugin 'readingtime' - seconds_per_image failed regex vadation");
}
}
}

$round = $options['round'];
if ($round == 'minutes') {
$minutes_short_count = round(($minutes_short_count*60 + $seconds_short_count) / 60);
Expand All @@ -52,8 +91,22 @@ public function getReadingTime( $content, $params = array() )

$minutes_long_count = number_format($minutes_short_count, 2);
$seconds_long_count = number_format($seconds_short_count, 2);
$minutes_text = $language->translate(( $minutes_short_count == 1 ) ? 'PLUGIN_READINGTIME.MINUTE' : 'PLUGIN_READINGTIME.MINUTES');
$seconds_text = $language->translate(( $seconds_short_count == 1 ) ? 'PLUGIN_READINGTIME.SECOND' : 'PLUGIN_READINGTIME.SECONDS');

if (array_key_exists('minute_label', $options) and $minutes_short_count == 1) {
$minutes_text = $options['minute_label'];
} elseif (array_key_exists('minutes_label', $options) and $minutes_short_count > 1) {
$minutes_text = $options['minutes_label'];
} else {
$minutes_text = $language->translate(( $minutes_short_count == 1 ) ? 'PLUGIN_READINGTIME.MINUTE' : 'PLUGIN_READINGTIME.MINUTES');
}

if (array_key_exists('second_label', $options) and $seconds_short_count == 1) {
$seconds_text = $options['second_label'];
} elseif (array_key_exists('seconds_label', $options) and $seconds_short_count > 1) {
$seconds_text = $options['seconds_label'];
} else {
$seconds_text = $language->translate(( $seconds_short_count == 1 ) ? 'PLUGIN_READINGTIME.SECOND' : 'PLUGIN_READINGTIME.SECONDS');
}

$replace = [
'minutes_short_count' => $minutes_short_count,
Expand Down
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "getgrav/grav-plugin-readingtime",
"type": "grav-plugin",
"description": "Grav Plugin Reading Time",
"keywords": ["form"],
"homepage": "https://github.com/getgrav/grav-plugin-readingtime",
"license": "MIT",
"authors": [
{
"name": "Team Grav",
"email": "[email protected]",
"homepage": "http://getgrav.org",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/getgrav/grav-plugin-readingtime/issues",
"irc": "https://chat.getgrav.org",
"forum": "https://discourse.getgrav.org",
"docs": "https://github.com/getgrav/grav-plugin-readingtime/blob/master/README.md"
},
"require": {
"php": ">=7.1.3"
},
"autoload": {
"psr-4": {
"Grav\\Plugin\\ReadingTime\\": "classes/"
},
"classmap": [
"readingtime.php"
]
},
"config": {
"platform": {
"php": "7.1.3"
}
}
}
22 changes: 22 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,27 @@ fr:
MINUTE: minute
MINUTES: minutes

hr:
PLUGIN_READINGTIME:
SECOND: sekunda
SECONDS: sekundi
MINUTE: minuta
MINUTES: minuta

it:
PLUGIN_READINGTIME:
SECOND: secondo
SECONDS: secondi
MINUTE: minuto
MINUTES: minuti

nl:
PLUGIN_READINGTIME:
SECOND: seconde
SECONDS: seconden
MINUTE: minuut
MINUTES: minuten

pt-BR:
PLUGIN_READINGTIME:
SECOND: segundo
Expand Down
59 changes: 38 additions & 21 deletions readingtime.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
<?php namespace Grav\Plugin;
<?php

namespace Grav\Plugin;

use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use Grav\Plugin\ReadingTime\TwigReadingTimeFilters;

class ReadingTimePlugin extends Plugin
{
public static function getSubscriptedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => [
['autoload', 100000],
['onPluginsInitialized', 0]
]
];
}

public function onPluginsInitialized()
{
if ( $this->isAdmin() ) {
$this->active = false;
return;
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload()
{
return require __DIR__ . '/vendor/autoload.php';
}

$this->enable([
'onTwigExtensions' => ['onTwigExtensions', 0]
]);
}
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}

public function onTwigExtensions()
{
require_once( __DIR__ . '/classes/TwigReadingTimeFilters.php' );
$this->enable([
'onTwigExtensions' => [
['onTwigExtensions', 0]
]
]);
}

$this->grav['twig']->twig->addExtension( new \Grav\Common\TwigReadingTimeFilters() );
}
public function onTwigExtensions()
{
$this->grav['twig']->twig->addExtension(new TwigReadingTimeFilters());
}
}
2 changes: 2 additions & 0 deletions readingtime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ enabled: true
words_per_minute: 200
format: "{minutes_short_count} {minutes_text}, {seconds_short_count} {seconds_text}"
round: seconds
include_image_views: false
seconds_per_image: '12,11,10,9,8,7,6,5,4,3'
7 changes: 7 additions & 0 deletions vendor/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitba8cc29b2b127a27a57127d33945f8aa::getLoader();
Loading

0 comments on commit fee2bf2

Please sign in to comment.