org-xoxo.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ;;; org-xoxo.el --- XOXO export for Org-mode
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; XOXO export
  23. ;;; Code:
  24. (require 'org-exp)
  25. (defvar org-export-xoxo-final-hook nil
  26. "Hook run after XOXO export, in the new buffer.")
  27. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  28. (with-current-buffer buffer
  29. (apply 'insert output)))
  30. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  31. ;;;###autoload
  32. (defun org-export-as-xoxo (&optional buffer)
  33. "Export the org buffer as XOXO.
  34. The XOXO buffer is named *xoxo-<source buffer name>*"
  35. (interactive (list (current-buffer)))
  36. (run-hooks 'org-export-first-hook)
  37. ;; A quickie abstraction
  38. ;; Output everything as XOXO
  39. (with-current-buffer (get-buffer buffer)
  40. (let* ((pos (point))
  41. (opt-plist (org-combine-plists (org-default-export-plist)
  42. (org-infile-export-plist)))
  43. (filename (concat (file-name-as-directory
  44. (org-export-directory :xoxo opt-plist))
  45. (file-name-sans-extension
  46. (file-name-nondirectory buffer-file-name))
  47. ".html"))
  48. (out (find-file-noselect filename))
  49. (last-level 1)
  50. (hanging-li nil))
  51. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  52. ;; Check the output buffer is empty.
  53. (with-current-buffer out (erase-buffer))
  54. ;; Kick off the output
  55. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  56. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  57. (let* ((hd (match-string-no-properties 1))
  58. (level (length hd))
  59. (text (concat
  60. (match-string-no-properties 2)
  61. (save-excursion
  62. (goto-char (match-end 0))
  63. (let ((str ""))
  64. (catch 'loop
  65. (while 't
  66. (forward-line)
  67. (if (looking-at "^[ \t]\\(.*\\)")
  68. (setq str (concat str (match-string-no-properties 1)))
  69. (throw 'loop str)))))))))
  70. ;; Handle level rendering
  71. (cond
  72. ((> level last-level)
  73. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  74. ((< level last-level)
  75. (dotimes (- (- last-level level) 1)
  76. (if hanging-li
  77. (org-export-as-xoxo-insert-into out "</li>\n"))
  78. (org-export-as-xoxo-insert-into out "</ol>\n"))
  79. (when hanging-li
  80. (org-export-as-xoxo-insert-into out "</li>\n")
  81. (setq hanging-li nil)))
  82. ((equal level last-level)
  83. (if hanging-li
  84. (org-export-as-xoxo-insert-into out "</li>\n")))
  85. )
  86. (setq last-level level)
  87. ;; And output the new li
  88. (setq hanging-li 't)
  89. (if (equal ?+ (elt text 0))
  90. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  91. (org-export-as-xoxo-insert-into out "<li>" text))))
  92. ;; Finally finish off the ol
  93. (dotimes (- last-level 1)
  94. (if hanging-li
  95. (org-export-as-xoxo-insert-into out "</li>\n"))
  96. (org-export-as-xoxo-insert-into out "</ol>\n"))
  97. (goto-char pos)
  98. ;; Finish the buffer off and clean it up.
  99. (switch-to-buffer-other-window out)
  100. (indent-region (point-min) (point-max) nil)
  101. (run-hooks 'org-export-xoxo-final-hook)
  102. (save-buffer)
  103. (goto-char (point-min))
  104. )))
  105. (provide 'org-xoxo)
  106. ;;; org-xoxo.el ends here