背景

背景

背景

URL 从 Markdown 转换为文档变量,旨在排版为可点击的超链接。

问题

ConTeXt 使用url()逐字排版 URL。由于 URL 是宏,因此宏名称会作为超链接插入,而不是 URL。

代码

复制问题的示例代码:

\setupcolors[state=start,]
\setupinteraction[state=start,color=blue,]

\define\TextCompanyName{
  \doifdocumentvariableelse{company}{\documentvariable{company}}{Company}
}

\define\TextCompanyAddressWebsite{
  \doifdocumentvariableelse{website}{\documentvariable{website}}{%
    https://www.company.com
  }
}

% Create hyperlink references within the document.
\define[2]\href{%
  \goto{#1}[url(#2)]%
}

\starttext
  Cliquez-vous \href{\TextCompanyName}{\TextCompanyAddressWebsite}.
\stoptext

问题

如何在宏接收参数#2之前强制扩展该参数\goto,以便将 URL 嵌入到 PDF 中(而不是宏名称)?

答案1

\goto 展开它的参数;问题是它的参数是不可展开的。\TextCompanyName\TextCompanyAddressWebsite都是使用 定义的\define,这会创建\protected在排版之前不会展开的宏。这可以通过使用\def而不是轻松解决\define。使问题更加复杂的是,\doifdocumentvariableelse也是不可展开的。不过,我们可以创建这个宏的可扩展版本:

\setupcolors[state=start,]
\setupinteraction[state=start,color=blue,]

\def\expandabledoifdocumentvariableelse#1#2#3{%
  \if\relax\documentvariable{#1}\relax%
    #3%
  \else%
    #2%
  \fi%
}

\def\TextCompanyName{
  \expandabledoifdocumentvariableelse{company}{\documentvariable{company}}{Company}
}

\def\TextCompanyAddressWebsite{
  \expandabledoifdocumentvariableelse{website}{\documentvariable{website}}{%
    https://www.company.com
  }
}

\define[2]\href{%
  \goto{#1}[url(#2)]%
}

\startdocument[company=TEST]
  Cliquez-vous \href{\TextCompanyName}{\TextCompanyAddressWebsite}.
\stopdocument

使用可扩展宏:

% Creates an expandable if statement that can be passed into goto.
\defineexpandable[3]\IfTextDocumentVarExists{%
  \if\relax\documentvariable{#1}\relax#3\else#2\fi%
} 

\defineexpandable\TextTitle{%
  \IfTextDocumentVarExists{title}{\documentvariable{title}}{Title}%
} 

\defineexpandable\TextCompanyName{%
  \IfTextDocumentVarExists{company}{\documentvariable{company}}{Company}%
} 

\defineexpandable\TextCompanyAddressWebsite{%
  \IfTextDocumentVarExists{website}{\documentvariable{website}}{%
    https://www.company.com
  }
} 

\defineexpandable\TextCompanyAddressPostal{%
  \IfTextDocumentVarExists{address}{\documentvariable{address}}{Postal Address}%
}

\defineexpandable\TextCompanyAddressPhone{%
  \IfTextDocumentVarExists{phone}{\documentvariable{phone}}{1-800-555-1212}%
}

\defineexpandable\TextCompanyAddressEmail{%
  \IfTextDocumentVarExists{email}{\documentvariable{email}}{[email protected]}%
}

相关内容