Module:usex: difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
adding "(canonical name) usage examples" as a category, just for one language, since someone might object |
throw an error if the language is reconstructed, or the template is used in the Reconstruction namespace |
||
Line 37: | Line 37: | ||
function export.format_usex(lang, sc, usex, translation, transliteration, noenum, inline, ref, quote, lit, substs, qualifier, source) |
function export.format_usex(lang, sc, usex, translation, transliteration, noenum, inline, ref, quote, lit, substs, qualifier, source) |
||
local namespace = mw.title.getCurrentTitle().nsText |
|||
if lang:getType() == "reconstructed" or namespace == "Reconstruction" then |
|||
error("Reconstructed languages and reconstructed terms cannot have usage examples, as we have no record of their use.") |
|||
end |
|||
if lit then |
if lit then |
||
lit = "(literally, “" .. span(class.literally, lit) .. "”)" |
lit = "(literally, “" .. span(class.literally, lit) .. "”)" |
Revision as of 18:32, 25 May 2017
- The following documentation is located at Module:usex/documentation. [edit]
- Useful links: subpage list • links • transclusions • testcases • sandbox
This modules generates usage examples (usexes) and quotations for senses. It is used by {{ux}}
, {{uxi}}
and {{quote}}
via Module:usex/templates. It is also used by {{quote-book}}
, {{quote-journal}}
and other quotation templates, which call directly into Module:usex.
Exported functions
export.format_usex
function export.format_usex(lang, sc, usex, translation, transliteration, noenum, inline, ref, quote, lit, substs, qualifier, source)
This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.
local export = {}
local translit_data = mw.loadData("Module:translations/data")
local needs_translit = translit_data[2]
-- microformat2 classes, see https://phabricator.wikimedia.org/T138709
local class = {
container_ux = 'h-usage-example',
container_quotation = 'h-quotation',
example = 'e-example',
quotation = 'e-quotation',
translation = 'e-translation',
-- The following is added by [[Module:script utilities]], using [[Module:script utilities/data]]
-- transliteration = 'e-transliteration',
literally = 'e-literally',
source = 'e-source'
}
-- helper functions
local function wrap(tag, class, text, lang)
if lang then
lang = ' lang="' .. lang .. '"'
else
lang = ""
end
if text and class then
return '<' .. tag .. ' class="' .. class .. '"' .. lang .. '>' .. text .. '</' .. tag .. '>'
else
return nil
end
end
local function span(class, text) return wrap('span', class, text) end
local function div(class, text) return wrap('div', class, text) end
function export.format_usex(lang, sc, usex, translation, transliteration, noenum, inline, ref, quote, lit, substs, qualifier, source)
local namespace = mw.title.getCurrentTitle().nsText
if lang:getType() == "reconstructed" or namespace == "Reconstruction" then
error("Reconstructed languages and reconstructed terms cannot have usage examples, as we have no record of their use.")
end
if lit then
lit = "(literally, “" .. span(class.literally, lit) .. "”)"
end
if source then
source = "(" .. span(class.source, source) .. ")"
end
local example_type = quote and "quote" or "usage example"
local categories = {}
if not sc then
sc = require("Module:scripts").findBestScript(usex, lang)
end
-- temporary category for japanese
if transliteration and (string.find(transliteration, "<br/>") or string.find(transliteration, "<br>")) then
table.insert(categories, "usex with multiple transliterations")
end
-- tr=- means omit transliteration altogether
if transliteration == "-" then
transliteration = nil
else
-- Try to auto-transliterate
if not transliteration and usex then
local subbed_usex = require("Module:links").remove_links(usex)
if substs then
--[=[
[[Special:WhatLinksHere/Template:tracking/quote/substs]]
[[Special:WhatLinksHere/Template:tracking/usex/substs]]
]=]
if quote then
require("Module:debug").track("quote/substs")
else
require("Module:debug").track("usex/substs")
end
local substs = mw.text.split(substs, ",")
for _, subpair in ipairs(substs) do
local subsplit = mw.text.split(subpair, "/")
subbed_usex = mw.ustring.gsub(subbed_usex, subsplit[1], subsplit[2])
end
end
transliteration = lang:transliterate(subbed_usex, sc)
end
-- If there is still no transliteration, then add a cleanup category
if not transliteration and needs_translit[lang] then
table.insert(categories, lang:getCanonicalName() .. " terms needing transliteration")
end
end
if transliteration then
local tag = lang:getCode() == "ja" and "span" or "i"
transliteration = require("Module:script utilities").tag_translit(transliteration, lang:getCode(), "usex")
end
if translation then
translation = span(class.translation, translation)
elseif lang:getCode() ~= "en" and lang:getCode() ~= "und" then
-- add trreq category if translation is unspecified and language is not english or undetermined
table.insert(categories, lang:getCanonicalName() .. " usage examples with the translation missing")
translation = "<small>(please add an English translation of this " .. example_type .. ")</small>"
end
if usex then
if mw.ustring.find(usex, "[[", nil, true) then
usex = require("Module:links").language_link({term = usex, lang = lang}, false)
end
local face
if quote then
face = nil
else
face = "term"
end
if lang and lang:getCode() == "orv" then
table.insert(categories, lang:getCanonicalName() .. " usage examples")
end
local class = quote and class.quotation or class.example
usex = require("Module:script utilities").tag_text(usex, lang, sc, face, class)
else
-- TODO: Trigger some kind of error here
usex = "<small>(please add the primary text of this " .. example_type .. ")</small>"
end
local result = {}
if sc:getDirection() == "rtl" then
table.insert(result, "‏")
end
table.insert(result, usex)
if sc:getDirection() == "rtl" then
table.insert(result, "‎")
end
if qualifier then
table.insert(result, " " .. require("Module:qualifier").format_qualifier(qualifier))
end
table.insert(result, ref)
if inline then
if transliteration then
table.insert(result, " ― " .. transliteration)
end
if translation then
table.insert(result, " ― " .. translation)
end
if lit then
table.insert(result, " " .. lit)
end
if source then
table.insert(result, " " .. source)
end
elseif transliteration or translation or lit then
table.insert(result, "<dl>")
if transliteration then
table.insert(result, "<dd>" .. transliteration .. "</dd>")
end
if translation then
table.insert(result, "<dd>" .. translation .. "</dd>")
end
if lit then
table.insert(result, "<dd>" .. lit .. "</dd>")
end
if source then
table.insert(result, "<dl><dd>" .. source .. "</dd></dl>")
end
table.insert(result, "</dl>")
end
local class = quote and class.container_quotation or class.container_ux
result = table.concat(result)
result = div(class, result)
result = result .. require("Module:utilities").format_categories(categories, lang)
if noenum then
result = "\n: " .. result
end
return result
end
return export