Skip to content

Commit 39b64d4

Browse files
committed
Merge pull request #19 from mdorman/newlines
Fix newline handling
2 parents 2a9a68b + d67abb9 commit 39b64d4

File tree

4 files changed

+109
-57
lines changed

4 files changed

+109
-57
lines changed

TODO.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
use. Gets around the =:blog-id= sort of stuff, or even having to
5555
know about =xmlrpc.php=.
5656

57-
5857
* DONE Fix date issues once and for all
5958
Added some tests to make the failure repeatable, then fixed the
6059
issue. The fix is admittedly a hack, but it would appear to be a
6160
reliable one, at least until WP changes its output.
61+
* DONE Fix line break behavior

org-blog-buffer.el

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,37 @@
4141
:initial-value nil))
4242

4343
(org-export-define-derived-backend 'blog 'html
44-
:options-alist org-blog-buffer-options-alist)
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+
48+
;;; Filters
49+
(defun org-blog-filter-tag-newline (content backend info)
50+
"Remove superfluous leading space and trailing newlines from tags
51+
52+
TREE is the parse tree being exported. BACKEND is the export
53+
back-end used. INFO is a plist used as a communication channel.
54+
55+
Assume BACKEND is `blog'."
56+
;; (print (format "content is: %s" content))
57+
;; <tag>, </tag>, <tag/>, (replace-regexp-in-string "\\(<\\([[:alpha:]]+\\|/[[:alpha:]]+\\|[[:alpha:]]+/\\)>\\)\n+" "\\1" content)
58+
(replace-regexp-in-string "\s*\\(<[^>]+>\\)\n+" "\\1" content))
59+
60+
;;; Filters
61+
(defun org-blog-filter-text-newlines (content backend info)
62+
"Remove superfluous newlines in elements (except verse blocks)
63+
64+
TREE is the parse tree being exported. BACKEND is the export
65+
back-end used. INFO is a plist used as a communication channel.
66+
67+
Assume BACKEND is `blog'."
68+
;; (print (format "content is: %s" content))
69+
(print (format "parent is %s" (org-export-get-parent content)))
70+
(cond ((eq 'verse-block (car (org-export-get-parent content)))
71+
(print "in verse")
72+
content)
73+
(t
74+
(replace-regexp-in-string "\n" " " content))))
4575

4676
(defun org-blog-buffer-extract-post ()
4777
"Transform a buffer into a post.
@@ -82,7 +112,7 @@ update the buffer to reflect the values it contains."
82112
;; Get the current values
83113
(let ((current (org-blog-buffer-extract-post)))
84114
(mapc
85-
#'(lambda (item)
115+
(lambda (item)
86116
(let ((k (car item))
87117
(v (cdr item))
88118
val existing)
@@ -112,8 +142,8 @@ update the buffer to reflect the values it contains."
112142
;; Reverse sort fields to insert alphabetically
113143
(sort
114144
(copy-alist merge)
115-
#'(lambda (a b)
116-
(string< (car b) (car a)))))))))
145+
(lambda (a b)
146+
(string< (car b) (car a)))))))))
117147

118148
;;;; Declare tests if ert is loaded
119149
(when (featurep 'ert)
@@ -137,10 +167,19 @@ update the buffer to reflect the values it contains."
137167
#+TITLE: Test 1 Title
138168
#+POST_TYPE: post
139169
140-
Just a little bit of content.")
170+
Just a little bit of content.
171+
There is still part of the paragraph. Line breaks are refolded.
172+
173+
#+BEGIN_VERSE
174+
Though the material in verse should
175+
retain
176+
its line
177+
breaks
178+
#+END_VERSE
179+
")
141180
(post-struct '((:blog . "t1b")
142181
(:category "t1c1" "t1c2")
143-
(:content . "<p>\nJust a little bit of content.</p>\n")
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>")
144183
(:date 20738 4432)
145184
(:description . "t1e")
146185
(:id . "1")

org-blog-wp.el

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,26 @@ For convenience in testing and inspection, the resulting alist is
5858
sorted."
5959
(sort
6060
(reduce
61-
#'(lambda (wp new)
62-
(let ((k (car new))
63-
(v (cdr new)))
64-
(cond ((eq v nil)
65-
wp)
66-
((eq :category k)
67-
(org-blog-post-to-wp-add-taxonomy wp "category" v))
68-
((eq :date k)
69-
;; Convert to GMT by adding seconds offset
70-
(let ((gmt (time-add v (seconds-to-time (- (car (current-time-zone)))))))
71-
(cons (cons "post_date_gmt" (list :datetime gmt)) wp)))
72-
((eq :keywords k)
73-
(org-blog-post-to-wp-add-taxonomy wp "post_tag" v))
74-
((eq :title k)
75-
(cons (cons "post_title" (or v "No Title")) wp))
76-
((assq k org-blog-wp-alist)
77-
(cons (cons (cdr (assq k org-blog-wp-alist)) v) wp)))))
61+
(lambda (wp new)
62+
(let ((k (car new))
63+
(v (cdr new)))
64+
(cond ((eq v nil)
65+
wp)
66+
((eq :category k)
67+
(org-blog-post-to-wp-add-taxonomy wp "category" v))
68+
((eq :date k)
69+
;; Convert to GMT by adding seconds offset
70+
(let ((gmt (time-add v (seconds-to-time (- (car (current-time-zone)))))))
71+
(cons (cons "post_date_gmt" (list :datetime gmt)) wp)))
72+
((eq :keywords k)
73+
(org-blog-post-to-wp-add-taxonomy wp "post_tag" v))
74+
((eq :title k)
75+
(cons (cons "post_title" (or v "No Title")) wp))
76+
((assq k org-blog-wp-alist)
77+
(cons (cons (cdr (assq k org-blog-wp-alist)) v) wp)))))
7878
post :initial-value nil)
79-
#'(lambda (a b)
80-
(string< (car a) (car b)))))
79+
(lambda (a b)
80+
(string< (car a) (car b)))))
8181

