Module:Sandbox/Nullzero/AutomaticTOC explained

z = ;

-- main functionfunction 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 "
" .. name .. "
\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("%") 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) end end

if #complete_list

0 then return errorPrinter end if #pre

0 then prebreak = "" end if #post

0 then postbreak = "" end out = '

' if args.db then out = '
' .. out .. '
' end return frame:preprocess(out)end

-- handle parameterfunction handle_param(value, default) if (value

nil or value

"¬") then return default else return value endend

-- from Module:Coordinatesfunction errorPrinter(errors) local result = "" for i,v in ipairs(errors) do local errorHTML = 'AutomaticTOC: ' .. v[2] .. '' result = result .. errorHTML .. "
" end return resultend

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

-- http://lua-users.org/wiki/SplitJoinfunction Split(str, delim, maxNb) -- Eliminate bad cases... if string.find(str, delim)

nil then if str

"" then return else return 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 resultend

return z

-- 2 May 2013:‎ Thanks to for helping improving the code :)