Module:Description: Difference between revisions
From Neodyland Wiki
More actions
No edit summary |
No edit summary |
||
| Line 16: | Line 16: | ||
local core = require('Module:Core') | local core = require('Module:Core') | ||
local p = {} | local p = {} | ||
------------------------------------------------------------------------------ | |||
--[[ | |||
display a language followed by a message. Like "English: Hello" with extensive HTML marking | |||
Code equivalent to https://commons.wikimedia.org/wiki/Template:Description with only 3 inputs | |||
Inputs: | |||
1) text - text to display | |||
2) text_lang - language code for the above text | |||
3) inline - Optional, default is false. When set to true, forces the template to be displayed | |||
inline, so that it does not break the current paragraph (that makes possible to put several | |||
descriptions side by side on a single line) | |||
]] | |||
function p.langWrapper(text, text_lang, args) | |||
local inline = core.yesno(args.inline, false) and 'inline' or nil | |||
local disp_lang = args.disp_lang and args.disp_lang or text_lang -- disp_lang same as text_lang by default | |||
local user_lang = args.user_lang and args.user_lang or disp_lang | |||
local langObj = mw.language.new( disp_lang ) | |||
local dir = langObj:getDir() -- text direction | |||
local wordsep = mw.message.new( "Word-separator" ):inLanguage(disp_lang):plain() -- space | |||
local lang_name = langObj:ucfirst(mw.language.fetchLanguageName(text_lang, disp_lang)) | |||
local hover = args.hover and args.hover or mw.language.fetchLanguageName( text_lang, user_lang) | |||
-- create HTML | |||
local ltag = mw.html.create('span') -- bold language name string | |||
:addClass('language') -- class: "language" | |||
:addClass(text_lang) -- class: "en", "de" etc. | |||
:attr('title', hover) -- add hover aka mouseover aka tooltip text | |||
:wikitext('<b>' .. lang_name ..':</b>') | |||
local dtag = mw.html.create('div') | |||
:addClass('description') -- div.description is tracked by mw:Extension:CommonsMetadata | |||
:addClass('mw-content-'..dir) -- mw-content-rtl and mw-content-ltr are defined in mediawiki-code (https://gerrit.wikimedia.org/r/c/mediawiki/core/+/208332) | |||
:addClass(text_lang) -- not sure where "en", "de" etc. are defined | |||
:attr('dir', dir) | |||
:attr('lang', disp_lang) | |||
:css('display', inline) | |||
:wikitext(tostring(ltag) .. wordsep .. text) | |||
return tostring(dtag) | |||
end | |||
function p.description(frame) | function p.description(frame) | ||
local args = core.getArgs(frame) | local args = core.getArgs(frame) | ||
local desc | local desc | ||
args.user_lang = args.lang | |||
args.inline = (args.inline ~= nil) -- convert to boolean | |||
if args.text == nil then -- if no string than add error message and a category | if args.text == nil then -- if no string than add error message and a category | ||
local cat = 'Category:Language templates with no text displayed' | local cat = 'Category:Language templates with no text displayed' | ||
| Line 27: | Line 67: | ||
msg = '[' .. frame:callParserFunction{ name = 'fullurl', args={cat} } .. tostring(msg) .. ']' | msg = '[' .. frame:callParserFunction{ name = 'fullurl', args={cat} } .. tostring(msg) .. ']' | ||
msg = mw.html.create('span'):addClass("plainlinks"):wikitext(msg) | msg = mw.html.create('span'):addClass("plainlinks"):wikitext(msg) | ||
desc = | desc = p.langWrapper(tostring(msg), args.lang, args) | ||
local namespace = mw.title.getCurrentTitle().namespace | local namespace = mw.title.getCurrentTitle().namespace | ||
local LUT = {[0]=1, [6]=1, [10]=1, [14]=1, [100]=1, [106]=1} | local LUT = {[0]=1, [6]=1, [10]=1, [14]=1, [100]=1, [106]=1} | ||
| Line 34: | Line 74: | ||
end | end | ||
else | else | ||
desc = | desc = p.langWrapper(args.text, args.lang, args) | ||
end | end | ||
return desc | return desc | ||
Revision as of 17:45, 1 March 2021
This documentation is transcluded from Module:Description/doc.
--[[
__ __ _ _ ____ _ _ _
| \/ | ___ __| |_ _| | ___ _| _ \ ___ ___ ___ _ __(_)_ __ | |_(_) ___ _ __
| |\/| |/ _ \ / _` | | | | |/ _ (_) | | |/ _ \/ __|/ __| '__| | '_ \| __| |/ _ \| '_ \
| | | | (_) | (_| | |_| | | __/_| |_| | __/\__ \ (__| | | | |_) | |_| | (_) | | | |
|_| |_|\___/ \__,_|\__,_|_|\___(_)____/ \___||___/\___|_| |_| .__/ \__|_|\___/|_| |_|
|_|
Description is a module implementing functionality of {{Description}}
template, which is used by {{en}}, {{de}} and other language templates.
Authors and maintainers:
* User:Jarekt
]]
local core = require('Module:Core')
local p = {}
------------------------------------------------------------------------------
--[[
display a language followed by a message. Like "English: Hello" with extensive HTML marking
Code equivalent to https://commons.wikimedia.org/wiki/Template:Description with only 3 inputs
Inputs:
1) text - text to display
2) text_lang - language code for the above text
3) inline - Optional, default is false. When set to true, forces the template to be displayed
inline, so that it does not break the current paragraph (that makes possible to put several
descriptions side by side on a single line)
]]
function p.langWrapper(text, text_lang, args)
local inline = core.yesno(args.inline, false) and 'inline' or nil
local disp_lang = args.disp_lang and args.disp_lang or text_lang -- disp_lang same as text_lang by default
local user_lang = args.user_lang and args.user_lang or disp_lang
local langObj = mw.language.new( disp_lang )
local dir = langObj:getDir() -- text direction
local wordsep = mw.message.new( "Word-separator" ):inLanguage(disp_lang):plain() -- space
local lang_name = langObj:ucfirst(mw.language.fetchLanguageName(text_lang, disp_lang))
local hover = args.hover and args.hover or mw.language.fetchLanguageName( text_lang, user_lang)
-- create HTML
local ltag = mw.html.create('span') -- bold language name string
:addClass('language') -- class: "language"
:addClass(text_lang) -- class: "en", "de" etc.
:attr('title', hover) -- add hover aka mouseover aka tooltip text
:wikitext('<b>' .. lang_name ..':</b>')
local dtag = mw.html.create('div')
:addClass('description') -- div.description is tracked by mw:Extension:CommonsMetadata
:addClass('mw-content-'..dir) -- mw-content-rtl and mw-content-ltr are defined in mediawiki-code (https://gerrit.wikimedia.org/r/c/mediawiki/core/+/208332)
:addClass(text_lang) -- not sure where "en", "de" etc. are defined
:attr('dir', dir)
:attr('lang', disp_lang)
:css('display', inline)
:wikitext(tostring(ltag) .. wordsep .. text)
return tostring(dtag)
end
function p.description(frame)
local args = core.getArgs(frame)
local desc
args.user_lang = args.lang
args.inline = (args.inline ~= nil) -- convert to boolean
if args.text == nil then -- if no string than add error message and a category
local cat = 'Category:Language templates with no text displayed'
local msg = core.formatMessage('I18n/DescriptionError.tab', 'missing text', lang)
msg = mw.html.create('span'):addClass("error"):wikitext(msg)
msg = '[' .. frame:callParserFunction{ name = 'fullurl', args={cat} } .. tostring(msg) .. ']'
msg = mw.html.create('span'):addClass("plainlinks"):wikitext(msg)
desc = p.langWrapper(tostring(msg), args.lang, args)
local namespace = mw.title.getCurrentTitle().namespace
local LUT = {[0]=1, [6]=1, [10]=1, [14]=1, [100]=1, [106]=1}
if LUT[namespace]==1 then
desc = desc .. '[[' .. cat .. ']]'
end
else
desc = p.langWrapper(args.text, args.lang, args)
end
return desc
end
return p