Write browser-friendly output for Scala objects.
Write browser-friendly output for various data structures.
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.
Data structures
Converters
div
and span
basedFor 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)
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))))
Using TableConverter.convert()
results in:
Map | |||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
While using ListConverter.convert()
results in:
Command School
Mountain View, CA
Andrew
Wiggin
ender@intfleet.com
10
Command School
Mountain View, CA
Hyrum
Graff
graff@intfleet.com
65
Command School
Mountain View, CA
Dept of Xenobiology
Lusitania
Ivanova
von Hesse
novinha@xenbio.com
25
Dept of Xenobiology
Lusitania