Use macro argument as a comma-separated words for filtering

filtermacros

So I have this list of \entry macro that takes one argument which is simply a text.

\entry{whatevercoutent}

I want to make use of the second argument as a comma-separated list of words which would be used for filtering entries.

For example,

Some way to declare to only filter "filter1"

\entry{content1}{filter1, filter2, filter3}
\entry{content2}{filter1}
\entry{content3}{filter3}

In the example above, we declared that we would filter filter1. This would result in only content1 and content2 are rendered.

Is there a way to achieve this?

Thanks in advance.

Best Answer

Here's the idea: each “filter” defines a new sequence where to store entries. At the end you can use those sequences. I also define a global sequence to store all entries.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\entry}{mo}
 {% #1 is an entry, #2 is a list of filters (optional)
  \egreg_filterentry:nn { #1 } { #2 }
 }

\NewDocumentCommand{\printentries}{o}
 {
  \seq_use:cn { g__egreg_filterentry_ \IfNoValueTF { #1 } { global } { filter_#1 } _seq } {,~}
 }

\seq_new:N \g__egreg_filterentry_global_seq

\cs_new_protected:Nn \egreg_filterentry:nn
 {
  % store the entry in the global list
  \seq_gput_right:Nn \g__egreg_filterentry_global_seq { #1 }
  \tl_if_novalue:nF { #2 }
   {% we have a list of filters
    \clist_map_inline:nn { #2 }
     {% add the entry to the list corresponding to each given filter
      \__egreg_filterentry_filter:nn { #1 } { ##1 }
     }
   }
 }

\cs_new_protected:Nn \__egreg_filterentry_filter:nn
 {%
  \seq_if_exist:cF { g__egreg_filterentry_filter_#2_seq }
   {% create the list if not yet existing
    \seq_new:c { g__egreg_filterentry_filter_#2_seq }
   }
  \seq_gput_right:cn { g__egreg_filterentry_filter_#2_seq } { #1 }
 }

\ExplSyntaxOff

\begin{document}

\entry{content0}
\entry{content1}[filter1, filter2, filter3]
\entry{content2}[filter1]
\entry{content3}[filter3]

Global list: \printentries

filter 1: \printentries[filter1]

filter 2: \printentries[filter2]

filter 3: \printentries[filter3]

\end{document}

enter image description here