Module:Quote: Difference between revisions
Fabrice P. Laussy's Web
Fabrice (talk | contribs)
Created page with "local p = {} function p.processLineBreaks(frame) local text = frame.args.text or frame.args[1] or "" if text == "" then return text end -- Step 1: Remove single newlines after a hyphen local result = text:gsub("%-([^\n]?)\n([^\n])", "%1%2") -- Step 2: Replace other single newlines with a space result = result:gsub("\n([^\n])", " %1") -- Step 3: Replace multiple consecutive newlines with a single newline result = result:gsub("\..."
 
Fabrice (talk | contribs)
Fabrice changed the content model of the page Module:Quote from "wikitext" to "Scribunto module"
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.processLineBreaks(frame)
-- Main function for processing quote text
     local text = frame.args.text or frame.args[1] or ""
function p.process(frame)
     if text == "" then
     local raw = frame.args.text or frame.args[1] or ""
        return text
     if raw == "" then return "" end
     end
 
     -- Step 1: Remove single newlines after a hyphen
     -- Optional: restore pipes/equals if you replace them earlier in template
     local result = text:gsub("%-([^\n]?)\n([^\n])", "%1%2")
    -- raw = raw:gsub("__EQUALS__", "=")
     -- Step 2: Replace other single newlines with a space
 
     result = result:gsub("\n([^\n])", " %1")
     -- Step 1: Remove continuation hyphens ( - at end of line, possibly with spaces)
     -- Step 3: Replace multiple consecutive newlines with a single newline
    -- Matches: optional spaces, then -, then optional spaces, then newline
     result = result:gsub("\n+", "\n")
     local text = raw:gsub("%s*%-+%s*\n", " ")
     -- Step 4: Restore equal signs from placeholder
 
     result = result:gsub("__EQUALS__", "=")
     -- Step 2: Replace any remaining single newlines with space (join lines into paragraph)
     return result
     text = text:gsub("\n", " ")
 
     -- Step 3: Collapse multiple spaces into one
     text = text:gsub("%s+", " ")
 
     -- Step 4: Trim leading/trailing space
    text = text:match("^%s*(.-)%s*$")
 
    return text
end
 
-- Alternative function if you want to PRESERVE deliberate line breaks (e.g. for poetry)
-- Use this if you want newlines → <br />, but still remove continuation hyphens
function p.processWithBreaks(frame)
    local raw = frame.args.text or frame.args[1] or ""
     if raw == "" then return "" end
 
    -- Remove continuation hyphens, join those lines seamlessly
    local text = raw:gsub("%s*%-+%s*\r?\n", "")
 
    -- Convert remaining newlines to <br /> for use inside <poem> or directly
    text = text:gsub("\r?\n", "<br />")
 
     return text
end
end


return p
return p

Latest revision as of 11:15, 31 December 2025

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

local p = {}

-- Main function for processing quote text
function p.process(frame)
    local raw = frame.args.text or frame.args[1] or ""
    if raw == "" then return "" end

    -- Optional: restore pipes/equals if you replace them earlier in template
    -- raw = raw:gsub("__EQUALS__", "=")

    -- Step 1: Remove continuation hyphens ( - at end of line, possibly with spaces)
    -- Matches: optional spaces, then -, then optional spaces, then newline
    local text = raw:gsub("%s*%-+%s*\n", " ")

    -- Step 2: Replace any remaining single newlines with space (join lines into paragraph)
    text = text:gsub("\n", " ")

    -- Step 3: Collapse multiple spaces into one
    text = text:gsub("%s+", " ")

    -- Step 4: Trim leading/trailing space
    text = text:match("^%s*(.-)%s*$")

    return text
end

-- Alternative function if you want to PRESERVE deliberate line breaks (e.g. for poetry)
-- Use this if you want newlines → <br />, but still remove continuation hyphens
function p.processWithBreaks(frame)
    local raw = frame.args.text or frame.args[1] or ""
    if raw == "" then return "" end

    -- Remove continuation hyphens, join those lines seamlessly
    local text = raw:gsub("%s*%-+%s*\r?\n", "")

    -- Convert remaining newlines to <br /> for use inside <poem> or directly
    text = text:gsub("\r?\n", "<br />")

    return text
end

return p