Elisp: Make 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)
 <ruby class=\"ruby88\">abc <rt>xyz</rt></ruby>

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

URL `http://ergoemacs.org/emacs/elisp_html-ruby-annotation-markup.html'
Version 2017-01-11"
    (if (null @begin)
          (if (use-region-p)
                (setq @begin (region-beginning))
                (setq @end (region-end)))
              (setq @begin (line-beginning-position))
              (setq @end (line-end-position)))))
        (setq @begin (line-beginning-position))
        (setq @end (line-end-position))))
      (narrow-to-region @begin @end)
        (goto-char (point-min))
        (while (search-forward "(" nil "NOERROR")
          (replace-match "<rt>")))
        (goto-char (point-min))
        (while (search-forward ")" nil "NOERROR")
          (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

Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.