Module:User:Wyang/1
Jump to navigation
Jump to search
- This module lacks a documentation subpage. You may create it.
- Useful links: root page • root page’s subpages • links • transclusions • testcases • user page • user talk page • userspace
This is a private module sandbox of Wyang, for their own experimentation. Items in this module may be added and removed at Wyang's discretion; do not rely on this module's stability.
local export = {}
local len = mw.ustring.len
local gsub = mw.ustring.gsub
local match = mw.ustring.match
function export.en(frame)
local contents = mw.title.new(frame.args[1]):getContent()
return mw.ustring.match(contents, '==English==.+%-%-%-')
end
function export.box(frame)
local text = frame.args[1]
local result = {}
for word in mw.ustring.gmatch(text, '%[%[([^%]]+)%]%]') do
table.insert(result, mw.ustring.len(word))
end
if mw.ustring.gsub(table.concat(result), '1', '') == "" or not mw.ustring.match(text, '[%[%]]') then
result = false
end
return '{{zh-forms' .. (result and '|type=' .. table.concat(result) or "") .. '}}'
end
function export.box_st(frame)
local text = frame.args[1]
local pagetitle = mw.title.getCurrentTitle().text
local content = mw.title.new(pagetitle):getContent()
local box = mw.ustring.match(content, '{{zh%-hanzi%-box|[^ A-Za-z0-9//}]+|[^ A-Za-z0-9//}]+}}')
if box == '' then
error("HO LEE FUK, SUM TING WONG! Hanzi box is absent or contains unhandleable characters.")
end
local simp, trad = mw.ustring.match(box, '{{zh%-hanzi%-box|([^ A-Za-z0-9//}]+)|([^ A-Za-z0-9//}]+)}}')
local function count_left(text)
return len(gsub(text, '[^%[]', ''))
end
local function count_right(text)
return len(gsub(text, '[^%]]', ''))
end
local simp_l, simp_r, trad_l, trad_r = count_left(simp), count_right(simp), count_left(trad), count_right(trad)
if simp_l ~= simp_r or trad_l ~= trad_r then
error("HO LEE FUK, SUM TING WONG! #left_bracket ~= #right_bracket.")
elseif simp_l ~= 2 and trad_l ~= 2 then
error("HO LEE FUK, SUM TING WONG! #left_bracket_in_simp and #left_bracket_in_trad are both ~= 2.")
elseif simp_l == 2 and trad_l == 2 and len(pagetitle) ~= 1 then
error("HO LEE FUK, SUM TING WONG! #left_bracket_in_simp and #left_bracket_in_trad are both == 2 and len(title) ~= 1.")
elseif simp_l == 0 or trad_l == 0 then
if len(pagetitle) == 1 then
simp = simp_l == 0 and '[[' .. simp .. ']]' or simp
trad = trad_l == 0 and '[[' .. trad .. ']]' or trad
simp_l, simp_r, trad_l, trad_r = count_left(simp), count_right(simp), count_left(trad), count_right(trad)
else
error("HO LEE FUK, SUM TING WONG! #left_bracket_in_simp or #left_bracket_in_trad == 0.")
end
elseif (simp_l % 2 ~= 0) or (trad_l % 2 ~= 0) then
error("HO LEE FUK, SUM TING WONG! #left_bracket_in_simp or #left_bracket_in_trad is odd.")
elseif not match(simp, '^%[%[.+%]%]$') or not match(trad, '^%[%[.+%]%]$') then
error("HO LEE FUK, SUM TING WONG! simp or trad is not enclosed by square brackets.")
end
if (len(pagetitle) > 1 and simp_l == 2 and trad_l ~= 2) or (len(pagetitle) == 1 and simp_l == 2 and trad_l == 2 and gsub(trad, '[%[%]]', '') == pagetitle) then
if gsub(trad, '[%[%]]', '') ~= pagetitle then
error("HO LEE FUK, SUM TING WONG! trad is not the same as pagetitle.")
end
local result = {}
for word in mw.ustring.gmatch(trad, '%[%[([^%]]+)%]%]') do
table.insert(result, len(word))
end
if gsub(table.concat(result), '1', '') == "" or not match(trad, '[%[%]]') then
result = false
end
new_box = '{{zh-forms|s=' .. gsub(simp, '[%[%]]', '') .. (result and '|type=' .. table.concat(result) or "") .. '}}'
local parts = mw.text.split(content, '{{zh%-hanzi%-box|[^}]+}}')
if #parts ~= 2 then
error("HO LEE FUK, SUM TING WONG! Hanzi box doesn't divide page into two.")
else
return table.concat(parts, new_box)
end
elseif (len(pagetitle) > 1 and simp_l ~= 2 and trad_l == 2) or (len(pagetitle) == 1 and simp_l == 2 and trad_l == 2 and gsub(simp, '[%[%]]', '') == pagetitle) then
if gsub(simp, '[%[%]]', '') ~= pagetitle then
error("HO LEE FUK, SUM TING WONG! simp is not the same as pagetitle.")
end
local sections = mw.text.split(content, '%-%-%-%-')
local section = len(pagetitle) == 1 and sections[2] or sections[1]
if not match(section, '%=%=Chinese%=%=') and not match(section, '%=%=Mandarin%=%=') then
error("HO LEE FUK, SUM TING WONG! Cannot find Chinese section on page.")
else
section = '==Chinese==\n{{zh-see|' .. gsub(trad, '[%[%]]', '') .. '}}'
if len(pagetitle) == 1 then
sections[2] = '\n' .. section .. '\n'
else
sections[1] = section .. '\n'
end
return table.concat(sections, '----')
end
else
error("HO LEE FUK, SUM TING WONG! Something impossible has happened.")
end
end
return export