Module:Sandbox/Nullzero/AutomaticTOC

z = {};

-- main function
function z.main(frame)
    local args = frame.args
    local title = handle_param(args.title, nil)
    title = title and mw.title.new(args.title) or mw.title.getCurrentTitle()
    local delim = handle_param(args.delim, ",")
    local sep = handle_param(args.sep, " ")
    local short = handle_param(args.short, nil)
    local name = handle_param(args.name, '')
    name = (name == '') and '' or "'''" .. name .. "'''"
    name = "|" .. (short and name or "<center>" .. name .. "</center>\n|-") .. "\n|"
    
    local include = args.include and Set(Split(args.include, "%" .. delim)) or {}
    local exclude = args.exclude and Set(Split(args.exclude, "%" .. delim)) or {}
    
    local pre = args.pre and Split(args.pre, "%" .. delim) or {}
    local prebreak = handle_param(args.prebreak, sep)
    
    local post = args.post and Split(args.post, "%" .. delim) or {}
    local postbreak = handle_param(args.postbreak, sep)
    
    local prefix = handle_param(args.prefix, "")
    local suffix = handle_param(args.suffix, "")
    
    local anchor = handle_param(args.anchor, nil)
    local wiki_text = title:getContent()
    local all = {}
    
    local level = handle_param(tonumber(args.level), 1)
    
    eval = function(v)
        v = v:match "^%s*(.-)%s*$"
        if v ~= "" then
            if (mw.ustring.len(v) <= level or include[v]) then table.insert(all, v) end
        end
    end
    
    for line in wiki_text:gmatch("[^\n]+") do
        ---
        eval(string.gsub(line:match("^=+%s*(.-)%s*=+%s-$") or "", "%b{}", ""))
        if anchor then
            for _, v in pairs(Split(line:match("%{%{[Aa]nchor%|(.-)%}%}") or "", "%|")) do eval(v) end
        end
        ---
    end
    
    table.sort(all, function(a, b) return mw.ustring.codepoint(a) < mw.ustring.codepoint(b) end)
    
    complete_list = {}
    for k, v in pairs(all) do
        if not exclude[v] and (k == 0 or (k > 0 and all[k - 1] ~= v)) then
            table.insert(complete_list, table.concat{"[[#", v, "|", prefix, v, suffix, "]]"})
        end
    end

    if #complete_list == 0 then
        return errorPrinter({{"", "no section link found[[Category:Page transcluded AutomaticTOC without section link]]"}})
    end
    if #pre == 0 then prebreak = "" end
    if #post == 0 then postbreak = "" end
    
    out = '{| class="toccolours" style="margin: 0px auto;"\n' .. name ..
          table.concat(pre, sep) .. prebreak .. table.concat(complete_list, sep) .. postbreak ..
          table.concat(post, sep) .. ' __NOTOC__\n|}'
          
    if args.db then out = '<pre>' .. out .. '</pre>' end
    return frame:preprocess(out)
end

-- handle parameter
function handle_param(value, default)
    if (value == nil or value == "¬") then
        return default
    else
        return value
    end
end

-- from Module:Coordinates
function errorPrinter(errors)
    local result = ""
    for i,v in ipairs(errors) do
        local errorHTML = '<strong class="error">AutomaticTOC: ' .. v[2] .. '</strong>'
        result = result .. errorHTML .. "<br />"
    end
    return result
end

-- http://stackoverflow.com/questions/656199/search-for-an-item-in-a-lua-list
function Set (list)
    local set = {}
    for _, l in pairs(list) do set[l] = true end
    return set
end

-- http://lua-users.org/wiki/SplitJoin
function Split(str, delim, maxNb)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then
        if str == "" then return {} else return {str} end
    end
    if maxNb == nil or maxNb < 1 then
        maxNb = 0    -- No limit
    end
    local result = {}
    local pat = "(.-)" .. delim .. "()"
    local nb = 0
    local lastPos
    for part, pos in string.gfind(str, pat) do
        nb = nb + 1
        result[nb] = part
        lastPos = pos
        if nb == maxNb then break end
    end
    -- Handle the last field
    if nb ~= maxNb then
        nb = nb + 1
        result[nb] = string.sub(str, lastPos)
    end
    if result[nb] == "" then table.remove(result, nb) end
    return result
end

return z

-- 2 May 2013:‎ Thanks to [[:en:User:Darklama]] for helping improving the code :)

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.