From 7ce3968e25c8c7a076f83f0b0d9d3b3b7d8afadc Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sat, 24 Jan 2015 04:00:46 -0200 Subject: Handle the `code` html tag --- sx-question-print.el | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/sx-question-print.el b/sx-question-print.el index c10c43e..5be3133 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -369,12 +369,46 @@ E.g.: (skip-chars-forward "\r\n[:blank:]") (forward-paragraph) (let ((end (point-marker))) + ;; Compact links. + (sx-question-mode--process-html-tags beg end) ;; Compact links. (sx-question-mode--process-links beg end) (goto-char end)) (fill-region beg (point))))) (replace-regexp-in-string "[[:blank:]]+\\'" "" (buffer-string)))) + +;;; HTML tags +(defconst sx-question-mode--html-tag-regexp + (rx "<" (group-n 1 "%s") (* (not (any ">"))) ">")) + +(defun sx-question-mode--process-html-tags (beg end) + "Hide all html tags between BEG and END and possibly interpret them. +END should be a marker." + ;; This code understands nested html, but not if the same tag is + ;; nested in itself (e.g., ). + (goto-char beg) + (while (search-forward-regexp + (format sx-question-mode--html-tag-regexp "[[:alpha:]]+") + end 'noerror) + (unless (save-match-data (markdown-code-at-point-p)) + (let ((tag (match-string 1)) + (l (match-beginning 0))) + (replace-match "") + (when (search-forward-regexp + (format sx-question-mode--html-tag-regexp (concat "/" tag)) + ;; Searching for a match has no bounds. + nil 'noerror) + (let ((r (copy-marker (match-beginning 0)))) + ;; The code tag is special, because it quotes everything in + ;; the middle. + (if (string= tag "quote") + (progn (replace-match "`") + (save-excursion (goto-char l) (insert "`"))) + (replace-match "") + ;; Handle stuff between the two tags. + (save-match-data (sx-question-mode--process-html-tags l r))))))))) + ;;; Handling links (defun sx-question-mode--process-links (beg end) -- cgit v1.2.3