diff options
Diffstat (limited to 'scalatex/api/src/main/scala/scalatex/stages/Trim.scala')
-rw-r--r-- | scalatex/api/src/main/scala/scalatex/stages/Trim.scala | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/scalatex/api/src/main/scala/scalatex/stages/Trim.scala b/scalatex/api/src/main/scala/scalatex/stages/Trim.scala new file mode 100644 index 0000000..8993734 --- /dev/null +++ b/scalatex/api/src/main/scala/scalatex/stages/Trim.scala @@ -0,0 +1,29 @@ +package scalatex.stages +import acyclic.file + +/** + * Preprocesses the input string to normalize things related to whitespace + * + * Find the "first" non-whitespace-line of the text and remove the front + * of every line to align that first line with the left margin. + * + * Remove all trailing whitespace from each line. + */ +object Trim extends (String => (String, Int)){ + def apply(str: String) = { + val lines = str.split("\n", -1) + val offset = lines.iterator + .filter(_.length > 0) + .next() + .takeWhile(_ == ' ') + .length + val res = lines.iterator + .map(_.replaceFirst("\\s+$", "")) + .mkString("\n") + (res, offset) + } + def old(str: String) = { + val (res, offset) = this.apply(str) + res.split("\n", -1).map(_.drop(offset)).mkString("\n") + } +} |