diff --git a/example/Makefile b/example/Makefile index 1775778..380cfe0 100644 --- a/example/Makefile +++ b/example/Makefile @@ -21,7 +21,8 @@ settings: .PHONY: clean clean: - rm ./pages/*.html - rm ./tags/*/*.html - rm ./webring/*.html - rmdir ./pages ./tags/* ./tags ./webring + - rm -f ./pages/*.html + - rm -f ./tags/*/*.html + - rm -f ./webring/*.html + - rm -f ./feeds/*.xml + - rmdir ./pages ./tags/* ./tags ./webring ./feeds diff --git a/example/settings.toml b/example/settings.toml index b519867..79c5f46 100644 --- a/example/settings.toml +++ b/example/settings.toml @@ -85,3 +85,4 @@ title="Your Microblog Title Here" title_tagged = "#{__tagname__} on {__title__}" url = "https://yourdomain.tld/microblog/" description = "Your description here." +trailing_punctuation = ".,)!?>];:" diff --git a/src/microblog.py b/src/microblog.py index 51aff70..03c7309 100644 --- a/src/microblog.py +++ b/src/microblog.py @@ -549,6 +549,7 @@ if __name__ == "__main__": updated = [] updated += writepage(tpl, tl, tc, cfg["page"], paginate=True if new_posts is None else False) + cfg["rss"]["accepted_images"] = cfg["post"]["accepted_images"] if cfg["rss"]["enabled"]: # ensure output directory for feeds exists if not os.path.exists("feeds"): diff --git a/src/rss.py b/src/rss.py index c10aa32..8922523 100644 --- a/src/rss.py +++ b/src/rss.py @@ -11,7 +11,7 @@ except ImportError: print("\trss disabled - missing dependency", file=sys.stderr) print("\tYour microblog still may have been generated.", file=sys.stderr) -def line2words(lines, limit): +def line2words(lines, limit, trailing_punctuation): output = [] char_count = 0 break_outer_loop = False @@ -29,15 +29,37 @@ def line2words(lines, limit): # last char of last word last_char = output[-1].strip()[-1] # print(output[-1], file=sys.stderr) - punctuation = [".", ")", ",", "!", "?", ">", ']'] - if last_char not in punctuation: + if last_char not in trailing_punctuation: output.append("...") return output +def _is_image_token(token: str, extensions): + parts = token.rsplit('.', 1) + return len(parts) == 2 and parts[1] in extensions + # this is similar to the markup function in microblog -def enrich_msg(lines, is_atom=True): +def enrich_msg( + lines, + is_atom=True, + accepted_images=[], + base_url="", + trailing_punctuation="", + desc_len_limit=-1, +): if not is_atom: - return lines + from urllib.parse import urljoin + words = line2words(lines, desc_len_limit, trailing_punctuation) + for i in range(len(words)): + token = words[i] + core = token.rstrip(trailing_punctuation) + suffix = token[len(core):] + if len(core) == 0 or "<" in core or ">" in core: + continue + if _is_image_token(core, accepted_images): + abs_url = urljoin(base_url, core) + anchor = f"{escape(abs_url)}" + words[i] = anchor + suffix + return words content = [] parser = My_Html_Parser([]) for line in lines: @@ -95,12 +117,17 @@ def write_feed(posts, filename, params, tagname=None): # len of post.message is number of lines msg = post.message ti = " ".join( - line2words(msg,TITLE_LEN_LIMIT)) - if params["use_atom"]: - de = " ".join(enrich_msg(msg)) - else: - de = " ".join( - line2words(msg,DESC_LEN_LIMIT)) + line2words(msg, TITLE_LEN_LIMIT, params["trailing_punctuation"])) + de = " ".join( + enrich_msg( + msg, + is_atom=params["use_atom"], + base_url=params["url"], + accepted_images=params["accepted_images"], + trailing_punctuation=params["trailing_punctuation"], + desc_len_limit=DESC_LEN_LIMIT, + ) + ) li = base_url + ("#%i" % post.num) p = dateutil.parser.parse(post.timestamp) if params["use_atom"]: