From e39a909dc722dfdb48ecc4533cf061dbb209abf1 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 17 Dec 2014 15:35:54 -0200 Subject: Implement identifying type, id, and site of a link. --- sx.el | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 4ad0fd5..7d67835 100644 --- a/sx.el +++ b/sx.el @@ -317,6 +317,22 @@ If ALIST doesn't have a `site' property, one is created using the (sx--ensure-site ,alist) (let-alist ,alist ,@body))) +(defun sx--link-to-data (link) + "Convert string LINK into data that can be displayed." + (let ((result (list (cons 'site (sx--site link))))) + (when (or + ;; Answer + (and (or (string-match "/a/\\([0-9]+\\)/[0-9]+\\(#.*\\|\\)\\'" link) + (string-match "/questions/[0-9]+/[^/]+/\\([0-9]\\)/?\\(#.*\\|\\)\\'" link)) + (push (cons 'type 'answer) result)) + ;; Question + (and (or (string-match "/q/\\([0-9]+\\)/[0-9]+\\(#.*\\|\\)\\'" link) + (string-match "/questions/\\([0-9]+\\)/" link)) + (push (cons 'type 'question) result))) + (push (cons 'id (string-to-number (match-string-no-properties 1 link))) + result)) + result)) + (defcustom sx-init-hook nil "Hook run when SX initializes. Run after `sx-init--internal-hook'." -- cgit v1.2.3 From 31a3e357261641228186692ab3a9ac0a053d197b Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 26 Dec 2014 16:40:30 -0500 Subject: Simpler syntax for quoted cons cells --- sx.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 7d67835..8913024 100644 --- a/sx.el +++ b/sx.el @@ -324,11 +324,11 @@ If ALIST doesn't have a `site' property, one is created using the ;; Answer (and (or (string-match "/a/\\([0-9]+\\)/[0-9]+\\(#.*\\|\\)\\'" link) (string-match "/questions/[0-9]+/[^/]+/\\([0-9]\\)/?\\(#.*\\|\\)\\'" link)) - (push (cons 'type 'answer) result)) + (push '(type . answer) result)) ;; Question (and (or (string-match "/q/\\([0-9]+\\)/[0-9]+\\(#.*\\|\\)\\'" link) (string-match "/questions/\\([0-9]+\\)/" link)) - (push (cons 'type 'question) result))) + (push '(type . question) result))) (push (cons 'id (string-to-number (match-string-no-properties 1 link))) result)) result)) -- cgit v1.2.3 From 0e54ca6ad3e4cf11b5512fadef39066e955e6281 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 26 Dec 2014 16:55:32 -0500 Subject: Use `rx' macro for some regular expressions --- sx.el | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 8913024..091526f 100644 --- a/sx.el +++ b/sx.el @@ -322,12 +322,29 @@ If ALIST doesn't have a `site' property, one is created using the (let ((result (list (cons 'site (sx--site link))))) (when (or ;; Answer - (and (or (string-match "/a/\\([0-9]+\\)/[0-9]+\\(#.*\\|\\)\\'" link) - (string-match "/questions/[0-9]+/[^/]+/\\([0-9]\\)/?\\(#.*\\|\\)\\'" link)) + (and (or (string-match + (rx "/a/" (group (1+ digit)) "/" + (1+ digit) + (group (or (sequence "#" (0+ any)) "")) + string-end) link) + (string-match + (rx "/questions/" (1+ digit) "/" + (1+ (not (any "/"))) "/" + (group digit) + (optional "/") + (group (or (sequence "#" (0+ any)) "")) + string-end) link)) (push '(type . answer) result)) ;; Question - (and (or (string-match "/q/\\([0-9]+\\)/[0-9]+\\(#.*\\|\\)\\'" link) - (string-match "/questions/\\([0-9]+\\)/" link)) + (and (or (string-match + (rx "/q/" + (group (1+ digit)) "/" + (1+ digit) + (group (or (sequence "#" (0+ any)) "")) + string-end) link) + (string-match + (rx "/questions/" + (group (1+ digit)) "/") link)) (push '(type . question) result))) (push (cons 'id (string-to-number (match-string-no-properties 1 link))) result)) -- cgit v1.2.3 From 60e483c5f6bfa5ea897f3bc6f85f402b09f63d9e Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 26 Dec 2014 17:21:26 -0500 Subject: Groups on their own lines Ideally, these groups would have explanations of what they capture. For now, the official stance is 'eh'. --- sx.el | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 091526f..7f6b901 100644 --- a/sx.el +++ b/sx.el @@ -323,7 +323,10 @@ If ALIST doesn't have a `site' property, one is created using the (when (or ;; Answer (and (or (string-match - (rx "/a/" (group (1+ digit)) "/" + (rx "/a/" + ;; Answer ID + (group (1+ digit)) + "/" (1+ digit) (group (or (sequence "#" (0+ any)) "")) string-end) link) @@ -344,7 +347,8 @@ If ALIST doesn't have a `site' property, one is created using the string-end) link) (string-match (rx "/questions/" - (group (1+ digit)) "/") link)) + (group (1+ digit)) + "/") link)) (push '(type . question) result))) (push (cons 'id (string-to-number (match-string-no-properties 1 link))) result)) -- cgit v1.2.3 From d732176007abdcc3395f7188dc918981d9ff2801 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 26 Dec 2014 17:22:03 -0500 Subject: `rx'-ify regular expression --- sx.el | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 7f6b901..19c5f12 100644 --- a/sx.el +++ b/sx.el @@ -294,7 +294,15 @@ DATA can also be the link itself." (cdr (assoc 'link data))))) (when (stringp link) (replace-regexp-in-string - "^https?://\\(?:\\(?1:[^/]+\\)\\.stackexchange\\|\\(?2:[^/]+\\)\\)\\.[^.]+/.*$" + (rx line-start "http" (optional "s") "://" + (or + (sequence + (group-n 1 (+ (not (any "/")))) + ".stackexchange") + (group-n 2 (+ (not (any "/"))))) + "." (+ (not (any "."))) + "/" (* any) + line-end) "\\1\\2" link)))) (defun sx--ensure-site (data) -- cgit v1.2.3 From 970acd6f7e5920ed6492a4a74e65eae9e29838b6 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 26 Dec 2014 17:34:37 -0500 Subject: Add some explanatory comments --- sx.el | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 19c5f12..c460f62 100644 --- a/sx.el +++ b/sx.el @@ -328,19 +328,24 @@ If ALIST doesn't have a `site' property, one is created using the (defun sx--link-to-data (link) "Convert string LINK into data that can be displayed." (let ((result (list (cons 'site (sx--site link))))) + ;; Try to strip a question or answer ID (when (or ;; Answer (and (or (string-match + ;; From 'Share' button (rx "/a/" - ;; Answer ID + ;; Question ID (group (1+ digit)) - "/" - (1+ digit) + ;; User ID + "/" (1+ digit) + ;; Answer ID (group (or (sequence "#" (0+ any)) "")) string-end) link) (string-match + ;; From URL (rx "/questions/" (1+ digit) "/" (1+ (not (any "/"))) "/" + ;; User ID (group digit) (optional "/") (group (or (sequence "#" (0+ any)) "")) @@ -348,13 +353,19 @@ If ALIST doesn't have a `site' property, one is created using the (push '(type . answer) result)) ;; Question (and (or (string-match + ;; From 'Share' button (rx "/q/" - (group (1+ digit)) "/" - (1+ digit) + ;; Question ID + (group (1+ digit)) + ;; User ID + "/" (1+ digit) + ;; Answer or Comment ID (group (or (sequence "#" (0+ any)) "")) string-end) link) (string-match + ;; From URL (rx "/questions/" + ;; Question ID (group (1+ digit)) "/") link)) (push '(type . question) result))) -- cgit v1.2.3 From b87861b6187ef4395e77e34882401f7fb28dfa26 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 26 Dec 2014 17:35:25 -0500 Subject: Make user IDs optional when parsing from link --- sx.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index c460f62..4ef6caf 100644 --- a/sx.el +++ b/sx.el @@ -346,7 +346,7 @@ If ALIST doesn't have a `site' property, one is created using the (rx "/questions/" (1+ digit) "/" (1+ (not (any "/"))) "/" ;; User ID - (group digit) + (optional (group digit)) (optional "/") (group (or (sequence "#" (0+ any)) "")) string-end) link)) @@ -358,7 +358,7 @@ If ALIST doesn't have a `site' property, one is created using the ;; Question ID (group (1+ digit)) ;; User ID - "/" (1+ digit) + (optional "/" (1+ digit)) ;; Answer or Comment ID (group (or (sequence "#" (0+ any)) "")) string-end) link) -- cgit v1.2.3 From a935ee7e5aa887f345b50aa4e922732e31157628 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 26 Dec 2014 17:35:51 -0500 Subject: User IDs are very often more than one digit --- sx.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 4ef6caf..d1c7633 100644 --- a/sx.el +++ b/sx.el @@ -346,7 +346,7 @@ If ALIST doesn't have a `site' property, one is created using the (rx "/questions/" (1+ digit) "/" (1+ (not (any "/"))) "/" ;; User ID - (optional (group digit)) + (optional (group (+ digit))) (optional "/") (group (or (sequence "#" (0+ any)) "")) string-end) link)) -- cgit v1.2.3 From 0354bf2c974b13967558187936918db4af125571 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 26 Dec 2014 17:37:17 -0500 Subject: Modify rx forms to be `rx-greedy-flag'-independent See `rx' documentation for details. --- sx.el | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index d1c7633..c9b5d76 100644 --- a/sx.el +++ b/sx.el @@ -335,20 +335,20 @@ If ALIST doesn't have a `site' property, one is created using the ;; From 'Share' button (rx "/a/" ;; Question ID - (group (1+ digit)) + (group (+ digit)) ;; User ID - "/" (1+ digit) + "/" (+ digit) ;; Answer ID - (group (or (sequence "#" (0+ any)) "")) + (group (or (sequence "#" (* any)) "")) string-end) link) (string-match ;; From URL - (rx "/questions/" (1+ digit) "/" - (1+ (not (any "/"))) "/" + (rx "/questions/" (+ digit) "/" + (+ (not (any "/"))) "/" ;; User ID (optional (group (+ digit))) (optional "/") - (group (or (sequence "#" (0+ any)) "")) + (group (or (sequence "#" (* any)) "")) string-end) link)) (push '(type . answer) result)) ;; Question @@ -356,17 +356,17 @@ If ALIST doesn't have a `site' property, one is created using the ;; From 'Share' button (rx "/q/" ;; Question ID - (group (1+ digit)) + (group (+ digit)) ;; User ID - (optional "/" (1+ digit)) + (optional "/" (+ digit)) ;; Answer or Comment ID - (group (or (sequence "#" (0+ any)) "")) + (group (or (sequence "#" (* any)) "")) string-end) link) (string-match ;; From URL (rx "/questions/" ;; Question ID - (group (1+ digit)) + (group (+ digit)) "/") link)) (push '(type . question) result))) (push (cons 'id (string-to-number (match-string-no-properties 1 link))) -- cgit v1.2.3 From 57976619d5bf17b6b822a4e0159dee4aab673b33 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 27 Dec 2014 00:01:34 -0500 Subject: Use string-start/-end instead of line-start/-end --- sx.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index c9fbf75..508de46 100644 --- a/sx.el +++ b/sx.el @@ -61,7 +61,8 @@ DATA can also be the link itself." (cdr (assoc 'link data))))) (when (stringp link) (replace-regexp-in-string - (rx line-start "http" (optional "s") "://" + (rx string-start + "http" (optional "s") "://" (or (sequence (group-n 1 (+ (not (any "/")))) @@ -69,7 +70,7 @@ DATA can also be the link itself." (group-n 2 (+ (not (any "/"))))) "." (+ (not (any "."))) "/" (* any) - line-end) + string-end) "\\1\\2" link)))) (defun sx--ensure-site (data) -- cgit v1.2.3