From 8394d3cf12e5e47379b8930609682c3d4dd84ef0 Mon Sep 17 00:00:00 2001 From: Nicolas Mailhot Date: Jun 04 2019 06:23:55 +0000 Subject: misc: add a wordwrap helper --- diff --git a/common.lua b/common.lua index 3f099bc..0f28a8d 100644 --- a/common.lua +++ b/common.lua @@ -128,6 +128,54 @@ local function writevars(macrofile, rpmvars) end end +-- https://github.com/rpm-software-management/rpm/issues/566 +-- Reformat a text intended to be used used in a package description, removing +-- rpm macro generation artefacts. +-- – remove leading and ending empty lines +-- – trim intermediary empty lines to a single line +-- – fold on spaces +-- Should really be a %%{wordwrap:…} verb +local function wordwrap(text) + text = rpm.expand(text .. "\n") + text = string.gsub(text, "\t", " ") + text = string.gsub(text, " +\n", "\n") + text = string.gsub(text, "\n+\n", "\n\n") + text = string.gsub(text, "^\n", "") + text = string.gsub(text, "\n( *)[-*—][  ]+", "\n%1– ") + output = "" + for line in string.gmatch(text, "[^\n]*\n") do + local pos = 0 + local advance = "" + for word in string.gmatch(line, "%s*[^%s]*\n?") do + local wl, bad = utf8.len(word) + if not wl then + print("%{warn: Invalid UTF-8 sequence detected in:\n" .. + word .. "\nIt may produce unexpected results.\n}") + wl = bad + end + if (pos == 0) then + advance = string.gsub(word, "^(%s*).*", "%1") + pos = pos + wl + elseif (pos + wl < 81) then + pos = pos + wl + else + word = advance .. string.gsub(word, "^%s*", "") + output = output .. "\n" + pos = utf8.len(word) + end + output = output .. word + if pos > 80 then + pos = 0 + if not string.match(word, "\n$") then + output = output .. "\n" + end + end + end + end + output = string.gsub(output, "\n*$", "\n") + return output +end + return { explicitset = explicitset, explicitunset = explicitunset, @@ -139,4 +187,5 @@ return { getsuffixes = getsuffixes, getbestsuffix = getbestsuffix, writevars = writevars, + wordwrap = wordwrap, } diff --git a/macros.fedora-misc-srpm b/macros.fedora-misc-srpm index 98aa6c2..3431190 100644 --- a/macros.fedora-misc-srpm +++ b/macros.fedora-misc-srpm @@ -9,3 +9,14 @@ # A directory for SWID tag files describing the installation %_swidtagdir %{_prefix}/lib/swidtag/fedoraproject.org + +# A helper to apply the fedora.wordwrap filter to the content of an rpm +# variable, and print the result. Optional parameter: +# – -v (default value: _description) +# Putting multiple lines of UTF-8 text inside a variable is usually +# accomplished with a %%{expand: some_text}. +%wordwrap(v:) %{lua: +local fedora = require "fedora.common" +local variable = "%{" .. rpm.expand("%{-v*}%{!-v:_description}") .. "}" +print(fedora.wordwrap(variable)) +}