CI multiple choice questions
Jump to navigation
Jump to search
Documentation for this module may be created at doc
local p = {}
local function trim(s)
--return mw.ustring.gsub(mw.ustring.gsub(s, '%s', ' '), '^%s*(.-)%s*$', '%1')
return mw.ustring.gsub(s, '^%s*(.-)%s*$', '%1')
end
local function firstChar(s)
return string.sub(s, 1, 1)
end
local function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function p.fn(frame)
local origArgs = (type(frame.getParent) == 'function') and frame:getParent().args or frame
local args = {}
for k, v in pairs(origArgs) do
if v ~= '' then
args[k] = v
--mw.log(v)
end
end
local tbl = mw.html.create('div')
tbl:addClass('ci_multiple_choice_questions')
-- toccolours mw-collapsible mw-collapsed
-- mw-collapsible-content
mw.log('args')
mw.log(args)
--local rows = math.ceil(table.getn(args) / 2)
--local rows = math.ceil(origArgs / 2)
local rows = tablelength(args)
mw.log('rows')
mw.log(rows)
for i = 1, rows do
local str = trim(args[i] or '')
mw.log(str)
if str ~= '' then
lines = {}
for s in str:gmatch("[^\n]+") do
--table.insert(lines, s)
lines[#lines+1] = s
end
--local lines = mw.text.split(str,"\n")
local rows_ = tablelength(lines)
local section = tbl:tag('div')
section:addClass('ci_multiple_choice_questions_section')
local list_element
for k, value in pairs(lines) do
value = trim(value)
mw.log('k')
mw.log(k)
mw.log('value')
mw.log(value)
if k == 1 then
question = section:tag('div')
question:addClass('ci_multiple_choice_questions_question')
question:wikitext(value)
list_element = section:tag('ul')
list_element:addClass('ci_multiple_choice_questions_answers')
end
if k > 1 then
local first_char = firstChar(value)
mw.log('first_char')
mw.log(first_char)
local str_ = trim(string.sub(value, 2))
mw.log('str_')
mw.log(str_)
local answer = list_element:tag('li')
--local input = item_content:tag('input')
--input:addClass('ci_multiple_choice_questions_input')
--input:attr('type','radio')
answer:wikitext(str_)
end
end
end
end
--return "{{CI_noparse_isHTML:" .. tostring(tbl) .. "}}"
return tostring(tbl)
end
--=p.fn({["a"]="1",["b"]="2",["c"]="3"})
-- =p.fn({"a","b","c"})
-- =p.fn({"a","b","c","d","e"})
-- =p.fn({"question 1 \n - answer 1 \n - answer 2 ","question 2 \n - answer 1 \n - answer 2 \n + answer 3"})
return p