Module:Electowidget: Difference between revisions
Content added Content deleted
(added datapage param to p.get_data() rather than hardcoding to "2009 Burlington, Vermont Mayoral Election data") |
(Giving up for now) |
||
Line 3: | Line 3: | ||
-- Comment: I think I'd like the new version of Electowidget to use ABIF |
-- Comment: I think I'd like the new version of Electowidget to use ABIF |
||
local p = {} |
local p = {} |
||
-- Function to parse the line and return a Lua native data structure |
|||
function p.parse_prefs_line(line) |
function p.parse_prefs_line(line) |
||
⚫ | |||
return {} |
|||
⚫ | |||
local qty, prefs = tostring(line):match('(%d+):%s*(.*)') |
local qty, prefs = tostring(line):match('(%d+):%s*(.*)') |
||
local prefs_table = {} |
local prefs_table = {} |
||
Line 18: | Line 13: | ||
local rank = 1 |
local rank = 1 |
||
if 1 == 1 then |
|||
⚫ | |||
return tostring(prefs) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- rating = tonumber(rating), |
|||
⚫ | |||
⚫ | |||
⚫ | |||
rating |
prefs_table[0]={name="foo1" , rating=1} |
||
prefs_table[1]={name="foo2" , rating=2} |
|||
⚫ | |||
qty = 9999 |
|||
rank = rank + 1 |
rank = rank + 1 |
||
end |
-- end |
||
return { |
return { |
||
qty = tonumber(qty), |
qty = tonumber(qty), |
||
prefs = prefs_table |
prefs = prefs_table, |
||
comment = nil, |
comment = nil, |
||
orderedlist = true |
orderedlist = true |
||
} |
} |
||
end |
end |
||
function p.prefs_line_json(prefsline) |
|||
data = p.parse_prefs_line(prefsline) |
|||
return mw.text.jsonEncode(data) |
|||
⚫ | |||
function p.get_data(datapage) |
function p.get_data(datapage) |
||
-- copied and adapted from |
-- copied and adapted from |
||
-- https://en.wikipedia.org/wiki/Module:Format_TemplateData |
-- https://en.wikipedia.org/wiki/Module:Format_TemplateData |
||
local title = mw.title.new(datapage) |
-- local title, s = mw.title.new(tostring(datapage)) |
||
local title = mw.title.new("2009 Burlington, Vermont Mayoral Election data") |
|||
local s = title:getContent() |
local s = title:getContent() |
||
⚫ | |||
-- return mw.text.jsonEncode(s) |
|||
-- return mw.text.jsonEncode(datapage) |
|||
return table.concat(datapage, ",") |
|||
⚫ | |||
local i, j = s:find( "<electowidget>", 1, true ) |
local i, j = s:find( "<electowidget>", 1, true ) |
||
local r |
local r |
Revision as of 04:36, 7 September 2023
This is the very beginning of a Lua implementation of Electowidget.
- Lua source: Module:Electowidget
- doc page: Module:Electowidget/doc
(see also: Module:ABIF)
Tests
Test 1
{{#invoke:Electowidget|parse_prefs_line|24: "蘇業"/5 > DGM/2 = AM/2 > SBJ/}}
Result 1
table
Test 2
{{#invoke:Electowidget|prefs_line_json|24: "蘇業"/5 > DGM/2 = AM/2 > SBJ/}}
Result 2
{"prefs":[{"name":"foo1","rating":1},{"name":"foo2","rating":2}],"qty":9999,"orderedlist":true}
Test 3
{{#invoke:Electowidget|get_data|2009 Burlington, Vermont Mayoral Election data}}
Result 3
-- From: User:RobLa
-- Date: ~~~~
-- Comment: I think I'd like the new version of Electowidget to use ABIF
local p = {}
function p.parse_prefs_line(line)
local qty, prefs = tostring(line):match('(%d+):%s*(.*)')
local prefs_table = {}
if prefs == '' then
return { qty = qty, comment = nil, orderedlist = true }
end
local rank = 1
--for name, rating in prefs:gmatch('([^/]+)/([^ ]+)') do
-- prefs_table[rank] = {
-- name = name,
-- rating = tonumber(rating),
-- }
prefs_table[0]={name="foo1" , rating=1}
prefs_table[1]={name="foo2" , rating=2}
qty = 9999
rank = rank + 1
-- end
return {
qty = tonumber(qty),
prefs = prefs_table,
comment = nil,
orderedlist = true
}
end
function p.prefs_line_json(prefsline)
data = p.parse_prefs_line(prefsline)
return mw.text.jsonEncode(data)
end
function p.get_data(datapage)
-- copied and adapted from
-- https://en.wikipedia.org/wiki/Module:Format_TemplateData
-- local title, s = mw.title.new(tostring(datapage))
local title = mw.title.new("2009 Burlington, Vermont Mayoral Election data")
local s = title:getContent()
if 1 == 1 then
-- return mw.text.jsonEncode(s)
-- return mw.text.jsonEncode(datapage)
return table.concat(datapage, ",")
end
local i, j = s:find( "<electowidget>", 1, true )
local r
if i then
local k = s:find( "</electowidget>", j, true )
if k then
r = mw.text.trim( s:sub( j + 1, k - 1 ) )
end
end
return r
end -- find()
return p