Module:Wikitext

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules
  • delistify: remove "*" and "#"s sequences in the beginning of each Wikitext line, optionally replacing the newlines with spaces between them (|inline=1)
{{#invoke:Wikitext|delistify|
* plop
wow
*# plop
*#plop}}

gives

plop
wow
plop
plop

Inline mode:

{{#invoke:Wikitext|delistify|inline=1|
* plop
wow
*# plop
*#plop}}

gives

 plop wow plop plop

Code

z = {    
	-- mw = require "Module:mw"
};

function z.listifyLines(text)
    local result = ""
    for idx,line in ipairs(mw.text.split(text, "\n", true)) do
        if line ~= "" then
        	result = result .."* " .. line .. "\n"
        end
    end
    return result
end

function string.starts(String,Start)
   return string.sub(String,1,string.len(Start))==Start
end

function z.delistifyLines(text, inline)
    local result = {}
    for idx,line in ipairs(mw.text.split(text, "\n", true)) do
        table.insert(result, (line:gsub("^[*#]+ *", "")))
    end
    return table.concat(result, inline and " " or "\n") .. (inline and "" or "\n")
end

function z.listify(frame)
	return z.listifyLines(frame.args[1])
end

function z.delistify(frame)
    return z.delistifyLines(frame.args[1], frame.args['inline'])
end

function z.countUserLinks(frame)
    -- assuming that user links all begin with "[[User:"
    y = 0
    for match in mw.ustring.gmatch(frame.args[1], "%[%[User:") do
        y = y + 1
    end
    return y
end

return z;