forked from renard/o-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho-blog.el
1150 lines (957 loc) · 33.5 KB
/
o-blog.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; o-blog.el --- Org-blog exporter
;; Copyright © 2012 Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
;; Author: Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
;; Keywords: emacs,
;; Created: 2012-01-04
;; Last changed: 2013-01-26 02:38:06
;; Licence: WTFPL, grab your copy here: http://sam.zoy.org/wtfpl/
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;;
;;; Code:
(eval-when-compile
(require 'cl nil t)
(require 'browse-url nil t))
(require 'ob-ditaa nil t)
(require 'htmlize nil t)
(require 'sgml-mode nil t)
(require 'html2text nil t)
(require 'time-stamp nil t)
(require 'org-xhtml nil t)
(require 'dired-sync nil t)
(require 'find-func nil t)
(mapcar (lambda (x) (require (intern (format "o-blog-%s" x)) nil t))
'("alert" "copy-files" "source" "grid" "i18n" "bootstrap"))
(defconst o-blog-version "1.2" "o-blog version number")
(defconst o-blog-bug-report-url "https://github.com/renard/o-blog/issues/new"
"Url for bug report.")
(defgroup o-blog nil "o-blog customization group"
:group 'org-export)
(defcustom o-blog-async-opts nil
"Extra options to be used when compiling with
`org-publish-blog-async'."
:group 'o-blog
:type 'list)
(defcustom o-blog-before-publish-hook nil
"Hook to be run before publishing a blog.
Each hook is a function that could be called with no parameter."
:group 'o-blog
:type 'hook)
(defcustom o-blog-after-publish-hook nil
"Hook to be run after publishing a blog.
Each hook is a function that could be called with no parameter."
:group 'o-blog
:type 'hook)
(defcustom o-blog-html-plugins-hook nil
"Hook to be run before exporting an entry to HTML in
`ob-get-entry-text'. Each hook is a function that could be called
with no parameter.
This is a good place for o-blog parser plugins."
:group 'o-blog
:type 'hook)
(defstruct (ob:blog :named)
"Blog structure
- file: the blog source file (read-only).
- buffer: buffer visiting the blog file (read-only).
- publish-dir: where to publish the blog defined by the
\"#+PUBLISH_DIR:\" header directive or out in the same
directory as the blog source file.
- template-dir: location of the template directory defined by
the \"#+TEMPLATE_DIR:\" header directive or the templates
directory of the o-blog library.
- style-dir: path of the \"css\" files defined by the
\"#STYLE_DIR:\" header directive or style. This directory is
relative to \"template-dir\".
- posts-filter: default filter for post defined by the
\"#POSTS_FILTER:\" header directive or \"+TODO=\\\"DONE\\\".
- static-filter: default filter for static pages defined by the
\"#STATIC_FILTER:\" header directive or \"+PAGES={.*}\.
- snippet-filter default filter for snippets defined by the
\"#SNIPPET_FILTER:\" header directive or \"+SNIPPET={.*}\".
- title: Blog title defined by the \"#+TITLE:\" header
directive.
- description: blog description defined by the
\"#+DESCRIPTION:\" header directive.
- url: Blog base URL defined by the \"#+URL:\" header.
- language: Blog language defined by the \"#+LANGUAGE:\" header
or \"en\".
- default-category: default category for posts defined by the
\"#DEFAULT_CATEGORY:\" header or \"Blog\".
- disqus: disqus account (called a forum on Disqus) this system
belongs to. Defined by the \"#DISQUS\" header.
- analytics: Property ID (UA-XXXXX-Y) of google analytics
account to use for analytics on this site. Defined by the
\"#ANALYTICS\" header.
- filename-sanitizer: 1-argument function to be used to sanitize
post filenames. Defined by \#+FILENAME_SANITIZER:\" or
\"ob-sanitize-string\".
- post-sorter: a 2-argument function to be used to sort the
posts. Defined by \"#+POSTS_SORTER:\"
or \"ob-sort-posts-by-date\".
- post-filepath: a 3-argument function to be used to generate
the post path in output directory. Defined by
\"#+POSTS_FILEPATH:\" or \"ob-set-default-filepath\".
- post-htmlfile: a 3-argument function to be used to generate
the post html filename in output directory. Defined by
\"#+POSTS_HTMLFILE:\" or \"ob-set-default-htmlfile\".
"
(file nil :read-only)
(buffer nil :read-only)
publish-dir
template-dir
style-dir
assets-dir
posts-filter
static-filter
snippet-filter
title
description
url
language
post-build-shell
default-category
disqus
analytics
filename-sanitizer
posts-sorter
posts-filepath
posts-htmlfile)
(defstruct (ob:post :named)
"Post structure
- id: the post numerical id. Posts are sort by reversed
chronological order. The most recent post get the id 0.
- title: the post title read from the entry title.
- timestamp: the post timestamp given by the \"CLOSED\" property
or the current time.
- year: numerical year computed from \"timestamp\".
- month: numerical month computed from \"timestamp\".
- day: numerical day computed from \"timestamp\".
- category: category read from \"CATEGORY\" property org
\"blog\".
- tags: list of ob:tags.
- template: template to use for current post read from
\"TEMPLATE\" property or \"blog_post.html\".
- filepath: relative path from the blog root directory to the
post directory (directory only).
- filename: sanitized filename generated from \"title\".
- htmlfile: full relative path to the post html file (file and
directory).
- path-to-root: relative path from the post html file to the
blog root.
- content: raw content of the post (org-mode format).
- content-html: HTML export of the post.
- sitemap: Whether to publish in sitemap."
id
title
timestamp
year
month
day
category
tags
template
filepath
filename
htmlfile
path-to-root
content
content-html
sitemap)
(defstruct (ob:tags :named)
"Tag structure with following slots:
- name: string defying the tag name.
- safe: web safe tag name for URL.
- count: how many time the tag is used.
- size: the font size in percent."
name safe count size)
(defstruct (ob:category :named)
"Category structure with following slots:
- name: string defying the category name.
- safe: web safe category name for URL."
name safe)
;;;###autoload
;;;###autoload
(defun o-blog-version (&optional here)
"Message the current o-blog version. If call using
`universal-argument', insert value in current position."
(interactive "P")
(let* ((default-directory (file-name-directory (locate-library "o-blog")))
(has-git (and (file-exists-p (expand-file-name ".git"))
(executable-find "git")))
(version (if has-git
(substring (shell-command-to-string "git describe") 0 -1)
o-blog-version))
(msg (format "o-blog version %s" version)))
(if here
(insert msg)
(message msg))))
(defun o-blog-bug-report (backtrace)
"Copy (`kill-new') information to be posted to o-blog github
issues page in selection buffer and open
`o-blog-bug-report-url'. Version information can be posted in the
message box on github page."
(interactive)
(let* ((default-directory (file-name-directory (locate-library "o-blog")))
(has-git (and (file-exists-p (expand-file-name ".git"))
(executable-find "git")))
(version (if has-git
(substring (shell-command-to-string "git describe") 0 -1)
o-blog-version))
(msg (format "o-blog version %s" version))
(id (if has-git
(shell-command-to-string "git --no-pager log -n1 --format=format:%H")
""))
(submodules (if has-git
(substring
(shell-command-to-string "git submodule") 0 -1)
""))
(msg (if has-git
(format " %s o-blog (%s)\n%s" id version submodules)
(format "o-blog version %s" version)))
(bug-report-str (format
"
Add your description here
%s
**Configuration**
* Emacs
```
%s
```
* Org-mode
```
%s
```
* o-blog
```
%s
```"
(if backtrace
(format "**Backtrace**\n\n```\n%s\n```\n\n" backtrace)
"")
(emacs-version) (org-version) msg)))
(switch-to-buffer (get-buffer-create "o-blog Bug-report"))
(erase-buffer)
(insert bug-report-str)
(when (functionp 'x-set-selection)
(x-set-selection 'PRIMARY bug-report-str))
(browse-url o-blog-bug-report-url)))
;;;###autoload
(defun org-publish-blog (&optional file async)
"Publish FILE as a blog synchronously execpt ib ASYNC is
defined, or interactivelly called with `prefix-arg'.
"
(interactive
(list (or
(when (eq major-mode 'org-mode) (buffer-file-name))
(read-file-name "Publish blog from: " nil nil t))
current-prefix-arg))
(if async
(org-publish-blog-async file)
(org-publish-blog-sync file)))
;;;###autoload
(defun org-publish-blog-sync (file)
"Publish FILE synchronously."
(with-current-buffer (or
(get-file-buffer file)
(find-file file))
(run-hooks 'o-blog-before-publish-hook)
(let* (;; Make sure `org-todo-keyword' is not set to a particular value
;; by user.
(org-todo-keywords (default-value 'org-todo-keywords))
(start-time (current-time)) ;; for statistic purposes only
;; make sure we are on the correct directory.
(default-directory (file-name-directory file))
STATIC
(BLOG (ob-parse-blog-headers))
(STATIC (append STATIC
(ob-parse-entries
(org-map-entries 'point-marker
(ob:blog-static-filter BLOG)
'file-with-archives))))
(POSTS (ob-parse-entries
(org-map-entries 'point-marker
(ob:blog-posts-filter BLOG)
'file-with-archives)))
(ALL-POSTS POSTS)
(SNIPPETS (ob-parse-entries
(org-map-entries 'point-marker
(ob:blog-snippet-filter BLOG)
'file-with-archives)))
(TAGS (ob-compute-tags POSTS)))
(ob-write-static)
(ob-write-posts)
(ob-write-tags)
(ob-write-index)
(ob-do-copy (format "%s"
(ob:blog-assets-dir BLOG))
(ob:blog-publish-dir BLOG))
(ob-do-copy (format "%s/%s"
(ob:blog-template-dir BLOG)
(ob:blog-style-dir BLOG))
(ob:blog-publish-dir BLOG))
(run-hooks 'o-blog-after-publish-hook)
(message (format "Blog %s published in %ss"
file
(format-time-string "%s.%3N"
(time-subtract (current-time) start-time)))))))
(defun ob-do-copy (src dst &optional copyf args)
"Copy SRC into DST. If `dired-do-sync' is found it would be
preferred. Otherwise, `copy-directory' or `copy-files' would be
used.
A copy function COPYF and its arguments ARGS could be specified."
(let* ((dirp (file-directory-p src))
(copyf (cond
(copyf copyf)
((functionp 'dired-do-sync) 'dired-do-sync)
(dirp 'copy-directory)
(t 'copy-file)))
(args (or args
(when (eq 'copy-file copyf) '(t t t)))))
(when (file-exists-p src)
(apply copyf src dst args))))
(defun org-blog-publish-run-processes-sentinel (proc change)
"Sentinel in charge of cleaning `org-publish-blog-async' on success."
(when (eq (process-status proc) 'exit)
(let ((status (process-exit-status proc))
(cmd (process-get proc :cmd))
(cmd-buf (process-get proc :cmd-buf)))
(if (not (eq 0 status))
(progn
(when (process-buffer proc)
(set-window-buffer (selected-window) cmd-buf))
(error "Org blog ERROR: %s" cmd))
(message "Org blog OK: %s" cmd))
(when cmd-buf (kill-buffer cmd-buf)))))
(defun org-publish-blog-async (file)
"Publish FILE synchronously."
(let* ((cmd-line (append command-line-args
`("--batch"
"-l" ,(concat (file-name-as-directory
user-emacs-directory)
"init.el")
,@ob-async-opts
"--eval"
,(format "(org-publish-blog \"%s\")" file))))
(cmd-buf (get-buffer-create (format "ORG blog build %s" file)))
(proc (apply 'start-process (car cmd-line)
cmd-buf (car cmd-line) (cdr cmd-line))))
(message "Run: %S" cmd-line)
(process-put proc :cmd (format "Build %s" file))
(process-put proc :cmd-buf cmd-buf)
(set-process-sentinel proc 'org-blog-publish-run-processes-sentinel)))
;; Internal functions
(defun o-blog-publish-linked-files()
"Copy files (defined by \"file:\" link prefix) to page related directory."
(save-match-data
(save-excursion
(goto-char (point-min))
(let (ret)
(while (re-search-forward "\\(\\[file:\\)\\([^]]+\\)\\(\\]\\)" nil t)
(let ((prefix (match-string-no-properties 1))
(file (match-string-no-properties 2))
(suffix (match-string-no-properties 3)))
(when (file-exists-p file)
(replace-match
(if page
(format "%s%s/%s%s"
prefix
(or (file-name-directory htmlfile) ".")
(file-name-nondirectory file) suffix)
(format "%s%s/%s/%s%s"
prefix
(file-relative-name "." filepath)
(file-name-sans-extension htmlfile)
(file-name-nondirectory file) suffix ))
(add-to-list 'ret file)))))
(when ret
(unless page
;; create a redirection page as index.html into files' directory
(with-temp-buffer
(insert
(mapconcat 'identity
`(,(format "* Redirect from (%s)" title)
":PROPERTIES:"
,(format ":PAGE: %s/index.html" (file-name-sans-extension htmlfile))
":TEMPLATE: page_redirect.html"
":END:")
"\n"))
(org-mode)
(goto-char (point-min))
(setf STATIC (append STATIC (list (ob-parse-entry))))))
;; copy all files into their target directory.
(loop for f in ret
do (let ((target
(if page
(format "%s/%s"
(ob:blog-publish-dir BLOG)
(file-name-nondirectory f))
(format "%s/%s/%s"
(ob:blog-publish-dir BLOG)
;; file path is nil when exporting static page?
;;(or filepath ".")
(file-name-sans-extension htmlfile)
(file-name-nondirectory f)))))
(mkdir (file-name-directory target) t)
(ob-do-copy f target))))))))
(add-hook 'o-blog-html-plugins-hook 'o-blog-publish-linked-files)
(defun ob-parse-blog-headers (&optional file)
"Parse blog related variable from current-buffer."
(let* ((file (or file (buffer-file-name)))
(blog (make-ob:blog :file file :buffer (current-buffer))))
(setf (ob:blog-publish-dir blog) (or (ob:get-header "PUBLISH_DIR") "out"))
(setf (ob:blog-template-dir blog) (or (ob:get-header "TEMPLATE_DIR")
(concat
(file-name-directory
(file-name-directory
(find-library-name "o-blog")))
"templates")))
(setf (ob:blog-style-dir blog) (or (ob:get-header "STYLE_DIR") "style"))
(setf (ob:blog-assets-dir blog) (or (ob:get-header "ASSETS_DIR") "assets"))
(setf (ob:blog-posts-filter blog) (or (ob:get-header "POSTS_FILTER") "+TODO=\"DONE\""))
(setf (ob:blog-static-filter blog) (or (ob:get-header "STATIC_FILTER") "+PAGE={.+\.html}"))
(setf (ob:blog-snippet-filter blog) (or (ob:get-header "SNIPPET_FILTER") "+SNIPPET={.+}"))
(setf (ob:blog-title blog) (or (ob:get-header "TITLE") "title"))
(setf (ob:blog-description blog) (or (ob:get-header "DESCRIPTION") "Description"))
(setf (ob:blog-url blog) (or (ob:get-header "URL") ""))
(setf (ob:blog-language blog) (or (ob:get-header "LANGUAGE") "en"))
(setf (ob:blog-post-build-shell blog) (ob:get-header "POST_BUILD_SHELL" t))
(setf (ob:blog-default-category blog) (or (ob:get-header "DEFAULT_CATEGORY") "Blog"))
(setf (ob:blog-disqus blog) (ob:get-header "DISQUS"))
(setf (ob:blog-analytics blog) (ob:get-header "ANALYTICS"))
(setf (ob:blog-filename-sanitizer blog)
(let ((ofs (ob:get-header "FILENAME_SANITIZER")))
(if (and ofs (functionp (intern ofs)))
(intern ofs)
'ob-sanitize-string)))
(setf (ob:blog-posts-sorter blog)
(let ((ops (ob:get-header "POSTS_SORTER")))
(if (and ops (functionp (intern ops)))
(intern ops)
'ob-sort-posts-by-date)))
(setf (ob:blog-posts-filepath blog)
(let ((ops (ob:get-header "POSTS_FILEPATH")))
(if (and ops (functionp (intern ops)))
(intern ops)
'ob-set-default-filepath)))
(setf (ob:blog-posts-htmlfile blog)
(let ((ops (ob:get-header "POSTS_HTMLFILE")))
(if (and ops (functionp (intern ops)))
(intern ops)
'ob-set-default-htmlfile)))
blog))
(defun ob-parse-entries (markers)
"Parse blog entries from current buffer.
MARKERS is a list of entries given by `org-map-entries'."
(save-excursion
(loop for marker in markers
with posts = nil
;; Parse each entry into posts
collect (with-current-buffer (marker-buffer marker)
(goto-char (marker-position marker))
(ob-parse-entry))
into posts
;; Then wee need to set the post id in all all sorted posts.
finally return (loop for post in (sort posts (ob:blog-posts-sorter BLOG))
with id = 0
do (setf (ob:post-id post) id)
and do (incf id 1)
and collect post))))
(defun ob-sort-posts-by-date (a b)
"Sort both A and B posts by date (newer posts first)."
(> (float-time (ob:post-timestamp a))
(float-time (ob:post-timestamp b))))
(defun ob-sort-posts-by-title (a b)
"Sort alphabetically both A and B posts by title."
(string> (ob:post-title a)
(ob:post-title b)))
(defun ob-set-default-filepath (category year month)
"Create a default filepath using CATEGEORY YEAR and MONTH which
looks like:
CATEGORY/YYYY/MM
See also `ob-set-default-htmlfile', `ob-parse-entry'."
(format "%s/%.4d/%.2d" category year month))
(defun ob-set-default-htmlfile (filepath day filename)
"Create a default htmlfile using FILEPATH DAY and FILENAME which
looks like:
FILEPATH/DD_FILENAME.html
See also `ob-set-default-filepath', `ob-parse-entry'."
(format "%s/%.2d_%s.html" filepath day filename))
(defun ob-parse-entry()
"Parse blog entry from current position"
(when (search-forward-regexp org-complex-heading-regexp
(point-at-eol)
t)
(let* ((title (match-string-no-properties 4))
;; tags is a list of `ob:tags'
;; to be compliant with org tag syntax (no "-" org space)
;; "_" would be replaced with " " and "@" by "-"
(tags (loop for tn in (org-get-local-tags)
with td
do (setf td
(replace-regexp-in-string
"_" " "
(replace-regexp-in-string "@" "-" tn)))
and collect (make-ob:tags
:name td
:safe (ob:sanitize-string td))))
;; Timestamp is taken from either the CLOSED property or the
;; current timestamp.
(timestamp (apply 'encode-time
(org-parse-time-string
(or (org-entry-get (point) "CLOSED")
(time-stamp-string "%:y-%02m-%02d %02H:%02M:%02S %u")))))
;; Some other time variables
(year (string-to-number (format-time-string "%Y" timestamp)))
(month (string-to-number (format-time-string "%m" timestamp)))
(day (string-to-number (format-time-string "%d" timestamp)))
(category (or (org-entry-get (point) "category")
(car (last (org-get-outline-path)))
(org-entry-get (point) "ARCHIVE_OLPATH")
(ob:blog-default-category BLOG)))
(category-safe (ob:sanitize-string category))
(page (org-entry-get (point) "PAGE"))
(filename (or (org-entry-get (point) "CUSTOM_ID")
(ob:sanitize-string title)))
(filepath (funcall (ob:blog-posts-filepath BLOG) category-safe year month))
(htmlfile (funcall (ob:blog-posts-htmlfile BLOG) filepath day filename)))
(when page
(setq htmlfile page
filename (file-name-sans-extension (file-name-nondirectory htmlfile))
filepath (file-name-directory htmlfile)))
(let ((content (ob-get-entry-text)))
(make-ob:post :title title
:tags tags
:timestamp timestamp
:year year
:month month
:day day
:filename filename
:filepath filepath
:path-to-root (file-relative-name "." filepath)
:htmlfile htmlfile
:template (or (org-entry-get (point) "TEMPLATE")
(if page "blog_static.html" "blog_post.html"))
:content content
:content-html (ob-export-string-to-html content)
:category (make-ob:category
:name category
:safe category-safe)
:sitemap (or (org-entry-get (point) "SITEMAP"))
)))))
(defun ob-get-entry-text ()
"Return entry text from point with not properties.
Please note that a blank line _MUST_ be present between entry
headers and body."
(save-excursion
(save-restriction
(save-match-data
(org-narrow-to-subtree)
(let ((text (buffer-string)))
(with-temp-buffer
(insert text)
(goto-char (point-min))
(org-mode)
(while (<= 2 (save-match-data (funcall outline-level)))
(org-promote-subtree))
(run-hooks 'o-blog-html-plugins-hook)
(goto-char (point-min))
(when (search-forward-regexp "^\\s-*$" nil t)
(goto-char (match-end 0)))
(save-excursion
(insert "#+OPTIONS: H:7 num:nil toc:nil d:nil todo:nil <:nil pri:nil tags:nil\n\n"))
(buffer-substring-no-properties (point) (point-max))))))))
(defun ob-export-string-to-html (string)
"Convert STRING to html using `org-mode' syntax."
(with-temp-buffer
(insert string)
(org-mode)
(goto-char (point-min))
;; exporting block with ditaa is kinda messy since it requires a real
;; file (does not work with a temp-buffer which is not associated to any
;; file).
(let ((saved-file
(when
(re-search-forward "^#\\+BEGIN_SRC:?[ \t]+\\(ditaa\\)" nil t)
(format "/%s/%s/%s.src.org"
default-directory
(ob:blog-publish-dir BLOG)
;; variable inherited from `ob-parse-entry'
htmlfile)))
(org-confirm-babel-evaluate nil)
ret)
(when saved-file
(ob-write-file saved-file))
(setq ret (substring-no-properties
;; `org-export-as-html' arguments has changed on new
;; org-version, then again with the new exporter.
;; First try old function signatures, then on failure
;; use new argument call.
(or
(ignore-errors (org-export-as-html nil nil nil 'string t))
(ignore-errors (org-export-as-html nil nil 'string t))
(ignore-errors (org-export-as 'html nil nil t nil))
(org-export-as-html nil nil 'string nil nil))))
(when saved-file
(delete-file saved-file))
ret)))
(defun ob-compute-tags (posts &optional min_r max_r)
"Return a list of all tags sorted by usage.
Each item is: \(TAG COUNT PERCENT \)
CONTENT-LIST is a list of all articles such as generated in
`org-publish-blog'.
MIN_R and MAX_R are the minimum and maximum percentage value. If
not provided 80 and 220 are used."
(let* ((tags (sort (loop for post in posts
append (loop for tn in (ob:post-tags post)
when (ob:tags-p tn)
collect (ob:tags-name tn)))
#'string<))
(min_r (or min_r 80))
(max_r (or max_r 220))
(min_f (length tags))
(max_f 0))
(loop for item in
;; Here extract uniq tags and count occurrences
;; (such as uniq -c does)
;; Each item of returned list is
;; (VALUE COUNT)
(loop for (i . j) on tags
with k = 1
when (string= i (car j)) do (incf k)
else collect (progn
(when (> k max_f) (setf max_f k))
(when (< k min_f) (setf min_f k))
(cons i (list k)))
and do (setf k 1))
collect (let ((val (cadr item)))
(make-ob:tags
:name (car item)
:safe (ob:sanitize-string (car item))
:count val
;; This is the tricky part
;; Formula is:
;; % = min_r + (val - min_f) * (max_r - min_r) / (max_f - min_f)
;; the `max' is on purpose in case of max_f = min_f
:size (+ min_r
(/
(* (- val min_f) (- max_r min_r))
(max 1.0 (float (- max_f min_f))))))))))
(defun ob-eval-lisp()
"Eval embeded lisp code defined by <lisp> tags in html fragment
when publishing a page."
(save-excursion
(save-restriction
(save-match-data
;; needed for thing-at-point
(html-mode)
(beginning-of-buffer)
(let ((open-tag "<lisp>\\|{lisp}\\|\\[lisp\\]")
(close-tag "</lisp>\\|{/lisp}\\|\\[/lisp\\]")
beg end sexp)
(while (search-forward-regexp open-tag nil t)
(setq beg (- (point) (length (match-string 0))))
(when (search-forward-regexp close-tag nil t)
(setq end (point))
(backward-char (length (match-string 0)))
(backward-sexp)
(setq sexp (substring-no-properties (thing-at-point 'sexp)))
(narrow-to-region beg end)
(delete-region (point-min) (point-max))
(insert
(save-match-data
(condition-case err
(let ((object (eval (read sexp))))
(cond
;; result is a string
((stringp object) object)
;; a list
((and (listp object)
(not (eq object nil)))
(let ((string (pp-to-string object)))
(substring string 0 (1- (length string)))))
;; a number
((numberp object)
(number-to-string object))
;; nil
((eq object nil) "")
;; otherwise
(t (pp-to-string object))))
;; error handler
(error
(format "Lisp error in %s: %s" (buffer-file-name) err)))))
(goto-char (point-min))
(widen))))))))
;; Publish functions
(defun ob-write-file (file)
"Write current buffer to FILE and create full path if necessary."
(mkdir (file-name-directory file) t)
(let ((coding-system-for-write
(with-current-buffer
(ob:blog-buffer BLOG)
buffer-file-coding-system)))
(write-file file)))
(defun ob-write-index()
"Publish all indexes (default, categories, year, month)"
(let ((BREADCRUMB "Archives"))
(ob-write-index-to-file "blog_archives.html"
(format "%s/archives.html"
(ob:blog-publish-dir BLOG))))
(ob-write-index-to-file "blog_rss.html"
(format "%s/index.xml"
(ob:blog-publish-dir BLOG)))
(ob-write-index-to-file "blog_sitemap.html"
(format "%s/sitemap.xml"
(ob:blog-publish-dir BLOG)))
(loop for CATEGORY in (ob:get-posts nil nil nil 'category)
with PATH-TO-ROOT = ".."
do
(loop for YEAR in (ob:get-posts
(lambda (x) (equal CATEGORY (ob:post-category x)))
nil nil 'year)
with PATH-TO-ROOT = "../.."
do
(loop for MONTH in (ob:get-posts
(lambda (x) (and
(equal CATEGORY (ob:post-category x))
(= YEAR (ob:post-year x))))
nil nil 'month)
with PATH-TO-ROOT = "../../.."
do (ob-process-index "blog_index_month.html" CATEGORY YEAR MONTH))
and do (ob-process-index "blog_index_year.html" CATEGORY YEAR))
and do (unless (equal "." CATEGORY)
(ob-process-index "blog_index_category.html" CATEGORY))))
(defun ob-process-index (template &optional category year month)
"Low-level function for `ob-write-index'.
Template is read from TEMPLATE file.
If provided CATEGORY YEAR and MONTH are used to select articles."
(let* ((fp (format "%s/%s/index.html"
(ob:blog-publish-dir BLOG)
(cond
((and category year month) (format "%s/%.4d/%.2d"
(ob:category-safe category)
year month))
((and category year) (format "%s/%.4d"
(ob:category-safe category) year))
(t (ob:category-safe category)))))
(POSTS (ob:get-posts
(lambda (x) (and
(if category (equal
(ob:category-name category)
(ob:category-name (ob:post-category x)))
t)
(if year (= year (ob:post-year x)) t)
(if month (= month (ob:post-month x)) t))))))
(ob-write-index-to-file template fp)))
(defun ob-write-index-to-file (template outfile)
""
(with-temp-buffer
"*ORG blog publish index*"
(erase-buffer)
(insert-file-contents
(format "%s/%s" (ob:blog-template-dir BLOG) template))
(ob-eval-lisp)
(ob-write-file outfile)))
(defun ob-write-static ()
"Publish static pages"
(loop for POST in STATIC
do (ob-write-index-to-file
(ob:post-template POST)
(format "%s/%s"
(ob:blog-publish-dir BLOG)
(ob:post-htmlfile POST)))))
(defun ob-write-posts ()
"Publish all posts"
(loop for POST in POSTS
do (ob-write-index-to-file
(ob:post-template POST)
(format "%s/%s"
(ob:blog-publish-dir BLOG)
(ob:post-htmlfile POST)))))
(defun ob-write-tags ()
"Publish all tags into directory named \"tags\"."
(let ((PATH-TO-ROOT "..")
(BREADCRUMB "Tags"))
(ob-write-index-to-file "blog_tags.html"
(format "%s/tags/index.html"
(ob:blog-publish-dir BLOG)))
(loop for TAG in TAGS
do
(ob-write-index-to-file "blog_tags-details.html"
(format "%s/tags/%s.html"
(ob:blog-publish-dir BLOG)
(ob:tags-safe TAG))))))
(defun ob-sanitize-string (s)
"Sanitize string S by:
- converting all charcters ton pure ASCII
- replacing non alphanumerical chars to \"-\"
- downcasing all letters
- trimming leading and tailing \"-\""
(loop for c across s
with cd
with gc
with ret
do (progn
(setf gc (get-char-code-property c 'general-category))
(setf cd (get-char-code-property c 'decomposition)))
if (or (member gc '(Lu Ll Nd)) (= ?- c))
collect (downcase
(char-to-string (if cd (car cd) c)))
into ret
else if (member gc '(Zs))
collect "-" into ret
finally return (replace-regexp-in-string
"--+" "-"
(replace-regexp-in-string
"^-+\\|-+$" ""
(mapconcat 'identity ret "")))))
;; template accessible functions
(defun ob:sanitize-string(s)
"Sanitize string S using function defined by
`filename-sanitizer' slot in `ob:blog' structure."
(funcall (ob:blog-filename-sanitizer BLOG) s))
(defun ob:get-posts (&optional predicate count sortfunc collect)
"Return posts (from `POSTS' as defined in `org-publish-blog')
matching PREDICATE. Limit to COUNT results if defined and sorted
using SORTFUNC.
PREDICATE is a function run for each post with the post itself as
argument. If PREDICATE is nil, no filter would be done on posts.
SORTFUNC is used a `sort' PREDICATE.
If COLLECT is defined, only returns the COLLECT field of a
`ob:post' structure.
Examples:
- Getting last 10 posts:
\(ob:get-posts nil 10\)
- Getting post from January 2012:
\(ob:get-posts
\(lambda \(x\)
\(and \(= 2012 \(ob:post-year x\)\)
\(= 1 \(ob:post-month x\)\)\)\)\)
- getting all categories