Debian Codegraph @

examples/prism-kotlin.html

raw symbols SCIP
1 <h2>Numbers</h2>
2 <pre><code>123
3 123L
4 0x0F
5 0b00001011
6 123.5
7 123.5e10
8 123.5f
9 123.5F</code></pre>
10
11 <h2>Strings and interpolation</h2>
12 <pre><code>'2'
13 '\uFF00'
14 '\''
15
16 "foo $bar \"baz"
17 """
18 foo ${40 + 2}
19 baz${bar()}
20 """</code></pre>
21
22 <h2>Labels</h2>
23 <pre><code>loop@ for (i in 1..100) {
24 for (j in 1..100) {
25 if (...)
26 break@loop
27 }
28 }</code></pre>
29
30 <h2>Annotations</h2>
31 <pre><code>public class MyTest {
32 lateinit var subject: TestSubject
33
34 @SetUp fun setup() {
35 subject = TestSubject()
36 }
37
38 @Test fun test() {
39 subject.method() // dereference directly
40 }
41 }</code></pre>
42
43 <h2>Full example</h2>
44 <pre><code>package com.example.html
45
46 interface Element {
47 fun render(builder: StringBuilder, indent: String)
48
49 override fun toString(): String {
50 val builder = StringBuilder()
51 render(builder, "")
52 return builder.toString()
53 }
54 }
55
56 class TextElement(val text: String): Element {
57 override fun render(builder: StringBuilder, indent: String) {
58 builder.append("$indent$text\n")
59 }
60 }
61
62 abstract class Tag(val name: String): Element {
63 val children = arrayListOf&lt;Element>()
64 val attributes = hashMapOf&lt;String, String>()
65
66 protected fun initTag&lt;T: Element>(tag: T, init: T.() -> Unit): T {
67 tag.init()
68 children.add(tag)
69 return tag
70 }
71
72 override fun render(builder: StringBuilder, indent: String) {
73 builder.append("$indent&lt;$name${renderAttributes()}>\n")
74 for (c in children) {
75 c.render(builder, indent + " ")
76 }
77 builder.append("$indent&lt;/$name>\n")
78 }
79
80 private fun renderAttributes(): String? {
81 val builder = StringBuilder()
82 for (a in attributes.keySet()) {
83 builder.append(" $a=\"${attributes[a]}\"")
84 }
85 return builder.toString()
86 }
87 }
88
89 abstract class TagWithText(name: String): Tag(name) {
90 operator fun String.plus() {
91 children.add(TextElement(this))
92 }
93 }
94
95 class HTML(): TagWithText("html") {
96 fun head(init: Head.() -> Unit) = initTag(Head(), init)
97
98 fun body(init: Body.() -> Unit) = initTag(Body(), init)
99 }
100
101 class Head(): TagWithText("head") {
102 fun title(init: Title.() -> Unit) = initTag(Title(), init)
103 }
104
105 class Title(): TagWithText("title")
106
107 abstract class BodyTag(name: String): TagWithText(name) {
108 fun b(init: B.() -> Unit) = initTag(B(), init)
109 fun p(init: P.() -> Unit) = initTag(P(), init)
110 fun h1(init: H1.() -> Unit) = initTag(H1(), init)
111 fun a(href: String, init: A.() -> Unit) {
112 val a = initTag(A(), init)
113 a.href = href
114 }
115 }
116
117 class Body(): BodyTag("body")
118
119 class B(): BodyTag("b")
120 class P(): BodyTag("p")
121 class H1(): BodyTag("h1")
122 class A(): BodyTag("a") {
123 public var href: String
124 get() = attributes["href"]!!
125 set(value) {
126 attributes["href"] = value
127 }
128 }
129
130 fun html(init: HTML.() -> Unit): HTML {
131 val html = HTML()
132 html.init()
133 return html
134 }</code></pre>