背景

背景

背景

希望允许用户定义交叉引用和标题。输入 XML 类似于:

<p>
  <a class="name" data-type="tab" name="ref"/>
</p>
<table>...</table>
<p>
  See <a class="href" data-type="tab" href="#ref"/> for details.
</p>

注意ref和的#ref不同之处(由于 HTML 锚链接的工作方式)。#必须修剪,我已经修剪了(如下面的问题部分所示)。

系统

ConTeXt 版本 2023.09.26 18:19。

问题

AnchorXref和宏xmlatt不能用于交叉引用。也就是说,以下操作不起作用:

\startbuffer[main]
<body>
<p><a class="name" data-type="tab" name="ref"/></p>
<table>a table</table>
<p>See <a class="href" data-type="tab" href="#ref"/> for details.</p>
</body>
\stopbuffer

\def\removefirst#1{\expandafter\removefirstA#1\removefirstA}
\def\removefirstA#1#2\removefirstA{#2}

\startxmlsetups xml:xhtml
  \xmlsetsetup{\xmldocument}{*}{-}
  \xmlsetsetup{\xmldocument}{body|p|table}{xml:*}
  \xmlsetsetup{\xmldocument}{a[@class='name']}{xml:anchorname}
  \xmlsetsetup{\xmldocument}{a[@class='href']}{xml:anchorhref}

  \define\AnchorName{}
\stopxmlsetups

\xmlregistersetup{xml:xhtml}

\startxmlsetups xml:body
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:p
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:table
  (name = '\AnchorName')

  \startplacetable[title={Table caption.}, reference={\AnchorName}]
    \startembeddedxtable
      \startxtablebody[body]
        \startxrow
          \startxcell
            Cell content
          \stopxcell
        \stopxrow
      \stopxtablebody
    \stopembeddedxtable
  \stopplacetable
\stopxmlsetups

\startxmlsetups xml:anchorname
  \define\AnchorName{\xmlatt{#1}{data-type}:\xmlatt{#1}{name}}
\stopxmlsetups

% Make a reference to a given anchor.
\startxmlsetups xml:anchorhref
  \define\AnchorXref{\xmlatt{#1}{data-type}:\removefirst{\xmlatt{#1}{href}}}
  \in[\AnchorXref]

  \ (xref = '\AnchorXref')
\stopxmlsetups

\starttext
  \xmlprocessbuffer{main}{main}{}
\stoptext

产生(注意??):

表参考1

使用简单的宏定义即可:

\starttext
  \chapter{Chapter}

  \define\AnchorXref{tab:ref}

  \startplacetable[title={Table caption.}, reference=\AnchorXref]
    \startembeddedxtable
      \startxtablebody[body]
        \startxrow
          \startxcell
            Table cell
          \stopxcell
        \stopxrow
      \stopxtablebody
    \stopembeddedxtable
  \stopplacetable

  See \in[\AnchorXref].
\stoptext

生成:

表参考2

问题

需要做哪些改变才能实现宏扩展?

有关的

答案1

使用\defineexpandable是关键并将\xmlrefatt自动处理#字符:

\startbuffer[main]
<body>
<p><a class="name" data-type="tab" name="ref"/></p>
<table>a table</table>
<p>See <a class="href" data-type="tab" href="#ref"/> for details.</p>
</body>
\stopbuffer

\startxmlsetups xml:xhtml
  \xmlsetsetup{\xmldocument}{*}{-}
  \xmlsetsetup{\xmldocument}{body|p|table}{xml:*}
  \xmlsetsetup{\xmldocument}{a[@class='name']}{xml:anchorname}
  \xmlsetsetup{\xmldocument}{a[@class='href']}{xml:anchorhref}
\stopxmlsetups

\xmlregistersetup{xml:xhtml}

\startxmlsetups xml:body
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:p
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:table
  \startplacetable[title={Table caption.}, reference={\AnchorName}]
    \startembeddedxtable
      \startxtablebody[body]
        \startxrow
          \startxcell
            Cell content
          \stopxcell
        \stopxrow
      \stopxtablebody
    \stopembeddedxtable
  \stopplacetable
\stopxmlsetups

\startxmlsetups xml:anchorname
  \defineexpandable\AnchorName{\xmlatt{#1}{data-type}:\xmlatt{#1}{name}}
\stopxmlsetups

% Make a reference to a given anchor.
\startxmlsetups xml:anchorhref
  \defineexpandable\AnchorXref{\xmlatt{#1}{data-type}:\xmlrefatt{#1}{href}}

  Table \in[\AnchorXref]
\stopxmlsetups

\starttext
  \xmlprocessbuffer{main}{main}{}
\stoptext

生成:

表参考3

相关内容