From 6a95a6c2510852447ce97b92dd0fc9cf236fd3ee Mon Sep 17 00:00:00 2001 From: Vladyslav Prudius Date: Wed, 5 Mar 2025 00:15:42 +0200 Subject: [PATCH 1/3] feat: add caption_id option --- caption/caption.py | 6 +++++- caption/image_caption.py | 4 ++++ caption/table_caption.py | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/caption/caption.py b/caption/caption.py index 8d5f495..c21ce20 100644 --- a/caption/caption.py +++ b/caption/caption.py @@ -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 @@ -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""" @@ -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" @@ -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) diff --git a/caption/image_caption.py b/caption/image_caption.py index 52d48d5..762b995 100644 --- a/caption/image_caption.py +++ b/caption/image_caption.py @@ -11,6 +11,7 @@ SPDX-License-Identifier: GPL-3.0-or-later """ + from markdown import Extension from .caption import CaptionTreeprocessor @@ -30,6 +31,7 @@ def __init__( content_class=None, strip_title=True, caption_top=False, + caption_id=True, ): super(ImageCaptionTreeProcessor, self).__init__( md=md, @@ -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 @@ -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) diff --git a/caption/table_caption.py b/caption/table_caption.py index a8c87fe..e509bf7 100644 --- a/caption/table_caption.py +++ b/caption/table_caption.py @@ -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) From 8d0cbdf541bbb5700cda3180f5dc2979282f8f99 Mon Sep 17 00:00:00 2001 From: Vladyslav Prudius Date: Wed, 5 Mar 2025 16:16:52 +0200 Subject: [PATCH 2/3] tests: add caption_id tests --- test/test_image_caption.py | 29 ++++++++++++++++++++++++++++- test/test_listing_caption.py | 20 ++++++++++++++++++++ test/test_table_caption.py | 18 ++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/test/test_image_caption.py b/test/test_image_caption.py index b6eb520..b7af3c0 100644 --- a/test/test_image_caption.py +++ b/test/test_image_caption.py @@ -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")""" @@ -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 = """\ +
+alt text +
Figure 1: Title
+
""" + 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 = """\ +
+alt text +
Figure 1: Title
+
""" + out_string = markdown.markdown( + in_string, + extensions=[ImageCaptionExtension(caption_id=True)], + ) + assert out_string == expected_string diff --git a/test/test_listing_caption.py b/test/test_listing_caption.py index ce694a4..db1e337 100644 --- a/test/test_listing_caption.py +++ b/test/test_listing_caption.py @@ -20,3 +20,23 @@ def test_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 = """\ +
+
Listing 1: Simple listing test
+
""" + 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 = """\ +
+
Listing 1: Simple listing test
+
""" + out_string = markdown.markdown(in_string, extensions=[CaptionExtension(caption_id=True)]) + assert out_string == expected_string diff --git a/test/test_table_caption.py b/test/test_table_caption.py index 90ec624..4dbc253 100644 --- a/test/test_table_caption.py +++ b/test/test_table_caption.py @@ -132,3 +132,21 @@ def test_caption_prefix(): """.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 1: Example with heading, two columns and a row
""".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 1: Example with heading, two columns and a row
""".format(TABLE_INNER_CONTENT) + out_string = markdown.markdown(BASE_MD_TABLE, extensions=["tables", TableCaptionExtension(caption_id=True)]) + assert out_string == expected_string From 0154a4ebbad12e341b5d4ce0cb584a38b4e675b0 Mon Sep 17 00:00:00 2001 From: Vladyslav Prudius Date: Wed, 5 Mar 2025 16:25:25 +0200 Subject: [PATCH 3/3] docs: update readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 14f677b..59d0602 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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?