[英]Detecting all links in markdown files in python and replace them with outputs of string function
我有一個 python function f(foo: string) -> string
。 我不寫 function 的細節,因為它可能會改變。
我需要從 markdown 文件中獲取所有鏈接,並將它們替換為 function 的結果。
示例:此鏈接
This is a text and this [is first link](http://example.com "Example Title") and
this [is a second](#example) link.
將替換為
This is a text and this [is first link](result1 "Example Title") and
this [is a second](result2) link.
其中f(http://example.com)=result1
和f(#example)=result2
。 也就是說result1
是f(http://example.com)
的 output , result2
是f(#example)
的 output 。
我們可以在 python 正則表達式或某些特定的 package 中執行 markdown 文件嗎?
修改mreinhart對此問題的響應,可以這樣做:
def find_md_links(md):
"""Returns dict of links in markdown:
'regular': [foo](some.url)
'footnotes': [foo][3]
[3]: some.url
"""
# https://stackoverflow.com/a/30738268/2755116
INLINE_LINK_RE = re.compile(r'\[([^\]]+)\]\(([^)]+)\)')
FOOTNOTE_LINK_TEXT_RE = re.compile(r'\[([^\]]+)\]\[(\d+)\]')
FOOTNOTE_LINK_URL_RE = re.compile(r'\[(\d+)\]:\s+(\S+)')
links = list(INLINE_LINK_RE.findall(md))
footnote_links = dict(FOOTNOTE_LINK_TEXT_RE.findall(md))
footnote_urls = dict(FOOTNOTE_LINK_URL_RE.findall(md))
footnotes_linking = []
for key in footnote_links.keys():
footnotes_linking.append((footnote_links[key], footnote_urls[footnote_links[key]]))
return {'regular': links, 'footnotes': footnotes_linking}
def replace_md_links(md, f):
"""Replace links url to f(url)"""
links = find_md_links(md)
newmd = md
for r in links['regular']:
newmd = newmd.replace(r[1], f(r[1]))
for r in links['footnotes']:
newmd = newmd.replace(r[1], f(r[1]))
return newmd
f
是 function。 例如,我使用這個 function 只更改屬於#
in replace_md_links
的鏈接
def mychange(s, prefix="/static/entrades/", suffix=".md.html"):
"""Change links from tiddlywiki syntax [foo](#something) to [foo](prefix + something + suffix)"""
if s.startswith('#'):
return prefix + slugify.slugify(urllib.parse.unquote( s.replace('#', '', 1) )) + suffix
else:
return s
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.