如何在SGML文件中配置基础URL变量以简化链接编写
如何配置基础URL简化SGML中的ulink链接?
我在单个.sgml文件中使用了重复前缀的链接代码:
<ulink url="https://github.com/postgres/postgres/commit/2af07e2f7"> [2af07e2f7] </ulink> <ulink url="https://github.com/postgres/postgres/commit/165d581f1"> [165d581f1] </ulink>, <ulink url="https://github.com/postgres/postgres/commit/617f9b7d4"> [617f9b7d4] </ulink>
希望配置一个基础URL变量,将链接简化为以下形式,避免重复编写https://github.com/postgres/postgres/commit前缀:
<ulink url="$variable/2af07e2f7"> [2af07e2f7] </ulink>
当前已有的XSL参数配置如下:
<xsl:param name="base.dir" select="'html/'"></xsl:param>
怀疑该配置可能和需求相关,请问能否实现?
实现方案
可以通过新增自定义XSL参数+改写模板的方式实现,步骤如下:
- 添加自定义基础URL参数
在你的stylesheet.xsl文件中新增一个参数,定义GitHub提交链接的基础前缀:
<xsl:param name="github.commit.base.url" select="'https://github.com/postgres/postgres/commit/'" />
- 编写ulink匹配模板
添加一个模板来处理ulink标签,自动识别简化写法并拼接完整URL:
<xsl:template match="ulink"> <xsl:variable name="raw-url" select="@url" /> <xsl:choose> <!-- 匹配以$github/开头的简化链接 --> <xsl:when test="starts-with($raw-url, '$github/')"> <ulink> <xsl:attribute name="url"> <xsl:value-of select="concat($github.commit.base.url, substring-after($raw-url, '$github/'))" /> </xsl:attribute> <xsl:apply-templates /> </ulink> </xsl:when> <!-- 保留原有正常链接的处理逻辑 --> <xsl:otherwise> <xsl:copy-of select="." /> </xsl:otherwise> </xsl:choose> </xsl:template>
- 使用简化写法
之后在sgml文件中就可以用简化形式编写链接:
<ulink url="$github/2af07e2f7"> [2af07e2f7] </ulink> <ulink url="$github/165d581f1"> [165d581f1] </ulink>, <ulink url="$github/617f9b7d4"> [617f9b7d4] </ulink>
注意:原有的base.dir参数仅用于指定HTML输出文件的存放目录,和URL拼接需求无关,需要通过新增自定义参数实现目标功能。
内容的提问来源于stack exchange,提问作者jian
相关产品推荐
相关产品推荐