8282
(defun org-blog-post-to-wp-add-taxonomy (wp taxonomy entries)
8383
"Handle adding taxonomy items to a WordPress struct.
@@ -90,8 +90,8 @@ convenience in testing and inspection."
9090
(if existing
9191
(setcdr terms (sort
9292
(cons struct existing)
93-
#'(lambda (a b)
94-
(string< (car a) (car b)))))
93+
(lambda (a b)
94+
(string< (car a) (car b)))))
9595
(push (list "terms_names" struct) wp))
9696
wp))
9797

@@ -106,23 +106,23 @@ For convenience in testing and inspection, the resulting alist is
106106
sorted."
107107
(sort
108108
(reduce
109-
#'(lambda (post new)
110-
"Do key and value transformations."
111-
(let ((k (car new))
112-
(v (cdr new)))
113-
(cond ((eq v nil)
114-
post)
115-
((string= "terms" k)
116-
(org-blog-wp-to-post-handle-taxonomy post v))
117-
((string= "post_date_gmt" k)
118-
(cons (cons (car (rassoc k org-blog-wp-alist)) (plist-get v :datetime)) post))
119-
((rassoc k org-blog-wp-alist)
120-
(cons (cons (car (rassoc k org-blog-wp-alist)) v) post))
121-
(t
122-
post))))
109+
(lambda (post new)
110+
"Do key and value transformations."
111+
(let ((k (car new))
112+
(v (cdr new)))
113+
(cond ((eq v nil)
114+
post)
115+
((string= "terms" k)
116+
(org-blog-wp-to-post-handle-taxonomy post v))
117+
((string= "post_date_gmt" k)
118+
(cons (cons (car (rassoc k org-blog-wp-alist)) (plist-get v :datetime)) post))
119+
((rassoc k org-blog-wp-alist)
120+
(cons (cons (car (rassoc k org-blog-wp-alist)) v) post))
121+
(t
122+
post))))
123123
wp :initial-value nil)
124-
#'(lambda (a b)
125-
(string< (car a) (car b)))))
124+
(lambda (a b)
125+
(string< (car a) (car b)))))
126126

127127
(defun org-blog-wp-to-post-handle-taxonomy (post entries)
128128
"Handle mapping WordPress taxonomy info into a post struct.
@@ -142,10 +142,10 @@ glomming them onto the existing post."
142142
143143
From here we can extract just the bits we need."
144144
(reduce
145-
#'(lambda (lists term)
146-
(let ((name (cdr (assoc "name" term)))
147-
(taxonomy (cdr (assoc "taxonomy" term))))
148-
(cons (append (list taxonomy) (cdr (assoc taxonomy lists)) (list name)) lists)))
145+
(lambda (lists term)
146+
(let ((name (cdr (assoc "name" term)))
147+
(taxonomy (cdr (assoc "taxonomy" term))))
148+
(cons (append (list taxonomy) (cdr (assoc taxonomy lists)) (list name)) lists)))
149149
terms :initial-value nil))
150150

151151
(defun org-blog-wp-params (blog)
@@ -188,22 +188,22 @@ function to make other calls."
188188
;; Then shove the blog info into complete
189189
(t
190190
(reduce
191-
#'(lambda (entry)
192-
(when (string= (cdr (assoc "blogName" entry)))
193-
(setcdr (assq :xmlrpc complete) (cdr (assoc "xmlrpc" userblog)))
194-
(cdr (assoc "blogid" userblog))))
191+
(lambda (entry)
192+
(when (string= (cdr (assoc "blogName" entry)))
193+
(setcdr (assq :xmlrpc complete) (cdr (assoc "xmlrpc" userblog)))
194+
(cdr (assoc "blogid" userblog))))
195195
userblogs
196196
:initial-value (completing-read
197197
"Blog Name: "
198-
(mapcar #'(lambda (entry)
199-
(cdr (assoc "blogName" entry)))
198+
(mapcar (lambda (entry)
199+
(cdr (assoc "blogName" entry)))
200200
userblogs) nil t))))))
201201
(error "Posting cancelled")))
202202
complete)
203203
(sort
204204
complete
205-
#'(lambda (a b)
206-
(string< (car a) (car b))))))
205+
(lambda (a b)
206+
(string< (car a) (car b))))))
207207

208208
;;;; RPC entry point
209209

org-blog.el

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,20 @@ available in org-blog-alist."
303303
304304
There's *really* not much to see here. This is an automated post
305305
for testing org-blog, so we're really just focussed on whether it
306-
works at all, not the content of the post."))
306+
works at all, not the content of the post.
307+
308+
I do want some paragraph breaks
309+
and some odd
310+
short
311+
lines that should necessarily end up being part of a continuous paragraph.
312+
313+
#+BEGIN_VERSE
314+
Though the material in verse should
315+
retain
316+
its line
317+
breaks
318+
#+END_VERSE
319+
"))
307320
(org-blog-save)
308321
(goto-char (point-max))
309322
(insert "\n\nThis is a little additional text")

0 commit comments

Comments
 (0)