Module:Redirect hatnote: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Updated from sandbox: Rewrote p._quote to use mw.html, divided functions into helper functions and main functions, and tweaked getTitle
(add kerning for redir titles beginning or ending with apostrophes or quotation marks, as at Neighbourhood and Kurtis Blow)
(Updated from sandbox: Rewrote p._quote to use mw.html, divided functions into helper functions and main functions, and tweaked getTitle)
Line 13:
 
local p = {}
 
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
 
local function getTitle(...)
--Calls mw.title.new and returns either a title object, or nil on error
local success, titleObj = pcall(mw.title.new, ...)
ifreturn success thenand titleObj or nil
end
return titleObj
 
else
function p._quote(title)
return nil
--Wraps titles in quotation marks. If the title starts/ends with a quotation
--mark, kerns that side as with {{-'}}
local quotationMarks = {
local quotationMarks = { ["'"]=true, ['"']=true, ['“']=true, ["‘"]=true, ['”']=true, ["’"]=true}
}
local quoteLeft, quoteRight = -- Test if start/end are quotation marks
if quotationMarks[string.sub(title, 1, 1)] then,
if quotationMarks[string.sub(title, -1, -1)] then
if quoteLeft or quoteRight then
title = mw.html.create("span"):wikitext(title)
end
if quoteLeft then title:css("padding-left", "0.15em") end
if quoteRight then title:css("padding-right", "0.15em") end
return '"' .. tostring(title) .. '"'
end
 
--------------------------------------------------------------------------------
-- Main functions
--------------------------------------------------------------------------------
 
function p.redirect(frame)
Line 119 ⟶ 141:
 
return mHatnote._hatnote(text, mhOptions) .. category
end
 
function p._quote(title)
local quotationMarks = {["'"]=true, ['"']=true, ['“']=true, ["‘"]=true, ['”']=true, ["’"]=true}
local style = ""
-- If string starts with anything in quotationMarks, kern to same degree {{-'}} does
if quotationMarks[string.sub(title, 1, 1)] then
style = "padding-left:0.15em;"
end
-- Likewise if it ends with any of them
if quotationMarks[string.sub(title, -1, -1)] then
style = style .. "padding-right:0.15em;"
end
return style ~= "" and '"<span style="' .. style .. '">' .. title .. '</span>"' or '"' .. title .. '"'
end
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu