Jump to content

Module:Sandbox

From Meta, a Wikimedia project coordination wiki
Module documentation

Scribunto Testing Area

[edit]

This is not an actual Lua module. It exists to provide a convenient pseudo-namespace for code testing, hopefully preventing the main Module: namespace from becoming littered with experiments, as Lua modules cannot exist as subpages in the User: namespace.

Please name your experimental modules in the following format to help keep things tidy:

Module:Sandbox/Your User Name/Module name

You can use Special:PrefixIndex/Module:Sandbox to list modules in this area.

-- Placeholder
-- THIS IS BETA SOFTWARE. USE CAREFULLY AND TEST EXTENSIVELY.

local p = {}


function p.assembleMessage(frame)
     --[[ This function takes existing translations for a given page
        translated using the translate extension and puts them together to
        generate the wikicode ready to be delivered by [Global message delivery]
    ]]

    -- Initialize the message's static content

    local message = '<pre>{{subst:#switch:{{subst:CONTENTLANG}}'

    --[[ Build the list of language codes for which there is a translation,
        and remove English if it was mistakenly added since we already include
        it as default.
        We can't automatically list all available translations of the issue, so
        we need to input them manually as part of the module's call.
    ]]
    
    translations = {}
    
    for key, value in pairs( frame.args ) do
        if ( mw.language.isKnownLanguageTag( value ) and value ~= 'en' )
        then
            translations[value] = value
        end
    end
    
    -- Add English as default
    
    translations['#default'] = 'en'

    -- Loop through the languages to assemble the available translations

    for switchKey, langCode in pairs( translations ) do
        
        --[[ TODO: add existence check of the translation in case of human
            error. Ignore the incorrect language code and give warning.
        ]]
        
        -- Add new switch key, and language and directionality metadata
        
        local direction = mw.language.new( langCode ):getDir()
        
        --[[ TODO: add switch keys for languages that have a fallback language
            for which we do have a translation, instead of English.
        ]]
    
        message = message .. '|' .. switchKey .. '=<div class="plainlinks" lang="' .. langCode .. '" dir="' .. direction .. '">'
        
        -- Get the translation's content
        
        local page = frame.args['page']
        
        local content = mw.title.new( page .. '/' .. langCode ):getContent()
        
        --[[ Clean up stuff we don't want to include (headers, footers, etc.)
            There's probably a cleaner way to do this but for now it'll work.
            TODO: add error checks.
        ]]
        return startIndex
    end
    
    -- Add footer static content
    
    message = message .. '}} ~~~~~</pre>'
    
    -- Hide nowiki tags from the preprocessor
    
    message = mw.ustring.gsub ( message, '<nowiki>(.-)</nowiki>', '&lt;nowiki&gt;%1&lt;/nowiki&gt;' )
    
    -- Preprocess to make <pre> work
    
    message = frame:preprocess( message )
    
    -- We're done

    return message

end


return p