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
- Basic Types
- Iterables
- Maps
- Product and Case classes
- Line Plots (using scalaplot)
- Matrices as Images
- Histograms
- Pie charts
- Graphs
- Sequence Tagging
Converters
- toString
- Tables for everything
- Lists for everything
-
div
andspan
based - D3js/Canvas based
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", "[email protected]", 10, c2)
val p2 = Person("Hyrum", "Graff", "[email protected]", 65, c2)
val p3 = Person("Ivanova", "von Hesse", "[email protected]", 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,[email protected],10,Company(Command School,Mountain View, CA)), Person(Hyrum,Graff,[email protected],65,Company(Command School,Mountain View, CA))), Company(Dept of Xenobiology,Lusitania) -> List(Person(Ivanova,von Hesse,[email protected],25,Company(Dept of Xenobiology,Lusitania))))
Using TableConverter.convert()
results in:
Map | |||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
While using ListConverter.convert()
results in:
- Company
- name:
Command School
- address:
Mountain View, CA
- Person
- first:
Andrew
- last:
Wiggin
- email:
[email protected]
- age:
10
- employedBy: Company
- name:
Command School
- address:
Mountain View, CA
- name:
- first:
- Person
- first:
Hyrum
- last:
Graff
- email:
[email protected]
- age:
65
- employedBy: Company
- name:
Command School
- address:
Mountain View, CA
- name:
- first:
- name:
- Company
- name:
Dept of Xenobiology
- address:
Lusitania
- Person
- first:
Ivanova
- last:
von Hesse
- email:
[email protected]
- age:
25
- employedBy: Company
- name:
Dept of Xenobiology
- address:
Lusitania
- name:
- first:
- name: