summaryrefslogtreecommitdiff
path: root/scalatexApi/src/main/scala/scalatex/stages/Trim.scala
diff options
context:
space:
mode:
Diffstat (limited to 'scalatexApi/src/main/scala/scalatex/stages/Trim.scala')
-rw-r--r--scalatexApi/src/main/scala/scalatex/stages/Trim.scala23
1 files changed, 23 insertions, 0 deletions
diff --git a/scalatexApi/src/main/scala/scalatex/stages/Trim.scala b/scalatexApi/src/main/scala/scalatex/stages/Trim.scala
new file mode 100644
index 0000000..02d9cc4
--- /dev/null
+++ b/scalatexApi/src/main/scala/scalatex/stages/Trim.scala
@@ -0,0 +1,23 @@
+package scalatex.stages
+
+/**
+ * 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){
+ def apply(str: String) = {
+ val lines = str.split("\n")
+ val offset = lines.iterator
+ .filter(_.length > 0)
+ .next()
+ .takeWhile(_ == ' ')
+ .length
+ lines.iterator
+ .map(_.drop(offset).replaceFirst("\\s+$", ""))
+ .mkString("\n")
+ }
+}