跳转到内容

模組:Subject bar

维基百科,自由的百科全书

这是本页的一个历史版本,由A1Cafel留言 | 贡献2017年6月28日 (三) 12:16编辑。这可能和当前版本存在着巨大的差异。

local getPortalImage = require('Module:Portal').image.chinese
local getPortalImage2 = getImageName(s)

local p = {}

local function matchImagePage(s)
	-- Finds the appropriate image subpage given a lower-case
	-- portal name plus the first letter of that portal name.
	if type(s) ~= 'string' or #s < 1 then return end
	local firstLetter = mw.ustring.sub(s, 1, 1)
	local imagePage
	if mw.ustring.find(firstLetter, '^[a-z]') then
		imagePage = 'Module:Portal/images/letter'
	else
		imagePage = 'Module:Portal/images/chinese'
	end
	-- Hey, we have a thing called "other"
	return mw.loadData(imagePage)[s] or
		   mw.loadData('Module:Portal/images/other')[s]
end

local function getAlias(s)
	-- Gets an alias from the image alias data page.
	local aliasData = mw.loadData('Module:Portal/images/aliases')
	return aliasData[s]
end

local function getImageName(s)
	-- Gets the image name and the un-aliased "normal name" for a given portal.
	local default = 'Portal-puzzle.svg|link=|alt='
	if type(s) ~= 'string' or #s < 1 then
		return default
	end
	local sl = mw.ustring.lower(s)
	local sr = s
	local img = matchImagePage(sl)
	if not img then
		sr = getAlias(sl)
		if sr then
			img = matchImagePage(mw.ustring.lower(sr))
		end
		if not img then
			img = default
			sr = s
		end
	end
	return img  , sr
end


local function getArgNums(prefix, args)
    -- Returns a table containing the numbers of the arguments that exist for the specified prefix. For example, if the
    -- prefix was 'data', and 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
    local nums = {}
    for k, v in pairs(args) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end

local function makeHorizontalRule()
    local row = mw.html.create('tr')
    row
        :tag('td')
            :attr('colspan', '2')
            :tag('hr', {selfClosing = true})
    return tostring(row)
end

local image, text = getImageName(s) 

local function makeItem(image, text)
   	
    local root = mw.html.create('table')
    root
        :css('float', 'left')
        :css('padding', '0px 5px')
        :tag('tr')
            :tag('td')
                :wikitext(image)
                :done()
            :tag('td')
                :css('width', '125px')
                :css('text-align', 'left')
                :wikitext(text)
    return tostring(root)
end

local function makeRow(items, heading, subheading, options)
    if #items < 1 then return end
    local swapHeadingSize = type(options) == 'table' and options.swapHeadingSize or false
    local row = mw.html.create('tr')
    row
        :tag('td')
            :css('width', '175px')
            :tag('span')
                :css('font-size', swapHeadingSize and '90%' or '125%')
                :wikitext(heading)
                :done()
            :tag('br', {selfClosing = true})
                :done()
            :tag('span')
                :css('font-size', swapHeadingSize and '125%' or '90%')
                :wikitext(subheading)
    local cell = row:tag('td')
    for i, item in ipairs(items) do
        local image = item[1]
        local text = item[2]
        cell
            :wikitext(makeItem(image, text))
    end
    return tostring(row)
end

local function makeNumberedRow(prefix, args, heading, subheading, getItemValsFunc, options)
    if args[prefix] then
        args[prefix .. '1'] = args[prefix]
    end
    local argNums = getArgNums(prefix, args)
    local items = {}
    for i, argNum in ipairs(argNums) do
        local image, text = getItemValsFunc(args[prefix .. tostring(argNum)])
        table.insert(items, {image, text})
    end
    return makeRow(items, heading, subheading, options)
end

