[Tex/LaTex] Ignoring all but certain text

macros

I'm wondering if there's a way to define a command so that, when a flag is specified, I can ignore everything but text within the tag. For instance, if I had

...
This is a \dontignore{bird}.

I would want the output to be either "This is a bird." or "bird" depending on whether or not I switch this flag.

Is this doable? Obviously surrounding everything but the text I want in some tag would be easy, but that seems quite inelegant.

Edit: Here's exactly what I have in mind:

\documentclass{article}
\newcommand{\dontignore}[1]{...}
\begin{document}
Hello world.  This is my full document with a lot of extra bells and whistles and lines.  But in the end, \dontignore{this is all that matters}.
\end{document}

With the flag off, the output should be

Hello world. This is my full document with a lot of extra bells and whistles and lines. But in the end, this is all that matters.

Otherwise,

this is all that matters

Optimally, I would also be able to tell LaTeX not to ignore things like section and enumerate/itemize/item commands as well, as follows

\documentclass{article}
\newcommand{\dontignore}[1]{...}
\begin{document}
\section{First part}
Hello world.  This is my full document with a lot of extra bells and whistles and lines.  But in the end, \dontignore{this is all that matters}.
\begin{enumerate}
  \item \dontignore{Item 1}
  \item Something like \dontignore{Item 2}
\end{enumerate}
\end{document}

to produce either everything (eg, with dontignore defined as \newcommand{\dontignore}[1]{#1}) or otherwise (with some flag specifying section, enumerate, etc.) as follows

1 First part

this is all that matters

  1. Item 1
  2. Item 2

Best Answer

enter image description here


enter image description here


In the following you get one or the other output shown above depending on whether this line is commented out or not

%\let\ignoreflag\relax

\documentclass{article}

\long\def\dontignore#1{#1}
\makeatletter

\long\def\ignoreflag{%
\@makeother\{%
\@makeother\}%
\xignore}
\long\def\xignore#1\dontignore#2{%
\catcode`\{\@ne
\catcode`\}\tw@
\afterassignment\xxdontignore\toks@\bgroup}
\long\def\xxdontignore{%
\the\toks@\ignoreflag}
\makeatother

%\let\ignoreflag\relax
\begin{document}
\ignoreflag

This is some text 

\begin{itemize}
\item aaa
\item bbb
\item ccc \dontignore{hello }
\end{itemize}

more text 
\dontignore{
\begin{enumerate}
\item this
\item that
\end{enumerate}
}

more stuff

\dontignore{\end{document}}