Difference between revisions of "Module:Languages"

From Minetest Wiki
Jump to navigation Jump to search
m (Bold language text if language is current language)
m
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
local HtmlBuilder = require("Module:HtmlBuilder")
+
local TableBuilder = require("Module:TableBuilder")
 +
local getLanguageCode = require("Module:Languages").getLanguageCode
 +
local animateImages = require("Module:Animated")._images
 
local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc
 
local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc
local headers = mw.loadData('Module:Languages/headers')
+
local dataPrefix = "Module:Infobox/"
  
-- Get the language code of a page.
+
-- Get data (IDs and names) for infobox row headers.
 
--
 
--
-- @param titleObj Title object of the page, defaults to current page
+
-- @param type Infobox type (e.g. block)
function p.getLanguageCode(titleObj)
+
-- @param langCode Language code, defaults to current language
     titleObj = titleObj or mw.title.getCurrentTitle() -- Set default if necessary
+
local function getRowHeadersData(type, langCode, frame)
 
+
    langCode = langCode or getLanguageCode()
     local title = titleObj.text -- e.g. Dirt/fr or Mods/Quartz
+
     frame = frame or mw.getCurrentFrame()
    local langCode = titleObj.subpageText -- e.g. fr or Quartz
+
     local title = dataPrefix .. langCode
 
+
     if frame:preprocess("{{#ifexist:" .. title .. "|true}}") ~= "" then
     -- titleObj.subpageText returns titleObj.text if not a subpage.
+
        local data = mw.loadData(title)
    -- If titleObj.subpageText is a subpage, check if it is a language code.
+
         return data and data[type] or nil
    if langCode == title or not headers[langCode] then
 
         return "en" -- Default to English
 
 
     end
 
     end
 
+
     return nil
     return langCode
 
 
end
 
end
  
-- Create the linked text for a translation subpage if it exists.
+
-- Create an infobox.
 
--
 
--
-- @param frame Current frame, used for preprocessing
+
-- @param args Infobox arguments
-- @param title Page title
+
-- @param type Infobox type (e.g. block)
-- @param langCode Language code
+
local function infobox(args, type)
local function createLanguageText(frame, title, langCode)
+
    local rowHeadersData = getRowHeadersData(type) or getRowHeadersData(type, "en")
     title = title .. "/" .. langCode
+
    local name = args.name
 +
    local width = args.width or "200px"
 +
    local description = args.description
 +
    local image = args.image or "Blank.png"
 +
    local imagesize = args.imagesize
 +
    local imageText
 +
    if string.match(image, ",") then
 +
        imageText = animateImages({image, imagesize})
 +
     else
 +
        if imagesize then
 +
            imageText = "[[File:" .. image .. "|" .. imagesize .. "]]"
 +
        else
 +
            imageText = "[[File:" .. image .. "]]"
 +
        end
 +
    end
  
    -- Get language name in native language (a.k.a. autonym)
+
     local infobox = TableBuilder.create()
     local parsedLang = mw.language.fetchLanguageName(langCode)
 
  
     -- Use <bdi> to support both LTR languages and RTL languages
+
     infobox
    local bdi = HtmlBuilder.create("bdi")
+
        :addClass("wikitable")
         :attr("lang", langCode)
+
        :css("float", "right")
         :wikitext("[[:", title, "|", parsedLang, "]]")
+
        :css("text-align", "left")
 +
        :css("margin", "0 0 0.5em 1em")
 +
        :css("padding", "5px")
 +
        :css("font-size", "90%")
 +
        :css("position", "relative")
 +
        :css("clear", "right")
 +
        :css("overflow", "auto")
 +
        :css("z-index", "1")
 +
         :attr("width", width)
 +
         :addRow()
 +
            :addHeader()
 +
                :attr("colspan", 2)
 +
                :css("font-size", "110%")
 +
                :css("text-align", "center")
 +
                :wikitext(name)
 +
                :done()
 +
            :done()
 +
        :addRow()
 +
            :addHeader()
 +
                :attr("colspan", 2)
 +
                :tag("div")
 +
                    :addClass("center")
 +
                    :wikitext(imageText)
 +
                    :done()
 +
                :done()
 +
            :done()
 +
        :addRow()
 +
            :addHeader()
 +
                :attr("colspan", 2)
 +
                :attr("align", "center")
 +
                :wikitext(description)
  
     -- Use preprocessing with #ifexist to check if page exists
