Module:Pages
模块文档[创建]
您可能想要创建本Scribunto模块的文档。 编者可以在本模块的沙盒 (编辑 | 差异)和测试样例 (编辑 | 运行)页面进行实验。 请在/doc子页面中添加分类。 本模块的子页面。 |
local p = {}
-- Function to check if a page exists in MediaWiki
local function pageExists(pageName)
local title = mw.title.new(pageName)
return title and title.exists
end
-- Helper function to parse ranges (like "2-5,9") into a table of pages
local function parseRange(rangeStr)
local pages = {}
for part in string.gmatch(rangeStr, "[^,]+") do
local from, to = part:match("(%d+)%-(%d+)")
if from and to then
for i = tonumber(from), tonumber(to) do
pages[i] = true
end
else
pages[tonumber(part)] = true
end
end
return pages
end
-- Function to apply template styles
function p.templateStyle(frame, src)
return frame:extensionTag('templatestyles', '', { src = src })
end
-- Function to join lines using the LineJoiner module
local function joinLines(text, sep)
local lineJoiner = require('Module:LineJoiner')
return lineJoiner.joinLines({
args = {
text = text,
sep = sep,
}
})
end
-- Function to create a page link or transclude page content using Module:Lst
local function transcludePage(frame, page, section, index, onlySection, debug)
local invokeArgs = {"lst", "joinedLines", "\n", "Page:" .. index .. "/" .. page}
if section then
table.insert(invokeArgs, section)
elseif onlySection then
table.insert(invokeArgs, onlySection)
end
if debug then
-- Debug mode: simulate transclusion
mw.log("Debug: Transcluding Page: " .. table.concat(invokeArgs, ", "))
return string.format("Page %s (link: %s)", page, table.concat(invokeArgs, ", "))
else
-- Actual transclusion using Module:Lst
local successInvoke, output = pcall(function()
return frame:callParserFunction{name = "#invoke", args = invokeArgs}
end)
if not successInvoke then
mw.log("Error calling #invoke: " .. tostring(output))
return "Error calling #invoke: " .. tostring(output)
end
return output
end
end
-- Main function to handle the <pages /> tag functionality
function p.pages(frame)
local args = frame.args
local index = args.index -- Index filename (e.g., foo.djvu)
-- Debugging: Log index
mw.log("Index: " .. index)
-- Check if the index page exists
if not pageExists("Index:" .. index) then
mw.log("Error: Index does not exist: " .. index)
return "Error: Index does not exist"
end
-- Check if styles.css exists
local cssPage = "Index:" .. index .. "/styles.css"
local cssContent = ""
if pageExists(cssPage) then
-- Load CSS using TemplateStyles if available
cssContent = p.templateStyle(frame, cssPage)
mw.log("Loaded CSS from: " .. cssPage)
else
mw.log("CSS file does not exist: " .. cssPage)
end
local from = tonumber(args.from) or 1 -- Start page
local to = tonumber(args.to) or from -- End page
local step = tonumber(args.step) or 1 -- Step for page inclusion
local excludePages = args.exclude and parseRange(args.exclude) or {} -- Pages to exclude
local includePages = args.include and parseRange(args.include) or nil -- Pages to include
local onlySection = args.onlysection -- Only include a specific section on each page
local debug = args.debug == "true" -- Check if debug mode is enabled
local sep = args.sep -- Default separator
-- Set fromSection and toSection based on onlySection
local fromSection = onlySection and onlySection or args.fromsection
local toSection = onlySection and onlySection or args.tosection
-- Debugging: Log parameters
mw.log("From: " .. from .. ", To: " .. to .. ", Step: " .. step)
mw.log("Exclude Pages: " .. (args.exclude or "none"))
mw.log("Include Pages: " .. (args.include or "none"))
mw.log("Only Section: " .. (onlySection or "none"))
mw.log("Debug mode: " .. tostring(debug))
-- Template for page numbers
local result = {}
for i = from, to, step do
if not excludePages[i] and (not includePages or includePages[i]) then
local section = nil
if i == from then
section = fromSection -- Set fromSection for the first page
elseif i == to then
section = toSection -- Set toSection for the last page
end
-- Only check page existence if debug mode is disabled
if not debug and not pageExists("Page:" .. index .. "/" .. i) then
mw.log("Error: Unable to recognize range for Page: " .. index .. "/" .. i)
return "Error: Unable to recognize range"
end
-- Transclude the page and store the result
table.insert(result, transcludePage(frame, i, section, index, onlySection, debug))
else
mw.log("Skipping Page: " .. i .. " (Excluded or not included)")
end
end
-- Join the results with the specified separator
local joinedResults = joinLines(table.concat(result, "\n"), sep)
-- Concatenate the CSS and joined results
return cssContent .. joinedResults
end
return p