如何在 ConTeXt 中随机替换逐项列表中的文本?

如何在 ConTeXt 中随机替换逐项列表中的文本?

我需要随机用填空题(又名)空白处替换文本。

我一直在使用以下代码,在大多数情况下都可以正常工作。这会随机用空白行替换单词,比空白行略长,因此如果打印出来,读者可以写下缺失的单词:

\definefield[cloze][line]

\newcount\tmpnum
\def\randomtext#1{\def\tmpbuf{}\tmpnum=0\randomtextA#1;;}
\def\randomtextA#1;{\if;#1;\getrandomnumber\randomnumber{1}{\tmpnum}%
      \expandafter\ifcase\expandafter\randomnumber\tmpbuf\fi
   \else \expandafter\def\expandafter\tmpbuf\expandafter{\tmpbuf\or#1}%
      \advance\tmpnum by1
      \expandafter\randomtextA
   \fi
}

\def\processword{\inframed[frame=off, bottomframe=on, empty=yes]}

\newcount\clozecount
\def\mycloze{%
  \global \advance \clozecount by 1
  \definefield[uniqcloze\the\clozecount][line]%
  \field[uniqcloze\the\clozecount]%
}

\def\processword#1{\randomtext{{\expanded{#1}};{\expanded{\hskip0pt\framed[frame=off, bottomframe=on, empty=yes]{... #1\ ...}\hskip0pt}}}}

\starttext

    \processwords{
    \input knuth
    }
\stoptext

这很有效,但是,我遇到了一种新情况,我需要替换出现在逐项列表中的文本。当我将逐项列表放在宏中时\processwords,它变得非常混乱,我怀疑可能是替换了\item,但我不确定:

\definefield[cloze][line]

\newcount\tmpnum
\def\randomtext#1{\def\tmpbuf{}\tmpnum=0\randomtextA#1;;}
\def\randomtextA#1;{\if;#1;\getrandomnumber\randomnumber{1}{\tmpnum}%
      \expandafter\ifcase\expandafter\randomnumber\tmpbuf\fi
   \else \expandafter\def\expandafter\tmpbuf\expandafter{\tmpbuf\or#1}%
      \advance\tmpnum by1
      \expandafter\randomtextA
   \fi
}

\def\processword{\inframed[frame=off, bottomframe=on, empty=yes]}

\newcount\clozecount
\def\mycloze{%
  \global \advance \clozecount by 1
  \definefield[uniqcloze\the\clozecount][line]%
  \field[uniqcloze\the\clozecount]%
}

\def\processword#1{\randomtext{{\expanded{#1}};{\expanded{\hskip0pt\framed[frame=off, bottomframe=on, empty=yes]{... #1\ ...}\hskip0pt}}}}

\starttext

    \processwords{
    \startitemize[n]
        \item This is some text.
        \item This is other text.
        \startitemize[1]
            \item This is yet more text.
        \stopitemize
    \stopitemize
    }
\stoptext

注意:对于上述情况,每一个文本的一段 clozed 将会出现在 items 中。我尝试找到某种方法来设置 \setupitemizegroup放置\processwords{}在以 开头的每个文本上\item,但找不到解决方案。

这是可行的,但是,包含列表的文件是\input,因此手动添加\processwords{}所有内容是不现实的:

\starttext

    \startitemize[n]
        \item \processwords{This is some text.}
        \item \processwords{This is other text.}
        \startitemize[1]
            \item \processwords{This is yet more text.}
        \stopitemize
    \stopitemize
\stoptext

我怎样才能让它随机用空白替换文本,而不弄乱逐项列表的结构?

相关内容