Jump to content

Module:Sandbox/Black lemon

From Wikipedia, the free encyclopedia
local p = {}

local page_content = {}
local function get_page(page_name)
	if page_content[page_name] == nil then
		local title = mw.title.new(page_name)
		if title then
			if title.redirectTarget then title = title.redirectTarget end
			page_content[page_name] = title:getContent()
		end
	end
end

local function get_sectionh(frame, page_name, section_name, error_text)
	get_page(page_name)
	if page_content[page_name] then
		if section_name ~= '' then
			if mw.ustring.find(page_content[page_name], "=%s*" .. section_name .. "%s*=", 1, false) then
				return frame:preprocess("{{#section-h::" .. page_name .. "|".. section_name .. "}}")
			end
		else
			return page_content[page_name]
		end
	end
	return error_text
end

local function get_section(frame, page_name, section_name, error_text)
	get_page(page_name)
	if page_content[page_name] then
		if section_name ~= '' then
			if mw.ustring.find(page_content[page_name], "%<[Ss][Ee][Cc][Tt][Ii][Oo][Nn][^%<%>]*" .. section_name, 1, false) then
				return frame:preprocess("{{#section::" .. page_name .. "|".. section_name .. "}}")
			end
		else
			return page_content[page_name]
		end
	end
	return error_text
end

local function trim(s)
	return s:match('^%s*(.-)%s*$')
end

function p.sectionh(frame)
	local args = require('Module:Arguments').getArgs(frame)
	return get_sectionh(frame, trim(args[1] or ''), trim(args[2] .. ''), args['error'] or '')
end

function p.section(frame)
	local args = require('Module:Arguments').getArgs(frame)
	return get_section(frame, trim(args[1] or ''), trim(args[2] .. ''), args['error'] or '')
end

function p.lsth(frame)
	return p.sectionh(frame)
end

function p.lst(frame)
	return p.section(frame)
end

return p