~wrycode/plr

69a8106cbab51716222e3612c5012fcf6ccb13c1 — Nick Econopouly 5 months ago f9e4bbb
Add --list option
1 files changed, 11 insertions(+), 8 deletions(-)

M plr.py
M plr.py => plr.py +11 -8
@@ 13,7 13,7 @@ def parseargs():
    parser.add_argument('-o', '--output-file', type=argparse.FileType('w'),
                        default=sys.stdout, action='store', dest='output',
                        help='specify an output file (default: stdout)')
    parser.add_argument('-l', action='store_true',
    parser.add_argument('-l', '--list', action='store_true',
                        help='output a list of archive links instead of replacing them \
                        in the text')
    parser.add_argument('urls', nargs=argparse.REMAINDER)


@@ 29,13 29,13 @@ def main():
    else:
        writeoutput = print

    # read the input from extra command-line args, stdin, or a file
    if len(args.urls) != 0 :
        # Ugly workaround because the regex is written for markdown links
        content ='\n'.join(args.urls)
    content = args.input.read()
    if args.l:
        writeoutput(replaceURLs(content, True))
    else:
        content = args.input.read()
    writeoutput(replaceLinks(content))
        writeoutput(replaceURLs(content, False))

        

def isurl(s):
    if re.match(r'(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))', s):


@@ 59,7 59,7 @@ def getWebArchiveLink(url):
    except:
      return url

def replaceLinks(text):
def replaceURLs(text, l):
    urls = []
    # Regex needs a rewrite:
    for url in re.findall(r'(https?://[^\s]+)', text):


@@ 70,6 70,9 @@ def replaceLinks(text):
    newurls = []
    for url in urls:
        newurls.append(getWebArchiveLink(url))
    if l:
        return '\n'.join(newurls)
    else:
        curPos = 0
        for url, newurl in zip(urls,newurls):
            if url in text[curPos:]: