Строка 55: |
Строка 55: |
| end) * quote | | end) * quote |
| end -- local function quoted (quote) | | end -- local function quoted (quote) |
− |
| |
− | -- This pattern matches a JavaScript regular expression (//) and cinverts it to a PREG:
| |
− | local regex = slash * ((escape * slash + any - slash) ^ 1 / function (pattern)
| |
− | local sanitised = rex.gsub (pattern, '\\\\u([0-9A-F]{2,4})', '\\x{%1}', nil, convert_flags'ig')
| |
− | return sanitised
| |
− | end) * slash * C (S'gim' ^ 0)
| |
| | | |
| -- These function is used in both partial parsing JavaScript and applying wikifier transformations: | | -- These function is used in both partial parsing JavaScript and applying wikifier transformations: |
Строка 87: |
Строка 81: |
| | | |
| -- These JavaScript functions are allowed in wikifying rules and are reimplemented in Lua: | | -- These JavaScript functions are allowed in wikifying rules and are reimplemented in Lua: |
− | local functions = { | + | local allowed = { |
| r = preg_replace, | | r = preg_replace, |
| hide = hide, | | hide = hide, |
Строка 115: |
Строка 109: |
| } | | } |
| | | |
− | local s, str = P's', quoted'"' + quoted"'" | + | local s = P's' |
| | | |
− | local transform = P { 'call', | + | local transform = P { 'trans', |
− | call = s * spaces * equals * spaces * V'func' * spaces * open | + | trans = s * spaces * equals * spaces * V'call' * spaces * ';' * Cp (), |
− | * ( spaces * s * ( spaces * comma * spaces * V'arg' ) ^ 0 ) | + | call = V'func' * spaces * open |
| + | * ( spaces * V'arg' * ( spaces * comma * spaces * V'arg' ) ^ 0 ) |
| * spaces * close / function (func, ...) | | * spaces * close / function (func, ...) |
| + | mw.log 'In call rule.' |
| local args = {...} | | local args = {...} |
| return function (string) | | return function (string) |
− | return func (string, unpack (args)) | + | local substituted = {} |
| + | for i, arg in ipairs (args) do |
| + | substituted [i] = arg == 's' and string or arg |
| + | end |
| + | return func (string, unpack (substituted)) |
| end | | end |
| end, | | end, |
Строка 129: |
Строка 129: |
| -- Only allowed functions: | | -- Only allowed functions: |
| local choice = never | | local choice = never |
− | for name, func in pairs (functions) do | + | mw.log 'In func rule' |
| + | for name, func in pairs (allowed) do |
| + | mw.log ('In func rule. name = ' .. tostring (name)) |
| choice = choice + P (name) * Cc (func) | | choice = choice + P (name) * Cc (func) |
| end | | end |
| return choice | | return choice |
| end)(), | | end)(), |
− | arg = V'call' + str + regex + V'func' | + | arg = V's' + V'call' + V'string' + V'regex' + V'func', |
| + | s = C (s), |
| + | string = quoted'"' + quoted"'", |
| + | regex = slash * ((escape * slash + any - slash) ^ 1 / function (pattern) |
| + | local sanitised = rex.gsub (pattern, '\\\\u([0-9A-F]{2,4})', '\\x{%1}', nil, convert_flags'ig') |
| + | return sanitised |
| + | end) * slash * C (S'gim' ^ 0) |
| } | | } |
| | | |
− | local transform_anywhere = P{ transform * Cp () + 1 * V(1) } | + | local transform_anywhere = P{ transform + 1 * V(1) } |
| + | local wikifier = 'Gadget-wikifier.js' |
| + | local code = tostring (mw.message.new (wikifier)) |
| | | |
| -- Iterate over wikifier code yielding processing functions: | | -- Iterate over wikifier code yielding processing functions: |
− | local function transforms (code) | + | local transforms = (function (code) |
| -- Remove comments: | | -- Remove comments: |
| local code = preg_replace (code, '//.*$', 'mg', '') | | local code = preg_replace (code, '//.*$', 'mg', '') |
Строка 152: |
Строка 162: |
| end | | end |
| end) | | end) |
− | end -- local function transforms (code) | + | end) (code) --local transforms = (function (code) |
| | | |
− | local wikifier = 'Gadget-wikifier.js'
| + | local function wikify (text, page, start, finish) |
− | local code = tostring (mw.message.new (wikifier))
| |
− | | |
− | local function wikify (text, code, page, start, finish) | |
| local counter = 1 | | local counter = 1 |
− | for func in transforms (code) do | + | for func in transforms do |
| if (not start or counter >= start) and (not finish or counter <= finish) then | | if (not start or counter >= start) and (not finish or counter <= finish) then |
| text = func (text, page) | | text = func (text, page) |
Строка 198: |
Строка 205: |
| return wikify ( | | return wikify ( |
| frame.args [1] or frame.args.text, | | frame.args [1] or frame.args.text, |
− | code,
| |
| frame:callParserFunction ('FULLPAGENAME', ''), | | frame:callParserFunction ('FULLPAGENAME', ''), |
| tonumber (frame.args.start), | | tonumber (frame.args.start), |
Строка 207: |
Строка 213: |
| return wikify ( | | return wikify ( |
| test, | | test, |
− | code,
| |
| frame:callParserFunction ('FULLPAGENAME', ''), | | frame:callParserFunction ('FULLPAGENAME', ''), |
| tonumber (frame.args.start), | | tonumber (frame.args.start), |
| tonumber (frame.args.finish) | | tonumber (frame.args.finish) |
| ) | | ) |
| + | end, |
| + | test2 = function () |
| + | return wikify (test, code, '') |
| end | | end |
| } -- return | | } -- return |