2012-08-17

Enhance htmlize.el , now can export org-link.

Several Days ago, I found a problem when write blog using org-mode.
The 'html-region command in htmlize.el, just generate a CSS style for org-link,  not a real link.

So I went to great stackoverflow.com:
 http://stackoverflow.com/questions/11937649/in-emacs-how-to-export-links-to-a-clickable-link-when-htmlize-emacs-buffer


BACKGROUND
  1. I using great htmlize.el to export my org-mode buffer contents with font hi-lock.
  2. Emacs org-mode has a Link format.
PROBLEM
For Example, here is a org-mode file with contents:
[[http://hunmr.blogspot.com][blog]]
When I Using Htmlize.el to htmlize buffer to HTML contents, The link was missing. produces HTML like:
<span style="hyperlinkFOOBAR">blog</span>
EXPECTED
I expected it produces clickable link like:
<a style="hyperlinkFOOBAR" href="http://hunmr.blogspot.com">blog</a>
QUESTION
EDIT1 The org-export-as-html can export link, but can not create CSS for the Hi-locks.
  • Do you know other ways to to export org-mode links to HTML?
  • To read the real link in org-mode buffer using elisp, how to do that? read text property?



----------------------------------------------------------------------------------------------------------------
@Andreas gave a great hint, so I tried to read the code of org-mode,
after write some elisp function, finally it worked.

The new htmlize.el was shared on github.com now:

https://github.com/whunmr/dotemacs/blob/master/site-lisp/htmlize.el
----------------------------------------------------------------------------------------------------------------


And this the main code:

(defun expand-org-link (&optional buffer)
  "Change [[url][shortname]] to [[url] [shortname]] by adding a space between url and shortname"
  (goto-char (point-min))
  (while (re-search-forward "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
                nil t)
    (let ((url (match-string 1))
      (link-text (match-string 3)))
      (delete-region (match-beginning 0) (match-end 0))
      (insert "[[" url "] [" link-text "]]"))))

(defun shrink-org-link (&optional buffer)
  "Change [[url] [shortname]] to [[url][shortname]], remove the space between url and shortname"
  (goto-char (point-min))
  (while (re-search-forward "\\[\\[\\([^][]+\\)\\] \\(\\[\\([^][]+\\)\\]\\)?\\]"
                nil t)
    (let ((url (match-string 1))
      (link-text (match-string 3)))
      (delete-region (match-beginning 0) (match-end 0))
      (insert "[[" url "][" link-text "]]"))))

(defun transform-org-link ()
  "transform htmlized  to "
  (goto-char (point-min))
  (while (re-search-forward "\\[\\[]+\\)>\\([^][]+\\)\\] \\[\\([^][]+\\)\\]\\]"
                nil t)
    (let ((style (match-string 1))
          (url (match-string 2))
      (link-text (match-string 3)))
      (delete-region (match-beginning 0) (match-end 0))
      (insert "" link-text ""))))




No comments:

Post a Comment