Skip to content

Commit 5b8771f

Browse files
committed
move files
1 parent d4614dd commit 5b8771f

23 files changed

+320
-206
lines changed

converter/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ version = "1.0-SNAPSHOT"
1010

1111
repositories {
1212
mavenCentral()
13+
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/compose/dev") }
14+
1315
}
1416

1517
dependencies {
1618
testImplementation(kotlin("test"))
1719
implementation("org.jsoup:jsoup:1.14.3")
20+
// implementation("org.jetbrains.compose.web:web-core:1.0.0")
1821
}
1922

2023
tasks.test {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.jensklingenberg.htmltocfw.converter
2+
3+
import org.jsoup.nodes.Attribute
4+
5+
6+
fun getAttributesText(attributesList: List<Attribute>): String {
7+
8+
var str = ""
9+
attributesList.forEachIndexed { index, attribute ->
10+
if (index == 0) {
11+
str += "attrs = {\n"
12+
}
13+
str += when (attribute.key) {
14+
"class" -> {
15+
val classes = attribute.value.split(" ").joinToString { "\"$it\"" }
16+
"classes($classes)"
17+
}
18+
"draggable" -> {
19+
"draggable(Draggable.${attribute.value.capitalize()})"
20+
}
21+
"style" -> {
22+
getStyleText(attribute)
23+
}
24+
"required", "hidden", "selected", "disabled" -> {
25+
attribute.key + "()"
26+
}
27+
"readonly" -> {
28+
"readOnly()"
29+
}
30+
31+
"id", "name", "rowspan", "colspan" -> {
32+
val attrValue = if (attribute.value.isNumber) {
33+
attribute.value
34+
} else {
35+
formatStringValue(attribute.value)
36+
}
37+
38+
"${attribute.key}($attrValue)"
39+
}
40+
41+
else -> {
42+
"attr(\"${attribute.key}\",\"${attribute.value}\")"
43+
}
44+
}
45+
str += "\n"
46+
if (index == attributesList.lastIndex) {
47+
str += "}"
48+
}
49+
}
50+
return str
51+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package de.jensklingenberg.htmltocfw.converter
2+
3+
enum class CFWAttributeNames(val htmlAttrName: String, val cfwAttrName: String) {
4+
BACKGROUNDIMAGE("background-image", "backgroundImage"),
5+
BORDERRADIUS("border-radius", "borderRadius"),
6+
FONT("font", "font"),
7+
MARGIN("margin", "margin"),
8+
PADDING("padding", "padding"),
9+
FONTSIZE("font-size", "fontSize"),
10+
TEXTALIGN("text-align", "textAlign"),
11+
TEXTDECORATION("text-decoration", "textDecoration"),
12+
FONTFAMILY("font-family", "fontFamily");
13+
}
Lines changed: 13 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,181 +1,37 @@
11
package de.jensklingenberg.htmltocfw.converter
22

3+
import de.jensklingenberg.htmltocfw.converter.node.getMyNode
34
import org.jsoup.Jsoup
4-
import org.jsoup.nodes.Attribute
5-
import org.jsoup.nodes.Comment
6-
import org.jsoup.nodes.Element
7-
import org.jsoup.nodes.Node
8-
import org.jsoup.nodes.TextNode
95
import java.io.File
106

117
fun main() {
128

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

1512
val text = htmlToCompose(html)
1613

17-
File("/home/jens/Code/2021/jk/HtmlToComposeWeb/converter/src/main/kotlin/de/jensklingenberg/htmltocfw/converter/Result.txt").writeText(text)
14+
File("/home/jens/Code/2021/jk/HtmlToComposeWeb/converter/src/main/kotlin/de/jensklingenberg/htmltocfw/converter/Result.txt").writeText(
15+
text
16+
)
1817
}
1918

20-
fun htmlToCompose(html:String): String {
19+
fun htmlToCompose(html: String): String {
2120
val doc = Jsoup.parse(html);
2221

23-
val text = doc.body().childNodes().joinToString(separator = "") {
22+
return doc.body().childNodes().joinToString(separator = "") {
2423
getMyNode(it).print()
2524
}
26-
27-
return text
28-
}
29-
30-
interface MyNode {
31-
fun print(): String
3225
}
3326

34-
fun getStyleText(attribute: Attribute): String {
35-
var str = "style {\n"
36-
37-
//TODO Number oder StrinG?
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-
}
6327

64-
str += "\n"
28+
val String.isNumber: Boolean
29+
get() {
30+
return this.toIntOrNull() != null
6531
}
66-
return "$str}"
67-
}
68-
69-
70-
fun printAttributes(attributesList: List<Attribute>): String {
71-
var str = ""
72-
attributesList.forEachIndexed { index, attribute ->
73-
if (index == 0) {
74-
str += "attrs = {\n"
75-
}
76-
str += when (attribute.key) {
77-
"class" -> {
78-
val classes = attribute.value.split(" ").joinToString { "\"$it\"" }
79-
"classes($classes)"
80-
}
81-
"draggable"->{
82-
"draggable(Draggable.${attribute.value.capitalize()})"
83-
}
84-
"style" -> {
85-
getStyleText(attribute)
86-
}
87-
"required", "hidden", "selected", "disabled", -> {
88-
attribute.key + "()"
89-
}
90-
"readonly"-> {
91-
"readOnly()"
92-
}
93-
"onclick343"->{
94-
"onClick { js(\"${attribute.value}\") }"
95-
}
96-
"onblur"->{
97-
"onBlur { js(\"${attribute.value}\") }"
98-
}
99-
"ondblclick"->{
100-
"onDoubleClick { js(\"${attribute.value}\") }"
101-
}
10232

103-
"id" -> {
104-
"id(\"${attribute.value}\")"
105-
}
106-
107-
else -> {
108-
"attr(\"${attribute.key}\",\"${attribute.value}\")"
109-
}
110-
}
111-
str += "\n"
112-
if (index == attributesList.lastIndex) {
113-
str += "}"
114-
}
115-
}
116-
return str
33+
fun formatStringValue(text: String): String {
34+
return "\"${text}\""
11735
}
11836

119-
class UnsupportedNode : MyNode{
120-
override fun print(): String {
121-
return ""
122-
}
123-
124-
}
125-
126-
fun getMyNode(node: Node): MyNode {
127-
return when (node) {
128-
is TextNode -> {
129-
MyTextNode(node)
130-
}
131-
is Comment -> {
132-
MyComment(node)
133-
}
134-
is Element -> {
135-
when (node.tagName()) {
136-
"a"->{
137-
ANode(node)
138-
}
139-
"form"->{
140-
FormNode(node)
141-
}
142-
"img" -> {
143-
ImgNode(node)
144-
}
145-
"input" -> {
146-
InputNode(node)
147-
}
148-
"label" -> {
149-
LabelNode(node)
150-
}
151-
"option" -> {
152-
OptionNode(node)
153-
}
154-
"optgroup"->{
155-
OptGroupNode(node)
156-
}
157-
"path" -> {
158-
PathNode(node)
159-
}
160-
"textarea" -> {
161-
TextAreaNode(node)
162-
}
163-
"script"->{
164-
return UnsupportedNode()
165-
}
166-
167-
else -> {
168-
GenericNode(node)
169-
}
170-
}
171-
}
172-
else -> {
173-
println("Not found: ${node.nodeName()}")
174-
return UnsupportedNode()
175-
}
176-
177-
}
178-
179-
180-
}
18137

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
H1 (attrs = {
2-
style {
3-
fontFamily("verdana")
1+
H1 {
2+
Text("The input element")
3+
4+
}
5+
Form (action = "/action_page.php"){
6+
Label (forId = "fname"){
7+
Text("First name:")
8+
49
}
5-
}){
6-
Text("This is a heading")
10+
Input (attrs = {
11+
id("fname")
12+
name("fname")
13+
}, type = InputType.Text)
14+
Br {}
15+
Br {}
16+
Label (forId = "lname"){
17+
Text("Last name:")
718

819
}
9-
P (attrs = {
10-
style {
11-
fontFamily("courier")
20+
Input (attrs = {
21+
id("lname")
22+
name("lname")
23+
}, type = InputType.Text)
24+
Br {}
25+
Br {}
26+
Input (attrs = {
27+
attr("value","Submit")
28+
}, type = InputType.Submit)
29+
1230
}
13-
}){
14-
Text("This is a paragraph.")
31+
P {
32+
Text("Click the \"Submit\" button and the form-data will be sent to a page on the server called \"action_page.php\".")
1533

1634
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package de.jensklingenberg.htmltocfw.converter
2+
3+
import org.jsoup.nodes.Attribute
4+
5+
6+
fun getStyleText(attribute: Attribute): String {
7+
var str = "style {\n"
8+
9+
//Find better way to parse style
10+
val styles = attribute.value.split(";")
11+
12+
styles.filter { it.isNotEmpty() }.map { it.trimStart() }.forEach {
13+
val propName = it.substringBefore(":").trimStart()
14+
val propValue = it.substringAfter(":").trimStart().replace("\"", "\\\"")
15+
16+
str += when (val test = CFWAttributeNames.values().find { it.htmlAttrName == propName }) {
17+
18+
CFWAttributeNames.MARGIN, CFWAttributeNames.PADDING, CFWAttributeNames.FONTSIZE, CFWAttributeNames.BORDERRADIUS -> {
19+
val unitType = unitsMap.entries.find { propValue.endsWith(it.value) }
20+
21+
if (unitType == null) {
22+
"property(\"$propName\",\"${propValue}\")"
23+
} else {
24+
val newValue = propValue.replace(unitType.value, "") + ".${unitType.key}"
25+
"${test.cfwAttrName}(${newValue})"
26+
}
27+
}
28+
29+
CFWAttributeNames.TEXTDECORATION, CFWAttributeNames.TEXTALIGN, CFWAttributeNames.BACKGROUNDIMAGE, CFWAttributeNames.FONTFAMILY, CFWAttributeNames.FONT -> {
30+
"${test.cfwAttrName}(\"${propValue}\")"
31+
}
32+
else -> {
33+
"property(\"$propName\",\"${propValue}\")"
34+
}
35+
}
36+
37+
str += "\n"
38+
}
39+
return "$str}"
40+
}

0 commit comments

Comments
 (0)