bdewey/org-ruby — pull request #32 by vonavi (2012-09-28 17:27:56+00:00)
Title: Correct parsing of org-mode emphasis

The code follows Emacs' org-mode parsing of emphasized text as close as possible. It relies on Emacs' org-mode code, which I enclose with slight modifications. I combine regexp `org-emph-re`
``` scheme
(setq org-emph-re
(concat "\\([" pre "]\\|^\\)"
"\\("
"\\([" markers "]\\)"
"\\("
"[^" border "]\\|"
"[^" border "]"
body1
"[^" border "]"
"\\)"
"\\3\\)"
"\\([" post "]\\|$\\)"))
```
with emphazing procedure in `org-html.el`
``` scheme
(defun org-export-html-convert-emphasize (string)
"Apply emphasis."
(let ((s 0) rpl)
(while (string-match org-emph-re string s)
(if (not (equal
;; Return preceding marker
(substring string (match-beginning 3) (1+ (match-beginning 3)))
;; Return char after preceding marker
(substring string (match-beginning 4) (1+ (match-beginning 4)))))

;; If preceding marker and the following char are different
;; Set position `s' to the regexp beginning
(setq s (match-beginning 0)
rpl
(concat
;; Leave pre_emphasis unchanged
(match-string 1 string)
;; Replace preceding marker by the corresponding open tag
(nth 2 (assoc (match-string 3 string) org-emphasis-alist))
;; Leave body unchanged
(match-string 4 string)
;; Replace posterior marker by the corresponding close tag
(nth 3 (assoc (match-string 3 string)
org-emphasis-alist))
;; Leave post_emphasis unchanged
(match-string 5 string))
;; Incorporate changes into the primary string
string (replace-match rpl t t string)
;; Adjust position `s' to the next regexp search
s (+ s (- (length rpl) 2)))

;; If preceding marker and the following char are the same
;; Do nothing, go forward
(setq s (1+ s))))
string))
```
Newlines within emphasis follows from
``` scheme
(setq e org-emphasis-regexp-components)
(setq body (nth 3 e))
(setq nl (nth 4 e))
(setq body1 (concat body "*?"))

(if (> nl 0)
(setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
(int-to-string nl) "\\}")))
```