Skip to content

Commit d4614dd

Browse files
committed
add properties
1 parent d00c487 commit d4614dd

File tree

7 files changed

+117
-49
lines changed

7 files changed

+117
-49
lines changed

converter/src/main/kotlin/de/jensklingenberg/htmltocfw/converter/ImgNode.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ImgNode(val element: Element) : MyNode {
2222
}
2323
str += ("src = \"${srcValue}\"")
2424
str += (", alt = \"${altValue}\"")
25-
str += (")")
25+
str += (")\n")
2626

2727
return str
2828
}

converter/src/main/kotlin/de/jensklingenberg/htmltocfw/converter/Main.kt

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import java.io.File
1010

1111
fun main() {
1212

13-
val html = File("/home/jens/Code/2021/jk/HtmlToComposeWeb/converter/src/de.jensklingenberg.htmltocfw.converter.main/kotlin/html.text").readText()
13+
val html = File("/home/jens/Code/2021/jk/HtmlToComposeWeb/converter/src/main/kotlin/de/jensklingenberg/htmltocfw/converter/html.text").readText()
1414

1515
val text = htmlToCompose(html)
1616

17-
File("/home/jens/Code/2021/jk/HtmlToComposeWeb/converter/src/de.jensklingenberg.htmltocfw.converter.main/kotlin/Result.txt").writeText(text)
17+
File("/home/jens/Code/2021/jk/HtmlToComposeWeb/converter/src/main/kotlin/de/jensklingenberg/htmltocfw/converter/Result.txt").writeText(text)
1818
}
1919

