Emacs: HTML Ruby Annotation 🚀

By Xah Lee. Date: . Last updated: .

Here's a command to turn text into a HTML ruby annotation markup.

(defun xah-html-markup-ruby (&optional Begin End)
  "Wrap HTML ruby annotation tag on current line or selection.
Chars inside paren are wrapped with “rt” tag.
For example
 abc (xyz)
becomes
 <ruby class=\"ruby88\">abc <rt>xyz</rt></ruby>

When called in lisp code, Begin End are region begin/end positions.

URL `http://xahlee.info/emacs/emacs/elisp_html-ruby-annotation-markup.html'
Created: 2017-01-11
Version: 2017-01-11"
  (interactive)
  (progn
    (if (and Begin End)
        (progn
          (setq Begin (line-beginning-position))
          (setq End (line-end-position)))
      (progn
        (if (region-active-p)
            (progn
              (setq Begin (region-beginning))
              (setq End (region-end)))
          (progn
            (setq Begin (line-beginning-position))
            (setq End (line-end-position))))))
    (save-restriction
      (narrow-to-region Begin End)
      (progn
        (goto-char (point-min))
        (while (search-forward "(" nil :move)
          (replace-match "<rt>")))
      (progn
        (goto-char (point-min))
        (while (search-forward ")" nil :move)
          (replace-match "</rt>")))
      (goto-char (point-min))
      (insert "<ruby class=\"ruby88\">")
      (goto-char (point-max))
      (insert "</ruby>"))))

in Emacs: Xah HTML Mode 📦

see also HTML “ruby” Annotation Example