Skip to content

Commit

Permalink
Merge pull request #44 from Bastian-Krause/bst/feed-absolute-urls
Browse files Browse the repository at this point in the history
plugins/feeds: make relative URLs absolute
  • Loading branch information
SmithChart committed Oct 24, 2023
2 parents 8a15c31 + 8d4d2ef commit 1411092
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions flamingo/plugins/feeds.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import logging
from urllib.parse import urljoin

from bs4 import BeautifulSoup
from feedgen.feed import FeedGenerator

logger = logging.getLogger('flamingo.plugins.Feeds')


def make_urls_absolute(html, base_url):
soup = BeautifulSoup(html, 'html.parser')

for element in soup.find_all(['img', 'script', 'asciinema-player']):
try:
element['src'] = urljoin(base_url, element['src'])
except KeyError:
# not all elements might have a src attribute
continue

for link in soup.find_all('a'):
try:
link['href'] = urljoin(base_url, link['href'])
except KeyError:
# not all elements might have an href attribute
continue

return str(soup)


class Feeds:
def pre_build(self, context):
FEEDS_DOMAIN = getattr(context.settings, 'FEEDS_DOMAIN', '/')
Expand Down Expand Up @@ -122,6 +144,8 @@ def pre_build(self, context):

if i['content_body']:
exitcode, output = context.pre_render(i)
output = make_urls_absolute(output, fe_link['href'])

if 'html_filter' in feed_config:
output = feed_config['html_filter'](output)
fe.content(output, type='html')
Expand Down

0 comments on commit 1411092

Please sign in to comment.