Weitere Sprachen
Weitere Optionen
Jaiden (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „-- 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) 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"…“) |
Jaiden (Diskussion | Beiträge) KKeine Bearbeitungszusammenfassung |
||
(Eine dazwischenliegende Version derselben Benutzerin wird nicht angezeigt) | |||
Zeile 1: | Zeile 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: | -- invoke on any page or template with {{#invoke:Lesezeit|estimateReadingTime|YourPageHere}} (change YourPageHere) | ||
local p = {} | local p = {} | ||
Zeile 40: | Zeile 40: | ||
-- Create the reading time string | -- Create the reading time string | ||
local readingTime = minReadingTime .. "-" .. maxReadingTime .. " | local readingTime = minReadingTime .. "-" .. maxReadingTime .. " Minuten" | ||
return readingTime | return readingTime |
Aktuelle Version vom 19. Februar 2025, 12:27 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Lesezeit/Doku erstellt werden
-- Calculate estimated reading time for any page rounded to multiple of 5. Outputs "X-Y minutes".
-- invoke on any page or template with {{#invoke:Lesezeit|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 = 160
local maxSpeed = 238
-- Calculate reading times for both minimum and maximum speeds
local minReadingTime = math.floor(wordCount / maxSpeed)
local maxReadingTime = math.ceil(wordCount / minSpeed)
-- Round the reading times to the nearest multiple of 5
minReadingTime = math.floor(minReadingTime / 5) * 5
maxReadingTime = math.ceil(maxReadingTime / 5) * 5
-- Ensure that minReadingTime is at least 1
if minReadingTime == 0 then
minReadingTime = 1
end
-- Create the reading time string
local readingTime = minReadingTime .. "-" .. maxReadingTime .. " Minuten"
return readingTime
else
return "Error: Invalid content"
end
else
return "Error: Invalid page title"
end
end
return p