Created
August 27, 2020 07:46
-
-
Save olpaw/d95b35925fdfc62fb2ae8b7e286c9ff1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import fileinput | |
import re | |
import sys | |
from pathlib import Path | |
from urllib.parse import urlparse, urlunparse | |
parser = argparse.ArgumentParser(description='Make relative links in markup file absolute with given base-url.') | |
parser.add_argument('base_url', type=str, help='the base-url used to make relative links absolute to') | |
parser.add_argument('md_file', type=str, nargs='+', help='path to markup file') | |
args = parser.parse_args() | |
base_url = urlparse(args.base_url) | |
allowed_schemes = ['http', 'https'] | |
if base_url.scheme not in allowed_schemes: | |
sys.exit('Please use a base_url the following kind: ' + ', '.join(allowed_schemes)) | |
base_url_path = Path(base_url.path) | |
link_pattern = re.compile(r'\[.*?\]\((.+?)\)') | |
nop_path = Path('.') | |
report = [] | |
for md_file in args.md_file: | |
with fileinput.FileInput(md_file, inplace=True) as file: | |
for line in file: | |
def replace(match): | |
try: | |
matchstr = match.group(0) | |
link = match.group(1) | |
url = urlparse(link) | |
if not url.hostname and not url.scheme and not url.fragment: | |
rel_path = Path(url.path) | |
if not rel_path.is_absolute() and rel_path != nop_path and not rel_path.exists(): | |
scheme, netloc, _, params, query, fragment = base_url | |
path = str((base_url_path / rel_path).resolve(strict=False)) | |
fixed_link = urlunparse((scheme, netloc, path, params, query, fragment)) | |
report.append((md_file, f'{matchstr} ==> {fixed_link}')) | |
return matchstr.replace(link, fixed_link) | |
except Exception as ex: | |
report.append((md_file, ex)) | |
return matchstr | |
fixed_line = link_pattern.sub(replace, line.rstrip('\n')) | |
print(fixed_line) | |
for md_file, info in report: | |
sink = sys.stderr if isinstance(info, Exception) else sys.stdout | |
print(f'{md_file}: {info}', file=sink) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment