Module:Tech news

From Meta, a Wikimedia project coordination wiki
Jump to: navigation, search
Module documentation[create]
-- THIS IS BETA SOFTWARE. USE CAREFULLY AND TEST EXTENSIVELY.

local p = {}



function p.substTranslatedTemplates( content, langCode )
    --[[ This function goes through the content of a translation of a [Tech/News]
        issue and subst:s translated templates included via {{TNT}} or one of 
        its aliases. This allows translations or templates to be included into
        the globally-delivered message.
        Not sure it can be done since substitution happens earlier in the page
        parse process.
    ]]
    
    
    -- {{TNT|template(|)}}
    -- {{Tnt|template(|)}}
    -- {{Translatable navigation template|template(|)}}
    -- {{Translatable template|template(|)}}
    
    -- replace by: {{subst:Translatable template
    
    -- Initialize variables
    
    local templateLang = 'en'
    local template = ''
    local substedContent = content
    
    -- Check if a translation of the template exists in that language; if so, put it in templateLang
    local translation = mw.title.new(template .. '/' .. langCode, 'Template:')
    if (translation.id ~= 0)
    then
        templateLang = langCode
    end

    -- Then render the template and preprocess the content to make subst: work
    
    test = mw.ustring.gsub( content, "{{TNT|", '{{susbt:TNT|', n ) 
    
    frame:preprocess( content )
    
    return content
    
end


function p.assembleNewsletter(frame)
     --[[ This function takes existing translations for a given [Tech/News]
        issue and puts them together to generate the wikicode ready to be
        delivered by [Global message delivery].
    ]]

    -- Initialize the newsletter's static content



    local message = '<pre><section begin="technews-' .. frame.args['year'] .. '-W' .. frame.args['week'] .. '"/>{{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 mw-content-' .. direction .. '" lang="' .. langCode .. '" dir="' .. direction .. '">'
        
        -- Get the translation's content
        
        local issue = 'Tech/News/' .. frame.args['year'] .. '/' .. frame.args['week']
        
        local content = mw.title.new( issue .. '/' .. 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.
        ]]
    
        local startSection, startIndex = mw.ustring.find( content, '<section begin="tech-newsletter-content"/>', 1, true )
        local endIndex = mw.ustring.find( content, '<section end="tech-newsletter-content"/>', 1, true )
    
        content = mw.ustring.sub( content, startIndex +1 , endIndex -1 )
        
        message = message .. content .. '</div>'

        -- TODO: Substs translated templates
        -- content = substTranslatedTemplates( content, langCode )
        
    end
    
    -- Add footer static content
    
    message = message .. '}} <section end="technews-' .. frame.args['year'] .. '-W' .. frame.args['week'] .. '"/> ~~~~~</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