Line 234: |
Line 234: |
| end | | end |
| end | | end |
| + | end |
| + | |
| + | function Utils.format(s, lookup) |
| + | if not lookup then |
| + | lookup = s |
| + | s = lookup[1] |
| + | table.remove(lookup, 1) |
| + | end |
| + | return (string.gsub(s, '${([^%.%!%:%}%[%]]+)%[?([^%.%!%:%}%]]*)%]?%.?([^%!%:%}]*)!?([^%:%}]?):?([^%}]*)}', |
| + | function(name, element, attribute, conversion, format_spec) |
| + | --local start_of_value, end_of_value, value = string.find(x, '^${(.-)[[.!:}]') |
| + | --local start_of_access, end_of_access, access = string.find(x, '^${.-%[(.-)%][!:}]') |
| + | --if not access then |
| + | -- start_of_access, end_of_access, access = string.find(x, '^${[^:]-%.(.-)[!:}]') |
| + | --end |
| + | --local start_of_conversion, end_of_conversion, conversion = string.find(x, '^${.-!(.)[:}]') |
| + | --local start_of_format_spec, end_of_format_spec, format_spec = string.find(x, ':(.*)}$') |
| + | |
| + | local value = lookup[name] |
| + | if string.len(element) > 0 then |
| + | value = value[element] |
| + | elseif string.len(attribute) > 0 then |
| + | value = value[attribute] |
| + | end |
| + | |
| + | if string.len(conversion) > 0 then |
| + | if conversion == 's' then |
| + | value = tostring(value) |
| + | end |
| + | end |
| + | |
| + | if string.len(format_spec) > 0 then |
| + | local start_of_sign, end_of_sign, sign = string.find(format_spec, '([+%- ])') |
| + | local start_of_width, end_of_width, width, comma, precision, option = string.find(format_spec, '(%d*)(,?)([.0-9]*)([bcdeEfFgGnosxX%%]?)$') |
| + | precision = string.sub(precision, 2) |
| + | local number = tonumber(value) |
| + | if #width > 0 then |
| + | if number then |
| + | value = string.format(string.format(number % 1 ~= 0 and "%%0%s.%sf" or "%%0%sd", width, #precision > 0 and precision or 0), number) |
| + | end |
| + | value = string.format(string.format("%%0%ss", width), value) |
| + | elseif #precision > 0 and number then |
| + | value = string.format(string.format("%%0%s.%sf", width, precision), number) |
| + | end |
| + | if sign then |
| + | if number and number > 0 then |
| + | if sign == "+" then |
| + | value = "+" .. value |
| + | elseif sign == " " then |
| + | value = " " .. value |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
| + | return value |
| + | end)) |
| + | end |
| + | |
| + | function Utils.split(string, separator, max_split, plain) |
| + | assert(separator ~= '') |
| + | assert(max_split == nil or max_split >= 1) |
| + | |
| + | local default_separator = false |
| + | |
| + | local result = {} |
| + | |
| + | if not separator or separator == '' then |
| + | separator = '%s+' |
| + | plain = false |
| + | string = mw.text.trim(string) |
| + | if string == '' then |
| + | return result |
| + | end |
| + | end |
| + | |
| + | max_split = max_split or -1 |
| + | |
| + | local item_index, start_index = 1, 1 |
| + | local separator_start, separator_end = mw.ustring.find(string, separator, start_index, plain) |
| + | while separator_start and max_split ~= 0 do |
| + | result[item_index] = mw.ustring.sub(string, start_index, separator_start - 1) |
| + | item_index = item_index + 1 |
| + | start_index = separator_end + 1 |
| + | separator_start, separator_end = mw.ustring.find(string, separator, start_index, plain) |
| + | max_split = max_split - 1 |
| + | end |
| + | result[item_index] = mw.ustring.sub(string, start_index) |
| + | |
| + | return result |
| end | | end |
| | | |
| return Utils | | return Utils |