It trims the body by removing all leading and trailing whitespaces from each 
line of the string

The new operation can be useful for writing human-readable, multi-line macros. 
For example, the macros from 
[ghc-rpm-macros-extra](https://src.fedoraproject.org/rpms/ghc-rpm-macros/blob/rawhide/f/macros.ghc-extra)
 can be rewritten as follows.

From
<details><summary>Before</summary>

```
%ghc_lib_subpackage(c:dl:mx)\
%define pkgname %{?2:%{1}}%{!?2:%{lua:\
local pv = rpm.expand("%1")\
local _, _, name = string.find(pv, "^([%a%d-]+)-")\
print(name)\
}}\
%define pkgver %{?2}%{!?2:%{lua:\
local pv = rpm.expand("%1")\
print(string.sub(pv, string.find(pv, "[%d.]+$")))\
}}\
%global ghc_subpackages_list %{?ghc_subpackages_list} %{pkgname}-%{pkgver}\
%{!-x:%{?1:%global ghc_packages_list %{?ghc_packages_list} 
%{pkgname}-%{pkgver}}}\
%define basepkg %{ghc_prefix}-%{pkgname}\
%if 0%{!-m:1}\
%package -n %{basepkg}\
Summary:        Haskell %{pkgname} library\
%{?1:Version:        %{pkgver}}\
%{-l:License:        %{-l*}}\
Url:            https://hackage.haskell.org/package/%{pkgname}\
%{?ghc_pkg_obsoletes:Obsoletes:      %(echo "%{ghc_pkg_obsoletes}" | 
sed -e "s/\\(%{ghc_prefix}-[^, ]*\\)-devel/\\1/g")}\
%{?ghc_obsoletes_name:Obsoletes:        %{ghc_obsoletes_name}-%{pkgname} < 
%{pkgver}-%{release}}\
\
%description -n %{basepkg}\
This package provides the Haskell %{pkgname} library.\
\
%endif\
%package -n %{basepkg}-devel\
Summary:        Haskell %{pkgname} library development files\
%{?1:Version:        %{pkgver}}\
%{-l:License:        %{-l*}}\
Provides:       %{basepkg}-static = %{pkgver}-%{release}\
Provides:       %{basepkg}-static%{?_isa} = %{pkgver}-%{release}\
Requires:       %{ghc_prefix}-compiler = 
%{ghc_version}%{?ghc_version_override:-%{release}}\
%if 0%{!-m:1}\
Requires:       %{ghc_prefix}-%{pkgname}%{?_isa} = %{pkgver}-%{release}\
%endif\
%{?ghc_pkg_c_deps:Requires:       %{ghc_pkg_c_deps}}\
%{-c:Requires:       %{-c*}}\
%{?ghc_obsoletes_name:Obsoletes:        %{ghc_obsoletes_name}-%{pkgname}-devel 
< %{pkgver}-%{release}}\
%{?ghc_pkg_obsoletes:Obsoletes:      %{ghc_pkg_obsoletes}}\
\
%description -n %{basepkg}-devel\
This package provides the Haskell %{pkgname} library development files.\
\
%if 0%{!-m:1}\
%if %{with haddock}\
%package -n %{basepkg}-doc\
Summary:        Haskell %{pkgname} library documentation\
%{?1:Version:        %{pkgver}}\
%{-l:License:        %{-l*}}\
BuildArch:      noarch\
Requires:       %{ghc_prefix}-filesystem\
%{?ghc_obsoletes_name:Obsoletes:        %{ghc_obsoletes_name}-%{pkgname}-doc 
< %{pkgver}-%{release}}\
Supplements:    (%{basepkg}-devel and %{ghc_prefix}-doc)\
\
%description -n %{basepkg}-doc\
This package provides the Haskell %{pkgname} library documentation.\
%endif\
\
%if %{with ghc_prof}\
%package -n %{basepkg}-prof\
Summary:        Haskell %{pkgname} profiling library\
%{?1:Version:        %{pkgver}}\
%{-l:License:        %{-l*}}\
Requires:       %{ghc_prefix}-%{pkgname}-devel%{?_isa} = %{pkgver}-%{release}\
%{?ghc_obsoletes_name:Obsoletes:        %{ghc_obsoletes_name}-%{pkgname}-prof 
< %{pkgver}-%{release}}\
Supplements:    (%{basepkg}-devel and %{ghc_prefix}-prof)\
\
%description -n %{basepkg}-prof\
This package provides the Haskell %{pkgname} profiling library.\
%endif\
\
%files -n %{basepkg} -f %{!-d:%{pkgname}-%{pkgver}/}%{basepkg}.files\
\
%endif\
%files -n %{basepkg}-devel -f 
%{!-d:%{pkgname}-%{pkgver}/}%{basepkg}-devel.files\
%if 0%{!-m:1}\
\
%if %{with haddock}\
%files -n %{basepkg}-doc -f %{!-d:%{pkgname}-%{pkgver}/}%{basepkg}-doc.files\
%endif\
\
%if %{with ghc_prof}\
%files -n %{basepkg}-prof -f %{!-d:%{pkgname}-%{pkgver}/}%{basepkg}-prof.files\
%endif\
%endif\
%{nil}
```

</details> 

To

<details>
  <summary>After</summary>
  
```
%ghc_lib_subpackage(c:dl:mx) %{trim:
    %define pkgname %{?2:%{1}}%{!?2:%{lua:
        local pv = rpm.expand("%1")
        local _, _, name = string.find(pv, "^([%a%d-]+)-")
        print(name)
    }}

    %define pkgver %{?2}%{!?2:%{lua:
        local pv = rpm.expand("%1")
        print(string.sub(pv, string.find(pv, "[%d.]+$")))
    }}

    %global ghc_subpackages_list %{?ghc_subpackages_list} %{pkgname}-%{pkgver}
    %{!-x:%{?1:%global ghc_packages_list %{?ghc_packages_list} 
%{pkgname}-%{pkgver}}}
    %define basepkg %{ghc_prefix}-%{pkgname}

    %if 0%{!-m:1}
        %package -n %{basepkg}
        Summary:        Haskell %{pkgname} library
        %{?1:Version:        %{pkgver}}
        %{-l:License:        %{-l*}}
        Url:            https://hackage.haskell.org/package/%{pkgname}
        %{?ghc_pkg_obsoletes:Obsoletes:      %(echo 
"%{ghc_pkg_obsoletes}" | sed -e "s/\\(%{ghc_prefix}-[^, 
]*\\)-devel/\\1/g")}
        %{?ghc_obsoletes_name:Obsoletes:        
%{ghc_obsoletes_name}-%{pkgname} < %{pkgver}-%{release}}

        %description -n %{basepkg}
        This package provides the Haskell %{pkgname} library.

    %endif
    %package -n %{basepkg}-devel

    Summary:        Haskell %{pkgname} library development files

    %{?1:Version:        %{pkgver}}
    %{-l:License:        %{-l*}}

    Provides:       %{basepkg}-static = %{pkgver}-%{release}
    Provides:       %{basepkg}-static%{?_isa} = %{pkgver}-%{release}
    Requires:       %{ghc_prefix}-compiler = 
%{ghc_version}%{?ghc_version_override:-%{release}}

    %if 0%{!-m:1}
        Requires:       %{ghc_prefix}-%{pkgname}%{?_isa} = %{pkgver}-%{release}
    %endif

    %{?ghc_pkg_c_deps:Requires:       %{ghc_pkg_c_deps}}
    %{-c:Requires:       %{-c*}}
    %{?ghc_obsoletes_name:Obsoletes:        
%{ghc_obsoletes_name}-%{pkgname}-devel < %{pkgver}-%{release}}
    %{?ghc_pkg_obsoletes:Obsoletes:      %{ghc_pkg_obsoletes}}

    %description -n %{basepkg}-devel
    This package provides the Haskell %{pkgname} library development files.

    %if 0%{!-m:1}
        %if %{with haddock}\
            %package -n %{basepkg}-doc
            Summary:        Haskell %{pkgname} library documentation
            %{?1:Version:        %{pkgver}}
            %{-l:License:        %{-l*}}
            BuildArch:      noarch

            Requires:       %{ghc_prefix}-filesystem
            %{?ghc_obsoletes_name:Obsoletes:        
%{ghc_obsoletes_name}-%{pkgname}-doc < %{pkgver}-%{release}}
            Supplements:    (%{basepkg}-devel and %{ghc_prefix}-doc)

            %description -n %{basepkg}-doc
            This package provides the Haskell %{pkgname} library documentation.
        %endif

        %if %{with ghc_prof}
            %package -n %{basepkg}-prof
            Summary:        Haskell %{pkgname} profiling library

            %{?1:Version:        %{pkgver}}
            %{-l:License:        %{-l*}}

            Requires:       %{ghc_prefix}-%{pkgname}-devel%{?_isa} = 
%{pkgver}-%{release}
            %{?ghc_obsoletes_name:Obsoletes:        
%{ghc_obsoletes_name}-%{pkgname}-prof < %{pkgver}-%{release}}
            Supplements:    (%{basepkg}-devel and %{ghc_prefix}-prof)

            %description -n %{basepkg}-prof
            This package provides the Haskell %{pkgname} profiling library.
        %endif

        %files -n %{basepkg} -f %{!-d:%{pkgname}-%{pkgver}/}%{basepkg}.files

    %endif

    %files -n %{basepkg}-devel -f 
%{!-d:%{pkgname}-%{pkgver}/}%{basepkg}-devel.files
    %if 0%{!-m:1}
        %if %{with haddock}
            %files -n %{basepkg}-doc -f 
%{!-d:%{pkgname}-%{pkgver}/}%{basepkg}-doc.files
        %endif

        %if %{with ghc_prof}
            %files -n %{basepkg}-prof -f 
%{!-d:%{pkgname}-%{pkgver}/}%{basepkg}-prof.files
        %endif
    %endif
}
```
  
</details>

This macro serves as an example of something quite large and complex, which is 
not immediately easy to read or understand. Logical indentation and flexible 
line breaks greatly enhance the readability. 

We can't use the %{expand: ...} construct here because, the %package 
directives won't be processed correctly (leading spaces matter in their 
context).

You can view, comment on, or merge this pull request online at:

  https://github.com/rpm-software-management/rpm/pull/3663

-- Commit Summary --

  * Add new "%{trim: ...}" string operation

-- File Changes --

    M docs/manual/macros.md (3)
    M rpmio/macro.cc (28)
    M tests/rpmmacro.at (23)

-- Patch Links --

https://github.com/rpm-software-management/rpm/pull/3663.patch
https://github.com/rpm-software-management/rpm/pull/3663.diff

-- 
Reply to this email directly or view it on GitHub:
https://github.com/rpm-software-management/rpm/pull/3663
You are receiving this because you are subscribed to this thread.

Message ID: <rpm-software-management/rpm/pull/3...@github.com>
_______________________________________________
Rpm-maint mailing list
Rpm-maint@lists.rpm.org
https://lists.rpm.org/mailman/listinfo/rpm-maint

Reply via email to