Convert URLs into HTML links using sed?

后端 未结 5 1347
一生所求
一生所求 2021-01-06 03:45

I\'m wondering if it\'s possible (recommended might be the better word) to use sed to convert URLs into HTML hyperlinks in a document. Therefore, it would look for things li

5条回答
  •  心在旅途
    2021-01-06 04:12

    While you could use sed, and I will typically only use sed if I need something that's write-only (that is, it only needs to work and doesn't need to be maintained).

    I find the Python regular expression library to be more accessible (and gives the ability to add more powerful constructs).

    import re
    import sys
    
    def href_repl(matcher):
        "replace the matched URL with a hyperlink"
        # here you could analyze the URL further and make exceptions, etc
        #  to how you did the substitution. For now, do a simple
        #  substitution.
        href = matcher.group(0)
        return '{href}'.format(**vars())
    
    text = open(sys.argv[1]).read()
    url_pattern = re.compile(re.escape('http://') + '[^ ]*')
    sys.stdout.write(url_pattern.sub(href_repl, text))
    

    Personally, I find that much easier to read and maintain.

提交回复
热议问题