Skip to content

Instantly share code, notes, and snippets.

@tomykaira
Created June 13, 2011 02:47
Show Gist options
  • Save tomykaira/1022256 to your computer and use it in GitHub Desktop.
Save tomykaira/1022256 to your computer and use it in GitHub Desktop.
convert Org-mode file to hatena like
(defun org-to-hatena (begin end)
(interactive "r")
(if (and transient-mark-mode (not mark-active))
(setq begin (point-min)
end (point-max)))
(save-excursion
(goto-char begin)
;; if the first heading is ** (not *)
(re-search-forward "^\\*\\*?" end nil)
(when (> (- (match-end 0) (match-beginning 0)) 1)
(message "init")
(goto-char begin)
(while (re-search-forward "^\\*\\*" end t)
(replace-match "*")))
;; move tag
(goto-char begin)
(while (re-search-forward "^\\(\\**\\)\\(.*?\\)\\s-*:\\(.*\\):$" end t)
(let* ((b (match-beginning 0))
(e (match-end 0))
(h (match-string 1))
(title (match-string 2))
(tags (split-string (match-string 3) ":"))
(hatena-tags (mapconcat 'identity tags "]["))
)
(insert (concat h "[" hatena-tags "]" title))
(delete-region b e)))
;; one blank line to two blank lines
(goto-char begin)
(while (search-forward "\n\n" end t)
(replace-match "\n\n\n" nil t))
;; convert URL
(goto-char begin)
(org-url-to-hatena)
))
(defun org-url-to-hatena ()
(interactive)
(let (repl b e)
(save-excursion
(while (re-search-forward "\\[\\[\\(.*\\)\\]\\[\\(.*\\)\\]\\]" nil nil)
(setq b (match-beginning 0)
e (match-end 0))
(insert "[" (match-string 1) ":title=" (match-string 2) "]")
(delete-region b e)
))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment