Skip to content

Commit

Permalink
Merge pull request #88 from liang2kl/main
Browse files Browse the repository at this point in the history
Provide unlimited abstract length option
  • Loading branch information
Guts authored Sep 14, 2021
2 parents 65db2c2 + 2dc2364 commit f396f10
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Guts/mkdocs-rss-plugin/master.svg)](https://results.pre-commit.ci/latest/github/Guts/mkdocs-rss-plugin/master)
[![Documentation Status](https://readthedocs.org/projects/mkdocs-rss-plugin/badge/?version=latest)](https://mkdocs-rss-plugin.readthedocs.io/en/latest/?badge=latest)
[![📚 Documentation](https://github.com/Guts/mkdocs-rss-plugin/actions/workflows/documentation.yml/badge.svg)](https://github.com/Guts/mkdocs-rss-plugin/actions/workflows/documentation.yml)

A plugin for [MkDocs](https://www.mkdocs.org), the static site generator, which creates [RSS 2.0](https://wikipedia.org/wiki/RSS) feeds using the creation and modification dates from [git log](https://git-scm.com/docs/git-log) and page metadata ([YAML frontmatter](https://www.mkdocs.org/user-guide/writing-your-docs/#yaml-style-meta-data)).

Expand All @@ -36,7 +36,7 @@ Full options:
```yaml
plugins:
- rss:
abstract_chars_count: 160
abstract_chars_count: 160 # -1 for full content
comments_path: "#__comments"
date_from_meta:
as_creation: "date"
Expand Down
6 changes: 4 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ Output:

### Item description length

To fill each [item description element](https://www.w3schools.com/xml/rss_tag_title_link_description_item.asp), the plugin first tries to retrieve the value of the keyword `description` from the [page metadata].
To fill each [item description element](https://www.w3schools.com/xml/rss_tag_title_link_description_item.asp):

If the page has no meta, then the plugin retrieves the first number of characters of the page content defined by this setting. Retrieved content is the raw markdown converted rougthly into HTML.
- If this value is set to `-1`, then the articles' full HTML content will be filled into the description element.
- Otherwise, the plugin first tries to retrieve the value of the keyword `description` from the [page metadata].
- If the value is non-negative and no `description` meta is found, then the plugin retrieves the first number of characters of the page content defined by this setting. Retrieved content is the raw markdown converted rougthly into HTML.

`abstract_chars_count`: number of characters to use as item description.

Expand Down
19 changes: 9 additions & 10 deletions mkdocs_rss_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,25 @@ def on_config(self, config: config_options.Config) -> dict:
# ending event
return config

def on_page_markdown(
self, markdown: str, page: Page, config: config_options.Config, files
def on_page_content(
self, html: str, page: Page, config: config_options.Config, files
) -> str:
"""The page_markdown event is called after the page's markdown is loaded
from file and can be used to alter the Markdown source text.
The meta- data has been stripped off and is available as page.meta
at this point.
"""The page_content event is called after the Markdown text is rendered
to HTML (but before being passed to a template) and can be used to alter
the HTML body of the page.
https://www.mkdocs.org/user-guide/plugins/#on_page_markdown
https://www.mkdocs.org/user-guide/plugins/#on_page_content
:param markdown: Markdown source text of page as strin
:type markdown: str
:param html: HTML rendered from Markdown source as string
:type html: str
:param page: mkdocs.nav.Page instance
:type page: Page
:param config: global configuration object
:type config: config_options.Config
:param files: global navigation object
:type files: [type]
:return: Markdown source text of page as strin
:return: HTML rendered from Markdown source as string
:rtype: str
"""
# skip pages that don't match the config var match_path
Expand Down
36 changes: 20 additions & 16 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,31 +258,34 @@ def get_date_from_meta(

return out_date.timestamp()

def get_description_or_abstract(self, in_page: Page, chars_count: int = 150) -> str:
def get_description_or_abstract(self, in_page: Page, chars_count: int = 160) -> str:
"""Returns description from page meta. If it doesn't exist, use the \
{chars_count} first characters from page content (in markdown).
:param Page in_page: page to look at
:param int chars_count: if page.meta.description is not set, number of chars \
of the content to use. Defaults to: 150 - optional
of the content to use. Defaults to: 160 - optional
:return: page description to use
:rtype: str
"""
if in_page.meta.get("description"):
return in_page.meta.get("description")
elif in_page.content:
if len(in_page.content) < chars_count:
return markdown.markdown(
in_page.content[:chars_count], output_format="html5"
)
else:
return markdown.markdown(
"{}...".format(in_page.content[: chars_count - 3]),
output_format="html5",
)
description = in_page.meta.get("description")

# Set chars_count to None if it is set to be unlimited, for slicing.
if chars_count < 0:
chars_count = None

# If the abstract chars is not unlimited and the description exists,
# return the description.
if description and chars_count != None:
return description
# If chars count is unlimited, use the html content
elif in_page.content and chars_count == None:
if chars_count == None or len(in_page.content) < chars_count:
return in_page.content[:chars_count]
# Use markdown
elif in_page.markdown:
if len(in_page.markdown) < chars_count:
if chars_count == None or len(in_page.markdown) < chars_count:
return markdown.markdown(
in_page.markdown[:chars_count], output_format="html5"
)
Expand All @@ -291,8 +294,9 @@ def get_description_or_abstract(self, in_page: Page, chars_count: int = 150) ->
"{}...".format(in_page.markdown[: chars_count - 3]),
output_format="html5",
)
# Unlimited chars_count but no content is found, then return the description.
else:
return ""
return description if description else ""

def get_image(self, in_page: Page, base_url: str) -> tuple:
"""Get image from page meta and returns properties.
Expand Down

0 comments on commit f396f10

Please sign in to comment.