function p._main(args)
    local rows = {}

    -- Get the book row text.
    local bookHeading = "'''[[Wikipedia:Books|Books]]'''"
    local bookSubheading = 'View or order collections of articles'
    local function getBookItemVals(book)
        local image = '[[File:Office-book.svg|30px|alt=|link=]]'
        local text = mw.ustring.format("'''''[[Book:%s|%s]]'''''", book, book)
        return image, text
    end
    local bookRow = makeNumberedRow('book', args, bookHeading, bookSubheading, getBookItemVals)
    table.insert(rows, bookRow)

    -- Get the portal row text
    local portalHeading = "'''[[Portal:Contents/Portals|Portals]]'''"
    local portalSubheading = 'Access related topics'
    local function getPortalItemVals(portal)
        local image = mw.ustring.format('[[File:%s|30x30px]]', getPortalImage2)
        local text = mw.ustring.format("'''''[[Portal:%s|%s portal]]'''''", portal, portal)
        return image, text
    end
    local portalRow = makeNumberedRow('portal', args, portalHeading, portalSubheading, getPortalItemVals)
    table.insert(rows, portalRow)

    -- Get the sister projects row text.
    local sisters = {
        {arg = 'commons', image = 'Commons-logo.svg', prefix = 'commons', display = 'Media', from = 'Commons'},
        {arg = 'species', image = 'Wikispecies-logo.svg', prefix = 'wikispecies', display = 'Species directories', from = 'Wikispecies'},
        {arg = 'voy', image = 'Wikivoyage-Logo-v3-icon.svg', prefix = 'voy', display = 'Travel guides', from = 'Wikivoyage'},
        {arg = 'n', image = 'Wikinews-logo.svg', prefix = 'wikinews', display = 'News stories', from = 'Wikinews'},
        {arg = 'wikt', image = 'Wiktionary-logo-en.svg', prefix = 'wiktionary', postfix = 'English', display = 'Definitions', from = 'Wiktionary'},
        {arg = 'b', image = 'Wikibooks-logo.svg', prefix = 'wikibooks', display = 'Textbooks', from = 'Wikibooks'},
        {arg = 'q', image = 'Wikiquote-logo.svg', prefix = 'wikiquote', display = 'Quotations', from = 'Wikiquote'},
        {arg = 's', image = 'Wikisource-logo.svg', prefix = 'wikisource', display = 'Source texts', from = 'Wikisource'},
        {arg = 'v', image = 'Wikiversity-logo.svg', prefix = 'wikiversity', display = 'Learning resources', from = 'Wikiversity'},
        {arg = 'd', image = 'Wikidata-logo.svg', prefix = 'wikidata', display = 'Data', from = 'Wikidata'},
        {arg = 'spoken', image = 'Sound-icon.svg', prefix = 'spoken wikipedia', display = 'Listen to this page', from = 'Spoken Wikipedia'},
    }
    local sisterItems = {}
    for i, t in ipairs(sisters) do
        if args[t.arg] then
            -- Get the image value.
            local image = mw.ustring.format('[[File:%s|30x30px|alt=|link=]]', t.image)
            -- Get the text value.
            local prefix = t.prefix
            local search = args[t.arg .. '-search'] or mw.title.getCurrentTitle().text
            local postfix = t.postfix
            postfix = postfix and ('#' .. postfix) or ''
            local display = t.display
            local from = t.from
            local text = mw.ustring.format(
                '[[%s:Special:Search/%s%s|%s]]<br />from %s',
                prefix,    search,    postfix, display, from
            )
            if t.arg == 'spoken' then
            	 text = mw.ustring.format('%s on %s<br />[[File:%s]]',
                				display, from, args[t.arg] 
                )		
            end
            -- Add the values to the items table.
            table.insert(sisterItems, {image, text})
        end
    end
    local sisterHeading = "Find out more on Wikipedia's"
    local sisterSubheading = "'''[[Wikipedia:Wikimedia sister projects|Sister projects]]'''"
    local sisterRow = makeRow(sisterItems, sisterHeading, sisterSubheading, {swapHeadingSize = true})
    table.insert(rows, sisterRow)

    -- Make the table.
    local root = mw.html.create('table')
    root
        :attr('role', 'presentation')
        :addClass('noprint')
        :addClass('navbox')
        :addClass('metadata')
        :addClass('plainlist')
        :css('background-color', '#f9f9f9')
        :css('border', '1px solid #aaa')
        :css('clear', 'both')
        :css('margin-bottom', '0.5em')
        :css('margin-top', '0.5em')
        :wikitext(table.concat(rows, makeHorizontalRule()))

    return tostring(root)
end

function p.main(frame)
    -- If called via #invoke, use the args passed into the invoking template, or the args passed to #invoke if any exist. Otherwise
    -- assume args are being passed directly in from the debug console or from another Lua module.
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs(frame.args) do
            origArgs = frame.args
            break
        end
    else
        origArgs = frame
    end
    -- Remove blank arguments.
    local args = {}
    for k, v in pairs(origArgs) do
        if v ~= '' then
            args[k] = v
        end
    end
    return p._main(args)
end

return p