From 7ecd6404ea465c547f3217016c1940f94682fb96 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 3 Dec 2014 15:24:15 +0000 Subject: Add comments to sx--unindent-text --- sx.el | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index f1d3634..3010bdc 100644 --- a/sx.el +++ b/sx.el @@ -189,24 +189,31 @@ Anything before the (sub)domain is removed." "" url))) (defun sx--unindent-text (text) - "Remove indentation from TEXT." + "Remove indentation from TEXT. +Primarily designed to extract the content of markdown code +blocks." (with-temp-buffer (insert text) (goto-char (point-min)) (let (result) + ;; Get indentation of each non-blank line (while (null (eobp)) (skip-chars-forward "[:blank:]") (unless (looking-at "$") (push (current-column) result)) (forward-line 1)) (when result + ;; Build a regexp with the smallest indentation (let ((rx (format "^ \\{0,%s\\}" (apply #'min result)))) (goto-char (point-min)) + ;; Use this regexp to remove that much indentation + ;; throughout the buffer. (while (and (null (eobp)) (search-forward-regexp rx nil 'noerror)) (replace-match "") (forward-line 1))))) + ;; Return the buffer (buffer-string))) -- cgit v1.2.3 From 034430657c658e712de5ce8b827cf63ff45ca6e3 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 3 Dec 2014 15:28:49 +0000 Subject: sx--site gracefully accepts nil argument --- sx.el | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 3010bdc..431643c 100644 --- a/sx.el +++ b/sx.el @@ -269,11 +269,10 @@ with a `link' property). DATA can also be the link itself." (let ((link (if (stringp data) data (cdr (assoc 'link data))))) - (unless (stringp link) - (error "Data has no link property")) - (replace-regexp-in-string - "^https?://\\(?:\\(?1:[^/]+\\)\\.stackexchange\\|\\(?2:[^/]+\\)\\)\\.[^.]+/.*$" - "\\1\\2" link))) + (when (stringp link) + (replace-regexp-in-string + "^https?://\\(?:\\(?1:[^/]+\\)\\.stackexchange\\|\\(?2:[^/]+\\)\\)\\.[^.]+/.*$" + "\\1\\2" link)))) (defun sx--deep-dot-search (data) "Find symbols somewhere inside DATA which start with a `.'. -- cgit v1.2.3