Replace relative links in converted markdown

This commit is contained in:
Fabio Manganiello 2021-11-27 01:19:55 +01:00
parent 1e1bf46f32
commit 2914a74b75
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 11 additions and 0 deletions

View File

@ -1,8 +1,10 @@
import datetime import datetime
import json import json
import os import os
import re
import subprocess import subprocess
import tempfile import tempfile
from urllib.parse import urlparse
from platypush.plugins import action from platypush.plugins import action
from platypush.plugins.http.request import Plugin 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: with subprocess.Popen(proc, stdout=subprocess.PIPE, stderr=None) as parser:
return parser.communicate()[0].decode() 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 # noinspection PyShadowingBuiltins
@action @action
def simplify(self, url, type='html', html=None, outfile=None): def simplify(self, url, type='html', html=None, outfile=None):
@ -101,6 +109,9 @@ class HttpWebpagePlugin(Plugin):
except Exception as e: except Exception as e:
raise RuntimeError('Could not parse JSON: {}. Response: {}'.format(str(e), response)) 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)) self.logger.debug('Got response from Mercury API: {}'.format(response))
title = response.get('title', '{} on {}'.format( title = response.get('title', '{} on {}'.format(
'Published' if response.get('date_published') else 'Generated', 'Published' if response.get('date_published') else 'Generated',