aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--[-rwxr-xr-x]osurl52
1 files changed, 37 insertions, 15 deletions
diff --git a/osurl b/osurl
index 2deb753..303d45f 100755..100644
--- a/osurl
+++ b/osurl
@@ -1,19 +1,41 @@
-#!/usr/bin/luajit
-
local lpeg = require 'lpeg'
-local P,C,Cs,R = lpeg.P, lpeg.C, lpeg.Cs, lpeg.R
+local P,C,Cs,Cf,R = lpeg.P, lpeg.C, lpeg.Cs, lpeg.Cf, lpeg.R
+
+local tokens = {
+ NONHASH = C((1 - P("#"))^1), -- any nonempty string not containing #
+ NUM = C(R("09")^1), -- nonempty digit string
+ WORD = C(R("AZ","az","09","__")^1) -- nonempty word
+}
+local patterns = {
+ ["bgo#%NUM"] = "http://bugzilla.gnome.org/show_bug.cgi?id=%1",
+ ["bko#%NUM"] = "http://bugzilla.kernel.org/show_bug.cgi?id=%1",
+ ["bmo#%NUM"] = "http://bugzilla.mozilla.org/show_bug.cgi?id=%1",
+ ["boo#%NUM"] = "http://bugzilla.opensuse.org/show_bug.cgi?id=%1",
+ ["bsc#%NUM"] = "http://bugzilla.suse.com/show_bug.cgi?id=%1",
+ ["gh#%NONHASH#%WORD"] = "https://github.com/%1/issues/%2",
+ ["lp#%NUM"] = "https://launchpad.net/bugs/%1",
+ ["rh#%NUM"] = "http://bugzilla.redhat.com/show_bug.cgi?id=%1"
+}
+
+-- turn "foo#%BLAH" into a pattern that matches everything literally except
+-- %BLAH
+local makepat = Cf(
+ (
+ ((P("%") * C(R("AZ")^1)) / tokens) -- %BLAH is from tokens
+ + (P"%%" / function() return P"%" end) -- %% treat as literal %
+ + (C(P(1)) / P) -- anything else is literal
+ )^0,
+ function(a,b) return a * b end)
+local function make_pattern(str)
+ return assert(makepat:match(str), "invalid pattern")
+end
-local cnum = C(R("09")^1)
-local cword = C(R("AZ","az","09","__")^1)
-local bugpat =
- (P"bgo#" * cnum) / "http://bugzilla.gnome.org/show_bug.cgi?id=%1"
- + (P"bko#" * cnum) / "http://bugzilla.kernel.org/show_bug.cgi?id=%1"
- + (P"bmo#" * cnum) / "http://bugzilla.mozilla.org/show_bug.cgi?id=%1"
- + (P"boo#" * cnum) / "http://bugzilla.opensuse.org/show_bug.cgi?id=%1"
- + (P"bsc#" * cnum) / "http://bugzilla.suse.com/show_bug.cgi?id=%1"
- + (P"gh#" * C((1 - P"#")^1) * P"#" * cword) / "https://github.com/%1/issues/%2"
- + (P"lp#" * cnum) / "https://launchpad.net/bugs/%1"
- + (P"rh#" * cnum) / "http://bugzilla.redhat.com/show_bug.cgi?id=%1"
+local bugpat = P(false)
+for k,v in pairs(patterns) do
+ bugpat = bugpat + (make_pattern(k) / v)
+end
local pat = Cs((bugpat + P(1))^0)
-os.execute("xdg-open " .. pat:match(arg[1]))
+print(pat:match("gh#python/cpython#123"))
+print(pat:match("lp#123456"))
+print(pat:match("bramboříček"))