Skip to content

Make a figure ID optional #14

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

Open
wants to merge 3 commits into
base: major-refactor
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ Currently supported options are listed below:

Whether the caption should be on the top of the element.

* `id_caption`:

Add an id to the element.

The default values for each type of content is synthesised in the following table:

| Config | Image | Table | Other |
Expand All @@ -174,6 +178,7 @@ The default values for each type of content is synthesised in the following tabl
| `caption_class` | - | - | - |
| `caption_prefix_class` | - | - | - |
| `caption_top` | False | True | True |
| `caption_id` | True | True | True |

## Why?

Expand Down
6 changes: 5 additions & 1 deletion caption/caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
content_class=None,
link_process=None,
caption_top=True,
caption_id=True,
):
self.caption_prefix = caption_prefix
self.numbering = numbering
Expand All @@ -43,6 +44,7 @@ def __init__(
self.content_class = content_class
self.link_process = link_process
self.caption_top = caption_top
self.caption_id = caption_id

def build_content_element(self, par, caption, replace=True):
"""Format the content element containing the caption"""
Expand All @@ -54,7 +56,8 @@ def build_content_element(self, par, caption, replace=True):
par.set(k, v)
if self.content_class:
par.set("class", self.content_class)
par.set("id", "_{}-{}".format(self.name, self.number))
if self.caption_id:
par.set("id", "_{}-{}".format(self.name, self.number))
if replace:
par.text = "\n"
par.tail = "\n"
Expand Down Expand Up @@ -144,6 +147,7 @@ def __init__(self, **kwargs):
"content_class": ["", "CSS class to add to the content element."],
"link_process": ["", "Some content types support linked processes."],
"caption_top": [False, "Put the caption at the top of the content."],
"caption_id": [True, "Add an id to the element."],
}
super(CaptionExtension, self).__init__(**kwargs)

Expand Down
4 changes: 4 additions & 0 deletions caption/image_caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

SPDX-License-Identifier: GPL-3.0-or-later
"""

from markdown import Extension

from .caption import CaptionTreeprocessor
Expand All @@ -30,6 +31,7 @@ def __init__(
content_class=None,
strip_title=True,
caption_top=False,
caption_id=True,
):
super(ImageCaptionTreeProcessor, self).__init__(
md=md,
Expand All @@ -39,6 +41,7 @@ def __init__(
caption_class=caption_class,
content_class=content_class,
caption_top=caption_top,
caption_id=caption_id,
)
self.strip_title = strip_title

Expand Down Expand Up @@ -90,6 +93,7 @@ def __init__(self, **kwargs):
"content_class": ["", "CSS class to add to the content element."],
"strip_title": [True, "Remove the title from the img tag."],
"caption_top": [False, "Put the caption at the top of the image."],
"caption_id": [True, "Add an id to the element."],
}
super(ImageCaptionExtension, self).__init__(**kwargs)

Expand Down
1 change: 1 addition & 0 deletions caption/table_caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, **kwargs):
"caption_class": ["", "CSS class to add to the caption element."],
"content_class": ["", "CSS class to add to the content element."],
"caption_top": [True, "Put the caption at the top of the table."],
"caption_id": [True, "Add an id to the element."],
}
super(TableCaptionExtension, self).__init__(**kwargs)

Expand Down
29 changes: 28 additions & 1 deletion test/test_image_caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ def test_caption_prefix_class():
)
assert out_string == expected_string


def test_caption_prefix():
in_string = """\
![alt text](/path/to/image.png "Title")"""
Expand Down Expand Up @@ -248,3 +247,31 @@ def test_combined_options():
],
)
assert out_string == expected_string

def test_caption_id_false():
in_string = """\
![alt text](/path/to/image.png "Title")"""
expected_string = """\
<figure>
<img alt="alt text" src="/path/to/image.png" />
<figcaption><span>Figure&nbsp;1:</span> Title</figcaption>
</figure>"""
out_string = markdown.markdown(
in_string,
extensions=[ImageCaptionExtension(caption_id=False)],
)
assert out_string == expected_string

def test_caption_id_true():
in_string = """\
![alt text](/path/to/image.png "Title")"""
expected_string = """\
<figure id="_figure-1">
<img alt="alt text" src="/path/to/image.png" />
<figcaption><span>Figure&nbsp;1:</span> Title</figcaption>
</figure>"""
out_string = markdown.markdown(
in_string,
extensions=[ImageCaptionExtension(caption_id=True)],
)
assert out_string == expected_string
20 changes: 20 additions & 0 deletions test/test_listing_caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ def test_listing():
</div class=listing>"""
out_string = markdown.markdown(in_string, extensions=[CaptionExtension()])
assert out_string == expected_string

def test_listing_id_false():
in_string = """\
Listing: Simple listing test"""
expected_string = """\
<div class=listing>
<figcaption><span>Listing&nbsp;1:</span> Simple listing test</figcaption>
</div class=listing>"""
out_string = markdown.markdown(in_string, extensions=[CaptionExtension(caption_id=False)])
assert out_string == expected_string

def test_listing_id_true():
in_string = """\
Listing: Simple listing test"""
expected_string = """\
<div class=listing id="_listing-1">
<figcaption><span>Listing&nbsp;1:</span> Simple listing test</figcaption>
</div class=listing>"""
out_string = markdown.markdown(in_string, extensions=[CaptionExtension(caption_id=True)])
assert out_string == expected_string
18 changes: 18 additions & 0 deletions test/test_table_caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ def test_caption_prefix():
</table>""".format(TABLE_INNER_CONTENT)
out_string = markdown.markdown(BASE_MD_TABLE, extensions=["tables", TableCaptionExtension(caption_prefix="Tabula")])
assert out_string == expected_string

def test_caption_id_false():
expected_string = """\
<table>
<caption><span>Table&nbsp;1:</span> Example with heading, two columns and a row</caption>
{}
</table>""".format(TABLE_INNER_CONTENT)
out_string = markdown.markdown(BASE_MD_TABLE, extensions=["tables", TableCaptionExtension(caption_id=False)])
assert out_string == expected_string

def test_caption_id_true():
expected_string = """\
<table id="_table-1">
<caption><span>Table&nbsp;1:</span> Example with heading, two columns and a row</caption>
{}
</table>""".format(TABLE_INNER_CONTENT)
out_string = markdown.markdown(BASE_MD_TABLE, extensions=["tables", TableCaptionExtension(caption_id=True)])
assert out_string == expected_string