Detecting all links in markdown files in python and replace them with outputs of string function

后端 未结 1 908
一生所求
一生所求 2021-01-07 13:12

I have a python function f(foo: string) -> string. I don\'t write the details of the function because it could be change.

I need to get all l

相关标签:
1条回答
  • 2021-01-07 13:53

    Modifying mreinhart response to this question, this could be done:

    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 is a function. For example, I use this function which only changes links which belong from # 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
    
    0 讨论(0)
提交回复
热议问题