Module:Wiktionary Explained

--Implements .

require ('strict');local local_wiki_tag = mw.language.getContentLanguage.code; -- 'en' at enwiki

--position=|project=wiktionary|text= }}

local function error_side_box (args_t, message, categories_t, frame) return string.format ('

%s: %s%s', -- render the error message with categories ' error: ', message, table.concat (categories_t) ); end

--position=|project=wiktionary|text= }}

local function side_box (args_t, items_t, categories_t, frame) local links_t = ; -- holds a list of interwiki-linked terms for i, item_t in ipairs (items_t) do -- build an interwiki link to wiktionary local title = item_t.term; -- wiktionary article title if item_t.anchor then title = title .. item_t.anchor; -- add a language-specific anchor if language tag or #anchor supplied in template call end links_t[i] = string.format ('%s', -- create interwiki link for each individual term (item_t.isSearch and "Special:Search/" or ""), title, -- wiktionary article title + anchor item_t.lang_text -- term wrapped in -provided markup for display ); end local join1 = ', '; -- create appropriate separators for the list of terms local join2 = #items_t > 2 and ', or ' or ' or ';

return string.format ('Look up %s in Wiktionary, the free dictionary.%s', -- construct the text the goes inside the box mw.text.listToText (links_t, join1, join2), -- concatenate the list of terms table.concat (categories_t) -- add categories );end

--

local added_cats_t = ;

local function category_add (category, categories_t) if not mw.title.getCurrentTitle:inNamespace (0) then -- when not in mainspace return; -- abandon end if added_cats_t[category] then -- when we've already added this category return; -- abandon end local category_names_t =

table.insert (categories_t, table.concat);

added_cats_t[category] = true; -- remember that we've added this category to end

----------------------------< S E T _ A N C H O R >----------------------------------------------------------

create a language-specific anchor (URI fragment) where the fragment is the wiktionary language name associatedwith the language tag prefixed to the 'term'.

local function set_anchor (item_t) local wikt_lang_data = mw.loadData ('Module:Wikt-lang/data'); -- load wiktionary supported languages list

if not item_t.anchor then if wikt_lang_data.redirects[item_t.tag] then item_t.tag = wikt_lang_data.redirects[item_t.tag]; end local data_t = wikt_lang_data.languages[item_t.tag]; -- primary wiktionary data source

local mw_lang_name; -- holds MediaWiki name for language tag if not [item_t.tag] then -- these are supported by MediaWiki but have no value here mw_lang_name = mw.language.fetchLanguageName (item_t.tag, local_wiki_tag); -- get MediaWiki's language name as fallback end if data_t and data_t.name then -- if wiktionary has name for language tag ; not present for default tag 'und' item_t.anchor = mw.uri.anchorEncode ('#' .. data_t.name); -- make an encoded anchor from the wiktionary language name so that it matches the language tag from the template call elseif mw_lang_name and (mw_lang_name ~= item_t.tag) and ('und' ~= item_t.tag) then -- fetchLanguageName returns tag if tag not supported item_t.anchor = mw.uri.anchorEncode ('#' .. mw_lang_name); -- make an encoded anchor from the MediaWiki language name so that it matches the language tag from the template call elseif 'und' ~= item_t.tag then -- when neither wiktionary nor MediaWiki have a definition for ('und' excepted) item_t.error = 'language tag "' .. item_t.tag .. '" is not known to wiktionary'; return; -- abandon end end

if

item_t.anchor then -- if anchor is set to empty string item_t.anchor = nil; -- unset so we don't create a link with trailing '#' fragment marker endend

----------------------------< M A I N >----------------------------------------------------------------------

Return wikitext to display box.

local function main (frame) local args_t = require ('Module:Arguments').getArgs (frame);

local items_t = ; local categories_t = ; local lang_mod = require ('Module:lang'); -- for direct access to the functions in Module:Lang local lang_text; -- term and markup returned from Module:Lang.lang for _, arg in ipairs (args_t) do arg = mw.text.trim (arg); if arg ~= then local tag, term, anchor; if arg:match ('^%a%a%a?:.+') then -- does this arg have language tag prefix and a term? tag, term = arg:match ('^(%a%a%a?):(.*)'); -- yep, then extract them else term = arg; -- nope, just a term end

if term:match ('.+#.+') then -- does hav an anchor (URL fragment)? term, anchor = term:match ('(.+)(#.+)'); -- split the 'term#ancor' into and category_add ('anchor', categories_t); -- add a category end table.insert (items_t,); end end

if items_t[1] then for _, item_t in ipairs (items_t) do local italic = (item_t.tag

local_wiki_tag) and 'yes' or nil; -- local language same as language tag, italicize per words-as-words lang_text = lang_mod._lang ; -- let control italics if lang_text:match ('color: *#d33') or lang_text:match ('color: *#33aa33') then category_add ('error', categories_t); -- add a category return table.concat ; -- abandon with error message end item_t.lang_text = lang_text; set_anchor (item_t); if item_t.error then category_add ('error', categories_t); -- add a category return table.concat ; -- abandon with error message end end else -- no terms supplied in template call so use article title local title = mw.ustring.lower(mw.title.getCurrentTitle.subpageText); -- get the title; TODO: should we be setting page title to lowercase? lang_text = lang_mod._lang ; -- for consistency use for article title; italic per words-as-words items_t[1] = ; -- don't break side box category_add ('article title', categories_t); -- add a category end return side_box (args_t, items_t, categories_t, frame);end

----------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------

return