More languages
More actions
No edit summary |
No edit summary |
||
Line 35: | Line 35: | ||
-- Create the reading time string | -- Create the reading time string | ||
local readingTime = | local readingTime = minReadingTime .. "-" .. maxReadingTime .. " minutes" | ||
return readingTime | return readingTime |
Revision as of 15:09, 17 September 2023
-- Module:ReadingTime
-- invoke on any page or template with {{#invoke:Reading time|estimateReadingTime|YourPageHere}} (change YourPageHere)
local p = {}
local mw = require("mw")
function p.estimateReadingTime(frame)
-- Get the page title from the frame parameter
local pageTitle = frame.args[1]
-- Ensure that pageTitle is a string
if type(pageTitle) == "string" then
-- Use MediaWiki's built-in function to get the page content
local content = mw.title.new(pageTitle):getContent()
-- Ensure that content is a string
if type(content) == "string" then
-- Count words using a more robust pattern
local wordCount = 0
for word in content:gmatch("%S+") do
wordCount = wordCount + 1
end
-- Define the range of reading speeds
local minSpeed = 183
local maxSpeed = 250
-- Calculate reading times for both minimum and maximum speeds
local minReadingTime = math.ceil(wordCount / maxSpeed)
local maxReadingTime = math.ceil(wordCount / minSpeed)
-- Round the reading times to the nearest multiple of 5
minReadingTime = math.ceil(minReadingTime / 5) * 5
maxReadingTime = math.ceil(maxReadingTime / 5) * 5
-- Create the reading time string
local readingTime = minReadingTime .. "-" .. maxReadingTime .. " minutes"
return readingTime
else
return "Error: Invalid content"
end
else
return "Error: Invalid page title"
end
end
return p