Htmlgen

Write browser-friendly output for Scala objects.

View the Project on GitHub sameersingh/htmlgen

htmlgen

Build Status

Write browser-friendly output for various data structures.

Basic Usage

This library contains implicit conversion of any data structure into HTML source, where the format depends on which classes were imported. For example, the following code:

import org.sameersingh.htmlgen.HTML
import org.sameersingh.htmlgen.DivConverter.Implicits._

val html: HTML = Map(List(1,2) -> "string", Set(3,4) -> 1e5)

println(html.source)

produces

<div class="asMap Map"><span class="typeName">Map</span>
<ul class="fields">
  <li class="field"><span class="fieldName"><div class="asIterable List"><span class="typeName">List</span>
    <ol start="0" class="fields">
      <li class="fieldValue"><span class="asString String">1</span></li>
      <li class="fieldValue"><span class="asString String">2</span></li>
    </ol>
</div></span> <span class="fieldValue"><span class="asString String">string</span></span></li>
  <li class="field"><span class="fieldName"><div class="asIterable Set"><span class="typeName">Set</span>
    <ul start="0" class="fields">
      <li class="fieldValue"><span class="asString String">3</span></li>
      <li class="fieldValue"><span class="asString String">4</span></li>
    </ul>
</div></span> <span class="fieldValue"><span class="asString String">100000.0</span></span></li>
</ul>
</div>

These divs and spans can be customized to a great detail using CSS, since class attributes are named using the Scala classes.

You can replace DivConverter with other converters such as TableConverter, ListConverter and so on.

Status

Data structures

Converters

Examples

For more examples, check out the html files in src/main/resources/.

Consider the following Scala snippet:

    case class Company(name: String, address: String)
    case class Person(first: String, last: String, email: String, age: Int, employedBy: Company)

    val c1 = Company("Battle School", "Seattle, WA")
    val c2 = Company("Command School", "Mountain View, CA")
    val c3 = Company("Dept of Xenobiology", "Lusitania")
    val p1 = Person("Andrew", "Wiggin", "ender@intfleet.com", 10, c2)
    val p2 = Person("Hyrum", "Graff", "graff@intfleet.com", 65, c2)
    val p3 = Person("Ivanova", "von Hesse", "novinha@xenbio.com", 25, c3)

    Map(c2 -> List(p1, p2), c3 -> List(p3)

toString

StringConverter calls toString on the objects, i.e. looks like the following:

Map(Company(Command School,Mountain View, CA) -> List(Person(Andrew,Wiggin,ender@intfleet.com,10,Company(Command School,Mountain View, CA)), Person(Hyrum,Graff,graff@intfleet.com,65,Company(Command School,Mountain View, CA))), Company(Dept of Xenobiology,Lusitania) -> List(Person(Ivanova,von Hesse,novinha@xenbio.com,25,Company(Dept of Xenobiology,Lusitania))))

Tables

Using TableConverter.convert() results in:

Map
Company
name Command School
address Mountain View, CA

List
[0]
Person
first Andrew
last Wiggin
email ender@intfleet.com
age 10
employedBy
Company
name Command School
address Mountain View, CA
[1]
Person
first Hyrum
last Graff
email graff@intfleet.com
age 65
employedBy
Company
name Command School
address Mountain View, CA
Company
name Dept of Xenobiology
address Lusitania
List
[0]
Person
first Ivanova
last von Hesse
email novinha@xenbio.com
age 25
employedBy
Company
name Dept of Xenobiology
address Lusitania

Lists

While using ListConverter.convert() results in:

Map
  • Company
    • name: Command School
    • address: Mountain View, CA
    → List
    1. Person
      • first: Andrew
      • last: Wiggin
      • email: ender@intfleet.com
      • age: 10
      • employedBy: Company
        • name: Command School
        • address: Mountain View, CA
    2. Person
      • first: Hyrum
      • last: Graff
      • email: graff@intfleet.com
      • age: 65
      • employedBy: Company
        • name: Command School
        • address: Mountain View, CA
  • Company
    • name: Dept of Xenobiology
    • address: Lusitania
    → List
    1. Person
      • first: Ivanova
      • last: von Hesse
      • email: novinha@xenbio.com
      • age: 25
      • employedBy: Company
        • name: Dept of Xenobiology
        • address: Lusitania