From dfc2f0faec4b89c3f13425c6e370dce4ad476445 Mon Sep 17 00:00:00 2001 From: Crozzers Date: Sat, 17 Feb 2024 14:29:02 +0000 Subject: [PATCH 1/2] Add `prepend` arg to toc extra --- lib/markdown2.py | 2 +- test/tm-cases/toc_prepend.html | 40 ++++++++++++++++++++++++++++++ test/tm-cases/toc_prepend.opts | 1 + test/tm-cases/toc_prepend.tags | 1 + test/tm-cases/toc_prepend.text | 20 +++++++++++++++ test/tm-cases/toc_prepend.toc_html | 20 +++++++++++++++ 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 test/tm-cases/toc_prepend.html create mode 100644 test/tm-cases/toc_prepend.opts create mode 100644 test/tm-cases/toc_prepend.tags create mode 100644 test/tm-cases/toc_prepend.text create mode 100644 test/tm-cases/toc_prepend.toc_html diff --git a/lib/markdown2.py b/lib/markdown2.py index 3a1db281..01252ff8 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -513,7 +513,7 @@ def toc_sort(entry): self._toc_html = calculate_toc_html(self._toc) # Prepend toc html to output - if self.cli: + if self.cli or (self.extras['toc'] is not None and self.extras['toc'].get('prepend', False)): text = '{}\n{}'.format(self._toc_html, text) text += "\n" diff --git a/test/tm-cases/toc_prepend.html b/test/tm-cases/toc_prepend.html new file mode 100644 index 00000000..7aa0a0e5 --- /dev/null +++ b/test/tm-cases/toc_prepend.html @@ -0,0 +1,40 @@ + + +

README for Blah

+ +

Introduction

+ +

The Meat

+ +

Beef

+ +
Steak
+ +
Burgers
+ +

Chicken

+ +

Pork

+ +

Mmmmmmmm, bacon

+ +

At the top level again!?

diff --git a/test/tm-cases/toc_prepend.opts b/test/tm-cases/toc_prepend.opts new file mode 100644 index 00000000..261151cd --- /dev/null +++ b/test/tm-cases/toc_prepend.opts @@ -0,0 +1 @@ +{"extras": {"toc": {"prepend": True}}} diff --git a/test/tm-cases/toc_prepend.tags b/test/tm-cases/toc_prepend.tags new file mode 100644 index 00000000..2b2472e0 --- /dev/null +++ b/test/tm-cases/toc_prepend.tags @@ -0,0 +1 @@ +toc extra diff --git a/test/tm-cases/toc_prepend.text b/test/tm-cases/toc_prepend.text new file mode 100644 index 00000000..34f629a6 --- /dev/null +++ b/test/tm-cases/toc_prepend.text @@ -0,0 +1,20 @@ +# README for Blah + +## Introduction + +## The Meat + +### Beef + +##### Steak + +##### Burgers + +### Chicken + +### Pork + +#### Mmmmmmmm, bacon + +# At the *top* level again!? + diff --git a/test/tm-cases/toc_prepend.toc_html b/test/tm-cases/toc_prepend.toc_html new file mode 100644 index 00000000..08288703 --- /dev/null +++ b/test/tm-cases/toc_prepend.toc_html @@ -0,0 +1,20 @@ + From f90c2e1352d38ab6349b1dcc4b2ab91978889e48 Mon Sep 17 00:00:00 2001 From: Crozzers Date: Sat, 17 Feb 2024 14:32:24 +0000 Subject: [PATCH 2/2] Update changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 6430e856..1987d1c2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,7 @@ - [pull #519] Add support for custom extras - [pull #519] Drop Python 3.5 support +- [pull #568] Add `prepend` arg to toc extra (#397) ## python-markdown2 2.4.13