org-xoxo.el 4.1 KB

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