从 biblatex 中的 URL 中删除协议信息

从 biblatex 中的 URL 中删除协议信息

我想通过从 URL 的打印版本中删除协议信息(http://https://)来简化我的参考书目,而不会破坏超链接。我目前的解决方案如下

\documentclass{article}

\begin{filecontents}{bibliography.bib}
@misc{key1,
author="Google",
title="Google",
url="https://google.com",
}
@misc{key2,
author="Microsoft",
title="Bing",
url="http://bing.com",
}
@misc{key3,
author="DuckDuckGo",
title="DuckDuckGo",
url="https://duckduckgo.com",
}
\end{filecontents}

\usepackage{hyperref}
\usepackage{biblatex}

\DeclareSourcemap{%
  \maps[datatype=bibtex,overwrite=true]{%
    \map{
      \step[fieldsource=url, final=true]
      \step[fieldset=userd, origfieldval,final=true]
      \step[fieldsource=userd, match=\regexp{\A(ht|f)tp(s)?:\/\/([^/]+)},replace=\regexp{$3}]
    }
  }%
}

\DeclareFieldFormat{url}{\mkbibacro{URL}\addcolon\space\href{#1}{\url{\thefield{userd}}}}

\bibliography{bibliography}
\nocite{*}

\begin{document}
\printbibliography
\end{document}

但我确信一定有更简单的解决方案,尤其是Sourcemap考虑到这个问题,这对我来说似乎有点矫枉过正。至少我还没有想出最简单的正则表达式。

答案1

我认为你的方法非常明智,但我想改变三件事

  1. 对于 URL 路径,请使用逐字字段 ( verba),而不是文字字段 ( userd)。如果 URL 包含危险字符(如#或 ) ,则这一点很重要%。文字字段无法处理这些字符,除非对其进行转义。

  2. 您可以通过简单地用空值替换匹配的方案而不是捕获剩余的路径来使 RegEx 稍微缩短一些。

  3. 使用\nolinkurl里面的\href\href已经提供的链接。

平均能量损失

\documentclass{article}

\usepackage{biblatex}
\usepackage{hyperref}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=url, final=true]
      \step[fieldset=verba, origfieldval, final=true]
      \step[fieldsource=verba, match=\regexp{\A(ht|f)tp(s)?:\/\/}, replace={}]
    }
  }
}

\DeclareFieldFormat{url}{%
  \mkbibacro{URL}\addcolon\space
  \href{#1}{\nolinkurl{\thefield{verba}}}}

\begin{filecontents}{\jobname.bib}
@misc{key1,
  author = {Google},
  title  = {Google},
  url    = {https://google.com},
}
@misc{key2,
  author = {Microsoft},
  title  = {Bing},
  url    = {http://bing.com},
}
@misc{key3,
  author = {DuckDuckGo},
  title  = {DuckDuckGo},
  url    = {https://duckduckgo.com/_^a#?\u&6%k},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\nocite{*}

\begin{document}
\printbibliography
\end{document}

DuckDuckGo。DuckDuckGo。url:duckduckgo.com/_^a#?\u&6%k。//Google。Google。url:google.com。//Microsoft。Bing。url:bing.com。


我认为最好将字符串操作留给 Biber(在 TeX 中当然可以进行一些字符串操作,但通常比使用 Biber 更麻烦),因此如果您想保留有关协议(http 或 https)的信息,而不想只使用硬编码或链接所有 URL httphttps则需要在 中传递两个单独的字段.bbl。另一种选择是分别保存协议方案和路径,但随后您必须将所有内容拼凑在一起才能获得有效的链接,这似乎需要做更多工作。所以我认为您的方法非常简单。


在这种情况下,这可能有点矫枉过正,但通常我发现使用新声明的字段比通用占位符更好verba。所以这里是使用专用字段的相同解决方案protocollessurl

\documentclass{article}

\begin{filecontents}{protocollessurl.dbx}
\DeclareDatamodelFields[type=field, datatype=uri]{protocollessurl}

\DeclareDatamodelEntryfields{protocollessurl}
\end{filecontents}

\usepackage[datamodel=protocollessurl]{biblatex}
\usepackage{hyperref}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=url, final=true]
      \step[fieldset=protocollessurl, origfieldval, final=true]
      \step[fieldsource=protocollessurl, match=\regexp{\A(ht|f)tp(s)?:\/\/}, replace={}]
    }
  }
}

\DeclareFieldFormat{url}{%
  \mkbibacro{URL}\addcolon\space
  \href{#1}{\nolinkurl{\thefield{protocollessurl}}}}

\begin{filecontents}{\jobname.bib}
@misc{key1,
  author = {Google},
  title  = {Google},
  url    = {https://google.com},
}
@misc{key2,
  author = {Microsoft},
  title  = {Bing},
  url    = {http://bing.com},
}
@misc{key3,
  author = {DuckDuckGo},
  title  = {DuckDuckGo},
  url    = {https://duckduckgo.com/_^a#?\u&6%k},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\nocite{*}

\begin{document}
\printbibliography
\end{document}

相关内容