Skip to content

Commit

Permalink
Automatically render image as figure with figcaption if it has a title
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Jul 17, 2024
1 parent 563b2d6 commit ed295a7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.iamgio.quarkdown.rendering.html

import eu.iamgio.quarkdown.ast.FunctionCallNode
import eu.iamgio.quarkdown.ast.Image
import eu.iamgio.quarkdown.ast.Math
import eu.iamgio.quarkdown.ast.MathSpan
import eu.iamgio.quarkdown.ast.Node
Expand Down Expand Up @@ -48,6 +49,18 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context

// Quarkdown node rendering

// The Quarkdown flavor renders an image title as a figure caption, if present.
override fun visit(node: Image): String {
val imgTag = super.visit(node)

return node.link.title?.let { title ->
buildTag("figure") {
+imgTag
+buildTag("figcaption", title)
}
} ?: imgTag
}

// The function was already expanded by previous stages: its output nodes are stored in its children.
override fun visit(node: FunctionCallNode): CharSequence = node.children.joinToString(separator = "") { it.accept(this) }

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/resources/render/html-wrapper.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
width: 100%;
}

figure {
text-align: center;
}

.align {
width: 100%;
display: flex;
Expand Down
11 changes: 10 additions & 1 deletion core/src/test/kotlin/eu/iamgio/quarkdown/HtmlNodeRendererTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import eu.iamgio.quarkdown.context.Context
import eu.iamgio.quarkdown.context.MutableContext
import eu.iamgio.quarkdown.document.page.cm
import eu.iamgio.quarkdown.document.page.inch
import eu.iamgio.quarkdown.flavor.base.BaseMarkdownFlavor
import eu.iamgio.quarkdown.flavor.quarkdown.QuarkdownFlavor
import eu.iamgio.quarkdown.misc.Color
import eu.iamgio.quarkdown.pipeline.PipelineOptions
Expand Down Expand Up @@ -73,7 +74,7 @@ class HtmlNodeRendererTest {
)
}

return QuarkdownFlavor.rendererFactory.html(context).nodeRenderer
return context.flavor.rendererFactory.html(context).nodeRenderer
}

private fun Node.render(context: Context = MutableContext(QuarkdownFlavor)) = this.accept(renderer(context))
Expand Down Expand Up @@ -162,6 +163,14 @@ class HtmlNodeRendererTest {
height = null,
).render(),
)
assertEquals(
out.next(),
Image(
Link(label = listOf(), url = "/url", title = "Title"),
width = null,
height = null,
).render(MutableContext(BaseMarkdownFlavor)),
) // The figcaption appears only with the Quarkdown rendering!
assertEquals(
out.next(),
Image(
Expand Down
30 changes: 27 additions & 3 deletions core/src/test/resources/rendering/inline/image.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,36 @@

---

<img src="/url" alt="Foo bar" title="Title" />
<figure>
<img src="/url" alt="" title="Title" />
<figcaption>
Title
</figcaption>
</figure>

---

<img src="/url" alt="Foo bar" title="Title" />
<figure>
<img src="/url" alt="Foo bar" title="Title" />
<figcaption>
Title
</figcaption>
</figure>

---

<img src="/url" alt="Foo bar" title="Title" width="150" height="100" />
<figure>
<img src="/url" alt="Foo bar" title="Title" />
<figcaption>
Title
</figcaption>
</figure>

---

<figure>
<img src="/url" alt="Foo bar" title="Title" width="150" height="100" />
<figcaption>
Title
</figcaption>
</figure>
21 changes: 18 additions & 3 deletions core/src/test/resources/rendering/inline/refimage.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
<img src="/url" alt="Foo" title="Title" />
<figure>
<img src="/url" alt="Foo" title="Title" />
<figcaption>
Title
</figcaption>
</figure>

---

<img src="/url" alt="label" title="Title" />
<figure>
<img src="/url" alt="label" title="Title" />
<figcaption>
Title
</figcaption>
</figure>

---

<img src="/url" alt="label" title="Title" width="150" height="100" />
<figure>
<img src="/url" alt="label" title="Title" width="150" height="100" />
<figcaption>
Title
</figcaption>
</figure>

---

Expand Down

0 comments on commit ed295a7

Please sign in to comment.