summaryrefslogtreecommitdiff
path: root/scalatex/api/src/main/scala/scalatex/stages/Trim.scala
blob: 89937343ffb86365970ba7f2b655740ba3752c17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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")
  }
}