Skip to content

Commit 1cfe3ab

Browse files
authored
Merge pull request #9 from hendrikp/major-refactor
Fix/add compatibility with attr_list extension ids, classes Close #9
2 parents 68da7c5 + f0780f3 commit 1cfe3ab

6 files changed

Lines changed: 143 additions & 9 deletions

File tree

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ Listing: Example listing
8484
becomes
8585

8686
```html
87-
<caption><span>Listing&nbsp;1:</span> Example listing</caption>
87+
<div class="listing" id="_listing-1">
88+
<figcaption><span>Listing&nbsp;1:</span> Example listing</figcaption>
89+
</div>
8890
```
8991

9092
## How?
@@ -170,7 +172,7 @@ The default values for each type of content is synthesised in the following tabl
170172
|------------------------|---------|---------|-----------|
171173
| `caption_prefix` | "Image" | "Table" | "Listing" |
172174
| `numbering` | False | False | False |
173-
| `content_class` | - | - | - |
175+
| `content_class` | - | - | listing |
174176
| `caption_class` | - | - | - |
175177
| `caption_prefix_class` | - | - | - |
176178
| `caption_top` | False | True | True |
@@ -224,6 +226,67 @@ figcaption span:first-child {
224226
```
225227
There are further examples in the [wiki](https://github.com/flywire/caption/wiki).
226228

229+
## Compatibility with attr_list extension
230+
231+
*caption* supports preserving `attr_list` extension supplied `id` and `class` attributes by:
232+
233+
* giving priority to markdown defined `id` attributes
234+
* concatenating `class` attributes.
235+
236+
### `image_captions`
237+
238+
This samples shows how to create a captioned image with `id` and `class` through markdown `attr_list` extension.
239+
240+
```markdown
241+
![Alt text](/path/to/image.png "This is the title of the image."){ #title-image .test-class }
242+
```
243+
244+
becomes
245+
246+
```html
247+
<figure id="_figure-1">
248+
<img alt="Alt text" src="/path/to/image.png" id="title-image" class="test-class" />
249+
...
250+
```
251+
252+
### `table_captions`
253+
254+
This samples shows how to create a captioned table with `id` and `class` through markdown `attr_list` extension.
255+
256+
```markdown
257+
Table: Example with heading, two columns and a row
258+
{#example-with-heading .test-class}
259+
260+
| Syntax | Description |
261+
| ----------- | ----------- |
262+
| Header | Title |
263+
```
264+
265+
becomes
266+
267+
```html
268+
<table id="example-with-heading" class="test-class table">
269+
...
270+
```
271+
272+
### `caption`
273+
274+
This samples shows how to create a generic caption with `id` and `class` through markdown `attr_list` extension.
275+
276+
277+
```markdown
278+
Listing: Example listing
279+
{ #example-listing .test-class }
280+
```
281+
282+
becomes
283+
284+
```html
285+
<div class="listing test-class" id="example-listing">
286+
<figcaption><span>Listing&nbsp;1:</span> Example listing</figcaption>
287+
</div>
288+
```
289+
227290
## Customisable
228291

229292
If the settings aren't flexible enough the source code can be changed and

caption/caption.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ def build_content_element(self, par, caption, replace=True):
5252
par.tag = self.content_tag
5353
for k, v in attrib.items():
5454
par.set(k, v)
55+
5556
if self.content_class:
56-
par.set("class", self.content_class)
57-
par.set("id", "_{}-{}".format(self.name, self.number))
57+
if "class" in attrib:
58+
par.set("class", self.content_class + " " + attrib["class"])
59+
else:
60+
par.set("class", self.content_class)
61+
if "id" not in attrib:
62+
par.set("id", "_{}-{}".format(self.name, self.number))
63+
5864
if replace:
5965
par.text = "\n"
6066
par.tail = "\n"
@@ -116,7 +122,7 @@ def run(self, root):
116122

117123
class ListingCaptionTreeProcessor(CaptionTreeprocessor):
118124
name = "listing"
119-
content_tag = "div class=listing"
125+
content_tag = "div"
120126

121127
def matches(self, par):
122128
return par.text and par.text.startswith("Listing: ")
@@ -141,7 +147,7 @@ def __init__(self, **kwargs):
141147
"CSS class to add to the caption prefix <span /> element.",
142148
],
143149
"caption_class": ["", "CSS class to add to the caption element."],
144-
"content_class": ["", "CSS class to add to the content element."],
150+
"content_class": ["listing", "CSS class to add to the content element."],
145151
"link_process": ["", "Some content types support linked processes."],
146152
"caption_top": [False, "Put the caption at the top of the content."],
147153
}

caption/table_caption.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# Copyright (c) 2019 Philipp Trommler
77
#
88
# SPDX-License-Identifier: GPL-3.0-or-later
9-
from xml.etree import ElementTree
109

1110
from markdown import Extension
1211

@@ -42,6 +41,17 @@ def run(self, root):
4241
title = self.get_title(child)
4342
root.remove(child)
4443
caption = self.build_caption_element(title)
44+
45+
attrib = child.attrib
46+
if "class" in attrib:
47+
if "class" in next_item.attrib:
48+
next_item.set("class", attrib["class"] +
49+
" " + next_item.attrib["class"])
50+
else:
51+
next_item.set("class", attrib["class"])
52+
if "id" in attrib:
53+
next_item.set("id", attrib["id"])
54+
4555
self.build_content_element(next_item, caption, replace=False)
4656
self.add_caption_to_content(next_item, caption)
4757

test/test_image_caption.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,15 @@ def test_combined_options():
248248
],
249249
)
250250
assert out_string == expected_string
251+
252+
253+
def test_image_attr_list():
254+
in_string = """\
255+
![alt text](/path/to/image.png "Title"){#testid .testal}"""
256+
expected_string = """\
257+
<figure id="_figure-1">
258+
<img alt="alt text" class="testal" id="testid" src="/path/to/image.png" />
259+
<figcaption><span>Figure&nbsp;1:</span> Title</figcaption>
260+
</figure>"""
261+
out_string = markdown.markdown(in_string, extensions=["attr_list", ImageCaptionExtension()])
262+
assert out_string == expected_string

test/test_listing_caption.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@ def test_listing():
1515
in_string = """\
1616
Listing: Simple listing test"""
1717
expected_string = """\
18-
<div class=listing id="_listing-1">
18+
<div class="listing" id="_listing-1">
1919
<figcaption><span>Listing&nbsp;1:</span> Simple listing test</figcaption>
20-
</div class=listing>"""
20+
</div>"""
2121
out_string = markdown.markdown(in_string, extensions=[CaptionExtension()])
2222
assert out_string == expected_string
23+
24+
25+
def test_listing_attr_list():
26+
in_string = """\
27+
Listing: Simple listing test\n\
28+
{#testid .testclass}"""
29+
expected_string = """\
30+
<div class="listing testclass" id="testid">
31+
<figcaption><span>Listing&nbsp;1:</span> Simple listing test</figcaption>
32+
</div>"""
33+
out_string = markdown.markdown(in_string, extensions=["attr_list", CaptionExtension()])
34+
assert out_string == expected_string

test/test_table_caption.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ def test_no_table():
6464
</tbody>"""
6565

6666

67+
BASE_MD_TABLE_ATTR_LIST = """\
68+
Table: Example with heading, two columns and a row
69+
{#testid .testal}
70+
71+
| Syntax | Description |
72+
| ----------- | ----------- |
73+
| Header | Title |
74+
| Paragraph | Text |
75+
"""
76+
77+
6778
def test_defaults():
6879
expected_string = """\
6980
<table id="_table-1">
@@ -132,3 +143,23 @@ def test_caption_prefix():
132143
</table>""".format(TABLE_INNER_CONTENT)
133144
out_string = markdown.markdown(BASE_MD_TABLE, extensions=["tables", TableCaptionExtension(caption_prefix="Tabula")])
134145
assert out_string == expected_string
146+
147+
148+
def test_attr_list():
149+
expected_string = """\
150+
<table class="testal" id="testid">
151+
<caption><span>Table&nbsp;1:</span> Example with heading, two columns and a row</caption>
152+
{}
153+
</table>""".format(TABLE_INNER_CONTENT)
154+
out_string = markdown.markdown(BASE_MD_TABLE_ATTR_LIST, extensions=["attr_list", "tables", TableCaptionExtension()])
155+
assert out_string == expected_string
156+
157+
158+
def test_content_class_attr_list():
159+
expected_string = """\
160+
<table class="testclass testal" id="testid">
161+
<caption><span>Table&nbsp;1:</span> Example with heading, two columns and a row</caption>
162+
{}
163+
</table>""".format(TABLE_INNER_CONTENT)
164+
out_string = markdown.markdown(BASE_MD_TABLE_ATTR_LIST, extensions=["attr_list", "tables", TableCaptionExtension(content_class="testclass")])
165+
assert out_string == expected_string

0 commit comments

Comments
 (0)