+
     for _, rowHeaderData in ipairs(rowHeadersData) do
    return frame:preprocess("{{#ifexist:" .. title .. "|&nbsp;• " .. tostring(bdi) .. "}}")
+
        local rowData = args[rowHeaderData.id]
end
+
        if rowData and rowData ~= "" then
 
+
            infobox
function p._languages(frame, args)
+
                :addRow()
    local currentTitleObj = mw.title.getCurrentTitle()
+
                    :addData()
    local currentLangCode = p.getLanguageCode(currentTitleObj)
+
                        :wikitext("'''" .. rowHeaderData.name .. "'''")
    local currentHeader = headers[currentLangCode]
+
                        :done()
 
+
                    :addData()
    local title = args[1] -- Use input page name if given
+
                        :wikitext(rowData)
    if not title then -- Otherwise, use current page
 
        local namespace = currentTitleObj.nsText
 
        local basename = currentTitleObj.baseText
 
        title = namespace .. ":" .. basename -- e.g. Template:Block Data
 
 
 
        -- Check if current page is English subpage (e.g. Mods/Quartz)
 
        if currentLangCode == "en" then
 
            local subname = currentTitleObj.subpageText
 
            if subname ~= basename then
 
                title = title .. "/" .. subname
 
            end
 
 
         end
 
         end
 
     end
 
     end
  
     local languagesText = "[[:" .. title .. "|English]]" -- Start with English first
+
     return infobox
 +
end
  
    -- Bold English text if current language is English
+
function p._block(args)
     if currentLangCode == "en" then
+
     args.imagesize = args.imagesize or "150px"
        languagesText = "'''" .. languagesText .. "'''"
+
    return tostring(infobox(args, "block"))
    end
+
end
  
     local translated = false -- Flag for checking if any translation exists
+
function p._item(args)
 +
     args.imagesize = args.imagesize or "160px"
 +
    return tostring(infobox(args, "item"))
 +
end
  
    -- Create an alphabetically sorted table of language codes
+
function p._foodItem(args)
     local langCodes = {}
+
     args.imagesize = args.imagesize or "160px"
     for langCode in pairs(headers) do
+
     return tostring(infobox(args, "foodItem"))
        table.insert(langCodes, langCode)
+
end
    end
 
    table.sort(langCodes)
 
  
    -- Iterate through language codes in order
+
function p._mob(args)
    for _, langCode in ipairs(langCodes) do
+
    args.imagesize = args.imagesize or "150px"
        if langCode ~= "en" then -- Skip English because already added
+
    return tostring(infobox(args, "mob"))
            local languageText = createLanguageText(frame, title, langCode)
+
end
            if languageText ~= "" then -- Check if subpage exists
 
                -- Bold language text if language is current language
 
                if langCode == currentLangCode then
 
                    languageText = "'''" .. languageText .. "'''"
 
                end
 
                languagesText = languagesText .. languageText
 
                translated = true
 
            end
 
        end
 
    end
 
  
     local nmbox = frame:expandTemplate{
+
function p._game(args)
        title = "nmbox",
+
     args.imagesize = args.imagesize or "150px"
        args = {
+
    return tostring(infobox(args, "game"))
            image = "[[File:Geographylogo.png|25px|Languages|link=]]",
+
end
            header = currentHeader,
 
            text = languagesText
 
        }
 
    }
 
 
 
    if not args.nocat then
 
        local categories = "[[Category:Languages pages]]"
 
        if not translated then
 
            categories = categories .. "[[Category:Languages pages without translations]]"
 
        end
 
        return nmbox .. categories
 
    end
 
  
     return nmbox
+
function p._mod(args)
 +
    args.imagesize = args.imagesize or "150px"
 +
     return tostring(infobox(args, "mod"))
 
end
 
end
  
p.languages = makeInvokeFunc(p._languages, {passFrameParam = true, inherited = true})
+
p.block = makeInvokeFunc(p._block, {inherited = true})
 +
