From ccc94f4a8dcf92bdf3adacbdbf97c18b55fa0f8d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexander=20Steinb=C3=B6ck?= Date: Wed, 18 Feb 2009 19:18:12 +0000 Subject: [PATCH] grml-etc-core: Rewrite of function zurl(). Because of changes in the HTML source code of TinyURL, zurl() wasn't working properly anymore. For instance, it's now 'copy(http://tinyurl.com/7efkze)' instead of 'value="(http://tinyurl.com/7efkze)'. The function however is now just using zsh in-house means and tries to provide some decent output. Including TinyURL's new preview feature. --- etc/zsh/zshrc | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index 734a697..fde07a3 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -3922,17 +3922,30 @@ if check_com -c highlight ; then compdef _hl_complete hl fi -# create small urls via tinyurl.com using wget, grep and sed -zurl() { - [[ -z ${1} ]] && print "please give an url to shrink." && return 1 - local url=${1} - local tiny="http://tinyurl.com/create.php?url=" - #print "${tiny}${url}" ; return - wget -O- \ - -o/dev/null \ - "${tiny}${url}" \ - | grep -Eio 'value="(http://tinyurl.com/.*)"' \ - | sed 's/value=//;s/"//g' +# Create small urls via http://tinyurl.com using wget(1). +function zurl() { + [[ -z ${1} ]] && { print "USAGE: zurl " ; return 1 } + + local PN url tiny grabber search result preview + PN=${0} + url=${1} +# Prepend 'http://' to given URL where necessary for later output. + [[ ${url} != http(s|)://* ]] && url='http://'${url} + tiny='http://tinyurl.com/create.php?url=' + if check_com -c wget ; then + grabber='wget -O- -o/dev/null' + else + print "wget is not available, but mandatory for ${PN}. Aborting." + fi +# Looking for i.e.`copy('http://tinyurl.com/7efkze')' in TinyURL's HTML code. + search='copy\(?http://tinyurl.com/[[:alnum:]]##*' + result=${(M)${${${(f)"$(${=grabber} ${tiny}${url})"}[(fr)${search}*]}//[()\';]/}%%http:*} +# TinyURL provides the rather new feature preview for more confidence. + preview='http://preview.'${result#http://} + + printf '%s\n\n' "${PN} - Shrinking long URLs via webservice TinyURL ." + printf '%s\t%s\n\n' 'Given URL:' ${url} + printf '%s\t%s\n\t\t%s\n' 'TinyURL:' ${result} ${preview} } #f2# Print a specific line of file(s). -- 2.1.4