Module:Lang: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
No edit summary
m synch from sandbox;
Line 8: Line 8:
local p = {};
local p = {};


local initial_style_state; -- set by lang_xx_normal() and lang_xx_italic()
local initial_style_state; -- set by lang_xx_inherit() and lang_xx_italic()


local getArgs = require ('Module:Arguments').getArgs;
local getArgs = require ('Module:Arguments').getArgs;
Line 87: Line 87:
text = mw.ustring.gsub (text, '%[%[[^|]+|([^%]]+)%]%]', '%1'); -- remove the link and markup from complex wikilink in case interwiki to non-Latn wikipedia
text = mw.ustring.gsub (text, '%[%[[^|]+|([^%]]+)%]%]', '%1'); -- remove the link and markup from complex wikilink in case interwiki to non-Latn wikipedia
return not is_set (mw.ustring.gsub (text, latn, '')); -- replace all latn characters with empty space; if result is all empty space, text is latn
return not is_set (mw.ustring.gsub (text, latn, '')); -- replace all latn characters with empty space; if result is all empty space, text is latn
end


--[[--------------------------< I N V E R T _ I T A L I C S >-------------------------------------------------

This function attempts to invert the italic markup a args.text by adding/removing leading/trailing italic markup
in args.text. Like |italic=unset, |italic=invert disables automatic italic markup. Individual leading/trailing
apostrophes are converted to their html numeric entity equivalent so that the new italic markup doesn't become
bold markup inadvertently.

Leading and trailing wiki markup is extracted from args.text into separate table elements. Addition, removal,
replacement of wiki markup is handled by a string.gsub() replacement table operating only on these separate elements.
In the string.gsub() matching pattern, '.*' matches empty string as well as the three expected wiki markup patterns.

This function expects that markup in args.text is complete and correct; if it is not, oddness may result.

]]

local function invert_italics (source)
local invert_pattern_table = { -- leading/trailing markup add/remove/replace patterns
[""]="\'\'", -- empty string becomes italic markup
["\'\'"]="", -- italic markup becomes empty string
["\'\'\'"]="\'\'\'\'\'", -- bold becomes bold italic
["\'\'\'\'\'"]="\'\'\'", -- bold italic become bold
};
local seg = {};

source = source:gsub ("%f[\']\'%f[^\']", '&#38;'); -- protect single quote marks from being interpreted as bold markup

seg[1] = source:match ('^(\'\'+%f[^\']).+') or ''; -- get leading markup, if any; ignore single quote
seg[3] = source:match ('.+(%f[\']\'\'+)$') or ''; -- get trailing markup, if any; ignore single quote

if '' ~= seg[1] and '' ~= seg[3] then -- extract the 'text'
seg[2] = source:match ('^\'\'+%f[^\'](.+)%f[\']\'\'+$') -- from between leading and trailing markup
elseif '' ~= seg[1] then
seg[2] = source:match ('^\'\'+%f[^\'](.+)') -- following leading markup
elseif '' ~= seg[3] then
seg[2] = source:match ('(.+)%f[\']\'\'+$') -- preceding trailing markup
else
seg[2] = source -- when there is no markup
end

seg[1] = seg[1]:gsub (".*", invert_pattern_table, 1); -- replace leading markup according to pattern table
seg[3] = seg[3]:gsub (".*", invert_pattern_table, 1); -- replace leading markup according to pattern table

return table.concat (seg); -- put it all back together and done
end
end


Line 112: Line 158:
no - force args.text to be rendered in normal font; returns 'normal'
no - force args.text to be rendered in normal font; returns 'normal'
unset - disables font control so that font-style applied to text is dictated by markup inside or outside the template; returns 'inherit'
unset - disables font control so that font-style applied to text is dictated by markup inside or outside the template; returns 'inherit'
invert - disables font control so that font-style applied to text is dictated by markup outside or inverted inside the template; returns 'invert'


]]
]]


local function validate_italic (italic, italics)
local function validate_italic (italic, italics)
local properties = {['yes'] = 'italic', ['no'] = 'normal', ['unset'] = 'inherit', ['default'] = nil};
local properties = {['yes'] = 'italic', ['no'] = 'normal', ['unset'] = 'inherit', ['invert'] = 'invert', ['default'] = nil};


if italic and italics then -- return nil and an error message if both are set
if italic and italics then -- return nil and an error message if both are set
Line 424: Line 471:
if text:match ('^%*') then
if text:match ('^%*') then
table.insert (span, '&#42;'); -- move proto language text prefix outside of italic markup if any; use numeric entity because plan splat confuses MediaWiki
table.insert (span, '&#42;'); -- move proto language text prefix outside of italic markup if any; use numeric entity because plan splat confuses MediaWiki
text = text:gsub ('^%*', ''); -- remove the spat from the text
text = text:gsub ('^%*', ''); -- remove the splat from the text
end
end


Line 632: Line 679:
end
end


if ('unset' ~= args.italic) and ('unset' ~= args.italics) then -- allow italic markup when |italic=unset or |italics=unset
local style = args.italic or args.italics;

-- if args.text:match ("%f[\']\'\'[^\']+\'\'%f[^\']") or args.text:match ("\'\'\'\'\'[^\']+\'\'\'\'\'") then -- italic but not bold, or bold italic
-- if ('unset' ~= args.italic) and ('unset' ~= args.italics) then -- allow italic markup when |italic=unset or |italics=unset
if ('unset' ~= style) and ('invert' ~=style) then
if args.text:find ("%f[\']\'\'%f[^\']") or args.text:find ("%f[\']\'\'\'\'\'%f[^\']") then -- italic but not bold, or bold italic
if args.text:find ("%f[\']\'\'%f[^\']") or args.text:find ("%f[\']\'\'\'\'\'%f[^\']") then -- italic but not bold, or bold italic
return make_error_msg ('text has italic markup', args, template);
return make_error_msg ('text has italic markup', args, template);
Line 760: Line 809:
end
end


if 'invert' == args.italic then
args.text = invert_italics (args.text)
end
args.text = proto_prefix (args.text, language_name); -- prefix proto-language text with a splat
args.text = proto_prefix (args.text, language_name); -- prefix proto-language text with a splat


Line 928: Line 981:
end
end


if 'invert' == args.italic then
args.text = invert_italics (args.text)
end
args.text = proto_prefix (args.text, language_name); -- prefix proto-language text with a splat
args.text = proto_prefix (args.text, language_name); -- prefix proto-language text with a splat