p.item = makeInvokeFunc(p._item, {inherited = true})
 +
p.foodItem = makeInvokeFunc(p._foodItem, {inherited = true})
 +
p.mob = makeInvokeFunc(p._mob, {inherited = true})
 +
p.game = makeInvokeFunc(p._game, {inherited = true})
 +
p.mod = makeInvokeFunc(p._mod, {inherited = true})
  
 
return p
 
return p

Revision as of 08:03, 26 May 2017

local p = {}

local TableBuilder = require("Module:TableBuilder") local getLanguageCode = require("Module:Languages").getLanguageCode local animateImages = require("Module:Animated")._images local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc local dataPrefix = "Module:Infobox/"

-- Get data (IDs and names) for infobox row headers. -- -- @param type Infobox type (e.g. block) -- @param langCode Language code, defaults to current language local function getRowHeadersData(type, langCode, frame)

   langCode = langCode or getLanguageCode()
   frame = frame or mw.getCurrentFrame()
   local title = dataPrefix .. langCode
   if frame:preprocess("") ~= "" then
       local data = mw.loadData(title)
       return data and data[type] or nil
   end
   return nil

end

-- Create an infobox. -- -- @param args Infobox arguments -- @param type Infobox type (e.g. block) local function infobox(args, type)

   local rowHeadersData = getRowHeadersData(type) or getRowHeadersData(type, "en")
   local name = args.name
   local width = args.width or "200px"
   local description = args.description
   local image = args.image or "Blank.png"
   local imagesize = args.imagesize
   local imageText
   if string.match(image, ",") then
       imageText = animateImages({image, imagesize})
   else
       if imagesize then
           imageText = "" .. imagesize .. ""
       else
           imageText = "File:" .. image .. ""
       end
   end
   local infobox = TableBuilder.create()
   infobox
       :addClass("wikitable")
       :css("float", "right")
       :css("text-align", "left")
       :css("margin", "0 0 0.5em 1em")
       :css("padding", "5px")
       :css("font-size", "90%")
       :css("position", "relative")
       :css("clear", "right")
       :css("overflow", "auto")
       :css("z-index", "1")
       :attr("width", width)
       :addRow()
           :addHeader()
               :attr("colspan", 2)
               :css("font-size", "110%")
               :css("text-align", "center")
               :wikitext(name)
               :done()
           :done()
       :addRow()
           :addHeader()
               :attr("colspan", 2)
               :tag("div")
                   :addClass("center")
                   :wikitext(imageText)
                   :done()
               :done()
           :done()
       :addRow()
           :addHeader()
               :attr("colspan", 2)
               :attr("align", "center")
               :wikitext(description)
   for _, rowHeaderData in ipairs(rowHeadersData) do
       local rowData = args[rowHeaderData.id]
       if rowData and rowData ~= "" then
           infobox
               :addRow()
                   :addData()
                       :wikitext("" .. rowHeaderData.name .. "")
                       :done()
                   :addData()
                       :wikitext(rowData)
       end
   end
   return infobox

end

function p._block(args)

   args.imagesize = args.imagesize or "150px"
   return tostring(infobox(args, "block"))

end

function p._item(args)

   args.imagesize = args.imagesize or "160px"
   return tostring(infobox(args, "item"))

end

function p._foodItem(args)

   args.imagesize = args.imagesize or "160px"
   return tostring(infobox(args, "foodItem"))

end

function p._mob(args)

   args.imagesize = args.imagesize or "150px"
   return tostring(infobox(args, "mob"))

end

function p._game(args)

   args.imagesize = args.imagesize or "150px"
   return tostring(infobox(args, "game"))

end

function p._mod(args)

   args.imagesize = args.imagesize or "150px"
   return tostring(infobox(args, "mod"))

end

p.block = makeInvokeFunc(p._block, {inherited = true}) p.item = makeInvokeFunc(p._item, {inherited = true}) p.foodItem = makeInvokeFunc(p._foodItem, {inherited = true}) p.mob = makeInvokeFunc(p._mob, {inherited = true}) p.game = makeInvokeFunc(p._game, {inherited = true}) p.mod = makeInvokeFunc(p._mod, {inherited = true})

return p