Skip to content

Commit 31c4a10

Browse files
committed
add html img tag generation
1 parent 7250fe0 commit 31c4a10

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/main/scala/05-HtmlGen.scala

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,50 @@ object HtmlGen:
6565
def elemListItem(level: Int, cls: String = "")(et: ElemType)(after: String = ""): String =
6666
(" " * level) + s"""<li><span class="$cls"> $et</span>: $after </li>"""
6767

68-
def renderHtmlBody(m: Model, level: Int): String =
68+
def renderHtmlBody(m: Model, level: Int, insertImagesWithLocation: Boolean, parent: Option[Ent]): String =
6969
val ind = (" " * level)
7070
val lines: Vector[String] = m.elems.map:
7171
case Ent(t, id) => elemListItem(level, "entityColor" )(t)(id)
72-
case StrAttr(t, value) => elemListItem(level, "attributeColor")(t)(value)
72+
73+
case StrAttr(t, value) =>
74+
val imgTag: String =
75+
parent.map: p =>
76+
if p.t == Image && t == Location then
77+
"\n" + (" " * level) + s"""<img src="$value" alt="${p.t}: ${p.id} has $t: $value" class="imageLocation">"""
78+
else ""
79+
.getOrElse("")
80+
81+
if imgTag.nonEmpty then imgTag
82+
else elemListItem(level, "attributeColor")(t)(value)
83+
7384
case IntAttr(t, value) => elemListItem(level, "attributeColor")(t)(value.toString)
85+
7486
case Undefined(t) => elemListItem(level, "attributeColor")(t)("???")
87+
7588
case Rel(e, t, sub) =>
76-
s"""$ind<li><span class="entityColor"> ${e.t}</span>: ${e.id}"""
77-
+ s"""<span class="relationColor"> ${t.show}</span></li>\n"""
78-
+ (if sub.elems.nonEmpty then renderHtmlBody(sub, level + 1) else "")
89+
s"""$ind<li><span class="entityColor"> ${e.t}</span>: ${e.id}"""
90+
+ s"""<span class="relationColor"> ${t.show}</span></li>\n"""
91+
+ (if sub.elems.nonEmpty then renderHtmlBody(sub, level + 1, insertImagesWithLocation, Some(e)) else "")
92+
7993
lines.mkString(s"$ind<ul>\n","\n",s"\n$ind</ul>")
94+
end renderHtmlBody
8095

8196
extension (m: Model)
82-
def toHtmlBody: String =
97+
def toHtml: String = toHtml()
98+
def toHtmlBody: String = toHtmlBody()
99+
100+
def toHtmlBody(
101+
insertImagesWithLocation: Boolean = true,
102+
): String =
83103
if m.elems.isEmpty then "<ul><li>Empty Model</li></ul>"
84-
else renderHtmlBody(m, 0)
104+
else renderHtmlBody(m, 0, insertImagesWithLocation, parent = None)
85105

86-
def toHtml: String = toHtml()
87106

88-
def toHtml(fallBackTitle: String = defaultTitle, style: String = defaultStyle): String =
107+
def toHtml(
108+
insertImagesWithLocation: Boolean = true,
109+
fallBackTitle: String = defaultTitle,
110+
style: String = defaultStyle,
111+
): String =
89112
s"${preamble(m, fallBackTitle, style)}\n<body>\n${m.toHtmlBody}\n</body>\n</html>\n"
90113

91114
end HtmlGen

src/test/scala/TestHtmlGen.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package reqt
2+
3+
class TestHtmlGen extends munit.FunSuite:
4+
test("Model toHtml"):
5+
import scala.sys.process._
6+
val m = Model(
7+
Section("ContextDiagram").has(
8+
Product("x"),
9+
Image("ctxDiagram") has Location("ctx.png"),
10+
)
11+
)
12+
val html = m.toHtml
13+
val f1 = "target/m1.html"
14+
html.saveTo(f1)
15+
16+
assert(true) // TODO check that it includes the right stuff

0 commit comments

Comments
 (0)