-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkextr.py
More file actions
98 lines (83 loc) · 3.3 KB
/
Copy pathlinkextr.py
File metadata and controls
98 lines (83 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
from urllib.parse import urlparse
from mistletoe import Document
from mistletoe.base_renderer import BaseRenderer
from mistletoe.span_token import AutoLink, Link, Image
from mistletoe.utils import traverse
import argparse
import glob
import os.path
import sys
def frontmatter_split(lines):
delimiter = "---"
i = 0
while i < len(lines) and not lines[i].strip():
i += 1
if i >= len(lines) or lines[i].rstrip() != delimiter:
return [], lines
start = i
i += 1
while i < len(lines) and lines[i].rstrip() != delimiter:
i += 1
if i >= len(lines):
return [], lines
end = i
return [line.rstrip() + "\n" for line in lines[start:end+1]], lines[end+1:]
def findlinks(lines, prefix=None, images=False, alluri=False):
links = set()
if images:
searchfor = (Link, AutoLink, Image)
else:
searchfor = (Link, AutoLink)
with BaseRenderer():
doc = Document(lines)
for result in traverse(doc, searchfor):
node = result[0]
if isinstance(node, AutoLink) and node.mailto:
continue
uri = node.src if isinstance(node, Image) else node.target
uri = urlparse(uri)._replace(fragment="")
if not uri.geturl():
continue
if not uri.scheme and uri.netloc:
uri = uri._replace(scheme="https")
if prefix and not uri.netloc and uri.path.startswith("/"):
uri = prefix.rstrip("/") + uri.path
elif uri.scheme != "mailto" and (uri.netloc or alluri):
uri = uri.geturl()
else:
continue
links.add(uri)
return links
def main(args):
parser = argparse.ArgumentParser(description="Extract links from Markdown files")
parser.add_argument("path", nargs="*", help="Directory or zero or more Markdown files to extract the links from (default: stdin)", metavar="dir | file(s)")
parser.add_argument("-o", "--output", help="File to write extracted links (default: stdout)")
parser.add_argument("-p", "--prefix", help="Add a prefix to the links that start with a forward slash")
parser.add_argument("-a", "--alluri", action="store_true", help="Extract all links, even if they don't start with http:// or https://")
parser.add_argument("-i", "--images", action="store_true", help="Extract URLs of images in addition to links")
args = parser.parse_args(args)
path, output = args.path, args.output
del args.path, args.output
if len(path) == 1 and os.path.isdir(path[0]):
path = glob.iglob(os.path.join(path[0], "**", "*.md"), recursive=True)
links = set()
args = args.__dict__
if not path:
lines = sys.stdin.readlines()
_, lines = frontmatter_split(lines)
links = findlinks(lines, **args)
else:
for file in path:
with open(file, "r", encoding="utf-8") as md:
lines = md.readlines()
_, lines = frontmatter_split(lines)
links |= findlinks(lines, **args)
result = [line + "\n" for line in sorted(links)]
if output:
with open(output, "w", encoding="utf-8") as out:
out.writelines(result)
else:
sys.stdout.writelines(result)
if __name__ == "__main__":
main(sys.argv[1:])