Module:Citation/CS1/Utilities: Difference between revisions

    m>Redrose64
    (this kind of markup is no longer allowed, and puts pages in Category:Pages using invalid self-closed HTML tags)
    m>Trappist the monk
    (synch from sandbox;)
    Line 13: Line 13:
    ]]
    ]]


    local cfg; -- table of tables imported from slected Module:Citation/CS1/Configuration
    local cfg; -- table of tables imported from selected Module:Citation/CS1/Configuration




    Line 19: Line 19:


    Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.
    Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.
    This function is global because it is called from both this module and from Date validation


    ]]
    ]]
    local function is_set( var )
    local function is_set( var )
    return not (var == nil or var == '');
    return not (var == nil or var == '');
    Line 62: Line 62:


    ]]
    ]]
    local function error_comment( content, hidden )
    local function error_comment( content, hidden )
    return substitute( hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content );
    return substitute( hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content );
    end
    --[=[-------------------------< M A K E _ W I K I L I N K >----------------------------------------------------
    Makes a wikilink; when bot link and display text is provided, returns a wikilink in the form [[L|D]]; if only
    link is provided, returns a wikilink in the form [[L]]; if neither are provided or link is omitted, returns an
    empty string.
    ]=]
    local function make_wikilink (link, display)
    if is_set (link) then
    if is_set (display) then
    return table.concat ({'[[', link, '|', display, ']]'});
    else
    return table.concat ({'[[', link, ']]'});
    end
    else
    return '';
    end
    end
    end


    Line 73: Line 95:


    ]]
    ]]
    local function set_error( error_id, arguments, raw, prefix, suffix )
    local function set_error( error_id, arguments, raw, prefix, suffix )
    local error_state = cfg.error_conditions[ error_id ];
    local error_state = cfg.error_conditions[ error_id ];
    Line 80: Line 103:
    if error_state == nil then
    if error_state == nil then
    error( cfg.messages['undefined_error'] );
    error( cfg.messages['undefined_error'] ); -- because missing error handler in Module:Citation/CS1/Configuration
    elseif is_set( error_state.category ) then
    elseif is_set( error_state.category ) then
    table.insert( z.error_categories, error_state.category );
    table.insert( z.error_categories, error_state.category );
    Line 86: Line 109:
    local message = substitute( error_state.message, arguments );
    local message = substitute( error_state.message, arguments );
     
    message = message .. " ([[" .. cfg.messages['help page link'] ..  
    message = table.concat (
    "#" .. error_state.anchor .. "|" ..
    {
    cfg.messages['help page label'] .. "]])";
    message,
    ' (',
    make_wikilink (
    table.concat (
    {
    cfg.messages['help page link'],
    '#',
    error_state.anchor
    }),
    cfg.messages['help page label']),
    ')'
    });
     
    -- message = table.concat ({message, ' (', substitute (cfg.presentation['wikilink'],
    -- {cfg.messages['help page link'] .. '#' .. error_state.anchor, cfg.messages['help page label']}), ')'});
    -- message = message .. " ([[" .. cfg.messages['help page link'] ..  
    -- "#" .. error_state.anchor .. "|" ..
    -- cfg.messages['help page label'] .. "]])";
    z.error_ids[ error_id ] = true;
    z.error_ids[ error_id ] = true;
    Line 266: Line 306:


    Gets the display text from a wikilink like [[A|B]] or [[B]] gives B
    Gets the display text from a wikilink like [[A|B]] or [[B]] gives B
    The str:gsub() returns either A|B froma [[A|B]] or B from [[B]] or B from B (no wikilink markup).
    In l(), l:gsub() removes the link and pipe (if they exist); the second :gsub() trims white space from the label
    if str was wrapped in wikilink markup.  Presumably, this is because without wikimarkup in str, there is no match
    in the initial gsub, the replacement function l() doesn't get called.


    ]=]
    ]=]
    Line 273: Line 319:
    return l:gsub( "^[^|]*|(.*)$", "%1" ):gsub("^%s*(.-)%s*$", "%1");
    return l:gsub( "^[^|]*|(.*)$", "%1" ):gsub("^%s*(.-)%s*$", "%1");
    end));
    end));
    end
    --[=[-------------------------< I S _ W I K I L I N K >--------------------------------------------------------
    Determines if str is a wikilink, extracts, and returns the the wikilink type, link text, and display text parts.
    If str is a complex wikilink ([[L|D]]):
    returns wl_type 2 and D and L from [[L|D]];
    if str is a simple wikilink ([[D]])
    returns wl_type 1 and D from [[D]] and L as empty string;
    if not a wikilink:
    returns wl_type 0, str as D, and L as empty string.
    trims leading and trailing white space and pipes from L and D ([[L|]] and [[|D]] are accepted by MediaWiki and
    treated like [[D]]; while [[|D|]] is not accepted by MediaWiki, here, we accept it and return D without the pipes).
    ]=]
    local function is_wikilink (str)
    local D, L
    local wl_type = 2; -- assume that str is a complex wikilink [[L|D]]
    L, D = str:match ('%[%[([^|]+)|([^%]]+)%]%]'); -- get L and D from [[L|D]]
    if not is_set (D) then -- if no separate link
    D = str:match ('%[%[([^%]]*)|*%]%]'); -- get D from [[D]]
    wl_type = 1;
    end
    if not is_set (D) then -- no wikilink markup
    D = str; -- return the string as D
    wl_type = 0; -- but say that it is not a wikilink
    end
    D = mw.text.trim (D, '%s|'); -- trim white space and pipe characters
    L = L and mw.text.trim (L, '%s|');
    return wl_type, D, L or '';
    end
    end


    Line 284: Line 368:
    local function set_selected_modules (cfg_table_ptr)
    local function set_selected_modules (cfg_table_ptr)
    cfg = cfg_table_ptr;
    cfg = cfg_table_ptr;
    end
    end


    Line 296: Line 381:
    select_one = select_one,
    select_one = select_one,
    add_maint_cat = add_maint_cat,
    add_maint_cat = add_maint_cat,
    wrap_style = wrap_style;
    wrap_style = wrap_style,
    safe_for_italics = safe_for_italics;
    safe_for_italics = safe_for_italics,
    remove_wiki_link = remove_wiki_link;
    remove_wiki_link = remove_wiki_link,
    set_selected_modules = set_selected_modules;
    is_wikilink = is_wikilink,
    make_wikilink = make_wikilink,
    set_selected_modules = set_selected_modules,
    z = z,
    z = z,
    }
    }