Verified Commit 2914a74b authored by Fabio Manganiello's avatar Fabio Manganiello
Browse files

Replace relative links in converted markdown

parent 1e1bf46f
Pipeline #112 passed with stages
in 6 minutes and 50 seconds
import datetime
import json
import os
import re
import subprocess
import tempfile
from urllib.parse import urlparse
from platypush.plugins import action
from platypush.plugins.http.request import Plugin
......@@ -29,6 +31,12 @@ class HttpWebpagePlugin(Plugin):
with subprocess.Popen(proc, stdout=subprocess.PIPE, stderr=None) as parser:
return parser.communicate()[0].decode()
@staticmethod
def _fix_relative_links(markdown: str, url: str) -> str:
url = urlparse(url)
base_url = f'{url.scheme}://{url.netloc}'
return re.sub(r'(!?\[.+?])\((/.+?)\)', f'\1({base_url}\2)', markdown)
# noinspection PyShadowingBuiltins
@action
def simplify(self, url, type='html', html=None, outfile=None):
......@@ -101,6 +109,9 @@ class HttpWebpagePlugin(Plugin):
except Exception as e:
raise RuntimeError('Could not parse JSON: {}. Response: {}'.format(str(e), response))
if type == 'markdown':
self._fix_relative_links(response['content'], url)
self.logger.debug('Got response from Mercury API: {}'.format(response))
title = response.get('title', '{} on {}'.format(
'Published' if response.get('date_published') else 'Generated',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment