Zum Inhalt springen

„Modul:Vorlage:TemplateDataGenerator“ – Versionsunterschied

aus Wikipedia, der freien Enzyklopädie
[gesichtete Version][gesichtete Version]
Inhalt gelöscht Inhalt hinzugefügt
Grrrmpff
update
Zeile 1: Zeile 1:
--[=[ 2013-07-29
--[=[ 2013-07-30
{{TemplateDataGenerator}}
{{TemplateDataGenerator}}
Basic idea by [[w:en:User:Salix alba]]
Basic idea by [[w:en:User:Salix alba]]
Zeile 20: Zeile 20:
-- One of various indentation styles.
-- One of various indentation styles.
-- Feel free to compose a different one, also using config.shift etc.
-- Feel free to compose a different one, also using config.shift etc.
}
};






local function factory( analyze, alphabetical )
local function factory( analyze, alphabetical )
-- Make parameter sequence from template source text
-- analyze -- string; template source text
-- analyze -- string; template source text
-- alphabetical -- boolean or nil; sort parameter list
-- alphabetical -- boolean or nil; sort parameter list
-- Return:
-- Return:
-- string with JSON code
-- table (sequence) with parameter names
-- Uses:
local i, s;
-- > config.shift
local r = { };
-- > config.scheme
local i;
local params = { };
local r = '{ "description": "",\n';
local s;
local shift = config.shift or "";
local start = " ";
r = r .. ' "params": { ';
for s in analyze:gmatch( "{{{([^|}\n]+)" ) do
for s in analyze:gmatch( "{{{([^|}\n]+)" ) do
for i = 1, #params do
for i = 1, #r do
if params[ i ] == s then
if r[ i ] == s then
s = false;
s = false;
break; -- for i
break; -- for i
Zeile 47: Zeile 40:
end -- for i
end -- for i
if s then
if s then
table.insert( params, s );
table.insert( r, s );
end
end
end -- for s in :gmatch()
end -- for s in :gmatch()
if alphabetical then
if alphabetical then
table.sort( params, nil );
table.sort( r, nil );
end
end
return r;
end -- factory()



local function format( analyze, alphabetical )
-- Make JSON code from template source text
-- analyze -- string; template source text
-- alphabetical -- boolean or nil; sort parameter list
-- Return:
-- string with JSON code
-- Uses:
-- > config.shift
-- > config.scheme
-- factory()
local i;
local params = factory( analyze, alphabetical );
local r = '{ "description": "",\n';
-- local shift = config.shift or ""; -- currently unused
local start = " ";
r = r .. ' "params": { ';
for i = 1, #params do
for i = 1, #params do
if i > 1 then
if i > 1 then
Zeile 62: Zeile 76:
r = string.format( "%s\n%s}\n}", r, start );
r = string.format( "%s\n%s}\n}", r, start );
return r;
return r;
end -- factory()
end -- format()




Zeile 77: Zeile 91:
-- > config.start
-- > config.start
-- > config.suffix
-- > config.suffix
-- factory()
-- format()
local r;
local r;
local source = string.match( attempt.baseText .. "/",
local source = string.match( attempt.baseText .. "/",
Zeile 97: Zeile 111:
r = string.format( spec,
r = string.format( spec,
r,
r,
factory( title:getContent(), luxury ),
format( title:getContent(), luxury ),
config.suffix or "" );
config.suffix or "" );
-- note that format spec is ASCII only; string.format() will do
-- note that format spec is ASCII only; string.format() will do
Zeile 111: Zeile 125:
local p = {};
local p = {};


function p.test( about, ahead, alphabetical )
function p.getBlock( about, ahead, alphabetical )
-- Precondition:
-- Precondition:
-- about -- string; page title related to template code
-- about -- string; page title related to template code
Zeile 121: Zeile 135:
local lucky, r = pcall( fun, title, alphabetical );
local lucky, r = pcall( fun, title, alphabetical );
return r;
return r;
end -- .getBlock()
end




Zeile 133: Zeile 147:
local luxury;
local luxury;
local parental = frame:getParent().args;
local parental = frame:getParent().args;
local sort = parental[ 1 ] or parental.sort;
local sort = parental[ 1 ] or parental[ "1" ] or parental.sort;
if sort then
if sort then
luxury = ( tonumber( sort) == 1 );
luxury = ( tonumber( sort) == 1 );
Zeile 139: Zeile 153:
local lucky, r = pcall( fun, mw.title.getCurrentTitle(), luxury );
local lucky, r = pcall( fun, mw.title.getCurrentTitle(), luxury );
return "<pre>" .. r .. "</pre>";
return "<pre>" .. r .. "</pre>";
end
end -- .f()


return p
return p;

Version vom 30. Juli 2013, 12:35 Uhr

Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus

Dies ist die (produktive) Mutterversion eines global benutzten Lua-Moduls.
Wenn die serial-Information nicht übereinstimmt, müsste eine Kopie hiervon in das lokale Wiki geschrieben werden.
Versionsbezeichnung auf WikiData: 2016-11-23

--[=[ 2013-07-30
{{TemplateDataGenerator}}
Basic idea by [[w:en:User:Salix alba]]
]=]



local config = {
    luxury = false,    -- default alphabetical order for parameter list
    start  = "{{templateData|top}}",    -- preceeding lines
    shift  = "   ",    -- (not used now) indentation, like "   " or "\t"
    suffix = false,    -- following lines
    scheme = [=["%s":
              { "label":       "",
                "description": "",
                "type":        "string",
                "required":    false
              }]=]
    -- config.scheme has one placeholder %s for the parameter name.
    -- One of various indentation styles.
    -- Feel free to compose a different one, also using config.shift etc.
};



local function factory( analyze, alphabetical )
    -- Make parameter sequence from template source text
    --     analyze       -- string; template source text
    --     alphabetical  -- boolean or nil; sort parameter list
    -- Return:
    --     table (sequence) with parameter names
    local i, s;
    local r = { };
    for s in analyze:gmatch( "{{{([^|}\n]+)" ) do
        for i = 1, #r do
            if r[ i ] == s then
                s = false;
                break; -- for i
            end
        end -- for i
        if s then
            table.insert( r, s );
        end
    end -- for s in :gmatch()
    if alphabetical then
        table.sort( r, nil );
    end
    return r;
end -- factory()



local function format( analyze, alphabetical )
    -- Make JSON code from template source text
    --     analyze       -- string; template source text
    --     alphabetical  -- boolean or nil; sort parameter list
    -- Return:
    --     string with JSON code
    -- Uses:
    --     >  config.shift
    --     >  config.scheme
    --     factory()
    local i;
    local params = factory( analyze, alphabetical );
    local r      = '{ "description": "",\n';
--  local shift  = config.shift or "";    -- currently unused
    local start  = "            ";
    r = r ..       '  "params": { ';
    for i = 1, #params do
        if i > 1 then
            r = string.format( "%s,\n%s  ", r, start );
        end
        r = r .. string.format( config.scheme,  params[ i ] );
        -- common JSON pattern is ASCII; string.format() will do
    end -- for i
    r = string.format( "%s\n%s}\n}", r, start );
    return r;
end -- format()



local function fun( attempt, alphabetical )
    -- Retrieve used template params and build TemplateData skeleton
    -- Precondition:
    --     attempt       -- mw.title object; related to template code
    --     alphabetical  -- boolean or nil; sort parameter list
    -- Return:
    --     string to be applied
    -- Uses:
    --     >  config.luxury
    --     >  config.start
    --     >  config.suffix
    --     format()
    local r;
    local source = string.match( attempt.baseText .. "/",
                                 "^([^/]+)/" );
                   -- ensure top page in NS with no subpage property
                   -- note that pattern is ASCII; string.match() will do
    local title  = mw.title.makeTitle( attempt.namespace, source );
    if title.exists then
        local luxury = config.luxury;
        local spec   = "%s<templatedata>\n%s\n</templatedata>\n%s";
        if type( alphabetical ) == "boolean" then
            luxury = alphabetical;
        end
        if config.start then
            r = config.start .. "\n";
        else
            r = "";
        end
        r = string.format( spec,
                           r,
                           format( title:getContent(), luxury ),
                           config.suffix or "" );
        -- note that format spec is ASCII only; string.format() will do
    else    -- test only
        r = "ERROR * no page " .. title.fullText;
    end
    return r;
end -- fun()



-- Export
local p = {};

function p.getBlock( about, ahead, alphabetical )
    -- Precondition:
    --     about         -- string; page title related to template code
    --     ahead         -- string, number or nil; namespace (Template:)
    --     alphabetical  -- boolean or nil; sort parameter list
    -- Uses:
    --     fun()
    local title = mw.title.makeTitle( ahead or 10,  about );
    local lucky, r = pcall( fun, title, alphabetical );
    return r;
end -- .getBlock()



function p.f( frame )
    -- Precondition:
    --     frame  -- object
    --     Invoked on a template page or template subpage.
    -- Uses:
    --     fun()
    local luxury;
    local parental = frame:getParent().args;
    local sort     = parental[ 1 ] or parental[ "1" ] or parental.sort;
    if sort then
        luxury = ( tonumber( sort) == 1 );
    end
    local lucky, r = pcall( fun, mw.title.getCurrentTitle(), luxury );
    return "<pre>" .. r .. "</pre>";
end -- .f()

return p;