-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdlinks
executable file
·50 lines (36 loc) · 1.18 KB
/
mdlinks
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
#!/usr/bin/python
# /usr/local/bin/mdlinks:
# To generate markdown links syntax.
# Get/change from hk0i/dotfiles/bin/mdlinks.
import re
import sys, os
from sets import Set
document = ''
for line in sys.stdin:
document += line
link_pattern = re.compile(r'(\[([^\]]+)\]\[([^\]]*)\])', re.MULTILINE)
matches = link_pattern.findall(document)
document_links = Set()
if matches:
for match in matches:
link_title = match[1]
link_refname = match[2]
footer_text = ''
if link_refname:
footer_link = link_refname
else:
footer_link = link_title
document_links.add(footer_link)
# print "[{footer_link}]: ".format(footer_link = footer_link)
existing_link_pattern = re.compile(r'^\[([^\]]+)\]:', re.MULTILINE)
existing_links = existing_link_pattern.findall(document)
print document
if existing_links:
for link in existing_links:
if link in document_links:
document_links.discard(link)
# else:
# sys.stderr.write('WARNING: found unused link in document: ' \
# '[{link}]\n'.format(link = link))
for link in document_links:
print "[{link}]: ".format(link = link)