2020
fun htmlToCompose(html:String): String {
@@ -31,15 +31,37 @@ interface MyNode {
3131
fun print(): String
3232
}
3333

34-
fun printStyle(it: Attribute): String {
35-
var str = "style {"
34+
fun getStyleText(attribute: Attribute): String {
35+
var str = "style {\n"
3636

3737
//TODO Number oder StrinG?
38-
val styles = it.value.split(";")
39-
styles.filter { it.isNotEmpty() }.forEach {
40-
val (propName, propValue) = it.split(":")
41-
val escapedProp = propValue.replace("\"", "\\\"")
42-
str += "property(\"$propName\",\"${escapedProp}\")"
38+
//Find better way to parse style
39+
val styles = attribute.value.split(";")
40+
41+
styles.filter { it.isNotEmpty() }.map { it.trimStart() }.forEach {
42+
val propName = it.substringBefore(":").trimStart()
43+
val propValue = it.substringAfter(":").trimStart().replace("\"", "\\\"")
44+
45+
str += when(propName){
46+
"font"->{
47+
"font(\"${propValue}\")"
48+
}
49+
"font-family"->{
50+
"fontFamily(\"${propValue}\")"
51+
}
52+
"_font-size"->{
53+
var unit = propValue
54+
"fontFamily(\"${propValue}\")"
55+
}
56+
"background-image"->{
57+
"backgroundImage(\"${propValue}\")"
58+
}
59+
else ->{
60+
"property(\"$propName\",\"${propValue}\")"
61+
}
62+
}
63+
64+
str += "\n"
4365
}
4466
return "$str}"
4567
}
@@ -56,15 +78,27 @@ fun printAttributes(attributesList: List<Attribute>): String {
5678
val classes = attribute.value.split(" ").joinToString { "\"$it\"" }
5779
"classes($classes)"
5880
}
81+
"draggable"->{
82+
"draggable(Draggable.${attribute.value.capitalize()})"
83+
}
5984
"style" -> {
60-
printStyle(attribute)
85+
getStyleText(attribute)
6186
}
6287
"required", "hidden", "selected", "disabled", -> {
6388
attribute.key + "()"
6489
}
6590
"readonly"-> {
6691
"readOnly()"
6792
}
93+
"onclick343"->{
94+
"onClick { js(\"${attribute.value}\") }"
95+
}
96+
"onblur"->{
97+
"onBlur { js(\"${attribute.value}\") }"
98+
}
99+
"ondblclick"->{
100+
"onDoubleClick { js(\"${attribute.value}\") }"
101+
}
68102

69103
"id" -> {
70104
"id(\"${attribute.value}\")"
@@ -82,51 +116,62 @@ fun printAttributes(attributesList: List<Attribute>): String {
82116
return str
83117
}
84118

119+
class UnsupportedNode : MyNode{
120+
override fun print(): String {
121+
return ""
122+
}
85123

86-
fun getMyNode(ele: Node): MyNode {
87-
return when (ele) {
124+
}
125+
126+
fun getMyNode(node: Node): MyNode {
127+
return when (node) {
88128
is TextNode -> {
89-
MyTextNode(ele)
129+
MyTextNode(node)
90130
}
91131
is Comment -> {
92-
MyComment(ele)
132+
MyComment(node)
93133
}
94134
is Element -> {
95-
when (ele.tagName()) {
135+
when (node.tagName()) {
96136
"a"->{
97-
ANode(ele)
137+
ANode(node)
98138
}
99139
"form"->{
100-
FormNode(ele)
140+
FormNode(node)
101141
}
102142
"img" -> {
103-
ImgNode(ele)
143+
ImgNode(node)
104144
}
105145
"input" -> {
106-
InputNode(ele)
146+
InputNode(node)
107147
}
108148
"label" -> {
109-
LabelNode(ele)
149+
LabelNode(node)
110150
}
111151
"option" -> {
112-
OptionNode(ele)
152+
OptionNode(node)
113153
}
114154
"optgroup"->{
115-
OptGroupNode(ele)
155+
OptGroupNode(node)
116156
}
117157
"path" -> {
118-
PathNode(ele)
158+
PathNode(node)
119159
}
120160
"textarea" -> {
121-
TextAreaNode(ele)
161+
TextAreaNode(node)
122162
}
163+
"script"->{
164+
return UnsupportedNode()
165+
}
166+
123167
else -> {
124-
GenericNode(ele)
168+
GenericNode(node)
125169
}
126170
}
127171
}
128172
else -> {
129-
throw Exception("$ele Not found")
173+
println("Not found: ${node.nodeName()}")
174+
return UnsupportedNode()
130175
}
131176

132177
}

converter/src/main/kotlin/de/jensklingenberg/htmltocfw/converter/MyTextNode.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class MyTextNode(private val textNode: TextNode) : MyNode {
66
override fun print(): String {
77
return if (textNode.text().isNotBlank()) {
88
val text = textNode.text().replace("\"", "\\\"")
9-
("Text(\"${text}\")")
9+
("Text(\"${text}\")\n")
1010
} else {
1111
""
1212
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
Picture {
2-
Source (attrs = {
3-
attr("media","(min-width:650px)")
4-
attr("srcset","https://www.w3schools.com/tags/img_pink_flowers.jpg")
5-
}){}
6-
Source (attrs = {
7-
attr("media","(min-width:465px)")
8-
attr("srcset","https://www.w3schools.com/tags/img_pink_flowers.jpg")
9-
}){}
10-
Img (attrs = {
11-
style {property("width","auto")}
12-
}, src = "img_orange_flowers.jpg", alt = "Flowers")
1+
H1 (attrs = {
2+
style {
3+
fontFamily("verdana")
4+
}
5+
}){
6+
Text("This is a heading")
7+
8+
}
9+
P (attrs = {
10+
style {
11+
fontFamily("courier")
12+
}
13+
}){
14+
Text("This is a paragraph.")
15+
1316
}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
<picture>
2-
<source media="(min-width:650px)" srcset="https://www.w3schools.com/tags/img_pink_flowers.jpg">
3-
<source media="(min-width:465px)" srcset="https://www.w3schools.com/tags/img_pink_flowers.jpg">
4-
<img src="img_orange_flowers.jpg" alt="Flowers" style="width:auto;">
5-
</picture>
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
5+
<h1 style="font-family:verdana;">This is a heading</h1>
6+
<p style="font-family:courier;">This is a paragraph.</p>
7+
8+
</body>
9+
</html>
10+

converter/src/test/kotlin/myTest.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import de.jensklingenberg.htmltocfw.converter.getMyNode
2+
import de.jensklingenberg.htmltocfw.converter.htmlToCompose
23
import org.jsoup.Jsoup
34
import org.junit.jupiter.api.Test
45
import kotlin.test.assertEquals
@@ -44,17 +45,30 @@ class myTest {
4445

4546
@Test
4647
fun nodeATest(){
47-
val doc = Jsoup.parse(""" <a href="https://www.w3schools.com">Visit W3Schools.com!</a> """);
48+
val html = """ <a href="https://www.w3schools.com">Visit W3Schools.com!</a> """
4849
val exp = """
4950
A (href = "https://www.w3schools.com"){
5051
Text("Visit W3Schools.com!")
5152
}
5253
5354
""".trimIndent()
5455

55-
val text = doc.body().childNodes().joinToString(separator = "") {
56-
getMyNode(it).print()
57-
}
56+
val text = htmlToCompose(html)
57+
58+
assertEquals(exp,text)
59+
}
60+
61+
@Test
62+
fun failedTest(){
63+
val html = """ a href="https://www.w3schools.com<<">Visit W3Schools.com!</a> """
64+
val exp = """
65+
A (href = "https://www.w3schools.com"){
66+
Text("Visit W3Schools.com!")
67+
}
68+
69+
""".trimIndent()
70+
71+
val text = htmlToCompose(html)
5872

5973
assertEquals(exp,text)
6074
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ rootProject.name = "HtmlToComposeWebConverter"
33

44
include(
55
":converter",
6+
67
":ideaplugin"
78

89
)

0 commit comments

Comments
 (0)