Skip to content

Commit

Permalink
use fully qualified URLs for images in atom feed (#218)
Browse files Browse the repository at this point in the history
* use fully qualified URLs for images in atom feed

* fix comment capitalization

Co-authored-by: Nabil Freij <nabil.freij@gmail.com>

---------

Co-authored-by: Nabil Freij <nabil.freij@gmail.com>
  • Loading branch information
lexming and nabobalis authored Apr 11, 2023
1 parent 09b264b commit d14fd78
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/ablog/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,25 @@ def page_url(self, pagename):
return url


def html_builder_write_doc(self, docname, doctree):
def html_builder_write_doc(self, docname, doctree, img_url=False):
"""
Part of :meth:`sphinx.builders.html.StandaloneHTMLBuilder.write_doc` method
used to convert *doctree* to HTML.
Extra argument `img_url` enables conversion of `<img>` source paths to
fully qualified URLs based on `blog_baseurl`.
"""
# Source of images
img_folder = "_images"
if img_url and self.config["blog_baseurl"]:
img_src_path = urljoin(self.config["blog_baseurl"], img_folder)
else:
img_src_path = relative_uri(self.get_target_uri(docname), img_folder)

destination = StringOutput(encoding="utf-8")
doctree.settings = self.docsettings
self.secnumbers = {}
self.imgpath = relative_uri(self.get_target_uri(docname), "_images")
self.imgpath = img_src_path
self.dlpath = relative_uri(self.get_target_uri(docname), "_downloads")
self.current_docname = docname
self.docwriter.write(doctree, destination)
Expand Down Expand Up @@ -377,14 +387,17 @@ def __init__(self, blog, docname, info):
def __lt__(self, other):
return (self._computed_date, self.title) < (other._computed_date, other.title)

def to_html(self, pagename, fulltext=False, drop_h1=True):
def to_html(self, pagename, fulltext=False, drop_h1=True, img_url=False):
"""
Return excerpt or *fulltext* as HTML after resolving references with
respect to *pagename*.
By default, first `<h1>` tag is dropped from the output. More
than one can be dropped by setting *drop_h1* to the desired
number of tags to be dropped.
`img_url` enables conversion of `<img>` source paths to fully
qualified URLs based on `blog_baseurl`.
"""

doctree = new_document("")
Expand All @@ -404,7 +417,7 @@ def to_html(self, pagename, fulltext=False, drop_h1=True):
revise_pending_xrefs(doctree, pagename)
app.env.resolve_references(doctree, pagename, app.builder)
add_permalinks, app.builder.add_permalinks = (app.builder.add_permalinks, False)
html = html_builder_write_doc(app.builder, pagename, doctree)
html = html_builder_write_doc(app.builder, pagename, doctree, img_url=img_url)
app.builder.add_permalinks = add_permalinks
if drop_h1:
html = re.sub("<h1>(.*?)</h1>", "", html, count=abs(int(drop_h1)))
Expand Down
2 changes: 1 addition & 1 deletion src/ablog/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def generate_atom_feeds(app):
if blog.blog_feed_titles:
content = None
else:
content = post.to_html(pagename, fulltext=feed_fulltext)
content = post.to_html(pagename, fulltext=feed_fulltext, img_url=True)
feed_entry = feed.add_entry(order="append")
feed_entry.id(post_url)
feed_entry.link(href=post_url)
Expand Down

0 comments on commit d14fd78

Please sign in to comment.