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 | ||
− | t_variants = prepareAnswerVariants( frame, frame.args.answerVariants) | + | t_variants = prepareAnswerVariants( frame, frame.args.landingPage, frame.args.answerVariants) |
result_string = "" | result_string = "" | ||
Line 17: | Line 17: | ||
result_string = result_string .. "top level" .. "\n" | result_string = result_string .. "top level" .. "\n" | ||
for subkey, val in pairs(value) do | for subkey, val in pairs(value) do | ||
− | result_string = result_string .. "* " .. subkey .. " -- " .. val[' | + | result_string = result_string .. "* " .. subkey .. " -- " .. val['orig'] .. " -- " .. val['translation'] .. " -- " .. value['url'] .. "\n" |
end | end | ||
end | end | ||
Line 27: | Line 27: | ||
end | end | ||
− | function prepareAnswerVariants(frame, variants) | + | function prepareAnswerVariants(frame, landingPage, variants) |
-- get the variants | -- get the variants | ||
local t_variants = explode(";;", variants) | local t_variants = explode(";;", variants) | ||
Line 33: | Line 33: | ||
-- get the subvariants & translate it | -- get the subvariants & translate it | ||
for key, variant in pairs(t_variants) do | for key, variant in pairs(t_variants) do | ||
− | t_variants[key] = explode(";", variant) | + | t_variants[key] = { |
− | for subkey, subVariant in pairs(t_variants[key]) do | + | orig = variant, |
− | t_variants[key][ | + | split = explode(";", variant) |
− | + | } | |
+ | for subkey, subVariant in pairs(t_variants[key]['split']) do | ||
+ | t_variants[key]['split'] = { | ||
+ | orig=subVariant, | ||
translation=frame:expandTemplate{title = 'Localize', args = {subVariant,'Answer Variants'}} | translation=frame:expandTemplate{title = 'Localize', args = {subVariant,'Answer Variants'}} | ||
} | } | ||
end | end | ||
end | end | ||
+ | |||
+ | -- assamble link | ||
+ | for key, variant in pairs(t_variants) do | ||
+ | linkTitle = "" | ||
+ | for subkey, subvariant in pairs(variant['split']) do | ||
+ | if (string.len(linkTitle) > 0) then | ||
+ | linkTitle = linkTitle .. " & " .. subvariant['translation'] | ||
+ | else | ||
+ | linkTitle = subvariant['translation'] | ||
+ | end | ||
+ | end | ||
+ | link = "[[" .. landingPage .. "(" .. variant['orig'] .. ")/Answer_key" .. frame:expandTemplate{ title = 'GetLangSuffix' } .. "|" .. linkTitle .. "]]" | ||
+ | t_variants[key].url = link | ||
+ | end | ||
+ | |||
return t_variants | return t_variants | ||
end | end |
Revision as of 22:54, 27 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
t_variants = prepareAnswerVariants( frame, frame.args.landingPage, frame.args.answerVariants)
result_string = ""
for key, value in pairs(t_variants) do
result_string = result_string .. "top level" .. "\n"
for subkey, val in pairs(value) do
result_string = result_string .. "* " .. subkey .. " -- " .. val['orig'] .. " -- " .. val['translation'] .. " -- " .. value['url'] .. "\n"
end
end
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
t_variants[key]['split'] = {
orig=subVariant,
translation=frame:expandTemplate{title = 'Localize', args = {subVariant,'Answer Variants'}}
}
end
end
-- assamble link
for key, variant in pairs(t_variants) do
linkTitle = ""
for subkey, subvariant in pairs(variant['split']) do
if (string.len(linkTitle) > 0) then
linkTitle = linkTitle .. " & " .. subvariant['translation']
else
linkTitle = subvariant['translation']
end
end
link = "[[" .. landingPage .. "(" .. variant['orig'] .. ")/Answer_key" .. frame:expandTemplate{ title = 'GetLangSuffix' } .. "|" .. linkTitle .. "]]"
t_variants[key].url = link
end
return t_variants
end
-- explode(seperator, string)
function explode(seperator,str)
local t, ll
t={}
ll=0
if(#p == 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