aboutsummaryrefslogtreecommitdiff
path: root/sx.el
diff options
context:
space:
mode:
authorSean Allred <code@seanallred.com>2014-12-30 17:47:42 -0500
committerSean Allred <code@seanallred.com>2014-12-30 17:47:42 -0500
commitb20f56b0c59b1e99fac745e408369d01de0f0bb5 (patch)
treefa30f72a8fbe94e5b319c9163b1d70d8768ad65e /sx.el
parentc215e84da4dcfa63c7a0c05996cc131e031efe64 (diff)
parent1267f300c850173e74dda0b7f704261b4a25b85c (diff)
Merge branch 'master' into issue-151--dot-variables
Conflicts: sx.el
Diffstat (limited to 'sx.el')
-rw-r--r--sx.el93
1 files changed, 58 insertions, 35 deletions
diff --git a/sx.el b/sx.el
index d93719e..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
@@ -319,40 +376,6 @@ removed from the display name before it is returned."
string))
-;;; Site
-(defun sx--site (data)
- "Get the site in which DATA belongs.
-DATA can be a question, answer, comment, or user (or any object
-with a `link' property).
-DATA can also be the link itself."
- (let ((link (if (stringp data) data
- (cdr (assoc 'link data)))))
- (when (stringp link)
- (replace-regexp-in-string
- "^https?://\\(?:\\(?1:[^/]+\\)\\.stackexchange\\|\\(?2:[^/]+\\)\\)\\.[^.]+/.*$"
- "\\1\\2" link))))
-
-(defun sx--ensure-site (data)
- "Add a `site' property to DATA if it doesn't have one. Return DATA.
-DATA can be a question, answer, comment, or user (or any object
-with a `link' property)."
- (when data
- (unless (assq 'site data)
- (setcdr data (cons (cons 'site (sx--site data))
- (cdr data))))
- data))
-
-(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
-`link' property."
- (declare (indent 1) (debug t))
- (require 'let-alist)
- `(progn
- (sx--ensure-site ,alist)
- ,(macroexpand
- `(let-alist ,alist ,@body))))
-
(defcustom sx-init-hook nil
"Hook run when SX initializes.
Run after `sx-init--internal-hook'."