Module:ForEachLine

From Lotro-Wiki.com
Jump to navigation Jump to search

Documentation for this module may be created at Module:ForEachLine/doc

local p = {}

function p.rreplace(frame)
    local input = frame.args[1]:match('^%s*(.-)%s*$');
    local pattern = frame.args[2];
    local replacement = frame.args[3];

    local lines = {}
    for line in mw.text.gsplit(input, '\n') do
        local output = frame:preprocess('{{#rreplace:' .. line .. '|' .. pattern .. '|' .. replacement .. '}}');
        output = output:gsub('^\n', ''); -- workaround for https://phabricator.wikimedia.org/T14974
        table.insert(lines, output);
    end

    return table.concat(lines, '\n');
end

function p.AddPrefix(frame)
    local input = frame.args[1]:match('^%s*(.-)%s*$');
    local prefix = frame.args[2];

    local lines = {}
    for line in mw.text.gsplit(input, '\n') do
        local output = prefix .. line;
        table.insert(lines, output);
    end

    return table.concat(lines, '\n');
end

return p