HTML-Groovy.html.groovy 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Available context bindings:
  3. * COLUMNS List<DataColumn>
  4. * ROWS Iterable<DataRow>
  5. * OUT { append() }
  6. * FORMATTER { format(row, col); formatValue(Object, col) }
  7. * TRANSPOSED Boolean
  8. * plus ALL_COLUMNS, TABLE, DIALECT
  9. *
  10. * where:
  11. * DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
  12. * DataColumn { columnNumber(), name() }
  13. */
  14. import static com.intellij.openapi.util.text.StringUtil.escapeXml
  15. NEWLINE = System.getProperty("line.separator")
  16. def printRow = { values, tag, valueToString ->
  17. OUT.append("$NEWLINE<tr>$NEWLINE")
  18. values.each {
  19. def str = valueToString(it)
  20. def escaped = escapeXml((str as String).replaceAll("\\t|\\b|\\f", "")).replaceAll("\\r|\\n|\\r\\n", "<br/>")
  21. OUT.append(" <$tag>$escaped</$tag>$NEWLINE")
  22. }
  23. OUT.append("</tr>")
  24. }
  25. OUT.append(
  26. """<!DOCTYPE html>
  27. <html>
  28. <head>
  29. <title></title>
  30. </head>
  31. <body>
  32. <table border="1" style="border-collapse:collapse">""")
  33. if (!TRANSPOSED) {
  34. printRow(COLUMNS, "th") { it.name() }
  35. ROWS.each { row -> printRow(COLUMNS, "td") { FORMATTER.format(row, it) } }
  36. }
  37. else {
  38. def values = COLUMNS.collect { new ArrayList<String>( [it.name()] ) }
  39. ROWS.each { row -> COLUMNS.eachWithIndex { col, i -> values[i].add(FORMATTER.format(row, col)) } }
  40. values.each { printRow(it, "td", { it }) }
  41. }
  42. OUT.append("""
  43. </table>
  44. </body>
  45. </html>
  46. """)