Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jun 3, 2024
1 parent a5ab4f0 commit 4d9175e
Showing 1 changed file with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,48 @@ class Examples extends munit.FunSuite {
lazy val inputDir = outputDir / "input"

private val nl = System.lineSeparator()
private lazy val escapedNl =
if (nl == "\n") "\\n"
else if (nl == "\r\n") "\\r\\n"
else ???
private val shouldUpdateLineSep = System.lineSeparator() == "\r\n"

private def traverseAndUpdateLineSep(content: ujson.Value): Option[ujson.Value] =
private def traverseAndUpdateLineSep(content: ujson.Value, deep: Boolean = false): Option[ujson.Value] =
content.arrOpt match {
case Some(arr) =>
val updates = arr.map(traverseAndUpdateLineSep(_))
for ((elem, idx) <- arr.zipWithIndex)
for (updatedElem <- traverseAndUpdateLineSep(elem))
for (updatedElem <- traverseAndUpdateLineSep(elem, deep = deep))
content(idx) = updatedElem
None
case None =>
content.objOpt match {
case Some(obj) =>
for ((k, v) <- obj)
for (updatedElem <- traverseAndUpdateLineSep(v))
for (updatedElem <- traverseAndUpdateLineSep(v, deep = deep))
content(k) = updatedElem
None
case None =>
content.strOpt.map { str =>
str
.linesWithSeparators
.flatMap { line =>
if (line.endsWith("\n") && !line.endsWith(nl))
Iterator(line.stripSuffix("\n"), nl)
else
Iterator(line)
}
.mkString
if (deep)
str
.linesWithSeparators
.map { line =>
if (deep)
line.replace("\\n", escapedNl)
else
line
}
.mkString
else
str
.linesWithSeparators
.flatMap { line =>
if (line.endsWith("\n") && !line.endsWith(nl))
Iterator(line.stripSuffix("\n"), nl)
else
Iterator(line)
}
.mkString
}
}
}
Expand All @@ -90,6 +104,8 @@ class Examples extends munit.FunSuite {
k == "application/vnd.plotly.v1+json"
if (shouldUpdate)
traverseAndUpdateLineSep(v)
if ((k == "text/html" && v.arr.exists(_.str.contains("function(Plotly)"))))
traverseAndUpdateLineSep(v, deep = true)
}
if (cell.obj.contains("source"))
traverseAndUpdateLineSep(cell("source"))
Expand Down

0 comments on commit 4d9175e

Please sign in to comment.