Skip to content

Commit

Permalink
Don't try to escape valid LaTeX symbols on paste, see #3463
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Apr 14, 2024
1 parent 2769726 commit 6c388fe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.hannahsten.texifyidea.editor.pasteproviders

import nl.hannahsten.texifyidea.editor.pasteproviders.StyledTextHtmlToLatexConverter.Companion.closingTags
import nl.hannahsten.texifyidea.editor.pasteproviders.StyledTextHtmlToLatexConverter.Companion.escapeText
import nl.hannahsten.texifyidea.editor.pasteproviders.StyledTextHtmlToLatexConverter.Companion.openingTags
import nl.hannahsten.texifyidea.file.LatexFile
import nl.hannahsten.texifyidea.lang.LatexPackage
Expand All @@ -20,10 +19,11 @@ fun convertHtmlToLatex(nodes: List<Node>, latexFile: LatexFile): String {
for (node in nodes) {
if (node.childNodeSize() == 0) {
when (node) {
is TextNode -> out += escapeText(node.text())
is TextNode -> out += node.text()
is Element -> {
out += handleElement(node, latexFile)
}

else -> {
Log.error("Did not plan for " + node.javaClass.name + " please implement a case for this")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,44 @@ class StyledTextHtmlToLatexConverter : HtmlToLatexConverter {
* Map HTML tags to LaTeX
*/
val openingTags = hashMapOf(
"i" to "\\textit{",
"em" to "\\textit{",
"b" to "\\textbf{",
"u" to "\\underline{",
"p" to "",
"ol" to "\\begin{enumerate}\n",
"ul" to "\\begin{itemize}\n",
"li" to "\\item ",
"br" to "\n",
"sup" to "\\textsuperscript{",
"sub" to "\\textsubscript{",
"em" to "\\textit{",
"h1" to "\\chapter*{",
"h2" to "\\section*{",
"h3" to "\\subsection*{",
"h4" to "\\subsubsection*{",
"h5" to "\\subsubsubsection*{",
"i" to "\\textit{",
"li" to "\\item ",
"ol" to "\\begin{enumerate}\n",
"p" to "",
"sub" to "\\textsubscript{",
"sup" to "\\textsuperscript{",
"u" to "\\underline{",
"ul" to "\\begin{itemize}\n",
)

val closingTags = hashMapOf(
"i" to "}",
"em" to "}",
"b" to "}",
"u" to "}",
"p" to "\n\n",
"ol" to "\\end{enumerate}\n",
"ul" to "\\end{itemize}\n",
"li" to "\n",
"a" to "}",
"b" to "}",
"br" to "",
"sup" to "}",
"sub" to "}",
"div" to "\n",
"em" to "}",
"h1" to "}\n",
"h2" to "}\n",
"h3" to "}\n",
"h4" to "}\n",
"h5" to "}\n",
"i" to "}",
"li" to "\n",
"ol" to "\\end{enumerate}\n",
"p" to "\n\n",
"sub" to "}",
"sup" to "}",
"u" to "}",
"ul" to "\\end{itemize}\n",
)

val escapeChars = hashMapOf(
"%" to "\\%",
"&" to "\\&",
"_" to "\\_",
"#" to "\\#",
"$" to "\\$",
"{" to "\\{",
"}" to "\\}",
"^" to "\\^",
"~" to "\\~",
"" to "-"
)

fun escapeText(stringin: String): String {
var out = stringin.replace("\\", "\\textbackslash ")

escapeChars.forEach { out = out.replace(it.key, it.value) }

return out
}
}

override fun convertHtmlToLatex(htmlIn: Element, file: LatexFile): String {
Expand All @@ -84,7 +64,7 @@ class StyledTextHtmlToLatexConverter : HtmlToLatexConverter {
val content = if (htmlIn.childNodeSize() > 0)
convertHtmlToLatex(htmlIn.childNodes(), file)
else
escapeText(htmlIn.text())
htmlIn.text()

val postfix = getPostfix(htmlIn)

Expand Down Expand Up @@ -125,7 +105,7 @@ class StyledTextHtmlToLatexConverter : HtmlToLatexConverter {
if (element.attr("href").startsWith("#"))
"\\hyperlink{" + element.attr("href").replace(Regex("^#"), "") + "}{"
else
"\\href{" + escapeText(element.attr("href")) + "}{"
"\\href{" + element.attr("href") + "}{"
else if (element.hasAttr("name"))
"\\hypertarget{" + element.attr("name") + "}{"
else
Expand Down
20 changes: 20 additions & 0 deletions test/nl/hannahsten/texifyidea/editor/HtmlPasteProviderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,24 @@ class HtmlPasteProviderTest : BasePlatformTestCase() {
val latex = HtmlPasteProvider().convertHtmlToLatex(node, myFixture.file as LatexFile)
TestCase.assertEquals("\\textit{italic}", latex)
}

fun testLatex() {
myFixture.configureByText("main.tex", "")
val html = "\\canpaste{\\LaTex}"
val node = Jsoup.parse(html).select("body")[0]
val latex = HtmlPasteProvider().convertHtmlToLatex(node, myFixture.file as LatexFile)
TestCase.assertEquals("\\canpaste{\\LaTex}", latex)
}

fun testNewlines() {
myFixture.configureByText("main.tex", "")
val html = """
\newcommand{\mylabel}[1]{\label{#1}}
\section{One}\mylabel{sec:one}
""".trimIndent()
val node = Jsoup.parse(html).select("body")[0]
val latex = HtmlPasteProvider().convertHtmlToLatex(node, myFixture.file as LatexFile)
TestCase.assertEquals(html, latex)
}
}

0 comments on commit 6c388fe

Please sign in to comment.