Skip to content

daggerok/kotlin-html-dsl

Repository files navigation

kotlin-html-dsl

description

Simplest and small Kotlin HTML DSL library ever. Supports HTML5 standard (and some obsolete HTML4 tags according to that page)

installation

3 artifact versions are available:

  • 1.0.PLUS - Stateless PLUS API: operator + allow concat two or more siblings in same parent

  • 1.0.DOM - Stateful DOM API: function text and property innerHTML

  • 1.0.ALL - Both APIs

kotlin-html-dsl

build.gradle
repositories {
  jcenter()      // available in bintray jcenter
  // or:
  mavenCentral() // also available in maven central
}

dependencies {
  compile "com.github.daggerok:kotlin-html-dsl:1.0.ALL"
}

kotlin-html-dsl

pom.xml
<dependencies>
  <dependency>
    <groupId>com.github.daggerok</groupId>
    <artifactId>kotlin-html-dsl</artifactId>
    <version>1.0.ALL</version>
  </dependency>
</dependencies>

<!-- only if you want use bintray jcetner -->
<repositories>
  <repository>
    <id>jcentral</id>
    <url>https://jcenter.bintray.com</url>
  </repository>
</repositories>

usage

text function

1) Stateful DOM API usage (text function usage) — add text content to innerHTML by using text function:
import daggerok.extensions.html.dom.*

fun main(args: Array<String>) {
  println(
    html("lang" to "en") {
      head {
        meta("charset" to "UTF-8")
        title {
          text("Kotlin HTML DSL")
        }
      }
      body {
        h1 {
          text("Hello, World!")
        }
      }
    }
  )
}

innerHTML property

2) Stateful DOM API usage (innerHTML) — using innerHTML directly:
import daggerok.extensions.html.dom.*

fun main(args: Array<String>) {
  println(
    html("lang" to "en") {
      head {
        meta("charset" to "UTF-8")
        title {
          innerHTML += "Kotlin HTML DSL"
        }
      }
      body {
        h1 {
          innerHTML += "Hello, World!"
        }
      }
    }
  )
}

plus API

3) Stateles PLUS API usage — two or more siblings in the same parent could be concatinated by plus + operator:
import daggerok.extensions.html.plus.*

fun main(args: Array<String>) {
  println(
    html("lang" to "en") {
      head {
        meta("charset" to "UTF-8")+
        title {
          "Kotlin HTML DSL"
        }
      }+
      body {
        h1 {
          "Hello, World!"
        }
      }
    }
  )
}
all 3 examples will produce same HTML:
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset='UTF-8'/>
  <title>Kotlin HTML DSL</title>
</head>
<body>
  <h1>Hello, World!</h1>
</body>
</html>

tags support

HTML5 tags

The following tags are supported in HTML5 (and/or the WHATWG HTML Living Standard):

a abbr address area article aside audio b base bdi bdo blockquote body br button canvas caption cite code col colgroup data datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd keygen label legend li link main map mark menu menuitem meta meter nav noscript object ol optgroup option output p param pre progress q rb rp rt rtc ruby s samp script section select small source span strong style sub summary sup table tbody td template textarea tfoot th thead time title tr track u ul var video wbr

HTML4 / obsolete tags

The following tags are supported in HTML 4 but not HTML5. Therefore you should not use these if you need to be HTML5 compliant:

acronym applet basefont big center dir font frame frameset isindex

other installation variants

bintray user repository

gradle

build.gradle
repositories {
  maven { url "https://dl.bintray.com/daggerok/daggerok" }
}

dependencies {
  compile "com.github.daggerok:kotlin-html-dsl:1.0.ALL"
}

maven

pom.xml
<repositories>
  <repository>
    <id>bintray-daggerok-daggerok</id>
    <url>https://dl.bintray.com/daggerok/daggerok</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>com.github.daggerok</groupId>
    <artifactId>kotlin-html-dsl</artifactId>
    <version>1.0.ALL</version>
  </dependency>
</dependencies>

jitpack repository

gradle

build.gradle
repositories {
  maven { url "https://jitpack.io" }
}

dependencies {
  compile "com.github.daggerok:kotlin-html-dsl:1.0.ALL"
}

maven

pom.xml
<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>com.github.daggerok</groupId>
    <artifactId>kotlin-html-dsl</artifactId>
    <version>1.0.ALL</version>
  </dependency>
</dependencies>

contribution

Feel free extend and contribute to add more functionality. Personally I’d like to keep it simple as possible, but my Kotlin knowledge not yet good enough. So if you can improve it or make it more DSL-ish — please, create PR

build
# maven:
./mvnw

# or gradle:
./gradlew

generated by jvm yeoman generator

links: