org-xoxo.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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: 6.35d
  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. (require 'org-exp)
  25. ;;; XOXO export
  26. (defvar org-export-xoxo-final-hook nil
  27. "Hook run after XOXO export, in the new buffer.")
  28. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  29. (with-current-buffer buffer
  30. (apply 'insert output)))
  31. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  32. ;;;###autoload
  33. (defun org-export-as-xoxo (&optional buffer)
  34. "Export the org buffer as XOXO.
  35. The XOXO buffer is named *xoxo-<source buffer name>*"
  36. (interactive (list (current-buffer)))
  37. (run-hooks 'org-export-first-hook)
  38. ;; A quickie abstraction
  39. ;; Output everything as XOXO
  40. (with-current-buffer (get-buffer buffer)
  41. (let* ((pos (point))
  42. (opt-plist (org-combine-plists (org-default-export-plist)
  43. (org-infile-export-plist)))
  44. (filename (concat (file-name-as-directory
  45. (org-export-directory :xoxo opt-plist))
  46. (file-name-sans-extension
  47. (file-name-nondirectory buffer-file-name))
  48. ".html"))
  49. (out (find-file-noselect filename))
  50. (last-level 1)
  51. (hanging-li nil))
  52. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  53. ;; Check the output buffer is empty.
  54. (with-current-buffer out (erase-buffer))
  55. ;; Kick off the output
  56. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  57. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  58. (let* ((hd (match-string-no-properties 1))
  59. (level (length hd))
  60. (text (concat
  61. (match-string-no-properties 2)
  62. (save-excursion
  63. (goto-char (match-end 0))
  64. (let ((str ""))
  65. (catch 'loop
  66. (while 't
  67. (forward-line)
  68. (if (looking-at "^[ \t]\\(.*\\)")
  69. (setq str (concat str (match-string-no-properties 1)))
  70. (throw 'loop str)))))))))
  71. ;; Handle level rendering
  72. (cond
  73. ((> level last-level)
  74. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  75. ((< level last-level)
  76. (dotimes (- (- last-level level) 1)
  77. (if hanging-li
  78. (org-export-as-xoxo-insert-into out "</li>\n"))
  79. (org-export-as-xoxo-insert-into out "</ol>\n"))
  80. (when hanging-li
  81. (org-export-as-xoxo-insert-into out "</li>\n")
  82. (setq hanging-li nil)))
  83. ((equal level last-level)
  84. (if hanging-li
  85. (org-export-as-xoxo-insert-into out "</li>\n")))
  86. )
  87. (setq last-level level)
  88. ;; And output the new li
  89. (setq hanging-li 't)
  90. (if (equal ?+ (elt text 0))
  91. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  92. (org-export-as-xoxo-insert-into out "<li>" text))))
  93. ;; Finally finish off the ol
  94. (dotimes (- last-level 1)
  95. (if hanging-li
  96. (org-export-as-xoxo-insert-into out "</li>\n"))
  97. (org-export-as-xoxo-insert-into out "</ol>\n"))
  98. (goto-char pos)
  99. ;; Finish the buffer off and clean it up.
  100. (switch-to-buffer-other-window out)
  101. (indent-region (point-min) (point-max) nil)
  102. (run-hooks 'org-export-xoxo-final-hook)
  103. (save-buffer)
  104. (goto-char (point-min))
  105. )))
  106. (provide 'org-xoxo)
  107. ;; arch-tag: 16e6a31f-f4f5-46f1-af18-48dc89faa702
  108. ;;; org-xoxo.el ends here