Browse Source

Do not add links to/from H2 headings in feed

master
David Larlet 11 months ago
parent
commit
c5a10f2911
No known key found for this signature in database
2 changed files with 277 additions and 146 deletions
  1. 262
    131
      david/log/index.xml
  2. 15
    15
      site.py

+ 262
- 131
david/log/index.xml
File diff suppressed because it is too large
View File


+ 15
- 15
site.py View File

@@ -28,7 +28,7 @@ NORMALIZED_STRFTIME = "%Y-%m-%dT12:00:00+01:00"
TODAY = datetime.today() + timedelta(hours=6)


class CustomHTMLRenderer(mistune.HTMLRenderer):
class H2AnchorsHTMLRenderer(mistune.HTMLRenderer):
def heading(self, text, level):
# Set an anchor to h2 headings.
if level == 2:
@@ -44,9 +44,17 @@ class CustomHTMLRenderer(mistune.HTMLRenderer):
return super().heading(text, level)


# We want a custom renderer to create a hash/link for each H2 headings.
markdown_with_h2_anchors = mistune.create_markdown(
renderer=H2AnchorsHTMLRenderer(escape=False), plugins=[DirectiveInclude()]
)
# The second markdown is pertinent to generate articles for the feed,
# we do not need anchors in that case.
markdown = mistune.create_markdown(
renderer=CustomHTMLRenderer(escape=False), plugins=[DirectiveInclude()]
renderer=mistune.HTMLRenderer(escape=False), plugins=[DirectiveInclude()]
)

# This is the jinja2 configuration to locate templates.
environment = Env(loader=FileSystemLoader(str(DAVID / "templates")))


@@ -102,11 +110,13 @@ class Page:
return self.date < other.date

@staticmethod
def all(source: Path, only_published=True):
def all(source: Path, only_published=True, with_h2_anchors=True):
"""Retrieve all pages sorted by desc."""
page_list = []
md = markdown_with_h2_anchors if with_h2_anchors else markdown
for file_path in each_markdown_from(source):
result = markdown.read(file_path)
result = md.read(file_path)
# Extract (and remove) the title from the generated page.
title, content = result.split("</h1>", 1)
h1_opening_size = len("<h1>")
title = title[h1_opening_size:]
@@ -177,23 +187,13 @@ def feed():
"""Generate a feed from last published items."""
template = environment.get_template("feed.xml")
content = template.render(
page_list=Page.all(source=DAVID / "2020"),
page_list=Page.all(source=DAVID / "2020", with_h2_anchors=False),
current_dt=TODAY.strftime(NORMALIZED_STRFTIME),
BASE_URL=f"{DOMAIN}/david/",
)
open(DAVID / "log" / "index.xml", "w").write(content)


@cli
def mastodon():
"""Generate an activity feed from last published items."""
page_list = Page.all(source=DAVID / "2020")
current_dt = TODAY.strftime(NORMALIZED_STRFTIME)
BASE_URL = f"{DOMAIN}/david/"
content = {}
open(DAVID / "mastodon" / "outbox_content.json", "w").write(json.dumps(content))


@wrap
def perf_wrapper():
start = perf_counter()

Loading…
Cancel
Save