Make internal helper functions private
Added underscore prefix to functions used only internally: - _strip_tags_and_truncate (used only in parse_post_file) - _parse_headers (used only in parse_post_file) - _rfc_2822_format (used only in parse_post_file) - _make_links_absolute (used only in parse_post_file) - _strip_html_tags (used only in parse_post_file) - _get_friendly_date (used only in _setup_page_params) - _exit_program (used only in monitor.py main loop) This improves encapsulation by clearly marking implementation details.
This commit is contained in:
parent
424ea20bcf
commit
13d3d653de
2 changed files with 16 additions and 16 deletions
26
makesite.py
26
makesite.py
|
|
@ -71,12 +71,12 @@ def log(msg, *args):
|
|||
sys.stderr.write(msg.format(*args) + "\n")
|
||||
|
||||
|
||||
def strip_tags_and_truncate(text, words=25):
|
||||
def _strip_tags_and_truncate(text, words=25):
|
||||
"""Remove HTML tags and truncate text to the specified number of words."""
|
||||
return " ".join(re.sub(r"(?s)<.*?>", " ", text).split()[:words])
|
||||
|
||||
|
||||
def parse_headers(text):
|
||||
def _parse_headers(text):
|
||||
"""Parse HTML comment headers and yield (key, value, end-index) tuples."""
|
||||
for match in re.finditer(r"\s*<!--\s*(.+?)\s*:\s*(.+?)\s*-->\s*|.+", text):
|
||||
if not match.group(1):
|
||||
|
|
@ -84,7 +84,7 @@ def parse_headers(text):
|
|||
yield match.group(1), match.group(2), match.end()
|
||||
|
||||
|
||||
def rfc_2822_format(date_str):
|
||||
def _rfc_2822_format(date_str):
|
||||
"""Convert yyyy-mm-dd date string to RFC 2822 format date string."""
|
||||
d = datetime.datetime.strptime(date_str, "%Y-%m-%d")
|
||||
return d \
|
||||
|
|
@ -119,7 +119,7 @@ def parse_post_file(filename, params):
|
|||
|
||||
# Read headers.
|
||||
end = 0
|
||||
for key, val, end in parse_headers(text):
|
||||
for key, val, end in _parse_headers(text):
|
||||
content[key] = val
|
||||
|
||||
# slugify post title
|
||||
|
|
@ -132,20 +132,20 @@ def parse_post_file(filename, params):
|
|||
if filename.endswith((".md", ".mkd", ".mkdn", ".mdown", ".markdown")):
|
||||
summary_index = text.find("<!-- more")
|
||||
if summary_index > 0:
|
||||
summary = markdown(strip_html_tags(text[:summary_index]))
|
||||
summary = markdown(_strip_html_tags(text[:summary_index]))
|
||||
else:
|
||||
summary = strip_tags_and_truncate(markdown(strip_html_tags(text)))
|
||||
summary = _strip_tags_and_truncate(markdown(_strip_html_tags(text)))
|
||||
clean_text = text.replace("<!-- more -->", "")
|
||||
text = markdown(clean_text)
|
||||
else:
|
||||
summary = strip_tags_and_truncate(text)
|
||||
summary = _strip_tags_and_truncate(text)
|
||||
|
||||
# Update the dictionary with content and RFC 2822 date.
|
||||
content.update(
|
||||
{
|
||||
"content": text,
|
||||
"content_rss": make_links_absolute(params["site_url"], text),
|
||||
"rfc_2822_date": rfc_2822_format(content["date"]),
|
||||
"content_rss": _make_links_absolute(params["site_url"], text),
|
||||
"rfc_2822_date": _rfc_2822_format(content["date"]),
|
||||
"summary": summary,
|
||||
}
|
||||
)
|
||||
|
|
@ -153,7 +153,7 @@ def parse_post_file(filename, params):
|
|||
return content
|
||||
|
||||
|
||||
def make_links_absolute(site_url, text):
|
||||
def _make_links_absolute(site_url, text):
|
||||
"""Convert relative links to absolute URLs for RSS feed."""
|
||||
# TODO externalize links replacement configuration
|
||||
return text \
|
||||
|
|
@ -161,7 +161,7 @@ def make_links_absolute(site_url, text):
|
|||
.replace("href=\"/20", "href=\"" + site_url + "/20")
|
||||
|
||||
|
||||
def strip_html_tags(text):
|
||||
def _strip_html_tags(text):
|
||||
"""Remove HTML tags from text."""
|
||||
while True:
|
||||
original_text = text
|
||||
|
|
@ -285,7 +285,7 @@ def _process_comments(page_params, stacosys_url, comment_layout,
|
|||
return len(comments), comments_html, comment_section_html
|
||||
|
||||
|
||||
def get_friendly_date(date_str):
|
||||
def _get_friendly_date(date_str):
|
||||
"""Convert date string to French-formatted readable date.
|
||||
|
||||
Args:
|
||||
|
|
@ -334,7 +334,7 @@ def _setup_page_params(content, params):
|
|||
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["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"] + "/"
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ def get_comment_count():
|
|||
return 0 if not resp.ok else int(resp.json()["count"])
|
||||
|
||||
|
||||
def exit_program():
|
||||
def _exit_program():
|
||||
"""Exit the program with status code 0."""
|
||||
sys.exit(0)
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ while True:
|
|||
for _ in range(15):
|
||||
time.sleep(60)
|
||||
if initial_count != get_comment_count():
|
||||
exit_program()
|
||||
_exit_program()
|
||||
# check if git repo changed every 15 minutes
|
||||
if external_check_cmd and os.system(external_check_cmd):
|
||||
exit_program()
|
||||
_exit_program()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue