JS backend: format builder.kt according to code style.

(cherry picked from commit 7c2a2cd)
This commit is contained in:
develar
2013-08-08 19:51:49 +04:00
committed by Zalim Bashorov
parent 222c05dd00
commit 33d576c31f
@@ -9,117 +9,117 @@
*/ */
import java.util.* import java.util.*
fun main(args : Array<String>) { fun main(args: Array<String>) {
val result = val result =
html { html {
head { head {
title {+"XML encoding with Kotlin"} title { +"XML encoding with Kotlin" }
} }
body { body {
h1 {+"XML encoding with Kotlin"} h1 { +"XML encoding with Kotlin" }
p {+"this format can be used as an alternative markup to XML"} p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content // an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content // mixed content
p { p {
+"This is some" +"This is some"
b {+"mixed"} b { +"mixed" }
+"text. For more see the" +"text. For more see the"
a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project" +"project"
} }
p {+"some text"} p { +"some text" }
// content generated from command-line arguments // content generated from command-line arguments
p { p {
+"Command line arguments were:" +"Command line arguments were:"
ul { ul {
for (arg in args) for (arg in args)
li {+arg} li { +arg }
} }
} }
} }
} }
println(result) println(result)
} }
trait Element { trait Element {
fun render(builder : StringBuilder, indent : String) fun render(builder: StringBuilder, indent: String)
fun toString() : String? { fun toString(): String? {
val builder = StringBuilder() val builder = StringBuilder()
render(builder, "") render(builder, "")
return builder.toString() return builder.toString()
} }
} }
class TextElement(val text : String) : Element { class TextElement(val text: String) : Element {
override fun render(builder : StringBuilder, indent : String) { override fun render(builder: StringBuilder, indent: String) {
builder.append("$indent$text\n") builder.append("$indent$text\n")
} }
} }
abstract class Tag(val name : String) : Element { abstract class Tag(val name: String) : Element {
val children = ArrayList<Element>() val children = ArrayList<Element>()
val attributes = HashMap<String, String>() val attributes = HashMap<String, String>()
protected fun initTag<T : Element>(tag : T, init : T.() -> Unit) : T { protected fun initTag<T : Element>(tag: T, init: T.() -> Unit): T {
tag.init() tag.init()
children.add(tag) children.add(tag)
return tag return tag
} }
override fun render(builder : StringBuilder, indent : String) { override fun render(builder: StringBuilder, indent: String) {
builder.append("$indent<$name${renderAttributes()}>\n") builder.append("$indent<$name${renderAttributes()}>\n")
for (c in children) { for (c in children) {
c?.render(builder, indent + " ") c.render(builder, indent + " ")
} }
builder.append("$indent</$name>\n") builder.append("$indent</$name>\n")
} }
private fun renderAttributes() : String? { private fun renderAttributes(): String? {
val builder = StringBuilder() val builder = StringBuilder()
for (a in attributes.keySet()) { for (a in attributes.keySet()) {
builder.append(" $a=\"${attributes[a]}\"") builder.append(" $a=\"${attributes[a]}\"")
} }
return builder.toString() return builder.toString()
} }
} }
abstract class TagWithText(name : String) : Tag(name) { abstract class TagWithText(name: String) : Tag(name) {
fun String.plus() { fun String.plus() {
children.add(TextElement(this)) children.add(TextElement(this))
} }
} }
class HTML() : TagWithText("html") { class HTML() : TagWithText("html") {
fun head(init : Head.() -> Unit) = initTag(Head(), init) fun head(init: Head.() -> Unit) = initTag(Head(), init)
fun body(init : Body.() -> Unit) = initTag(Body(), init) fun body(init: Body.() -> Unit) = initTag(Body(), init)
} }
class Head() : TagWithText("head") { class Head() : TagWithText("head") {
fun title(init : Title.() -> Unit) = initTag(Title(), init) fun title(init: Title.() -> Unit) = initTag(Title(), init)
} }
class Title() : TagWithText("title") class Title() : TagWithText("title")
abstract class BodyTag(name : String) : TagWithText(name) { abstract class BodyTag(name: String) : TagWithText(name) {
fun b(init : B.() -> Unit) = initTag(B(), init) fun b(init: B.() -> Unit) = initTag(B(), init)
fun p(init : P.() -> Unit) = initTag(P(), init) fun p(init: P.() -> Unit) = initTag(P(), init)
fun h1(init : H1.() -> Unit) = initTag(H1(), init) fun h1(init: H1.() -> Unit) = initTag(H1(), init)
fun ul(init : UL.() -> Unit) = initTag(UL(), init) fun ul(init: UL.() -> Unit) = initTag(UL(), init)
fun a(href : String, init : A.() -> Unit) { fun a(href: String, init: A.() -> Unit) {
val a = initTag(A(), init) val a = initTag(A(), init)
a.href = href a.href = href
} }
} }
class Body() : BodyTag("body") class Body() : BodyTag("body")
class UL() : BodyTag("ul") { class UL() : BodyTag("ul") {
fun li(init : LI.() -> Unit) = initTag(LI(), init) fun li(init: LI.() -> Unit) = initTag(LI(), init)
} }
class B() : BodyTag("b") class B() : BodyTag("b")
@@ -127,11 +127,11 @@ class LI() : BodyTag("li")
class P() : BodyTag("p") class P() : BodyTag("p")
class H1() : BodyTag("h1") class H1() : BodyTag("h1")
class A() : BodyTag("a") { class A() : BodyTag("a") {
public var href : String public var href: String
get() = attributes["href"]!! get() = attributes["href"]!!
set(value) { set(value) {
attributes["href"] = value attributes["href"] = value
} }
} }
fun html(init: HTML.() -> Unit): HTML { fun html(init: HTML.() -> Unit): HTML {