Skip to content

Commit 1c0ee2a

Browse files
authored
Add new option abstract_delimiter (#173)
Superseedes #156
2 parents ca18790 + e92cac1 commit 1c0ee2a

File tree

8 files changed

+51
-8
lines changed

8 files changed

+51
-8
lines changed

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ plugins:
2323
datetime_format: "%Y-%m-%d %H:%M"
2424
default_timezone: "Europe/Paris"
2525
default_time: "22:00"
26+
abstract_chars_count: 160
27+
abstract_delimiter: <!-- more -->
2628
image: https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Feed-icon.svg/128px-Feed-icon.svg.png
2729
match_path: ".*"
2830
pretty_print: false

mkdocs_rss_plugin/plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class GitRssPlugin(BasePlugin):
4747

4848
config_scheme = (
4949
("abstract_chars_count", config_options.Type(int, default=160)),
50+
("abstract_delimiter", config_options.Type(str, default="<!-- more -->")),
5051
("categories", config_options.Type(list, default=None)),
5152
("comments_path", config_options.Type(str, default=None)),
5253
("date_from_meta", config_options.Type(dict, default=None)),
@@ -256,7 +257,9 @@ def on_page_content(
256257
),
257258
created=page_dates[0],
258259
description=self.util.get_description_or_abstract(
259-
in_page=page, chars_count=self.config.get("abstract_chars_count")
260+
in_page=page,
261+
chars_count=self.config.get("abstract_chars_count"),
262+
abstract_delimiter=self.config.get("abstract_delimiter"),
260263
),
261264
guid=page.canonical_url,
262265
image=self.util.get_image(

mkdocs_rss_plugin/util.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,21 @@ def get_date_from_meta(
345345

346346
return out_date
347347

348-
def get_description_or_abstract(self, in_page: Page, chars_count: int = 160) -> str:
348+
def get_description_or_abstract(
349+
self, in_page: Page, chars_count: int = 160, abstract_delimiter: str = None
350+
) -> str:
349351
"""Returns description from page meta. If it doesn't exist, use the \
350352
{chars_count} first characters from page content (in markdown).
351353
352354
:param Page in_page: page to look at
353355
:param int chars_count: if page.meta.description is not set, number of chars \
354356
of the content to use. Defaults to: 160 - optional
357+
:param str abstract_delimiter: description delimeter, defaults to None
355358
356359
:return: page description to use
357360
:rtype: str
358361
"""
362+
359363
description = in_page.meta.get("description")
360364

361365
# Set chars_count to None if it is set to be unlimited, for slicing.
@@ -374,8 +378,19 @@ def get_description_or_abstract(self, in_page: Page, chars_count: int = 160) ->
374378
"because an item must have a description."
375379
)
376380
return ""
381+
elif (
382+
abstract_delimiter
383+
and (
384+
excerpt_separator_position := in_page.markdown.find(abstract_delimiter)
385+
)
386+
> -1
387+
):
388+
return markdown.markdown(
389+
in_page.markdown[:excerpt_separator_position],
390+
output_format="html5",
391+
)
377392
# If chars count is unlimited, use the html content
378-
elif in_page.content and chars_count is None:
393+
elif in_page.content and chars_count == -1:
379394
if chars_count is None or len(in_page.content) < chars_count:
380395
return in_page.content[:chars_count]
381396
# Use markdown
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
date: 2023-02-12
3+
authors: [guts]
4+
categories:
5+
- Blog
6+
---
7+
8+
# Blog sample
9+
10+
I'm a really short intro.
11+
12+
<!-- more -->
13+
14+
## This part won't show up in RSS feed
15+
16+
### What is Lorem Ipsum?
17+
18+
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

tests/fixtures/docs/page_with_meta_bad_date.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ date: 2022-03-30 15:09:34
33
description: Test page with meta keys as singular and values as iterable
44
---
55

6-
# Test page with meta keys as singular
6+
# Test page with meta keys as singular but bad date
77

88
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

tests/fixtures/docs/page_with_meta_singulars.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@ tags:
1414
# Test page with meta keys as singular
1515

1616
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
17-
18-

tests/test_build.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,13 @@ def test_simple_build_item_length_unlimited(self):
322322
self.assertEqual(feed_parsed.bozo, 0)
323323

324324
for feed_item in feed_parsed.entries:
325-
if feed_item.title not in ("Page without meta with short text",):
326-
self.assertGreaterEqual(len(feed_item.description), 150)
325+
if feed_item.title not in (
326+
"Page without meta with short text",
327+
"Blog sample",
328+
):
329+
self.assertGreaterEqual(
330+
len(feed_item.description), 150, feed_item.title
331+
)
327332

328333
def test_simple_build_pretty_print_enabled(self):
329334
with tempfile.TemporaryDirectory() as tmpdirname:

tests/test_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def test_plugin_config_defaults(self):
5757
# default reference
5858
expected = {
5959
"abstract_chars_count": 160,
60+
"abstract_delimiter": "<!-- more -->",
6061
"categories": None,
6162
"comments_path": None,
6263
"date_from_meta": None,
@@ -80,6 +81,7 @@ def test_plugin_config_image(self):
8081
# reference
8182
expected = {
8283
"abstract_chars_count": 160,
84+
"abstract_delimiter": "<!-- more -->",
8385
"categories": None,
8486
"comments_path": None,
8587
"date_from_meta": None,

0 commit comments

Comments
 (0)