2323(provide 'org-blog-buffer )
2424
2525(require 'org )
26+ (require 'org-blog )
2627(require 'ox )
2728(require 'ox-html )
28- (require 'org-blog )
2929
3030(eval-when-compile
3131 (require 'cl ))
3232
3333(defconst org-blog-buffer-options-alist
3434 (reduce
3535 (lambda (l i )
36- (let ((field (plist-get (cdr i) :attr )))
36+ (let ((field (plist-get (cdr i) :to-buffer )))
37+ ; ; Only add our fields, distinguised by the POST_ prefix
3738 (if (string-prefix-p " POST_" field t )
3839 (cons (list (car i) field nil nil t ) l)
3940 l)))
4041 org-blog-post-mapping
4142 :initial-value nil ))
4243
43- (org-export-define-derived-backend 'blog 'html
44- :filters-alist '((:filter-final-output . org-blog-filter-tag-newline)
45- (:filter-plain-text . org-blog-filter-text-newlines))
46- :options-alist org-blog-buffer-options-alist)
47-
4844; ;; Filters
4945(defun org-blog-filter-tag-newline (content backend info )
5046 " Remove superfluous leading space and trailing newlines from tags
@@ -53,26 +49,38 @@ TREE is the parse tree being exported. BACKEND is the export
5349back-end used. INFO is a plist used as a communication channel.
5450
5551Assume BACKEND is `blog' ."
56- ; ; (print (format "content is: %s" content))
5752 ; ; <tag>, </tag>, <tag/>, (replace-regexp-in-string "\\ (<\\ ([[:alpha:]]+\\ |/[[:alpha:]]+\\ |[[:alpha:]]+/\\ )>\\ )\n+" "\\ 1" content)
5853 (replace-regexp-in-string " \s *\\ (<[^>]+>\\ )\n +" " \\ 1" content))
5954
60- ; ;; Filters
6155(defun org-blog-filter-text-newlines (content backend info )
6256 " Remove superfluous newlines in elements (except verse blocks)
6357
6458TREE is the parse tree being exported. BACKEND is the export
6559back-end used. INFO is a plist used as a communication channel.
6660
6761Assume BACKEND is `blog' ."
68- ; ; (print (format "content is: %s" content))
69- (print (format " parent is %s " (org-export-get-parent content)))
7062 (cond ((eq 'verse-block (car (org-export-get-parent content)))
71- (print " in verse" )
7263 content)
7364 (t
7465 (replace-regexp-in-string " \n " " " content))))
7566
67+ (defun org-blog-translate-link (link content info )
68+ " Fixup links"
69+ (let ((type (org-element-property :type link )))
70+ (cond ((member type '(" custom-id" " id" ))
71+ (let ((destination (org-export-resolve-id-link link info)))
72+ (format " <a href=\" %s \" >%s </a> " destination contents)))
73+ ((equal type " fuzzy" )
74+ ; ; This is not ideal
75+ (let ((destination (org-element-property :path link )))
76+ (format " <a href=\" %s \" >%s </a> " destination contents))))))
77+
78+ (org-export-define-derived-backend 'blog 'html
79+ :filters-alist '((:filter-final-output . org-blog-filter-tag-newline)
80+ (:filter-plain-text . org-blog-filter-text-newlines))
81+ :options-alist org-blog-buffer-options-alist
82+ :translate-alist '((link . org-blog-translate-link)))
83+
7684(defun org-blog-buffer-extract-post ()
7785 " Transform a buffer into a post.
7886
@@ -89,12 +97,16 @@ retain the maximum flexibility for further transformation."
8997 (sort
9098 (reduce
9199 (lambda (l i )
92- (let ((v (plist-get attrs (car i)))
93- (filter (plist-get (cdr i) :from-buffer )))
94- (if v
95- (cons (cons (car i) (if filter
96- (funcall filter v attrs)
97- v)) l)
100+ (let* ((v (plist-get attrs (car i)))
101+ (filter (plist-get (cdr i) :from-buffer ))
102+ (value (if (and v
103+ (not (= 0 (length v))))
104+ (if filter
105+ (funcall filter v attrs)
106+ v))))
107+ ; ; We should only cons if there's a v and the output of the filter is non-nil
108+ (if value
109+ (cons (cons (car i) value) l)
98110 l)))
99111 org-blog-post-mapping
100112 :initial-value (when content
@@ -111,6 +123,7 @@ update the buffer to reflect the values it contains."
111123 (save-restriction
112124 ; ; Get the current values
113125 (let ((current (org-blog-buffer-extract-post)))
126+ ; ; Iterate over the stuff to merge in
114127 (mapc
115128 (lambda (item )
116129 (let ((k (car item))
@@ -129,16 +142,24 @@ update the buffer to reflect the values it contains."
129142 " default" )))
130143 (goto-char (point-min ))
131144 (cond
132- ; ; Inserting a new keyword
145+ ; ; No existing value associated with keyword
133146 ((eq (cdr (assq k current)) nil )
134147 (when val
135- (insert (concat " #+" (plist-get (cdr (assq k org-blog-post-mapping)) :attr ) " : " val " \n " ))))
136- ; ; Updating an existing keyword
148+ (insert (concat " #+" (plist-get (cdr (assq k org-blog-post-mapping)) :to-buffer ) " : " val " \n " ))))
149+ ; ; Existing value associated with keyword does not match new value
137150 ((not (equal (cdr (assq k current)) val))
138- (let ((re (org-make-options-regexp (list (plist-get (cdr (assq k org-blog-post-mapping)) :attr )) nil ))
151+ ; ; Prepare to search for the keyword
152+ (let ((re (org-make-options-regexp (list (plist-get (cdr (assq k org-blog-post-mapping)) :to-buffer )) nil ))
139153 (case-fold-search t ))
140- (re-search-forward re nil t )
141- (replace-match (concat " #+" (plist-get (cdr (assq k org-blog-post-mapping)) :attr ) " : " val) t t )))))))
154+ (cond
155+ ; ; If it was found
156+ ((re-search-forward re nil t )
157+ (message " Updating existing value with %s " val)
158+ (replace-match (concat " #+" (plist-get (cdr (assq k org-blog-post-mapping)) :to-buffer ) " : " val) t t )
159+ (message " Done replacing value " ))
160+ ; ; It was not found
161+ (val
162+ (insert (concat " #+" (plist-get (cdr (assq k org-blog-post-mapping)) :to-buffer ) " : " val " \n " ))))))))))
142163 ; ; Reverse sort fields to insert alphabetically
143164 (sort
144165 (copy-alist merge)
@@ -176,10 +197,12 @@ retain
176197its line
177198breaks
178199#+END_VERSE
200+
201+ [[org-blog-buffer.el][There's a link in here, too]]
179202" )
180203 (post-struct '((:blog . " t1b" )
181204 (:category " t1c1" " t1c2" )
182- (:content . " <p>Just a little bit of content. There is still part of the paragraph. Line breaks are refolded.</p><p class=\" verse\" >Though the material in verse should<br/>retain<br/>its line<br/>breaks<br/></p>" )
205+ (:content . " <p>Just a little bit of content. There is still part of the paragraph. Line breaks are refolded.</p><p class=\" verse\" >Though the material in verse should<br/>retain<br/>its line<br/>breaks<br/></p><p><a href= \" org-blog-buffer.el \" >There's a link in here, too</a></p> " )
183206 (:date 20738 4432 )
184207 (:description . " t1e" )
185208 (:id . " 1" )
0 commit comments