Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): support cross references to images within tables #1050

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pkg/parser/cross_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,58 @@ some content`
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("to image in table cell", func() {
source := `a reference to <<cookie>>

|===
a|
[#cookie]
.A cookie
image::cookie.png[Cookie]
|===`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "a reference to ",
},
&types.InternalCrossReference{
ID: "cookie",
},
},
},
&types.Table{
Rows: []*types.TableRow{
{
Cells: []*types.TableCell{
{
Format: "a",
Elements: []interface{}{
&types.ImageBlock{
Attributes: types.Attributes{
types.AttrID: "cookie",
types.AttrTitle: "A cookie",
types.AttrImageAlt: "Cookie",
},
Location: &types.Location{
Path: "cookie.png",
},
},
},
},
},
},
},
},
},
ElementReferences: types.ElementReferences{
"cookie": "A cookie",
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("to attached element in a list", func() {
source := `a reference to <<table>>

Expand Down
20 changes: 11 additions & 9 deletions pkg/parser/delimited_block_open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ some content
[cols="2*^"]
|===
a|
[#id]
.A title
[#image-id]
.An image
image::image.png[]
a|
[#another-id]
.Another title
[#another-image-id]
.Another image
image::another-image.png[]
|===
--`
Expand Down Expand Up @@ -81,8 +81,8 @@ image::another-image.png[]
Elements: []interface{}{
&types.ImageBlock{
Attributes: types.Attributes{
types.AttrID: "id",
types.AttrTitle: "A title",
types.AttrID: "image-id",
types.AttrTitle: "An image",
},
Location: &types.Location{
Path: "image.png",
Expand All @@ -95,8 +95,8 @@ image::another-image.png[]
Elements: []interface{}{
&types.ImageBlock{
Attributes: types.Attributes{
types.AttrID: "another-id",
types.AttrTitle: "Another title",
types.AttrID: "another-image-id",
types.AttrTitle: "Another image",
},
Location: &types.Location{
Path: "another-image.png",
Expand All @@ -112,7 +112,9 @@ image::another-image.png[]
},
},
ElementReferences: types.ElementReferences{
"block-id": "Block Title",
"block-id": "Block Title",
"image-id": "An image",
"another-image-id": "Another image",
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
Expand Down
20 changes: 12 additions & 8 deletions pkg/parser/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1387,12 +1387,12 @@ image::another-image.png[]
source := `[cols="2*^"]
|===
a|
[#id]
.A title
[#image-id]
.An image
image::image.png[]
a|
[#another-id]
.Another title
[#another-image-id]
.Another image
image::another-image.png[]
|===`
expected := &types.Document{
Expand All @@ -1416,8 +1416,8 @@ image::another-image.png[]
Elements: []interface{}{
&types.ImageBlock{
Attributes: types.Attributes{
types.AttrID: "id",
types.AttrTitle: "A title",
types.AttrID: "image-id",
types.AttrTitle: "An image",
},
Location: &types.Location{
Path: "image.png",
Expand All @@ -1430,8 +1430,8 @@ image::another-image.png[]
Elements: []interface{}{
&types.ImageBlock{
Attributes: types.Attributes{
types.AttrID: "another-id",
types.AttrTitle: "Another title",
types.AttrID: "another-image-id",
types.AttrTitle: "Another image",
},
Location: &types.Location{
Path: "another-image.png",
Expand All @@ -1444,6 +1444,10 @@ image::another-image.png[]
},
},
},
ElementReferences: types.ElementReferences{
"image-id": "An image",
"another-image-id": "Another image",
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
Expand Down
31 changes: 31 additions & 0 deletions pkg/renderer/sgml/html5/cross_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,37 @@ image::cookie.jpg[]`
</div>
<div class="title">Figure 1. A cookie</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("to image in table cell", func() {
source := `a reference to <<cookie>>

|===
a|
[#cookie]
.A cookie
image::cookie.png[Cookie]
|===`
expected := `<div class="paragraph">
<p>a reference to <a href="#cookie">A cookie</a></p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 100%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div id="cookie" class="imageblock">
<div class="content">
<img src="cookie.png" alt="Cookie">
</div>
<div class="title">Figure 1. A cookie</div>
</div></div></td>
</tr>
</tbody>
</table>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
Expand Down
28 changes: 28 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3796,6 +3796,34 @@ func (t *Table) Reference(refs ElementReferences) {
if id != "" && title != nil {
refs[id] = title
}
// also, traverse all cells to find referencable content
if t.Header != nil {
for _, c := range t.Header.Cells {
for _, e := range c.Elements {
if e, ok := e.(Referencable); ok {
e.Reference(refs)
}
}
}
}
for _, r := range t.Rows {
for _, c := range r.Cells {
for _, e := range c.Elements {
if e, ok := e.(Referencable); ok {
e.Reference(refs)
}
}
}
}
if t.Footer != nil {
for _, c := range t.Footer.Cells {
for _, e := range c.Elements {
if e, ok := e.(Referencable); ok {
e.Reference(refs)
}
}
}
}
}

type HAlign string
Expand Down