Skip to content

Commit

Permalink
Fix #9.
Browse files Browse the repository at this point in the history
Preserve arguments for unknown commands
  • Loading branch information
tobiasdiez authored and tomtung committed Jun 3, 2019
1 parent 72e2f71 commit 3ad274a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object LaTeX2Unicode {
private val commandBlockInOption = P(name.flatMap(s => {
// Ignoring styles in command option is just for simplicity
if (helper.Style.names.contains(s)) PassWithEmptyString
else nameToParser.getOrElse(s, PassWith(s))
else nameToParser.getOrElse(s, unknownCommand(s))
}))
private val blockInOption: Parser[String] = P(literalCharsBlockInOption | bracketBlock | commandBlockInOption)

Expand Down Expand Up @@ -95,7 +95,21 @@ object LaTeX2Unicode {
builder.result()
}

val block: Parser[String] = name.flatMap(s => nameToParser.getOrElse(s, PassWith(s)))
val block: Parser[String] = name.flatMap(s => nameToParser.getOrElse(s, unknownCommand(s)))

private def unknownCommand(command: String): Parser[String] = {
if (!command.startsWith("\\")) {
// Is not a command in the strong sense, so just return
return PassWith(command)
}

val parserNoParam = PassWith(command)
val parserUnary = P(param).map(p => command + p)
val parserBinary = P(param ~ param).map({ case (p1, p2) => command + "{" + p1 + "}" + "{" + p2 + "}"})
val parserTernary = P(param ~ param ~ param).map({ case (p1, p2, p3) => command + "{" + p1 + "}" + "{" + p2 + "}" + "{" + p3 + "}"})

P(parserTernary | parserBinary | parserUnary | parserNoParam)
}
}

private val block: Parser[String] = P(spacesBlock | literalCharsBlock | bracketBlock | command.block)
Expand Down Expand Up @@ -130,4 +144,4 @@ object LaTeX2Unicode {
// If anything bad happens, just return the original string
case e: Throwable => latex
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class LaTeX2UnicodeTest extends FunSuite {
test("\\sqrt") {
LaTeX2Unicode.convert("\\sqrt{}") shouldBe ""
LaTeX2Unicode.convert("\\sqrt x") shouldBe "√x̅"
LaTeX2Unicode.convert("\\sqrt \\alpha") shouldBe "√α̅"
LaTeX2Unicode.convert("\\sqrt\nx") shouldBe "√x̅"
LaTeX2Unicode.convert("\\sqrt[]x") shouldBe "√x̅"
LaTeX2Unicode.convert("\\sqrt[]\nx") shouldBe "√x̅"
Expand Down Expand Up @@ -164,7 +165,11 @@ class LaTeX2UnicodeTest extends FunSuite {
}

test("Unknown commands") {
LaTeX2Unicode.convert("""\this \is \alpha test""") shouldBe """\this \is α test"""
LaTeX2Unicode.convert("\\this \\is \\alpha test") shouldBe "\\this \\is α test"
LaTeX2Unicode.convert("\\unknown command") shouldBe "\\unknown command"
LaTeX2Unicode.convert("\\unknown{} empty params") shouldBe "\\unknown{} empty params"
LaTeX2Unicode.convert("\\unknown{cmd}") shouldBe "\\unknown{cmd}"
LaTeX2Unicode.convert("\\unknown{1}{2}") shouldBe "\\unknown{1}{2}"
LaTeX2Unicode.convert("\\unknown{1}{2}{3}") shouldBe "\\unknown{1}{2}{3}"
}

}

0 comments on commit 3ad274a

Please sign in to comment.