Bước tới nội dung

Mô đun:Danh sách Đầu bài

Bách khoa toàn thư mở Wikipedia
Tài liệu mô đun[tạo]
local p = {};
local pagename = mw.text.trim( mw.getCurrentFrame().args[1] )
local head = mw.text.trim( mw.getCurrentFrame().args[2] )

p.init = function( frame )
	local content = mw.text.trim( mw.title.new( pagename ):getContent() )
	local lines = mw.text.split( content, "\n" )
	
	local result = ""
	for _, line in pairs(lines) do
		-- ignore "[[Tập tin"
		if not mw.ustring.match ( line, "%[%[Tập%stin" ) then
			-- match "[[.*]]" and ignore "|"
			if mw.ustring.match ( line, "%[%[.*%]%]" ) and not mw.ustring.find ( line, "|" ) then
				-- match "[[.*]]"
				local title = mw.ustring.sub( mw.ustring.match ( line, "%[%[.*%]%]" ), 3, -3 )
				local link = "[[" .. head .. "/" .. title .. "|" .. title .. "]]"
				line = mw.ustring.gsub( line, "%[%[.*%]%]", link )
				result = result .. line .. "\n"
			-- match "[[.*]]" and "|"
			elseif mw.ustring.match ( line, "%[%[.*%]%]" ) and mw.ustring.find ( line, "|" ) then
				-- match "[[.*|"
				local title = mw.ustring.sub( mw.ustring.match ( line, "%[%[.*|" ), 3, -2 )
				local halflink = "[[" .. head .. "/" .. title .. "|"
				line = mw.ustring.gsub( line, "%[%[.*|", halflink )
				result = result .. line .. "\n"
			else
				result = result .. line .. "\n"
			end
		else
			result = result .. line .. "\n"
		end
	end

	return mw.text.trim( result )
end

return p