aboutsummaryrefslogtreecommitdiff
path: root/sx.el
diff options
context:
space:
mode:
authorArtur Malabarba <bruce.connor.am@gmail.com>2014-12-27 15:28:59 -0200
committerArtur Malabarba <bruce.connor.am@gmail.com>2014-12-27 15:28:59 -0200
commit30e86cfce4bf8829e55beda91c1630f76b3338a9 (patch)
treecb972c7ab57c8ad4d711326efd29927a5ceda310 /sx.el
parent6fe85d66a7a519b9fc19db49b6404cbaa804ca5d (diff)
parent57976619d5bf17b6b822a4e0159dee4aab673b33 (diff)
Merge pull request #173 from vermiculus/visit-question-from-link
Visit question from link
Diffstat (limited to 'sx.el')
-rw-r--r--sx.el60
1 files changed, 59 insertions, 1 deletions
diff --git a/sx.el b/sx.el
index a31c0a0..508de46 100644
--- a/sx.el
+++ b/sx.el
@@ -61,7 +61,16 @@ DATA can also be the link itself."
(cdr (assoc 'link data)))))
(when (stringp link)
(replace-regexp-in-string
- "^https?://\\(?:\\(?1:[^/]+\\)\\.stackexchange\\|\\(?2:[^/]+\\)\\)\\.[^.]+/.*$"
+ (rx string-start
+ "http" (optional "s") "://"
+ (or
+ (sequence
+ (group-n 1 (+ (not (any "/"))))
+ ".stackexchange")
+ (group-n 2 (+ (not (any "/")))))
+ "." (+ (not (any ".")))
+ "/" (* any)
+ string-end)
"\\1\\2" link))))
(defun sx--ensure-site (data)
@@ -74,6 +83,54 @@ with a `link' property)."
(cdr data))))
data))
+(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/"
+ ;; Question ID
+ (group (+ digit))
+ ;; User ID
+ "/" (+ digit)
+ ;; Answer ID
+ (group (or (sequence "#" (* any)) ""))
+ string-end) link)
+ (string-match
+ ;; From URL
+ (rx "/questions/" (+ digit) "/"
+ (+ (not (any "/"))) "/"
+ ;; User ID
+ (optional (group (+ digit)))
+ (optional "/")
+ (group (or (sequence "#" (* any)) ""))
+ string-end) link))
+ (push '(type . answer) result))
+ ;; Question
+ (and (or (string-match
+ ;; From 'Share' button
+ (rx "/q/"
+ ;; Question ID
+ (group (+ digit))
+ ;; User ID
+ (optional "/" (+ digit))
+ ;; Answer or Comment ID
+ (group (or (sequence "#" (* any)) ""))
+ string-end) link)
+ (string-match
+ ;; From URL
+ (rx "/questions/"
+ ;; Question ID
+ (group (+ digit))
+ "/") link))
+ (push '(type . question) result)))
+ (push (cons 'id (string-to-number (match-string-no-properties 1 link)))
+ result))
+ result))
+
(defmacro sx-assoc-let (alist &rest body)
"Identical to `let-alist', except `.site' has a special meaning.
If ALIST doesn't have a `site' property, one is created using the
@@ -318,6 +375,7 @@ removed from the display name before it is returned."
(format "[%s]" (car kar)) (cdr kar) string)))
string))
+
(defcustom sx-init-hook nil
"Hook run when SX initializes.
Run after `sx-init--internal-hook'."