Module:script utilities: Difference between revisions

no edit summary
No edit summary
No edit summary
 
(25 intermediate revisions by the same user not shown)
Line 5: Line 5:
[[Module:script utilities/data]]
[[Module:script utilities/data]]
[[Module:scripts]]
[[Module:scripts]]
[[Module:senseid]] (only when id's present)
[[Module:string utilities]] (only when hyphens in Korean text or spaces in vertical text)
[[Module:languages]]
[[Module:languages]]
[[Module:parameters]]
[[Module:parameters]]
[[Module:utilities]]
[[Module:utilities/format_categories]]
]=]
]=]


function export.is_Latin_script(sc)
function export.is_Latin_script(sc)
-- Latn, Latf, Latinx, nv-Latn, pjt-Latn
-- Latn, Latf, Latinx, pjt-Latn
return sc:getCode():find("Lat") and true or false
return sc:getCode():find("Lat") and true or false
end
end
Line 37: Line 39:
local face = args["face"]
local face = args["face"]
if (face == "term_i" or face == "usex") and not export.is_Latin_script(sc) then
face = nil
end
return export.tag_text(text, lang, sc, face, class)
return export.tag_text(text, lang, sc, face, class)
Line 48: Line 54:
-- Replace space characters with newlines in Mongolian-script text, which is written top-to-bottom.
-- Replace space characters with newlines in Mongolian-script text, which is written top-to-bottom.
if sc and sc:getDirection() == "down" then
--[[if sc:getDirection() == "down" and text:find(" ") then
--[[ First, escape targets of wikilinks and HTML tags,
text = require("Module:munge_text")(text, function(txt)
which should not have their spaces
-- having extra parentheses makes sure only the first return value gets through
replaced with line breaks. ]]
return (txt:gsub(" +", "<br>"))
local escaped = {}
end)
local i = 0
end]]
local escape_format = "$%d"
 
-- Hack Korean script text to remove hyphens.
local function escape(text, pattern, prefix)
-- XXX: This should be handled in a more general fashion, but needs to
return text:gsub(
-- be efficient by not doing anything if no hyphens are present, and currently this is the only
pattern,
-- language needing such processing.
function(item)
-- 20220221: Also convert 漢字(한자) to ruby, instead of needing [[Template:Ruby]].
i = i + 1
if sc:getCode() == "Kore" and (text:find("%-") or text:find("[()]")) then
escaped[i] = item
text = require("Module:munge_text")(text, function(txt)
return (prefix or "") .. escape_format:format(i)
-- Hani/Hang regex is a reasonable subset of Hani/Hang from [[Module:scripts/data]],
end)
-- last checked on 20220221
end
txt = txt:gsub("%-", "")
txt = mw.ustring.gsub(txt, "([一-鿿㐀-䶿𠀀-𮯯𰀀-𱍏]+)%(([가-힣ᄀ-ᇿꥠ-ꥼힰ-ퟻ]+)%)", "<ruby>%1<rp>(</rp><rt>%2</rt><rp>)</rp></ruby>")
text = escape(text, "%[%[([^|]+|)", "[[")
return txt
text = escape(text, "<[^>]+>")
end)
text = text:gsub(" +", "<br>")
-- Unescape whatever was escaped.
text = text:gsub(
"$(%d)",
function(a)
a = tonumber(a)
return escaped[a]
end
)
end
end
 
if sc:getCode() == "Imag" then
if sc:getCode() == "Imag" then
face = nil
face = nil
end
end
 
if (face == "term_i" or face == "usex") and not export.is_Latin_script(sc) then
face = nil
end
local function class_attr(classes)
local function class_attr(classes)
table.insert(classes, 1, sc:getCode())
table.insert(classes, 1, sc:getCode())
Line 96: Line 95:
local output = {}
local output = {}
if id then
if id then
table.insert(output, 'id="' .. require("Module:utilities").make_id(lang, id) .. '"')
table.insert(output, 'id="' .. require("Module:senseid").anchor(lang, id) .. '"')
end
end
Line 123: Line 122:
end
end


function export.tag_translit(translit, lang, kind, attributes)
function export.tag_translit(translit, lang, kind, attributes, is_manual)
if type(lang) == "table" then
if type(lang) == "table" then
lang = lang.getCode and lang:getCode()
lang = lang.getCode and lang:getCode()
or error("Third argument to tag_translit should be a language code or language object.")
or error("Second argument to tag_translit should be a language code or language object.")
end
end
Line 135: Line 134:
table.insert(opening_tag, data.tag)
table.insert(opening_tag, data.tag)
if lang == "ja" then
if lang == "ja" then
table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. 'tr"')
table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. (is_manual and "manual-tr " or "") .. 'tr"')
else
else
table.insert(opening_tag, 'lang="' .. lang .. '-Latn"')
table.insert(opening_tag, 'lang="' .. lang .. '-Latn"')
table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. 'tr Latn"')
table.insert(opening_tag, 'class="' .. (data.classes and data.classes .. " " or "") .. (is_manual and "manual-tr " or "") .. 'tr Latn"')
end
end
Line 178: Line 177:


-- Add a notice to request the native script of a word
-- Add a notice to request the native script of a word
function export.request_script(lang, sc)
function export.request_script(lang, sc, usex, nocat, sort_key)
local scripts = lang.getScripts and lang:getScripts() or error('The language "' .. lang:getCode() .. '" does not have the method getScripts. It may be unwritten.')
local scripts = lang.getScripts and lang:getScripts() or error('The language "' .. lang:getCode() .. '" does not have the method getScripts. It may be unwritten.')
Line 219: Line 218:
end
end
-- If there are non-Latin scripts, return nothing.
-- If there are no non-Latin scripts, return nothing.
if not has_nonlatin then
if not has_nonlatin then
return ""
return ""
Line 225: Line 224:
end
end
local category = ""
local category
if mw.title.getCurrentTitle().nsText ~= "Template" then
if usex then
category = "[[Category:" .. lang:getCanonicalName() .. " terms needing " .. cat_script .. " script]]"
category = "Requests for " .. cat_script .. " script in " .. lang:getCanonicalName() .. " usage examples"
else
category = "Requests for " .. cat_script .. " script for " .. lang:getCanonicalName() .. " terms"
end
end
return "<small>[" .. disp_script .. " needed]</small>" .. category
return "<small>[" .. disp_script .. " needed]</small>" ..
(nocat and "" or require("Module:utilities/format_categories")({category}, lang, sort_key))
end
end


function export.template_rfscript(frame)
function export.template_rfscript(frame)
local args = frame.args
params = {
local lang = args[1] or error("The first parameter (language code) has not been given")
[1] = { required = true, default = "und" },
local sc = args["sc"]; if sc == "" then sc = nil end
["sc"] = {},
lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
["usex"] = { type = "boolean" },
sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
["nocat"] = { type = "boolean" },
["sort"] = {},
}
local args = require("Module:parameters").process(frame:getParent().args, params)
local ret = export.request_script(lang, sc)
local lang = require("Module:languages").getByCode(args[1], 1)
local sc = args.sc and require("Module:scripts").getByCode(args.sc, true)
 
local ret = export.request_script(lang, sc, args.usex, args.nocat, args.sort)
if ret == "" then
if ret == "" then
Line 269: Line 278:
error(result)
error(result)
else
else
error('The text "' .. originalText .. '" contains the letters "' .. text .. '" that do not belong to the ' .. scriptObject:getCategoryName() .. '.', 2)
error('The text "' .. originalText .. '" contains the letters "' .. text .. '" that do not belong to the ' .. scriptObject:getDisplayForm() .. '.', 2)
end
end
end
end