Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:MarkdownToWikitext: Difference between revisions

From Vault Hunters Official Wiki
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- Utility: unescape common HTML entities and escaped unicode sequences
function p._convert(markdown)
local function htmlUnescape(s)
  if not markdown or markdown == "" then return "(No content)" end
s = s:gsub("\\u003C", "<")
s = s:gsub("\\u003E", ">")
s = s:gsub("&lt;", "<")
s = s:gsub("&gt;", ">")
s = s:gsub("&amp;", "&")
s = s:gsub("&quot;", '"')
s = s:gsub("&#39;", "'")
return s
end


-- Utility: strip inline HTML (iframes, br, etc)
  -- 1. Decode JSON-style escapes
local function stripHTML(s)
  markdown = markdown:gsub("\\u003C", "<")
-- Strip iframe blocks
                    :gsub("\\u003E", ">")
s = s:gsub("<iframe.-</iframe>", "")
                    :gsub("\\n", "\n")
-- Strip all HTML tags
                    :gsub("\\r", "")
s = s:gsub("<.->", "")
                    :gsub("\\\"", '"')
return s
                    :gsub("\\/", "/")
end
                    :gsub("\\%", "%%") -- preserve percent signs
 
-- Main logic for Markdown → Wikitext conversion
function p._convert(markdown)
if not markdown or markdown == "" then return "(No content)" end


-- Unescape sequences
  -- 2. Strip all inline HTML tags
markdown = htmlUnescape(markdown)
  markdown = markdown:gsub("<iframe.-</iframe>", "")
markdown = markdown:gsub("\\n", "\n")    -- newline
                    :gsub("<.->", "")
markdown = markdown:gsub("\\r", "")  -- just in case
    markdown = markdown:gsub("\\\"", "\"") -- handle escaped quotes
    markdown = markdown:gsub("\\/", "/")  -- optional: fix slashes
markdown = markdown:gsub("\\%-", "–")    -- dash
markdown = markdown:gsub("\\!", "!")      -- exclamation
markdown = markdown:gsub("\\_", "_")      -- underscores
markdown = markdown:gsub("\\+", "+")     -- plus signs
markdown = markdown:gsub("\\*", "*")      -- asterisks
markdown = markdown:gsub("\\\\", "\\")   -- backslash


-- Strip inline HTML
  -- 3. Normalize blank-lines around headers
markdown = stripHTML(markdown)
  markdown = markdown:gsub("\n#+ ", "\n%1%s") -- ensure line breaks before headings


-- Headings
  -- 4. Convert headings
markdown = markdown:gsub("\n###### (.-)\n", "\n====== %1 ======\n")
  for i = 6,1,-1 do
markdown = markdown:gsub("\n##### (.-)\n", "\n===== %1 =====\n")
    local hashes = string.rep("#", i)
markdown = markdown:gsub("\n#### (.-)\n", "\n==== %1 ====\n")
    local equals = string.rep("=", i)
markdown = markdown:gsub("\n### (.-)\n", "\n=== %1 ===\n")
    markdown = markdown:gsub("\n"..hashes.." (.-)\n", "\n"..equals.." %1 "..equals.."\n")
markdown = markdown:gsub("\n## (.-)\n", "\n== %1 ==\n")
  end
markdown = markdown:gsub("\n# (.-)\n", "\n= %1 =\n")


-- Bold and Italic
  -- 5. Bold & Italics
markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''")
  markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''")
markdown = markdown:gsub("%*%*(.-)%*%*", "'''%1'''")
                    :gsub("%*%*(.-)%*%*", "'''%1'''")
markdown = markdown:gsub("%*(.-)%*", "''%1''")
                    :gsub("%*(.-)%*", "''%1''")


-- Nested lists: convert space-prefixed - to * with correct level
  -- 6. Nested lists from spaces
markdown = markdown:gsub("\n([ ]*)%- ", function(indent)
  markdown = markdown:gsub("(\n[ ]*)%- ", function(spaces)
local level = math.floor(#indent / 2) + 1
    local level = math.floor(#spaces / 2) + 1
return "\n" .. string.rep("*", level) .. " "
    return "\n" .. string.rep("*", level) .. " "
end)
  end)


-- Links: [text](url) → [url text]
  -- 7. Links
markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]")
  markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]")


-- Normalize line endings
  -- 8. Clean up extra blank lines
markdown = markdown:gsub("\r", "")
  markdown = markdown:gsub("\r", "")
markdown = markdown:gsub("\n\n\n+", "\n\n") -- Collapse extra breaks
                    :gsub("\n\n\n+", "\n\n")
markdown = markdown:gsub("^%s+", "") -- Trim leading space
                    :gsub("^%s+", ""):gsub("%s+$", "")
markdown = markdown:gsub("%s+$", "") -- Trim trailing space


return markdown
  return markdown
end
end


-- Entry point: used from #invoke with raw markdown string
function p.convertMarkdownToWikitext(frame)
function p.convertMarkdownToWikitext(frame)
local markdown = frame.args[1]
  local md = frame.args[1]
if not markdown then return "(No input received)" end
  if not md then return "(No input received)" end
return p._convert(markdown)
  return p._convert(md)
end
end


return p
return p

Revision as of 17:09, 24 July 2025

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

local p = {}

function p._convert(markdown)
  if not markdown or markdown == "" then return "(No content)" end

  -- 1. Decode JSON-style escapes
  markdown = markdown:gsub("\\u003C", "<")
                     :gsub("\\u003E", ">")
                     :gsub("\\n", "\n")
                     :gsub("\\r", "")
                     :gsub("\\\"", '"')
                     :gsub("\\/", "/")
                     :gsub("\\%", "%%")  -- preserve percent signs

  -- 2. Strip all inline HTML tags
  markdown = markdown:gsub("<iframe.-</iframe>", "")
                     :gsub("<.->", "")

  -- 3. Normalize blank-lines around headers
  markdown = markdown:gsub("\n#+ ", "\n%1%s") -- ensure line breaks before headings

  -- 4. Convert headings
  for i = 6,1,-1 do
    local hashes = string.rep("#", i)
    local equals = string.rep("=", i)
    markdown = markdown:gsub("\n"..hashes.." (.-)\n", "\n"..equals.." %1 "..equals.."\n")
  end

  -- 5. Bold & Italics
  markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''")
                     :gsub("%*%*(.-)%*%*", "'''%1'''")
                     :gsub("%*(.-)%*", "''%1''")

  -- 6. Nested lists from spaces
  markdown = markdown:gsub("(\n[ ]*)%- ", function(spaces)
    local level = math.floor(#spaces / 2) + 1
    return "\n" .. string.rep("*", level) .. " "
  end)

  -- 7. Links
  markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]")

  -- 8. Clean up extra blank lines
  markdown = markdown:gsub("\r", "")
                     :gsub("\n\n\n+", "\n\n")
                     :gsub("^%s+", ""):gsub("%s+$", "")

  return markdown
end

function p.convertMarkdownToWikitext(frame)
  local md = frame.args[1]
  if not md then return "(No input received)" end
  return p._convert(md)
end

return p