이 모듈에 대한 설명문서는 모듈:TFR목차/설명문서에서 만들 수 있습니다
local p = {}
local function trim(value)
return mw.text.trim(tostring(value or ''))
end
local function no(value)
value = mw.ustring.lower(trim(value))
return value == '아니오' or value == '아니요' or value == 'no' or value == 'n' or value == '0' or value == 'false'
end
local function removeBlocked(source)
source = source:gsub('<!%-%-[%s%S]-%-%->', '')
local tags = {'nowiki', 'pre', 'source', 'syntaxhighlight', 'math', 'code'}
for _, tag in ipairs(tags) do
source = source:gsub('<' .. tag .. '[^>]*>[%s%S]-</' .. tag .. '%s*>', '')
end
return source
end
local function stripWikiLinks(value)
local previous
repeat
previous = value
value = value:gsub('%[%[(.-)|(.-)%]%]', '%2')
value = value:gsub('%[%[(.-)%]%]', '%1')
until value == previous
value = value:gsub('%[https?://[^%s%]]+%s+([^%]]-)%]', '%1')
return value
end
local function plainHeading(frame, raw)
raw = stripWikiLinks(raw)
local rendered = frame:preprocess(raw)
rendered = mw.text.killMarkers(rendered)
rendered = stripWikiLinks(rendered)
rendered = rendered:gsub('<[^>]->', '')
rendered = mw.text.decode(rendered, true)
rendered = rendered:gsub("''+", '')
rendered = rendered:gsub('%s+', ' ')
return trim(rendered)
end
local function numberText(counters, level)
local parts = {}
for current = 2, level do
if counters[current] and counters[current] > 0 then
parts[#parts + 1] = tostring(counters[current])
end
end
return table.concat(parts, '.')
end
function p.main(frame)
local parent = frame:getParent()
local args = parent and parent.args or frame.args
local title = mw.title.getCurrentTitle()
local source = title:getContent() or ''
source = removeBlocked(source)
local showNumbers = not no(args['번호'] or args.number)
local initialState = trim(args['초기상태'] or args.state)
local collapsed = initialState == '접힘'
local headingTitle = trim(args['제목'] or args.title)
local subtitle = trim(args['부제'] or args.subtitle)
local status = trim(args['상태'] or args.status)
if headingTitle == '' then headingTitle = 'DOCUMENT INDEX' end
if subtitle == '' then subtitle = 'TFR ARCHIVE // SECTION DIRECTORY' end
if status == '' then status = 'SIGNAL ONLINE' end
local headings = {}
local counters = {}
local anchors = {}
for line in source:gmatch('[^\r\n]+') do
local marks, raw = line:match('^%s*(==+)%s*(.-)%s*%1%s*$')
if marks then
local level = #marks
if level >= 2 and level <= 6 and trim(raw) ~= '' then
counters[level] = (counters[level] or 0) + 1
for lower = level + 1, 6 do
counters[lower] = 0
end
local plain = plainHeading(frame, raw)
if plain ~= '' then
anchors[plain] = (anchors[plain] or 0) + 1
local anchor = plain
if anchors[plain] > 1 then
anchor = anchor .. '_' .. anchors[plain]
end
headings[#headings + 1] = {
level = level,
raw = trim(raw),
plain = plain,
anchor = anchor,
number = numberText(counters, level)
}
end
end
end
end
local output = {}
local rootClass = 'tfr-toc mw-collapsible'
if collapsed then rootClass = rootClass .. ' mw-collapsed' end
output[#output + 1] = '<div class="' .. rootClass .. '" data-expandtext="OPEN INDEX" data-collapsetext="CLOSE INDEX">'
output[#output + 1] = '<div class="tfr-toc-header">'
output[#output + 1] = '<div class="tfr-toc-kicker">' .. subtitle .. '</div>'
output[#output + 1] = '<div class="tfr-toc-title">' .. headingTitle .. '</div>'
output[#output + 1] = '<div class="tfr-toc-status">' .. status .. '</div>'
output[#output + 1] = '</div>'
output[#output + 1] = '<div class="tfr-toc-content mw-collapsible-content">'
if #headings == 0 then
output[#output + 1] = '<div class="tfr-toc-empty">NO SECTION SIGNAL DETECTED</div>'
else
for _, heading in ipairs(headings) do
output[#output + 1] = '<div class="tfr-toc-item tfr-toc-level-' .. (heading.level - 1) .. '">'
if showNumbers then
output[#output + 1] = '<span class="tfr-toc-number">' .. heading.number .. '</span>'
end
output[#output + 1] = '<span class="tfr-toc-text">[[#' .. heading.anchor .. '|' .. heading.plain .. ']]</span>'
output[#output + 1] = '<span class="tfr-toc-arrow">›</span>'
output[#output + 1] = '</div>'
end
end
output[#output + 1] = '</div>'
output[#output + 1] = '<div class="tfr-toc-footer">ARCHIVE LINK ESTABLISHED // AUTO INDEX</div>'
output[#output + 1] = '</div>'
return table.concat(output)
end
return p