Skip to content

Commit

Permalink
Merge pull request #3519 from Hannah-Sten/paste-tables-windows
Browse files Browse the repository at this point in the history
Prefer pasting tables as text instead of image
  • Loading branch information
PHPirates authored Apr 21, 2024
2 parents 897ea96 + 259273f commit 736ca09
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ open class ImagePasteProvider : PasteProvider {
}

override fun isPastePossible(dataContext: DataContext): Boolean {
// If both paste providers could handle the content, prefer the one that handles the text instead of pasting the text as an image
if (HtmlPasteProvider().isPastePossible(dataContext)) return false
val file = dataContext.getData(PlatformDataKeys.PSI_FILE) ?: return false
if (file.isLatexFile().not()) return false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nl.hannahsten.texifyidea.editor.pasteproviders

import nl.hannahsten.texifyidea.file.LatexFile
import org.jsoup.nodes.Element
import org.jsoup.nodes.Node

class StyledTextHtmlToLatexConverter : HtmlToLatexConverter {

Expand All @@ -13,6 +12,7 @@ class StyledTextHtmlToLatexConverter : HtmlToLatexConverter {
*/
val openingTags = hashMapOf(
"b" to "\\textbf{",
"body" to "",
"br" to "\n",
"em" to "\\textit{",
"h1" to "\\chapter*{",
Expand All @@ -33,6 +33,7 @@ class StyledTextHtmlToLatexConverter : HtmlToLatexConverter {
val closingTags = hashMapOf(
"a" to "}",
"b" to "}",
"body" to "",
"br" to "",
"div" to "\n",
"em" to "}",
Expand All @@ -54,10 +55,6 @@ class StyledTextHtmlToLatexConverter : HtmlToLatexConverter {

override fun convertHtmlToLatex(htmlIn: Element, file: LatexFile): String {
var latexString = ""
val environ = getCentering(htmlIn)
if (environ != "") {
latexString += "\\begin{$environ}"
}

val prefix = getPrefix(htmlIn)

Expand All @@ -75,22 +72,9 @@ class StyledTextHtmlToLatexConverter : HtmlToLatexConverter {
else {
prefix + content + postfix
}

if (environ != "") {
latexString += "\\end{$environ}"
}
return latexString
}

private fun getCentering(node: Node) = when {
node.attr("align") == "center" -> "center"
node.attr("align") == "left" -> "flushleft"
node.attr("align") == "right" -> "flushright"
// latex doesnt have native support here
// node.attr("align") == "justify" ->
else -> ""
}

private fun getPrefix(element: Element): String {
return specialOpeningTags[element.tagName()]?.invoke(element) ?: openingTags[element.tagName()] ?: ""
}
Expand Down
41 changes: 41 additions & 0 deletions test/nl/hannahsten/texifyidea/editor/HtmlPasteProviderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,47 @@ import org.jsoup.Jsoup

class HtmlPasteProviderTest : BasePlatformTestCase() {

// Uses UI elements so cannot be run
// fun testTable() {
// myFixture.configureByText("main.tex", "")
// val html = """
// <body>
// <table cellspacing="0" border="0">
// <colgroup span="2" width="85"></colgroup>
// <tr>
// <td height="17" align="left" valign=bottom>Column 1</td>
// <td align="left" valign=bottom>Column 2</td>
// </tr>
// <tr>
// <td height="17" align="left" valign=bottom>ABBR</td>
// <td align="right" valign=bottom sdval="1.27" sdnum="1043;">1</td>
// </tr>
// <tr>
// <td height="17" align="left" valign=bottom>DPTP</td>
// <td align="right" valign=bottom sdval="1,18" sdnum="1043;">1,18</td>
// </tr>
// </table>
// </body>
// """.trimIndent()
// val node = Jsoup.parse(html).select("body")[0]
// val latex = HtmlPasteProvider().convertHtmlToLatex(node, myFixture.file as LatexFile)
// TestCase.assertEquals("""
// \begin{table}
// \centering
// \begin{tabular}{ll}
// \toprule
// \textbf{Column 1} & \textbf{Column 2 cost} \\
// \midrule
// ABBR & 1.27 \\
// DPTP & 1,18 \\
// \bottomrule
// \end{tabular}
// \caption{}
// \label{tab:}
// \end{table}
// """.trimIndent(), latex)
// }

fun testItalic() {
myFixture.configureByText("main.tex", "")
val html = "<i>italic</i>"
Expand Down

0 comments on commit 736ca09

Please sign in to comment.