More languages
More actions
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
Line 1: | Line 1: | ||
-- Calculate estimated reading time for any page rounded to multiple of 5. Outputs "X-Y minutes". | -- Calculate estimated reading time for any page rounded to multiple of 5. Outputs "X-Y minutes". | ||
-- invoke on any page or template with {{#invoke:Reading time|estimateReadingTime|YourPageHere}} (change YourPageHere) | -- invoke on any page or template with {{#invoke:Reading time|estimateReadingTime|YourPageHere}} (change YourPageHere) | ||
-- Module:ReadingTime | |||
local p = {} | local p = {} | ||
Line 17: | Line 19: | ||
-- Ensure that content is a string | -- Ensure that content is a string | ||
if type(content) == "string" then | if type(content) == "string" then | ||
-- Remove MediaWiki | -- Remove MediaWiki comments | ||
content = content:gsub(" | content = content:gsub("<!--.- -->", "") | ||
-- Count words using a more robust pattern | -- Count words using a more robust pattern | ||
local wordCount = 0 | local wordCount = 0 | ||
for word in content:gmatch("%S+") do | for word in content:gmatch("%S+") do | ||
wordCount = wordCount + 1 | -- Check if the word contains an image or MediaWiki markup | ||
if not word:match("^%[%[File:.*%]%]") and not word:match("^{{.*}}$") then | |||
wordCount = wordCount + 1 | |||
end | |||
end | end | ||
Revision as of 16:04, 17 September 2023
-- Calculate estimated reading time for any page rounded to multiple of 5. Outputs "X-Y minutes".
-- invoke on any page or template with {{#invoke:Reading time|estimateReadingTime|YourPageHere}} (change YourPageHere)
-- Module:ReadingTime
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 page = mw.title.new(pageTitle)
local content = page:getContent()
-- Ensure that content is a string
if type(content) == "string" then
-- Remove MediaWiki comments
content = content:gsub("<!--.- -->", "")
-- Count words using a more robust pattern
local wordCount = 0
for word in content:gmatch("%S+") do
-- Check if the word contains an image or MediaWiki markup
if not word:match("^%[%[File:.*%]%]") and not word:match("^{{.*}}$") then
wordCount = wordCount + 1
end
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 minimum reading time down to the nearest multiple of 5
minReadingTime = math.floor(minReadingTime / 5) * 5
-- Round the maximum reading time up to the nearest multiple of 5
maxReadingTime = math.ceil(maxReadingTime / 5) * 5
-- Create the reading time string
local readingTime = "Between " .. minReadingTime .. " and " .. maxReadingTime .. " minutes"
return readingTime
else
return "Error: Invalid content"
end
else
return "Error: Invalid page title"
end
end
return p