Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented pretty-printing for the text back-end. #127

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 60 additions & 21 deletions scalatags/shared/src/main/scala/scalatags/Text.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,23 @@ object Text
Objects.requireNonNull(v)
def render = {
val strb = new StringBuilder()
writeTo(strb)
writeTo(strb, 0, 0, false)
strb.toString()
}
def writeTo(strb: StringBuilder) = Escaping.escape(v, strb)
def writeTo(strb: StringBuilder, indentBy: Int, depth: Int, fromTag: Boolean) = {
Escaping.escape(v, strb)
false
}
}
object StringFrag extends Companion[StringFrag]
object RawFrag extends Companion[RawFrag]
case class RawFrag(v: String) extends text.Frag {
Objects.requireNonNull(v)
def render = v
def writeTo(strb: StringBuilder) = strb ++= v
def writeTo(strb: StringBuilder, indentBy: Int, depth: Int, fromTag: Boolean) = {
strb ++= v
false
}
}

class GenericAttr[T] extends AttrValue[T]{
Expand Down Expand Up @@ -157,38 +163,63 @@ object Text
* because I've inlined a whole lot of things to improve the performance of this code
* ~4x from what it originally was, which is a pretty nice speedup
*/
def writeTo(strb: StringBuilder): Unit = {
override def writeTo(strb: StringBuilder, indentBy: Int, depth: Int, fromTag: Boolean) = {
val builder = new text.Builder()
build(builder)

val indenting = indentBy > 0
val indent: String = if(indenting) " " * (indentBy * depth) else null

// tag
if(indenting && fromTag) {
strb += '\n'
strb ++= indent
}
strb += '<' ++= tag

// attributes
var i = 0
while (i < builder.attrIndex){
val pair = builder.attrs(i)
strb += ' ' ++= pair._1 ++= "=\""
Escaping.escape(pair._2, strb)
strb += '\"'
i += 1
{
var i = 0
while (i < builder.attrIndex){
val pair = builder.attrs(i)
strb += ' ' ++= pair._1 ++= "=\""
Escaping.escape(pair._2, strb)
strb += '\"'
i += 1
}
}

if (builder.childIndex == 0 && void) {
// No children - close tag
strb ++= " />"
if (builder.childIndex == 0) {
if (void) {
// No children - self-closing tag
strb ++= " />"
} else {
// no children - close tag
strb ++= "></" ++= tag += '>'
}
} else {
strb += '>'
var ft = true

// Childrens
var i = 0
while(i < builder.childIndex){
builder.children(i).writeTo(strb)
i += 1
{
val d = depth + 1
var i = 0
while (i < builder.childIndex) {
ft = builder.children(i).writeTo(strb, indentBy, d, ft)
i += 1
}
}

// Closing tag
if(indenting && ft) {
strb += '\n'
strb ++= indent
}
strb ++= "</" ++= tag += '>'
}

true
}

def apply(xs: Modifier*): TypedTag[Output] = {
Expand All @@ -198,12 +229,20 @@ object Text
/**
* Converts an ScalaTag fragment into an html string
*/
override def toString = {
final def toString(indentBy: Int) = {
val strb = new StringBuilder
writeTo(strb)
writeTo(strb, indentBy, 0, false)
strb.toString()
}
def render: Output = this.toString.asInstanceOf[Output]

/**
* Converts an ScalaTag fragment into an html string
*/
override final def toString = toString(0)

def render(indentBy: Int): Output = this.toString(indentBy).asInstanceOf[Output]

def render: Output = render(0)
}


Expand Down
14 changes: 11 additions & 3 deletions scalatags/shared/src/main/scala/scalatags/text/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ class Builder(var children: Array[Frag] = new Array(4),
}
}
trait Frag extends generic.Frag[Builder, String]{
def writeTo(strb: StringBuilder): Unit
def render: String
def applyTo(b: Builder) = b.addChild(this)
/**
* Write into a string buffer.
*
* @param strb buffer to write into
* @param indentBy the amount to indent per unit depth
* @param depth depth of this node
* @param fromTag true if the previous node to be written to strb is a tag
* @return true if the last thing to be written to strb by this method was a tag
*/
def writeTo(strb: StringBuilder, indentBy: Int, depth: Int, fromTag: Boolean): Boolean
override def applyTo(b: Builder) = b.addChild(this)
}
72 changes: 72 additions & 0 deletions scalatags/shared/src/test/scala/scalatags/text/TextTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,77 @@ object TextTests extends TestSuite{
val sample = div("omg")
assert(sample.toString == "<div>omg</div>")
}

'helloWorld_indent{
val sample = div("omg").render(2)
val expected = "<div>omg</div>"
assert(sample == expected)
}

'nested_indent{
val sample = div(span("omg")).render(2)
val expected =
"""<div>
| <span>omg</span>
|</div>""".stripMargin
assert(sample == expected)
}

'voidTag_indent{
val sample = "void".voidTag[String].render(2)
val expected = "<void />"
assert(sample == expected)
}

'tag_indent{
val sample = "tag".tag[String].render(2)
val expected = "<tag></tag>"
assert(sample == expected)
}

'aList_indent{
val sample = ol(
`class` := "myList",
li("one"),
li(float.right),
li(
span(3)
)
).render(2)
val expected =
"""<ol class="myList">
| <li>one</li>
| <li style="float: right;"></li>
| <li>
| <span>3</span>
| </li>
|</ol>""".stripMargin
assert(sample == expected)
}

'multiSeq_indent{
val sample = span("one", "two", "three").render(2)
val expected = "<span>onetwothree</span>"
assert(sample == expected)
}

'mixedContent_indent{
val sample = div("one", span("two"), "three", span(span("four")), "five").render(2)
val expected =
"""<div>one<span>two</span>three<span>
| <span>four</span>
| </span>five</div>""".stripMargin
assert(sample == expected)
}

'whitespaceContent_indent{
val sample = span("one", " and ", "two\n").render(2)
val expected =
"""<span>one and two
|</span>""".stripMargin
assert(sample == expected)
}

/**
* Tests the usage of the pre-defined tags, as well as creating
* the tags on the fly from Strings
Expand Down Expand Up @@ -41,5 +112,6 @@ object TextTests extends TestSuite{
import scalatags.Text.all._
val thing: Tag = div
}

}
}