Skip to content

Commit

Permalink
feature(renderer): inline images deserve links, too (#754)
Browse files Browse the repository at this point in the history
They are used in libasciidoc's own README.adoc.
  • Loading branch information
pjanx authored Sep 13, 2020
1 parent 7a90b4e commit 3480071
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/html5/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ const (
{{ else }}
{{ end }}</div>
`
inlineImageTmpl = `<span class="image{{ if .Roles }} {{ .Roles }}{{ end }}"><img src="{{ .Path }}" alt="{{ .Alt }}"{{ if .Width }} width="{{ .Width }}"{{ end }}{{ if .Height }} height="{{ .Height }}"{{ end }}{{ if .Title }} title="{{ .Title }}"{{ end }}></span>`
inlineImageTmpl = `<span class="image{{ if .Roles }} {{ .Roles }}{{ end }}">{{ if ne .Href "" }}<a class="image" href="{{ .Href }}">{{ end }}<img src="{{ .Path }}" alt="{{ .Alt }}"{{ if .Width }} width="{{ .Width }}"{{ end }}{{ if .Height }} height="{{ .Height }}"{{ end }}{{ if .Title }} title="{{ .Title }}"{{ end }}>{{ if ne .Href "" }}</a>{{ end }}</span>`
)
9 changes: 9 additions & 0 deletions pkg/renderer/sgml/html5/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ image::appa.png[]`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("inline image with link", func() {
source := "image:foo.png[foo image, link=http://foo.bar]"
expected := `<div class="paragraph">
<p><span class="image"><a class="image" href="http://foo.bar"><img src="foo.png" alt="foo image"></a></span></p>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("paragraph with inline image with alt and dimensions", func() {
source := "a foo image:foo.png[foo image, 600, 400] bar"
expected := `<div class="paragraph">
Expand Down
1 change: 1 addition & 0 deletions pkg/renderer/sgml/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (r *sgmlRenderer) renderInlineImage(img types.InlineImage) (string, error)
}{
Title: r.renderElementTitle(img.Attributes),
Roles: roles,
Href: img.Attributes.GetAsStringWithDefault(types.AttrInlineLink, ""),
Alt: img.Attributes.GetAsStringWithDefault(types.AttrImageAlt, ""),
Width: img.Attributes.GetAsStringWithDefault(types.AttrWidth, ""),
Height: img.Attributes.GetAsStringWithDefault(types.AttrImageHeight, ""),
Expand Down
3 changes: 2 additions & 1 deletion pkg/renderer/sgml/xhtml5/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const (
"</div>\n"

inlineImageTmpl = `<span class="image{{ if .Roles }} {{ .Roles }}{{ end }}">` +
`{{ if .Href }}<a class="image" href="{{ .Href }}">{{ end }}` +
`<img src="{{ .Path }}" alt="{{ .Alt }}"` +
`{{ if .Width }} width="{{ .Width }}"{{ end }}` +
`{{ if .Height }} height="{{ .Height }}"{{ end }}` +
`{{ if .Title }} title="{{ .Title }}"{{ end }}` +
`/></span>`
`/>{{ if .Href }}</a>{{ end }}</span>`
)
9 changes: 9 additions & 0 deletions pkg/renderer/sgml/xhtml5/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ image::appa.png[]`
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})

It("inline image with link", func() {
source := "image:foo.png[foo image, link=http://foo.bar]"
expected := `<div class="paragraph">
<p><span class="image"><a class="image" href="http://foo.bar"><img src="foo.png" alt="foo image"/></a></span></p>
</div>
`
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})

It("paragraph with inline image with alt and dimensions", func() {
source := "a foo image:foo.png[foo image, 600, 400] bar"
expected := `<div class="paragraph">
Expand Down

0 comments on commit 3480071

Please sign in to comment.