Difference between revisions of "Module:HonorTabs"
From Pathfinder Wiki
DesignerThan (talk | contribs) |
DesignerThan (talk | contribs) |
||
Line 11: | Line 11: | ||
function p.createAnswerTab( frame ) | function p.createAnswerTab( frame ) | ||
if frame.args.answerVariants then | if frame.args.answerVariants then | ||
− | + | result_string = outputVariantsTab(frame, prepareAnswerVariants( frame, frame.args.landingPage, frame.args.answerVariants)) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
return result_string | return result_string | ||
else | else | ||
return "There aren't any answer variants!" | return "There aren't any answer variants!" | ||
end | end | ||
− | |||
end | end | ||
Line 62: | Line 53: | ||
return t_variants | return t_variants | ||
+ | end | ||
+ | |||
+ | function outputVariantsTab(frame, variants) | ||
+ | local HTMLCode | ||
+ | if (#variants > 1) then | ||
+ | HTMLCode = html.create( 'li' ) | ||
+ | HTMLCode | ||
+ | :addClass( 'honor-nav-item' ) | ||
+ | :wikitext( variants[1].url ) | ||
+ | else | ||
+ | |||
+ | end | ||
+ | return HTMLCode | ||
end | end | ||
Revision as of 22:03, 29 April 2021
Documentation for this module may be created at Module:HonorTabs/doc
local p = {}
function p.hello( frame )
return "Hello, world!"
end
function p.getLangSuffix( frame )
return frame:expandTemplate{ title = 'GetLangSuffix' }
end
function p.createAnswerTab( frame )
if frame.args.answerVariants then
result_string = outputVariantsTab(frame, prepareAnswerVariants( frame, frame.args.landingPage, frame.args.answerVariants))
return result_string
else
return "There aren't any answer variants!"
end
end
function prepareAnswerVariants(frame, landingPage, variants)
-- get the variants
local t_variants = explode(";;", variants)
-- get the subvariants & translate it
for key, variant in pairs(t_variants) do
t_variants[key] = {
orig = variant,
split = explode(";", variant)
}
for subkey, subVariant in pairs(t_variants[key]['split']) do
local trans = frame:expandTemplate{ title = 'Localize', args = { subVariant, 'Answer Variants' } }
local t_splitTrans = {
orig = subVariant,
translation = trans
}
t_variants[key]['split'][subkey] = t_splitTrans
end
end
-- assamble link
for key, variant in pairs(t_variants) do
linkTitle = ""
local len = #linkTitle
for subkey, subvariant in pairs(t_variants[key]['split']) do
if (#linkTitle > 0) then
linkTitle = linkTitle .. " & " .. subvariant['translation']
else
linkTitle = subvariant['translation']
end
end
t_variants[key]['url'] = "[[" .. landingPage .. "(" .. variant['orig'] .. ")/Answer_key" .. frame:expandTemplate{title='GetLangSuffix'} .. "|" .. linkTitle .. "]]"
end
return t_variants
end
function outputVariantsTab(frame, variants)
local HTMLCode
if (#variants > 1) then
HTMLCode = html.create( 'li' )
HTMLCode
:addClass( 'honor-nav-item' )
:wikitext( variants[1].url )
else
end
return HTMLCode
end
-- explode(seperator, string)
function explode(seperator,str)
local t, ll
t={}
ll=0
if(#str == 1) then
return {str}
end
while true do
l = string.find(str, seperator, ll, true) -- find the next d in the string
if l ~= nil then -- if "not not" found then..
table.insert(t, string.sub(str,ll,l-1)) -- Save it in our array.
ll = l + string.len(seperator) -- save just after where we found it for searching next time.
else
table.insert(t, string.sub(str,ll)) -- Save what's left in our array.
break -- Break at end, as it should be, according to the lua manual.
end
end
return t
end
return p