Module:Quote: Difference between revisions
Fabrice P. Laussy's Web
Fabrice (talk | contribs)
Fabrice changed the content model of the page Module:Quote from "wikitext" to "Scribunto module"
No edit summary
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

Revision as of 01:50, 31 December 2025

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 →
, 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 
for use inside <poem> or directly text = text:gsub("\r?\n", "
")
   return text

end

return p