Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions blueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ form:
min: 1
max: 1000

estimate_range:
type: text
size: x-small
append: ±%
help: 'Percentage to add and remove from the reading time estimate to get a reading time range. 10% on a 10-minute read would give 9–11 minutes.'
label: Reading time estimate range
validate:
type: int
min: 0
max: 100

range_str:
type: text
label: Estimate range string
help: 'String to use between the low and high estimate range numbers (e.g., "—", "-", " to ", etc.).'
size: x-small

format:
type: text
label: Format
Expand Down
49 changes: 41 additions & 8 deletions classes/TwigReadingTimeFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ public function getReadingTime( $content, $params = array() )

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

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

$minutes_low_range = floor(($words * (1 - $estimate_range)) / $wpm);
$minutes_high_range = floor(($words * (1 + $estimate_range)) / $wpm);

if ($options['include_image_views']) {
$stripped = strip_tags($content, "<img>");
$images_in_content = substr_count($stripped, "<img ");
Expand All @@ -72,6 +77,9 @@ public function getReadingTime( $content, $params = array() )

$minutes_short_count += floor($seconds_images / 60);
$seconds_short_count += $seconds_images % 60;

$minutes_low_range += floor(($seconds_images * (1 - $estimate_range)) / 60);
$minutes_high_range += floor(($seconds_images * (1 - $estimate_range)) / 60);
} else {
$this->grav['log']->error("Plugin 'readingtime' - seconds_per_image failed regex vadation");
}
Expand All @@ -80,18 +88,41 @@ public function getReadingTime( $content, $params = array() )

$round = $options['round'];
if ($round == 'minutes') {
$minutes_short_count = round(($minutes_short_count*60 + $seconds_short_count) / 60);
$minutes_short_count = round(($minutes_short_count * 60 + $seconds_short_count) / 60);

$minutes_low_range = round(($minutes_low_range * 60 + $seconds_low_range) / 60);
$minutes_high_range = round(($minutes_high_range * 60 + $seconds_high_range) / 60);

if ( $minutes_short_count < 1 ) {
$minutes_short_count = 1;

$minutes_low_range = 0;
$minutes_high_range = 1;
}

$seconds_short_count = 0;
}

$minutes_long_count = number_format($minutes_short_count, 2);
$seconds_long_count = number_format($seconds_short_count, 2);

$minutes_low_range_long = number_format($minutes_low_range, 2);
$minutes_high_range_long = number_format($minutes_high_range, 2);

if ($minutes_low_range == $minutes_high_range or $minutes_low_range == 0) {
$minutes_short_range = $minutes_short_count;
$minutes_long_range = $minutes_long_count;
} elseif ($minutes_low_range == 0) {
$minutes_short_range = $minutes_short_count;
$minutes_long_range = $minutes_long_count;
} else {
$minutes_short_range = (
$minutes_low_range . $range_str . $minutes_high_range
);
$minutes_long_range = (
$minutes_low_range_long . $range_str . $minutes_high_range_long
);
}

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) {
Expand All @@ -109,12 +140,14 @@ public function getReadingTime( $content, $params = array() )
}

$replace = [
'minutes_short_count' => $minutes_short_count,
'seconds_short_count' => $seconds_short_count,
'minutes_long_count' => $minutes_long_count,
'seconds_long_count' => $seconds_long_count,
'minutes_text' => $minutes_text,
'seconds_text' => $seconds_text
'minutes_short_count' => $minutes_short_count,
'minutes_short_range' => $minutes_short_range,
'seconds_short_count' => $seconds_short_count,
'minutes_long_count' => $minutes_long_count,
'minutes_long_range' => $minutes_long_range,
'seconds_long_count' => $seconds_long_count,
'minutes_text' => $minutes_text,
'seconds_text' => $seconds_text
];

$result = $options['format'];
Expand Down
2 changes: 2 additions & 0 deletions readingtime.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
enabled: true
words_per_minute: 200
estimate_range: 15
range_str: "–"
format: "{minutes_short_count} {minutes_text}, {seconds_short_count} {seconds_text}"
round: seconds
include_image_views: false
Expand Down