From a3a514eedcc576136faafee281c2c5519bb77004 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sat, 21 Dec 2019 16:20:12 -0500 Subject: [PATCH] parse links into markdown --- README.md | 2 ++ src/twit2blogpkg/twitter.nim | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0b5628f..7df2b00 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,5 @@ A simple tool to help make it easier to turn your tweet rants into real blog pos ### Running Requires `TWITTER_CONSUMER_KEY` and `TWITTER_CONSUMER_SECRET`, both of which you can only get if you have a registered developer account and an application created for twitter. + +Example: `/twit2blog -t:1234 -u:alice | pandoc --from=markdown --to=html > thread.html` diff --git a/src/twit2blogpkg/twitter.nim b/src/twit2blogpkg/twitter.nim index 01db36e..2c85a63 100644 --- a/src/twit2blogpkg/twitter.nim +++ b/src/twit2blogpkg/twitter.nim @@ -65,14 +65,23 @@ proc getThread*(tweetStart : string, user : string) : seq[string] = else: return nextTweetID.getThread(user) -proc stripAts(tweet : string) : string = +proc convertWords(tweet : string) : string = let words = tweet.split(" ") var stripped : seq[string] for word in words: - if word[0] != '@': + if word.len > 3 and word[0..3] == "http": + let parsedUri = word.parseUri + let scheme = parsedUri.scheme + let hostname = parsedUri.hostname + let path = parsedUri.path + if (scheme.len > 0 and hostname.len > 0): + stripped &= fmt"[{scheme}://{hostname}{path}]({scheme}://{hostname}{path})" + elif word[0] != '@': stripped &= word + else: + continue stripped.join(" ") proc renderThread*(tweetID : string, user : string) : string = - let thread = tweetID.getThread(user).map(stripAts).map(capitalizeAscii).join("\n\n") + let thread = tweetID.getThread(user).map(convertWords).map(capitalizeAscii).join("\n\n") fmt"### By {user}" & "\n" & fmt"{thread}"