~ecs/public-inbox

store bangs in a hashmap v1 APPLIED

Evan Johnston: 1
 store bangs in a hashmap

 1 files changed, 11 insertions(+), 21 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.d2evs.net/~ecs/public-inbox/patches/14/mbox | git am -3
Learn more about email & git

[PATCH] store bangs in a hashmap Export this patch

---
 bin/urlfilter | 32 +++++++++++---------------------
 1 file changed, 11 insertions(+), 21 deletions(-)

diff --git a/bin/urlfilter b/bin/urlfilter
index f445309..093d76a 100755
--- a/bin/urlfilter
@@ -2,7 +2,9 @@
BEGIN {
	for (i = 0; i <= 255; i++)
		ord[sprintf("%c", i)] = i
	bangs = ENVIRON["XDG_DATA_HOME"] "/bangs"
	bangsfile = ENVIRON["XDG_DATA_HOME"] "/bangs"
	while (getline <bangsfile) bangs[$1] = $2
	close(bangsfile)
}
function escape(str, c, len, res) {
	len = length(str)
@@ -68,6 +70,14 @@ function unescape(str, c, len, res) {
	$0 = "https://sci-hub.ru/" unescape($0)
}

# Bangs
/^!/ {
	search = $0
	sub(/^[^ ]* /, "", search);
	$0 = bangs[$1]
	sub(/\{\{\{s\}\}\}/, escape(search))
}

# Open protocol:... directly
/^[a-zA-Z][a-zA-Z0-9+.-]*:[^ ]*$/ { print $0; system(""); next }

@@ -80,25 +90,5 @@ function unescape(str, c, len, res) {
# IPv4 Addresses
/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[:\/].*$/ { print "http://" $0; next }

# Bangs
/^!/ {
	saved = $0
	provider = $1
	sub(/^[^ ]* /, "");
	search = $0
	while (getline <bangs) {
		if ($1 == provider) {
			close(bangs)
			url = $2
			sub(/\{\{\{s\}\}\}/, escape(search), url)
			print url
			system("")
			next
		}
	}
	close(bangs)
	$0 = saved
}

# Open everything else as a search
{ print "https://duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion/html/?q=" escape($0); system(""); next }
-- 
2.37.1
Thanks!

To git@git.d2evs.net:~ecs/dotfiles
   2b7a77f..90f5180  master -> master