将 XML 转换为 LaTeX

发布于 2024-10-02 23:40:27 字数 430 浏览 4 评论 0 原文

在 Scala 中,我如何转换:

<p>here we have a <a href="http://www.scala-lang.org/api/current/index.html">link</a> example.</p>

here we have a \url{http://www.scala-lang.org/api/current/index.html}{link} example.

映射到“nothing”,而 _ 映射到“nothing” 映射到 \url{_}{_}

In Scala, how can I transform:

<p>here we have a <a href="http://www.scala-lang.org/api/current/index.html">link</a> example.</p>

to

here we have a \url{http://www.scala-lang.org/api/current/index.html}{link} example.

where <p></p> maps to "nothing", and <a href"_">_</> maps to \url{_}{_}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所谓啦 2024-10-09 23:40:27

作为替代方案,如果您需要更多转换*,您可以从此开始。它还可以与嵌套的 标签一起使用,无论这有什么意义。

代码中需要进行一些转义处理。例如,某些字符在 XML 中被转义,而在 Latex 中则没有被转义,反之亦然。请随意添加此内容。

import xml._

val input = <p>And now try it on a <a href="link1">text</a> with <a href="link2">two urls</a></p>

def mkURL(meta: MetaData, text: String) = {
  val url = meta.asAttrMap.get("href")
  "\\url{%s}{%s}".format(url getOrElse "", text)
}

def transform(xhtml: NodeSeq): String = {
  xhtml.map { node =>
    node match {
      case Node("p", _, ch@_*) => transform(ch)
      case Node("a", meta, ch@_*) => mkURL(meta, transform(ch))
      case x => x.toString
    }
  } mkString
}

println(transform(input))

// And now try it on a \url{link1}{text} with \url{link2}{two urls}

[*] 添加对 \emph 的支持类似于

case Node("em", _, ch@_*) => transform(ch).mkString("\\emph{", "", "}")

As an alternative, if you need more transformations*, you can start with this. It will also work with nested <a/> tags, whatever sense this may make.

There’s some need of escape handling in the code. E.g. some characters are escaped in XML which are not escaped in Latex and the other way round. Feel free to add this.

import xml._

val input = <p>And now try it on a <a href="link1">text</a> with <a href="link2">two urls</a></p>

def mkURL(meta: MetaData, text: String) = {
  val url = meta.asAttrMap.get("href")
  "\\url{%s}{%s}".format(url getOrElse "", text)
}

def transform(xhtml: NodeSeq): String = {
  xhtml.map { node =>
    node match {
      case Node("p", _, ch@_*) => transform(ch)
      case Node("a", meta, ch@_*) => mkURL(meta, transform(ch))
      case x => x.toString
    }
  } mkString
}

println(transform(input))

// And now try it on a \url{link1}{text} with \url{link2}{two urls}

[*] Adding support for \emph would be something like

case Node("em", _, ch@_*) => transform(ch).mkString("\\emph{", "", "}")
温柔戏命师 2024-10-09 23:40:27

更通用的方法是使用解析器,例如 scala 的解析器组合器,或 java.util.constructor 的可用解析器。
如果文件是格式良好的xml,则处理xml的方式也可以。

More generic way is using parsers, like scala's parser combinator, or available ones of java.
if the file is well-formed xml, the way to process xml is ok too.

〆凄凉。 2024-10-09 23:40:27

定义正则表达式:

scala> val link = """<a href="(.+)">(.+)</a>""".r
link: scala.util.matching.Regex = <a href="(.+)">(.+)</a>

scala> val paragraph = """<p>(.+)</p>""".r
paragraph: scala.util.matching.Regex = <p>(.+)</p>

scala> val text = """<p>here we have a <a href="http://www.scala-lang.org/api/current/index.html">link</a> example.</p>"""
text: java.lang.String = <p>here we have a <a href="http://www.scala-lang.org/api/current/index.html">link</a> example.</p>

将它们应用到输入:

scala> val modifiedText = paragraph.replaceAllIn(text, {matched => val paragraph(content) = matched; content})
modifiedText: String = here we have a <a href="http://www.scala-lang.org/api/current/index.html">link</a> example.

scala> link.replaceAllIn(modifiedText, {matched => val link(href, title) = matched; "\\\\url{%s}{%s}" format(href, title)})
res11: String = here we have a \url{http://www.scala-lang.org/api/current/index.html}{link} example.

Define regexps:

scala> val link = """<a href="(.+)">(.+)</a>""".r
link: scala.util.matching.Regex = <a href="(.+)">(.+)</a>

scala> val paragraph = """<p>(.+)</p>""".r
paragraph: scala.util.matching.Regex = <p>(.+)</p>

scala> val text = """<p>here we have a <a href="http://www.scala-lang.org/api/current/index.html">link</a> example.</p>"""
text: java.lang.String = <p>here we have a <a href="http://www.scala-lang.org/api/current/index.html">link</a> example.</p>

Apply them to the input:

scala> val modifiedText = paragraph.replaceAllIn(text, {matched => val paragraph(content) = matched; content})
modifiedText: String = here we have a <a href="http://www.scala-lang.org/api/current/index.html">link</a> example.

scala> link.replaceAllIn(modifiedText, {matched => val link(href, title) = matched; "\\\\url{%s}{%s}" format(href, title)})
res11: String = here we have a \url{http://www.scala-lang.org/api/current/index.html}{link} example.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文