Created
June 4, 2018 12:23
-
-
Save cyrusn/efdcf18d994fc4a1c3c462dd03dbd1cb to your computer and use it in GitHub Desktop.
[markdown] convert reference link to inline link vice versa
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
import sys | |
import re | |
from math import log, floor | |
query = sys.argv[1] | |
# query = """ | |
# - [Riverbank | Software | PyQt | What is PyQt?](https://riverbankcomputing.com/software/pyqt/intro) | |
# - [![image](https://www.riverbankcomputing.com/@@/resources/logo.png)](https://riverbankcomputing.com/software/pyqt/intro) | |
# """ | |
def convertInlineLinkToReferenceLink(content): | |
MARKDOWN_LINK_PATTERN = r"\[\!\[([^\[]+?)\]\(([^\#].+?)\)\]\(([^\#].+?)\)|\[([^\[]+?)\]\(([^\#].+?)\)" | |
archors = [] | |
index = 0 | |
mdlinks = re.finditer(MARKDOWN_LINK_PATTERN, content) | |
lst = list(mdlinks) | |
size = len(lst) | |
for i, link in enumerate(lst): | |
index += 1 | |
pad = int(floor(log(max(size, 1), 10))) + 1 | |
if link.group(1): | |
formattedArchorImageString = "[@{:0{pad}d}-img]: {}".format( | |
index, link.group(2), pad=pad) | |
formattedArchorLinkString = "[@{:0{pad}d}-url]: {}".format( | |
index, link.group(3), pad=pad) | |
archors.append(formattedArchorImageString) | |
archors.append(formattedArchorLinkString) | |
formattedString = "[![{}][@{:0{pad}d}-img]][@{:0{pad}d}-url]".format( | |
link.group(1), index, index, pad=pad) | |
else: | |
formattedArchorString = "[@{:0{pad}d}]: {}".format( | |
index, link.group(5), pad=pad) | |
archors.append(formattedArchorString) | |
formattedString = "[{}][@{:0{pad}d}]".format( | |
link.group(4), index, pad=pad) | |
content = content.replace(link.group(0), formattedString) | |
content += "\n<!-- reference links -->\n\n" | |
for a in archors: | |
content += a + "\n" | |
return content | |
result = convertInlineLinkToReferenceLink(query) | |
sys.stdout.write(result) |
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
import sys | |
import re | |
query = sys.argv[1] | |
def convertReferenceLinkToInlineLink(content): | |
ARCHORS_PATTERN = r"\n\[(.+)\]\: (.+)" | |
links = re.finditer(ARCHORS_PATTERN, content, re.MULTILINE) | |
for i, l in enumerate(links): | |
referenceLinkPattern = '\[(.+)\]\[{}\]'.format(l.group(1)) | |
pattern = re.compile(referenceLinkPattern) | |
def replacer(obj): | |
return "[{}]({})".format(obj.group(1), l.group(2)) | |
content = re.sub(pattern, replacer, content) | |
content = l.re.sub('', content) | |
content = content.replace("\n<!-- reference links -->\n\n", "") | |
return content | |
result = convertReferenceLinkToInlineLink(query) | |
sys.stdout.write(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment