Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pasting of LaTeX #3520

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,12 @@ fun convertHtmlToLatex(nodes: List<Node>, latexFile: LatexFile): String {
for (node in nodes) {
if (node.childNodeSize() == 0) {
when (node) {
is TextNode -> out += escapeText(node.text())
// use wholeText to preserve newlines
is TextNode -> out += node.wholeText
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
26 changes: 26 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,30 @@ 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", "")
// Source: github
val html = """
<pre class="notranslate"><span class="pl-k">\newcommand</span>{<span class="pl-c1">\mylabel</span>}[1]{<span class="pl-k">\label</span>{#<span class="pl-v">1</span>}}

<span class="pl-c1">\section</span>{<span class="pl-en">One</span>}<span class="pl-c1">\mylabel</span>{sec:one}</pre>
""".trimIndent()
val result = """
\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(result, latex)
}
}
Loading