Jump to content

Module:NAQ content block

From Wikisource
local p = {} --p stands for package

--[=[
Return a formatted block of contents of the format suitable for Notes and Queries.

Basically, this is just a list of em-dash-separated links, which are given one per line:

Link -> [[NaQ/Series S/Volume V/Number NNN/Link]]
Link\Display -> [[NaQ/Series S/Volume V/Number NNN/Link|Display]]
Link, 123 -> [[NaQ/Series S/Volume V/Number NNN/Link]], 123

Series, Volume and Number are params #1, 2, 3

Parm 4 is an optional prefix which will be unlinked. Often "NOTES:" or similar.

Formatting is not applied - this is done in the template.
]=]
function p.content_links( frame )

	local link_prefix = "Notes and Queries/Series " .. frame.args[1]
		.. "/Volume " .. frame.args[2]
		.. "/Number " .. frame.args[3]
	
	-- the parts of the page title
    local parts = mw.text.split( frame.args['content'], "\n", true )
    -- collected links for each parent
    local links = {}
    
    -- count forwards from the highest level to the second-lowest
    -- (the lowest level is the current page, not a parent)
    for k, v in pairs(parts) do
    	
        -- first, any page suffixes can come out, we don't need them in the link
        local content, unlinked_suffix = mw.ustring.match(v, "(.*)%s+(%d+%p?)$" )

        if content == nil then
    	  content = v
    	  unlinked_suffix = ""
        end
       
    	local display_text
    	local link_target
    	-- then, check if we have a link\display pair
    	local disp_parts = mw.text.split(content, "\\", true)
        
    	if #disp_parts < 2 then
        	link_target = content
        	display_text = content
		else
        	link_target = disp_parts[1]
        	display_text = disp_parts[2]
    	end
    	
    	-- don't include trailing commas or full stops in link targets
    	link_target = mw.ustring.gsub(link_target, "[%.,](%p?)$", "%1")
        
        link = "[[" .. link_prefix .. "/" .. link_target .. "|" .. display_text .. "]]"
        
        if unlinked_suffix ~= nil and unlinked_suffix ~= "" then
        	link = link .. " " .. unlinked_suffix
        end

    	table.insert(links, link)
    end
    
    -- build up the final output
    local ret = ""
    
	if frame.args[4] ~= nil and frame.args[4] ~= "" then
		ret = ret .. frame.args[4] .. "—"
	end
    
    ret = ret .. table.concat(links, "—")
    return ret
end

return p