Module:Endnote/sandbox
Appearance
This is the module sandbox page for Module:Endnote (diff). |
This module depends on the following other modules: |
This template transcludes a specific section from one or multiple pages.
Usage
[edit]- 1: The name of the Index file.
- 2: A single page (e.g.
45
) or a range of pages (e.g.45-47
) - 3: The name of the section to be transcluded.
Example
[edit]See Page:Modern poets and poetry of Spain.djvu/369.
local getArgs = require('Module:Arguments').getArgs
local warn = require('Module:Warning')
local complain = require('Module:Error').error
local p = {}
function p._checkArguments(index, pages, section)
local missing = {}
if index == nil or index == "" then
table.insert(missing, "page name (argument #1)")
end
if pages == nil or pages == "" then
table.insert(missing, "page number or range (argument #2)")
end
if section == nil or section == "" then
table.insert(missing, "section name (argument #3)")
end
if #missing ~= 0 then
local messages = ""
for k, v in ipairs(missing) do
messages = messages .. v
if k < #missing then
messages = messages .. ", "
end
end
warn("One or more calls to Endnote is missing required arguments: " .. messages, 0)
return complain({message = "Endnote is missing required arguments: " .. messages})
end
end
function p.main(frame)
local args = getArgs(frame)
local index = args[1] or ""
local pages = args[2] or ""
local section = args[3] or ""
local complaint = p._checkArguments(index, pages, section)
if complaint ~= nil and complaint ~= "" then
return complaint
end
-- Get the pages from the arguments. Lua's anemic pseudo-regex patterns are
-- too limited to do this in one step, so we need to check each case
-- seperately in the conditional below.
local startPage
local endPage
if mw.ustring.match(pages, "^(%d+)$") then
-- single page
local page = mw.ustring.match(pages, "^(%d+)$")
startPage = tonumber(page)
endPage = tonumber(page)
elseif mw.ustring.match(pages, "^%d+%-%d+$") then
-- page range
startPage, endPage = mw.ustring.match(pages, "^(%d+)%-(%d+)$")
startPage = tonumber(startPage)
endPage = tonumber(endPage)
else
-- otherwise bail
warn("One or more calls to Endnote on this page has an invalid page range argument", 0)
return complain({message = "No valid page or page range given"})
end
-- Now iterate over the given page(s) and get their contents.
-- startPage may be equal to endPage, in which case we execute only once
local s = ""
for i = startPage, endPage do
local wikipage = "Page:" .. index .. "/" .. i
s = s .. frame:callParserFunction{
name = '#lst', args = {wikipage, section}
}
end
return s
end
return p