CI multiple choice questions
Jump to navigation
Jump to search
(Created page with "local p = {} local function trim(s) return mw.ustring.gsub(mw.ustring.gsub(s, '%s', ' '), '^%s*(.-)%s*$', '%1') end local function tablelength(T) local count = 0 for _...") |
|||
| Line 2: | Line 2: | ||
local function trim(s) | local function trim(s) | ||
| − | return mw.ustring.gsub(mw.ustring.gsub(s, '%s', ' '), '^%s*(.-)%s*$', '%1') | + | --return mw.ustring.gsub(mw.ustring.gsub(s, '%s', ' '), '^%s*(.-)%s*$', '%1') |
| + | return mw.ustring.gsub(s, '^%s*(.-)%s*$', '%1') | ||
end | end | ||
| + | |||
| + | |||
| + | local function firstChar(s) | ||
| + | return string.sub(s, 1, 1) | ||
| + | end | ||
| + | |||
local function tablelength(T) | local function tablelength(T) | ||
| Line 21: | Line 28: | ||
if v ~= '' then | if v ~= '' then | ||
args[k] = v | args[k] = v | ||
| − | mw.log(v) | + | --mw.log(v) |
end | end | ||
end | end | ||
| Line 28: | Line 35: | ||
local tbl = mw.html.create('div') | local tbl = mw.html.create('div') | ||
| − | tbl:addClass(' | + | tbl:addClass('ci_multiple_choice_questions') |
-- toccolours mw-collapsible mw-collapsed | -- toccolours mw-collapsible mw-collapsed | ||
| Line 39: | Line 46: | ||
--local rows = math.ceil(table.getn(args) / 2) | --local rows = math.ceil(table.getn(args) / 2) | ||
--local rows = math.ceil(origArgs / 2) | --local rows = math.ceil(origArgs / 2) | ||
| − | local rows = | + | local rows = tablelength(args) |
mw.log('rows') | mw.log('rows') | ||
mw.log(rows) | mw.log(rows) | ||
| + | |||
| + | |||
| + | |||
for i = 1, rows do | for i = 1, rows do | ||
| − | local | + | 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') | ||
| − | + | 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) | ||
| + | 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 = section:tag('div') | ||
| + | answer:addClass('ci_multiple_choice_questions_answer') | ||
| + | --local input = item_content:tag('input') | ||
| + | --input:addClass('ci_multiple_choice_questions_input') | ||
| + | --input:attr('type','radio') | ||
| + | |||
| + | answer:wikitext(str_) | ||
| + | |||
| + | end | ||
| + | |||
end | end | ||
| − | + | ||
| − | + | ||
| − | |||
| − | |||
end | end | ||
| + | |||
| + | |||
end | end | ||
| + | --return "{{CI_noparse_isHTML:" .. tostring(tbl) .. "}}" | ||
return tostring(tbl) | return tostring(tbl) | ||
| Line 66: | Line 124: | ||
-- =p.fn({"a","b","c"}) | -- =p.fn({"a","b","c"}) | ||
-- =p.fn({"a","b","c","d","e"}) | -- =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 | return p | ||
Revision as of 08:10, 7 November 2020
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')
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)
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 = section:tag('div')
answer:addClass('ci_multiple_choice_questions_answer')
--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