Refactor make_posts()
This commit is contained in:
parent
939a8708be
commit
9853ed4327
1 changed files with 142 additions and 59 deletions
201
makesite.py
201
makesite.py
|
|
@ -179,12 +179,141 @@ def get_header_list_value(header_name, page_params):
|
|||
return header_list
|
||||
|
||||
|
||||
def _render_comment(comment, comment_detail_layout):
|
||||
"""Render a single comment using the comment detail layout.
|
||||
|
||||
Args:
|
||||
comment: Dict with keys: author, content, date, and optional: site, avatar
|
||||
comment_detail_layout: Template string for rendering a comment
|
||||
|
||||
Returns:
|
||||
str: Rendered HTML for the comment
|
||||
"""
|
||||
site = comment.get("site", "")
|
||||
if site:
|
||||
site_start = '<a href="' + site + '">'
|
||||
site_end = '</a>'
|
||||
else:
|
||||
site_start = ''
|
||||
site_end = ''
|
||||
|
||||
return render(
|
||||
comment_detail_layout,
|
||||
author=comment["author"],
|
||||
avatar=comment.get("avatar", ""),
|
||||
site_start=site_start,
|
||||
site_end=site_end,
|
||||
date=comment["date"],
|
||||
content=markdown(comment["content"]),
|
||||
)
|
||||
|
||||
|
||||
def _fetch_comments(post_url, stacosys_url):
|
||||
"""Fetch comments from Stacosys API for a given post URL.
|
||||
|
||||
Args:
|
||||
post_url: Relative URL of the post (e.g., "2024/my-post/")
|
||||
stacosys_url: Base URL of the Stacosys comment service
|
||||
|
||||
Returns:
|
||||
list: List of comment dictionaries from API response
|
||||
"""
|
||||
req_url = stacosys_url + "/comments"
|
||||
query_params = dict(url="/" + post_url)
|
||||
resp = requests.get(url=req_url, params=query_params)
|
||||
return resp.json()["data"]
|
||||
|
||||
|
||||
def _process_comments(page_params, stacosys_url, comment_layout,
|
||||
comment_detail_layout):
|
||||
"""Process comments for a post: fetch, render, and return comment data.
|
||||
|
||||
Args:
|
||||
page_params: Dict containing page parameters (must have 'comment' and 'post_url')
|
||||
stacosys_url: Base URL of Stacosys service (empty string disables comments)
|
||||
comment_layout: Template for the overall comment section
|
||||
comment_detail_layout: Template for individual comments
|
||||
|
||||
Returns:
|
||||
tuple: (comment_count, comments_html, comment_section_html)
|
||||
- comment_count: Number of comments (int)
|
||||
- comments_html: Rendered HTML of all comments (str)
|
||||
- comment_section_html: Complete comment section with form (str)
|
||||
"""
|
||||
# Check if comments are enabled for this page
|
||||
page_comment = page_params.get("comment", "yes")
|
||||
is_page_comment_enabled = (page_comment != "no")
|
||||
|
||||
# Default values when comments are disabled
|
||||
if not stacosys_url or not is_page_comment_enabled:
|
||||
return 0, "", ""
|
||||
|
||||
# Fetch and render comments
|
||||
comments = _fetch_comments(page_params["post_url"], stacosys_url)
|
||||
out_comments = [
|
||||
_render_comment(comment, comment_detail_layout)
|
||||
for comment in comments
|
||||
]
|
||||
comments_html = "".join(out_comments)
|
||||
|
||||
# Render complete comment section
|
||||
temp_params = dict(page_params)
|
||||
temp_params["comments"] = comments_html
|
||||
temp_params["comment_count"] = len(comments)
|
||||
comment_section_html = render(comment_layout, **temp_params)
|
||||
|
||||
return len(comments), comments_html, comment_section_html
|
||||
|
||||
|
||||
def get_friendly_date(date_str):
|
||||
dt = datetime.datetime.strptime(date_str, "%Y-%m-%d")
|
||||
french_month = FRENCH_MONTHS[dt.month - 1]
|
||||
return f"{dt.day:02d} {french_month} {dt.year}"
|
||||
|
||||
|
||||
def _process_categories(page_params, category_layout):
|
||||
"""Process categories from page params and return rendered category label.
|
||||
|
||||
Args:
|
||||
page_params: Dict containing page parameters (must have 'category' key)
|
||||
category_layout: Template string for rendering individual categories
|
||||
|
||||
Returns:
|
||||
tuple: (list of category strings, rendered category label HTML)
|
||||
"""
|
||||
categories = get_header_list_value("category", page_params)
|
||||
out_cats = []
|
||||
for category in categories:
|
||||
out_cat = render(category_layout,
|
||||
category=category,
|
||||
url=slugify(category))
|
||||
out_cats.append(out_cat.strip())
|
||||
category_label = "".join(out_cats)
|
||||
return categories, category_label
|
||||
|
||||
|
||||
def _setup_page_params(content, params):
|
||||
"""Set up page parameters from content and global params.
|
||||
|
||||
Args:
|
||||
content: Dict containing parsed content (must have 'date' key)
|
||||
params: Global parameters dict
|
||||
|
||||
Returns:
|
||||
dict: Page parameters with date_path, friendly_date, year, post_url, etc.
|
||||
"""
|
||||
page_params = dict(params, **content)
|
||||
page_params["header"] = ""
|
||||
page_params["footer"] = ""
|
||||
page_params["date_path"] = page_params["date"].replace("-", "/")
|
||||
page_params["friendly_date"] = get_friendly_date(page_params["date"])
|
||||
page_params["year"] = page_params["date"].split("-")[0]
|
||||
page_params["post_url"] = (
|
||||
page_params["year"] + "/" + page_params["slug"] + "/"
|
||||
)
|
||||
return page_params
|
||||
|
||||
|
||||
def make_posts(
|
||||
src, src_pattern, dst, layout, category_layout,
|
||||
comment_layout, comment_detail_layout, **params
|
||||
|
|
@ -200,72 +329,26 @@ def make_posts(
|
|||
content["content"] = render(content["content"], **params)
|
||||
content["summary"] = render(content["summary"], **params)
|
||||
|
||||
page_params = dict(params, **content)
|
||||
page_params["header"] = ""
|
||||
page_params["footer"] = ""
|
||||
page_params["date_path"] = page_params["date"].replace("-", "/")
|
||||
page_params["friendly_date"] = get_friendly_date(page_params["date"])
|
||||
page_params["year"] = page_params["date"].split("-")[0]
|
||||
page_params["post_url"] = \
|
||||
page_params["year"] \
|
||||
+ "/" \
|
||||
+ page_params["slug"] + "/"
|
||||
# setup page parameters
|
||||
page_params = _setup_page_params(content, params)
|
||||
|
||||
# categories
|
||||
categories = get_header_list_value("category", page_params)
|
||||
out_cats = []
|
||||
for category in categories:
|
||||
out_cat = render(category_layout,
|
||||
category=category,
|
||||
url=slugify(category))
|
||||
out_cats.append(out_cat.strip())
|
||||
# process categories
|
||||
categories, category_label = _process_categories(page_params, category_layout)
|
||||
page_params["categories"] = categories
|
||||
page_params["category_label"] = "".join(out_cats)
|
||||
page_params["category_label"] = category_label
|
||||
|
||||
# tags
|
||||
tags = get_header_list_value("tag", page_params)
|
||||
page_params["tags"] = tags
|
||||
|
||||
# comments
|
||||
page_comment = page_params.get("comment", "yes")
|
||||
if page_comment == "no":
|
||||
is_page_comment_enabled = False
|
||||
else:
|
||||
is_page_comment_enabled = True
|
||||
|
||||
page_params["comment_count"] = 0
|
||||
page_params["comments"] = ""
|
||||
page_params["comment"] = ""
|
||||
|
||||
if params["stacosys_url"] and is_page_comment_enabled:
|
||||
req_url = params["stacosys_url"] + "/comments"
|
||||
query_params = dict(
|
||||
url="/" + page_params["post_url"]
|
||||
)
|
||||
resp = requests.get(url=req_url, params=query_params)
|
||||
comments = resp.json()["data"]
|
||||
out_comments = []
|
||||
for comment in comments:
|
||||
site = comment.get("site", "")
|
||||
if site:
|
||||
site_start = '<a href="' + site + '">'
|
||||
site_end = '</a>'
|
||||
else:
|
||||
site_start = ''
|
||||
site_end = ''
|
||||
out_comment = render(
|
||||
comment_detail_layout,
|
||||
author=comment["author"],
|
||||
avatar=comment.get("avatar", ""),
|
||||
site_start=site_start,
|
||||
site_end=site_end,
|
||||
date=comment["date"],
|
||||
content=markdown(comment["content"]),
|
||||
)
|
||||
out_comments.append(out_comment)
|
||||
page_params["comments"] = "".join(out_comments)
|
||||
page_params["comment_count"] = len(comments)
|
||||
page_params["comment"] = render(comment_layout, **page_params)
|
||||
# comments
|
||||
comment_count, comments_html, comment_section = _process_comments(
|
||||
page_params, params.get("stacosys_url", ""),
|
||||
comment_layout, comment_detail_layout
|
||||
)
|
||||
page_params["comment_count"] = comment_count
|
||||
page_params["comments"] = comments_html
|
||||
page_params["comment"] = comment_section
|
||||
|
||||
content["year"] = page_params["year"]
|
||||
content["post_url"] = page_params["post_url"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue