-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefblog.el
1973 lines (1751 loc) · 90.8 KB
/
defblog.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
;;; defblog --- A wrapper for org-publish, for producing blogs from
;;; local Org-mode files.
;; Copyright (C) 2021 John Maraist
;; Author: John Maraist <[email protected]>
;; Maintainer: John Maraist <[email protected]>
;; Keywords: org-publish, web, blog
;; Version: 0.1.0
;; X-URL: https://github.com/jphmrst/defblog
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file LICENSE. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; DEFBLOG is a wrapper around ORG-PUBLISH which allows you to declare
;; a simple structured blog. This package offers an all-Emacs
;; solution to maintaining a web site (except for any uploading via
;; rsync, which DEFBLOG can triggered through ORG-PUBLISH). For more
;; information, see the README.md file.
;;; NOTES AND CURRENT TODOs
;;; Things to have before calling it Version 1.0
;; TODO Format string for each page list entry in category.txt file
;; property.
;;
;; TODO Add options for CATEGORY-LINKS pragma in
;; DEFBLOG/PAGE-COPY-WITH-SUBSTITUTIONS
;;
;; Develop a scheme for tags, essentially as a virtual superset of
;; categories:
;;
;; - TODO Receive TAGS property in posts.
;;
;; - TODO Write TAG-LINKS pragma for DEFBLOG/PAGE-COPY-WITH-SUBSTITUTIONS.
;;
;; Refine HTACCESS file generation:
;;
;; - TODO Use the remote-htaccess argument for the remote destination
;; - of the htaccess file. If defined:
;;
;; -- Generate to a gen directory file
;;
;; -- Exclude .htaccess from rsync file list
;;
;; -- Second call to rsync for the htaccess target
;;
;; -- Remove this file in cleanup
;;
;; - TODO Add a forwards-list argument to defdoc for other forwards.
;;
;; TODO Ignore/warn about index.org source files in the category
;; directories.
;;; Other ideas and tasks
;; TODO Add a way to create the ORG for a page/post from scratch.
;;; Code:
(require 'anaphora-anywhere)
(defmacro if-show-state-dump (&rest forms)
"Debugging flag: execute FORMS when debugging."
`(progn ,@forms) ;; nil
)
(defconst +defblog/debug-level+ 0)
(defconst +defblog/debug-topics+ '(:control))
(cl-defmacro debug-msg ((level topic-or-topics) &rest args)
(when (and (<= level +defblog/debug-level+)
(or (eq topic-or-topics t)
(cl-intersection (cond
((symbolp topic-or-topics)
(list topic-or-topics))
(t topic-or-topics))
+defblog/debug-topics+ :test 'eq)))
`(message ,@args)))
(cl-defmacro defblog (name source-directory blog-title
&key
blog-url blog-desc
published-directory generated-directory
retain-published-directory
retain-generated-directories
(post-copy-function 'defblog/page-copy-verbatim)
(page-copy-function 'defblog/page-copy-verbatim)
(front-copy-function 'defblog/page-copy-verbatim)
(cat-index-title-fn
'(lambda (cat-plist blog-title)
(concatenate 'string
blog-title ": " (plist-get cat-plist :title))))
;;
css-style-rel-path
frontpage-css-style-rel-path
page-css-style-rel-path
post-css-style-rel-path
category-index-css-style-rel-path
;;
frontpage-section-numbers
page-section-numbers
post-section-numbers
category-index-section-numbers
;;
frontpage-table-of-contents
page-table-of-contents
post-table-of-contents
category-index-table-of-contents
;;
upload
;;
rsync-dest rsync-rsh
(rsync-options '("-rLptgoD"))
(rsync-delete-excluded t)
;;
(generate-xml-sitemap t)
(sitemap-default-change-freq 'monthly)
(sitemap-default-priority 0.5)
;;
(generate-rss t)
(generate-atom t)
default-author-name
;;
(generate-htaccess t)
remote-htaccess
;;
feed-entry-sunset)
"Declare a simple structured blog to be published with ORG-PUBLISH.
Information about file layout, Org property use, and other
details is in the README.md file at https://github.com/jphmrst/defblog .
Required parameters to this macro:
- NAME, a string used to identify this blog. This NAME is used by
ORG-PUBLISH to publish this particular blog, and it is also used to
name generated storage locations so that they do not conflict with
the names used for other blogs.
- SOURCE-DIRECTORY, a string giving the absolute pathname of the
directory containing the source directory, scratch work space,
and HTML output directory for this blog.
- BLOG-TITLE, a string with the human-oriented name for this web
site.
Optional parameters:
- BLOG-URL (respectively BLOG-DESC) gives the URL for the top of
this blog (human-facing description of the web site). The URL
is required when generating most of the XML artifacts.
- PUBLISHED-DIRECTORY and GENERATED-DIRECTORY, if given, are used
as temporary directories in generating the final website.
GENERATED-DIRECTORY is used to hold various partial images.
PUBLISHED-DIRECTORY is used to hold the final web site image before
uploading it to a remote server. If omitted, space in /tmp/ is
used for these directories. Normally the contents of these
directories is erased after generation; however
RETAIN-PUBLISHED-DIRECTORY and RETAIN-GENERATED-DIRECTORIES may
be set to non-null values to keep the directories' contents
instead.
- CSS-STYLE-SUBPATH, the local path from the SOURCE-DIRECTORY to
default CSS stylesheet for the blog.
- FRONTPAGE-CSS-STYLE-REL-PATH, PAGE-CSS-STYLE-REL-PATH,
POST-CSS-STYLE-REL-PATH and CATEGORY-INDEX-CSS-STYLE-REL-PATH are
local paths from the SOURCE-DIRECTORY to the CSS stylesheets for
those groups of pages. If not given, these arguments take the
value of CSS-STYLE-REL-PATH. For any of these, a NIL value means
there should be no CSS style sheet.
- GENERATE-XML-SITEMAP, if non-nil, indicates that an XML sitemap
should be generated. The SITEMAP-DEFAULT-CHANGE-FREQ (respectively
SITEMAP-DEFAULT-PRIORITY) argument gives the default value for
pages/posts which do not otherwise have a setting for the announced
change frequency (sitemap priority).
- GENERATE-RSS and GENERATE-ATOM indicate
whether the published blog should include these XML artifacts.
The RSS and Atom feeds are validated via
https://validator.w3.org/feed/ . The XML sitemap is validated via
https://www.xml-sitemaps.com/validate-xml-sitemap.html .
- FEED-ENTRY-SUNSET gives the length of time that a post should be
included in any XML feed (RSS or Atom). The value may be
1. An Emacs Lisp time value (used as-is: the age of a post
calculated via TIME-SUBTRACT, and compared to this upper
bound).
2. A Lisp list (passed as arguments to MAKE-DECODED-TIME,
whose result is used as a sunset bound as above).
3. A string (passed to PARSE-TIME-STRING, and used as an
absolute limit of the earliest date included.
- Most Org files in the source directory are copied into the
temporary space before being generated. By default, they are
copied with the function DEFBLOG/PAGE-COPY-VERBATIM, which copies
a file verbatim. However any function may be used. Defblog
also provides the function DEFBLOG/PAGE-COPY-WITH-SUBSTITUTIONS,
which expands certain pragmas in Org-mode comments to additional
content (see that function's documentation for additional
information). These copier functions are provided via two
arguments:
1. POST-COPY-FUNCTION, applied to posts.
2. FRONT-COPY-FUNCTION, applied to the top-level page.
"
;; Check fatal combinations of present/missing arguments.
(unless (file-directory-p source-directory)
(error "Expected a directory for SOURCE-DIRECTORY %s" source-directory))
(when (or generate-rss generate-atom)
(unless blog-url
(error "Generating an RSS/Atom feed requires BLOG-URL")))
(unless (or (null upload) (eq upload :rsync))
(error "Unrecognized value for upload: %s" upload))
(when (and (null upload) (null published-directory))
(warn
"No upload method specified, but no local :PUBLISHED-DIRECTORY given"))
;; Refinements to the given arguments.
(unless (string-match "/$" source-directory)
(setf source-directory (concatenate 'string source-directory "/")))
;; CSS-STYLE-REL-PATH as the default for various page groups.
(unless frontpage-css-style-rel-path
(setf frontpage-css-style-rel-path css-style-rel-path))
(unless page-css-style-rel-path
(setf page-css-style-rel-path css-style-rel-path))
(unless post-css-style-rel-path
(setf post-css-style-rel-path (concatenate 'string
"../" css-style-rel-path)))
(unless category-index-css-style-rel-path
(setf category-index-css-style-rel-path (concatenate 'string
"../" css-style-rel-path)))
(let (;; The stateful structures associated with this blog, to be
;; updated before each time the blog HTML is built. Each of
;; these names is associated with a DEFVAR in the macro
;; expansion.
(site-plist-var (intern (concatenate 'string
"+defblog/" name "/site-plist+")))
(file-plists-hash (intern (concatenate 'string
"+defblog/" name "/file-plists-hash+")))
(category-tags (intern (concatenate 'string
"*defblog/" name "/category-tags*")))
(category-plists-hash (intern (concatenate 'string
"+defblog/" name
"/category-plists-hash+")))
;; Names of global constants associated with this blog. Each
;; of these names is also associated with a DEFVAR in the
;; macro expansion.
(source-directory-var (intern (concatenate 'string
"+defblog/" name "/src-basedir+")))
(publish-directory-var (intern (concatenate 'string
"+defblog/" name "/pub-basedir+")))
(gen-directory-var (intern (concatenate 'string
"+defblog/" name "/tmp-basedir+")))
(get-posts-directory-var (intern (concatenate 'string
"+defblog/" name "/posts-basedir+")))
(get-pages-directory-var (intern (concatenate 'string
"+defblog/" name "/pages-basedir+")))
(gen-cat-indices-directory-var (intern (concatenate 'string
"+defblog/" name "/cat-indices+")))
(gen-statics-directory (intern (concatenate 'string
"+defblog/" name "/gen-statics+")))
(last-blog-update (intern (concatenate 'string
"+defblog/" name "/last-blog-update+")))
(system-tmp-dir-var (intern (concatenate 'string
"+defblog/" name "/system-tmp-dir+")))
;; Names of functions associated with this blog. Each of
;; these names is associated with a DEFUN in the macro
;; expansion.
(cat-indices-prep-fn (intern (concatenate 'string
"defblog/" name "/cat-indices-prep")))
(gen-statics-prep-fn (intern (concatenate 'string
"defblog/" name "/gen-statics-prep")))
(posts-prep-fn (intern (concatenate 'string
"defblog/" name "/posts-prep")))
(pages-prep-fn (intern (concatenate 'string
"defblog/" name "/pages-prep")))
(overall-setup-fn (intern (concatenate 'string
"defblog/" name "/overall-setup")))
(overall-cleanup-fn (intern (concatenate 'string
"defblog/" name "/overall-cleanup")))
(state-dump-fn (intern (concatenate 'string
"defblog/" name "/state-dump")))
(system-tmp-dir-rm-fn (intern (concatenate 'string
"defblog/" name "/rm-system-tmp-dir")))
;; Sunset time for feed entries.
(feed-entry-sunset-pred
(cond
((null feed-entry-sunset)
(debug-msg (3 :internal) "feed-entry-sunset-pred (1)")
`#'(lambda (x) t))
((or (integerp feed-entry-sunset)
(and (consp feed-entry-sunset)
(integerp (car feed-entry-sunset))
(integerp (cdr feed-entry-sunset)))
(and (listp feed-entry-sunset)
(eql 4 (length feed-entry-sunset))
(integerp (car feed-entry-sunset))
(integerp (cadr feed-entry-sunset))
(integerp (caddr feed-entry-sunset))
(integerp (cadddr feed-entry-sunset))))
(debug-msg (3 :internal) "feed-entry-sunset-pred (2)")
`#'(lambda (x)
(let ((min (time-subtract (current-time)
',feed-entry-sunset)))
(debug-msg (3 :internal) " - Comparing to minimum %s"
(format-time-string "%d %b %Y" min))
(time-less-p min x))))
((listp feed-entry-sunset)
(debug-msg (3 :internal) "feed-entry-sunset-pred (3)")
(let ((bound (mapcar
#'(lambda (x) (cond ((null x) 0) (t (- x))))
(apply #'make-decoded-time feed-entry-sunset))))
`#'(lambda (x)
(let ((min (encode-time (decoded-time-add
(decode-time (current-time))
',bound))))
(debug-msg (3 :internal) " - Comparing to minimum %s"
(format-time-string "%d %b %Y" min))
(time-less-p min x)))))
((stringp feed-entry-sunset)
(debug-msg (3 :internal) "feed-entry-sunset-pred (4)")
(let ((min (parse-time-string feed-entry-sunset)))
`#'(lambda (x) (time-less-p ',min x))))
(t (error "Unrecognized value for FEED-ENTRY-SUNSET: %s"
feed-entry-sunset)))))
`(progn
;; DEFVARs corresponding to the stateful components of this
;; blog.
(when (boundp ',site-plist-var) (makunbound ',site-plist-var))
(defvar ,site-plist-var nil
,(concatenate 'string "General property list for the " name " blog."))
(when (boundp ',file-plists-hash) (makunbound ',file-plists-hash))
(defvar ,file-plists-hash (make-hash-table :test 'eq)
,(concatenate 'string
"Hashtable for holding properties of the posts and pages of the "
name " blog."))
(when (boundp ',category-tags) (makunbound ',category-tags))
(defvar ,category-tags nil
,(concatenate 'string
"Storage for the list of categories in the " name " blog."))
(when (boundp ',category-plists-hash)
(makunbound ',category-plists-hash))
(defvar ,category-plists-hash (make-hash-table :test 'eq)
,(concatenate 'string
"Hashtable for holding properties of the categories of the "
name " blog."))
;; Managing temporary directories. Since the Org source
;; directories in ORG-PUBLISH-PROJECT-ALIST are hardcoded, we
;; need to fix a directory in /tmp when calling defblog.
(when (boundp ',system-tmp-dir-var)
(when ,system-tmp-dir-var
(delete-directory ,system-tmp-dir-var t))
(makunbound ',system-tmp-dir-var))
(defvar ,system-tmp-dir-var
,(when (or (null published-directory) (null generated-directory))
`(make-temp-file "defblog-" t)))
(unless (string-match "/$" ,system-tmp-dir-var)
(setf ,system-tmp-dir-var
(concatenate 'string ,system-tmp-dir-var "/")))
(defun ,system-tmp-dir-rm-fn ()
(when ,system-tmp-dir-var
(delete-directory ,system-tmp-dir-var t)))
(add-hook 'kill-emacs-hook ',system-tmp-dir-rm-fn)
;; DEFVARs corresponding to the constants defined for this
;; blog.
(when (boundp ',source-directory-var)
(makunbound ',source-directory-var))
(defvar ,source-directory-var ,source-directory
,(concatenate 'string
"Directory with the source ORG files of the " name " blog."))
(when (boundp ',publish-directory-var)
(makunbound ',publish-directory-var))
(defvar ,publish-directory-var
,(cond
(generated-directory published-directory)
(t `(concatenate 'string ,system-tmp-dir-var "pub/")))
,(concatenate 'string
"Target directory for publishable files of the " name " blog."))
(when (boundp ',gen-directory-var)
(makunbound ',gen-directory-var))
(defvar ,gen-directory-var
,(cond
(generated-directory generated-directory)
(t system-tmp-dir-var))
,(concatenate 'string
"Scratch space directory for the " name " blog."))
(when (boundp ',get-posts-directory-var)
(makunbound ',get-posts-directory-var))
(defvar ,get-posts-directory-var
(concatenate 'string ,gen-directory-var "posts/")
,(concatenate 'string
"Scratch space directory for copying over posts for the " name " blog."))
(when (boundp ',get-pages-directory-var)
(makunbound ',get-pages-directory-var))
(defvar ,get-pages-directory-var
(concatenate 'string ,gen-directory-var "pages/")
,(concatenate 'string
"Scratch space directory for copying non-index top-level pages for the " name " blog."))
(when (boundp ',gen-cat-indices-directory-var) (makunbound ',gen-cat-indices-directory-var))
(defvar ,gen-cat-indices-directory-var
(concatenate 'string ,gen-directory-var "cat-indices/")
,(concatenate 'string
"Scratch space area for generating category index files for the "
name " blog."))
(when (boundp ',gen-statics-directory) (makunbound ',gen-statics-directory))
(defvar ,gen-statics-directory
(concatenate 'string ,gen-directory-var "gen-statics/")
,(concatenate 'string
"Scratch space area for generating XML files for the "
name " blog."))
(when (boundp ',last-blog-update) (makunbound ',last-blog-update))
(defvar ,last-blog-update nil
"Last post or update to this blog")
;; DEFUNs used in the ORG-PUBLISH-PROJECT-ALIST clauses for
;; this blog. Each of these will add additional blog-specific
;; parameters to a call to a related function defined after
;; this macro expansion.
(defun ,overall-setup-fn (properties)
(debug-msg (0 t) "Ensuring clean temporary directories...")
(ensure-ready-work-dir ,publish-directory-var)
(dolist (subdir +defblog/scratch-subdirectories+)
(ensure-ready-work-dir (concatenate 'string
,gen-directory-var subdir "/")))
(debug-msg (0 t) "Ensuring clean temporary directories...done")
(debug-msg (0 t) "Setting up defblog temp structures...")
(defblog/table-setup-fn properties
,gen-directory-var ,source-directory-var
,file-plists-hash ,category-plists-hash
#'(lambda (x) (setf ,category-tags x))
#'(lambda () ,category-tags)
#'(lambda (x) (setf ,last-blog-update x))
#'(lambda (x) (setf ,site-plist-var x))
,blog-title ,blog-desc ,blog-url)
(debug-msg (0 t) "Setting up defblog temp structures...done")
(,state-dump-fn)
;; The setup for the front page is just to copy it in to its
;; scratch area.
(let ((source-org (concatenate 'string
,source-directory-var "index.org")))
(funcall #',front-copy-function
source-org
(concatenate 'string ,gen-directory-var "front/index.org")
,site-plist-var
(gethash (intern source-org) ,file-plists-hash))))
(defun ,overall-cleanup-fn (properties)
(unless ,retain-published-directory
(debug-msg (0 t) "Removing pub directory...")
(delete-directory ,publish-directory-var t)
(debug-msg (0 t) "Removing pub directory...done"))
(unless ,retain-generated-directories
(debug-msg (0 t) "Removing scratch directories...")
(dolist (subdir +defblog/scratch-subdirectories+)
(let ((fullpath (concatenate 'string
,gen-directory-var subdir "/")))
(when (file-directory-p fullpath)
(delete-directory fullpath t))))
(debug-msg (0 t) "Removing scratch directories...done"))
(cond
((eq ,upload :rsync)
(debug-msg (0 t) "Uploading...")
(let ((args
(list "rsync" nil "*org-publish-rsync*" nil
,@rsync-options
,@(when rsync-rsh
(list (concatenate 'string
"--rsh=" rsync-rsh)))
,@(when rsync-delete-excluded `("--delete-excluded"))
,publish-directory-var
,rsync-dest)))
(debug-msg (3 :internal) "Invoking rsync:\n %s" args)
(apply #'call-process args))
(debug-msg (0 t) "Uploading...done"))
((null ,upload)
(debug-msg (0 t) "Uploading not selected")))
(debug-msg (0 t) "Cleaning up defblog temp structures...")
(clrhash ,file-plists-hash)
(clrhash ,category-plists-hash)
(setf ,category-tags nil)
(debug-msg (0 t) "Cleaning up defblog temp structures...done"))
(defun ,state-dump-fn ()
(if-show-state-dump
(defblog/state-dump ,site-plist-var ,file-plists-hash
,category-tags ,category-plists-hash)))
(defun ,cat-indices-prep-fn (properties)
(defblog/cat-indices-prep #'(lambda () ,category-tags)
,blog-title
,category-plists-hash ,file-plists-hash
,gen-directory-var ,source-directory-var
,category-index-css-style-rel-path #',cat-index-title-fn))
(defun ,gen-statics-prep-fn (properties)
(defblog/gen-statics-prep properties ,source-directory-var
,gen-directory-var ,publish-directory-var
,file-plists-hash ,category-plists-hash
,category-tags ,blog-title ,blog-desc ,blog-url ,last-blog-update
,generate-xml-sitemap ,generate-rss ,generate-atom
,generate-htaccess ,default-author-name ,feed-entry-sunset-pred
(symbol-name ',sitemap-default-change-freq)
,sitemap-default-priority))
(defun ,posts-prep-fn (properties)
(defblog/posts-prep ,category-tags ,category-plists-hash
,gen-directory-var ,source-directory-var #',post-copy-function
,site-plist-var ,file-plists-hash))
(defun ,pages-prep-fn (properties)
(defblog/pages-prep ,category-tags ,category-plists-hash
,gen-directory-var ,source-directory-var #',page-copy-function
,site-plist-var ,file-plists-hash))
;; Register this blog with org-project.
(let ((cleaned-alist (alist-remove-string-key
,(concatenate 'string name "-top-page")
(alist-remove-string-key
,(concatenate 'string name "-pages")
(alist-remove-string-key
,(concatenate 'string name "-cat-indices")
(alist-remove-string-key
,(concatenate 'string name "-gen-statics")
(alist-remove-string-key
,(concatenate 'string name "-src-statics")
(alist-remove-string-key
,(concatenate 'string name "-posts")
(alist-remove-string-key
,(concatenate 'string name)
org-publish-project-alist))))))))
;; Convert the top-level front page from the source
;; directory to the pub directory --- this file only, no
;; need to copy it anywhere.
(top-page-entry
(list :preparation-function ',overall-setup-fn
:base-directory (concatenate 'string
,gen-directory-var "front/")
:publishing-directory ,publish-directory-var
:publishing-function 'org-html-publish-to-html
:section-numbers ,frontpage-section-numbers
:table-of-contents ,frontpage-table-of-contents
:with-toc ,frontpage-table-of-contents
:exclude ".*" :include '("index.org")
:recursive nil
:html-postamble nil
,@(when frontpage-css-style-rel-path
`(:html-preamble
,(concatenate 'string
"<link rel=stylesheet type=\"text/css\" href=\""
frontpage-css-style-rel-path
"\" />")))))
;; Other (top-level) non-index pages: right now, convert
;; straight from the source directory to the pub area.
;;
;; But maybe these should really be copied with
;; header/footer org-text into scratch area?
(pages-entry
(list :preparation-function ',pages-prep-fn
:publishing-function 'org-html-publish-to-html
:base-directory ,get-pages-directory-var
:publishing-directory ,publish-directory-var
:exclude "index.org"
:html-postamble
"<a href=\"./\">Back to the top</a>."
:recursive nil
:section-numbers ,page-section-numbers
:table-of-contents ,page-table-of-contents
:with-toc ,page-table-of-contents
,@(when page-css-style-rel-path
`(:html-preamble
,(concatenate 'string
"<link rel=stylesheet type=\"text/css\" href=\""
page-css-style-rel-path
"\" />")))))
;; Category indices: generate ORG files into tmp space,
;; and then convert.
(cat-indices-entry
(list :preparation-function ',cat-indices-prep-fn
:publishing-function 'org-html-publish-to-html
:base-directory ,gen-cat-indices-directory-var
:publishing-directory ,publish-directory-var
:html-postamble
"<a href=\"../\">Back to the top</a>."
:recursive t
:section-numbers ,category-index-section-numbers
:table-of-contents ,category-index-table-of-contents
:with-toc ,category-index-table-of-contents
,@(when category-index-css-style-rel-path
`(:html-preamble
,(concatenate 'string
"<link rel=stylesheet type=\"text/css\" href=\""
category-index-css-style-rel-path
"\" />")))))
;; XML files: generate XML files into tmp space, and then
;; publishing copies over to pub space. Note that this
;; target contains the OVERALL-CLEANUP-FN, and so it
;; should be invoked last.
(gen-statics-entry
(list :publishing-function 'org-publish-attachment
:preparation-function ',gen-statics-prep-fn
:completion-function ',overall-cleanup-fn
:base-directory ,gen-statics-directory
:base-extension "xml"
:publishing-directory ,publish-directory-var
:recursive t))
;; Static files in the source directory that can be
;; copied over to pub space without translation.
(src-statics-entry
(list :publishing-function 'org-publish-attachment
:base-directory ,source-directory-var
:base-extension "html\\|css\\|jpg\\|gif\\|png\\|xml\\|pdf\\|el\\|gz\\|uu"
:publishing-directory ,publish-directory-var
:recursive t))
;; Individual posts are copied into tmp/posts (its
;; subdirectories created as well), and converted from
;; there.
(posts-entry
(list :preparation-function ',posts-prep-fn
:base-directory ,get-posts-directory-var
:publishing-directory ,publish-directory-var
:html-postamble "<a href=\"../\">Back to the top</a>, or <a href=\"./\">more like this</a>."
:recursive t
:publishing-function 'org-html-publish-to-html
:section-numbers ,post-section-numbers
:table-of-contents ,post-table-of-contents
:with-toc ,post-table-of-contents
,@(when post-css-style-rel-path
`(:html-preamble
,(concatenate 'string
"<link rel=stylesheet type=\"text/css\" href=\""
post-css-style-rel-path
"\" />")))))
(overall-target
'(:components (;; Do *-top-page first; it has the side
;; effect of updating the properties
;; hashtable.
,(concatenate 'string name "-top-page")
,(concatenate 'string name "-pages")
,(concatenate 'string name "-cat-indices")
,(concatenate 'string name "-posts")
,(concatenate 'string name "-src-statics")
;; Do *-gen-statics last; it has the side
;; effect of calling the OVERALL-CLEANUP
;; function (to discard temporary
;; structures and directories) after it
;; publishes.
,(concatenate 'string name "-gen-statics")
))))
(setf org-publish-project-alist cleaned-alist)
(push (cons ,(concatenate 'string name "-top-page") top-page-entry)
org-publish-project-alist)
(push (cons ,(concatenate 'string name "-pages") pages-entry)
org-publish-project-alist)
(push (cons ,(concatenate 'string name "-cat-indices")
cat-indices-entry)
org-publish-project-alist)
(push (cons ,(concatenate 'string name "-gen-statics")
gen-statics-entry)
org-publish-project-alist)
(push (cons ,(concatenate 'string name "-src-statics")
src-statics-entry)
org-publish-project-alist)
(push (cons ,(concatenate 'string name "-posts") posts-entry)
org-publish-project-alist)
(push (cons ,name overall-target) org-publish-project-alist))
(debug-msg (0 t) "Defined blog %s; use org-publish to generate" ',name))))
;;; =================================================================
;;; Preparing and cleaning temporary directories.
(defun ensure-ready-work-dir (pathname)
"Prepare the temporary directory PATHNAME for blog generation."
;; If the directory already exists, remove its contents.
(when (file-directory-p pathname)
(dolist (item (directory-files pathname t))
(cond
((string-match "/\\.\\.?$" item) nil) ;; . or .. --- skip
((file-directory-p item) (delete-directory item t))
(t (delete-file item)))))
;; If the directory does not exist at all, create it.
(unless (file-directory-p pathname)
(make-directory pathname t)))
;;; =================================================================
;;; Preparing the hash tables and reference lists at the start of a
;;; blog build.
(defun defblog/table-setup-fn (properties gen-directory source-directory
file-plist-hash category-plist-hash
cat-list-setter cat-list-getter
last-post-setter site-plist-setter
site-title site-desc site-url)
"Reset the global structures associated with a blog.
- PROPERTIES is the property list provided from ORG-PUBLISH.
- GEN-DIRECTORY is the root directory of the temporary files area
- FILE-PLIST-HASH is the hashtable from paths to ORG files, to the plist of
information extracted from that file.
- CAT-LIST-SETTER and CAT-LIST-GETTER are thunks which set (respectively, get)
the category list global variable for this blog."
(defblog/reset-categories-list source-directory cat-list-setter)
(defblog/reset-categories-plist-hash source-directory
(funcall cat-list-getter) category-plist-hash)
(defblog/reset-file-plist-hash source-directory file-plist-hash
category-plist-hash)
(defblog/add-table-summary-data file-plist-hash category-plist-hash
last-post-setter)
(funcall site-plist-setter
(list :file-plists-hash file-plist-hash
:cat-plists-hash category-plist-hash
:sorted-file-plists
(sort (hash-table-values file-plist-hash)
#'(lambda (x y)
(time-less-p (plist-get y :mod)
(plist-get x :mod))))
:title site-title :desc site-desc :url site-url)))
;;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;;; Managing the file-plist hashtable.
(defun defblog/fetch-file-plist (path file-plist-hash)
(let ((result (gethash (intern path) file-plist-hash)))
(debug-msg (3 :internal) "Cached %s --> %s" path result)
result))
(defun defblog/reset-file-plist-hash (source-directory file-plist-hash
category-plist-hash)
"Set up the properties hash."
(clrhash file-plist-hash)
(let ((top-contents (directory-files source-directory)))
(dolist (item top-contents)
(let ((item-fullpath (concatenate 'string source-directory item)))
(defblog/process-file-for-hash item nil 0 item-fullpath file-plist-hash
category-plist-hash))))
(debug-msg (3 :internal) "Finished property hash reset")
)
(defun defblog/process-file-for-hash (bare-name cat-tag depth full-path
file-plist-hash category-plist-hash)
"Recursive function for populating the properties hash from a given file."
(debug-msg (3 :internal) "Processing %s" full-path)
(when (file-directory-p full-path)
(setf full-path (concatenate 'string full-path "/")))
(cond
;; If it's directory, recursively traverse that directory.
((and (file-directory-p full-path) (not (string-match "^\\." bare-name)))
(debug-msg (3 :internal) "- Recurring for directory contents")
(let ((dir-contents (directory-files full-path)))
(dolist (dir-item dir-contents)
(let ((dir-item-fullpath (concatenate 'string full-path dir-item)))
(defblog/process-file-for-hash dir-item bare-name (+ 1 depth)
dir-item-fullpath file-plist-hash category-plist-hash)))))
;; If it's an ORGMODE file, pull and cache its properties.
((string-match "\\.org$" bare-name)
(multiple-value-bind (plist posted-date update-date)
(defblog/build-file-plist bare-name cat-tag full-path depth)
(debug-msg (3 :internal) "- Caching %s --> %s" full-path plist)
(puthash (intern full-path) plist file-plist-hash)
;; When debugging we may want to know about this fall-through.
;; (t (debug-msg (0 t) "- No action for %s" full-path))
))))
(defun defblog/build-file-plist (bare-file cat-tag path depth)
"Extract a list of the properties we need from the file at the given PATH.
- BARE-FILE and PATH should refer to the same file; the former excludes all
surrounding directories.
- CAT-TAG is either the category of a post, or NIL for pages. DEPTH is the
number of subdirectories from the top level where the file lives:
currently zero for pages, one for posts."
(debug-msg (3 :internal) "* Start defblog/build-file-plist %s" path)
(let ((keyvals (defblog/get-orgfile-properties path)))
(debug-msg (3 :internal) " keyvals %s" keyvals)
(multiple-value-bind (result posted-date update-date)
(defblog/format-orgprops-plist bare-file cat-tag path depth keyvals)
(debug-msg (3 :internal) " result %s" result)
(values result posted-date update-date))))
(defun defblog/format-orgprops-plist (bare-file cat-tag path depth keyvals)
"Given a key-values list, set up a plist for a file path."
(let* ((bare-date (assoc "DATE" keyvals))
(bare-updated (assoc "UPDATED" keyvals))
(priority (nth 1 (assoc "SITEMAP_PRIORITY" keyvals)))
(change-freq (nth 1 (assoc "CHANGE_FREQ" keyvals)))
(post-date (cond
(bare-date (date-to-time (nth 1 bare-date)))
(t nil)))
(post-updated (cond
(bare-updated (date-to-time (nth 1 bare-updated)))
(t nil)))
(post-mod (cond
((and (null post-date) (null post-updated))
+web-announcement-date+)
((null post-date) post-updated)
((null post-updated) post-date)
((time-less-p post-date post-updated) post-updated)
(t post-date))))
(values (list :bare bare-file :path path :depth depth
:title (nth 1 (assoc "TITLE" keyvals))
:desc (nth 1 (assoc "DESCRIPTION" keyvals))
:link (nth 1 (assoc "LINK" keyvals))
:cat cat-tag
:author-name (nth 1 (assoc "AUTHOR_NAME" keyvals))
:date post-date :updated post-updated
:old-urls (when (its (nth 1 (assoc "OLD_URL" keyvals)))
(split-string (it) " *, *"))
:mod post-mod
:sitemap-priority priority :change-freq change-freq)
post-date post-updated)))
(defun defblog/reset-categories-list (source-directory cat-list-setter)
(let ((category-tag-list nil))
(debug-msg (3 :internal) "srcdir %s" source-directory)
(debug-msg (3 :internal) "items %s" (directory-files source-directory))
;; Look at each file in the source directory.
(dolist (item (directory-files source-directory))
(debug-msg (3 :internal) "Checking %s" item)
;; We are skipping any dotfiles
(unless (string-match "^\\." item)
(debug-msg (3 :internal) "- not a dotfile")
(let ((cat-dir-path (concatenate 'string source-directory item "/")))
;; We are also only looking at directories
(when (file-directory-p cat-dir-path)
(debug-msg (3 :internal) "- is a directory")
;; Make sure there is a category.txt file in this
;; directory.
(let ((cat-path (concatenate 'string cat-dir-path "category.txt")))
(when (file-regular-p cat-path)
;; Add the tag to the result list
(debug-msg (3 :internal) "- include %s in category list" item)
(push item category-tag-list)))))))
(funcall cat-list-setter category-tag-list)))
(defun defblog/reset-categories-plist-hash (source-directory category-tag-list
category-plist-hash)
"Given the categories list, rebuild the cateogories plist hashtable."
;; Clear anything previously in the hashtable.
(clrhash category-plist-hash)
;; For each category tag
(dolist (cat-tag category-tag-list)
;; Extract the ORG properties of the category.txt file.
(let* ((cat-src-dir (concatenate 'string source-directory cat-tag "/"))
(full-path (concatenate 'string cat-src-dir "category.txt"))
(posts-list (filter #'(lambda (n) (string-match "\\.org$" n))
(directory-files cat-src-dir)))
(keyvals (defblog/get-orgfile-properties full-path)))
;; Form a plist for the category.
(let ((plist
`(:tag ,cat-tag
:src-dir ,cat-src-dir
:title ,(nth 1 (assoc "TITLE" keyvals))
:description ,(nth 1 (assoc "DESCRIPTION" keyvals))
:sitemap-priority ,(nth 1 (assoc "SITEMAP_PRIORITY"
keyvals))
:change-freq ,(nth 1 (assoc "CHANGE_FREQ" keyvals))
:post-files ,posts-list)))
;; Store the plist in the hash.
(debug-msg (3 :internal) "%s\n %s %s\n %s %s"
full-path cat-tag keyvals (intern cat-tag) plist)
(puthash (intern cat-tag) plist category-plist-hash)
(debug-msg (3 :internal) " %s"
(gethash (intern cat-tag)
+defblog/maraist/category-plists-hash+))))))
;;; =================================================================
;;; Crossreferencing information built into the hashtables.
(defun defblog/add-table-summary-data (file-plist-hash category-plist-hash
last-post-setter)
"Calculate additional summary information for the plist tables.
- FILE-PLIST-HASH (respectively CATEGORY-PLIST-HASH) maps absolute pathnames
\(category names) to their property list."
(let ((last-blog-update +web-announcement-date+))
(dolist (cat (hash-table-keys category-plist-hash))
(debug-msg (3 :internal) "Crossreferencing for category %s" cat)
(let* ((cat-plist (gethash cat category-plist-hash))
(cat-src-dir (plist-get cat-plist :src-dir))
(cat-post-files (plist-get cat-plist :post-files))
(latest-post +web-announcement-date+)
(latest-update +web-announcement-date+))
(debug-msg (3 :internal) "- Has plist %s" cat-plist)
(dolist (post-file cat-post-files)
(debug-msg (3 :internal) " - For file %s" post-file)
(let* ((file-fullpath (concatenate 'string cat-src-dir post-file))
(file-plist (gethash (intern file-fullpath) file-plist-hash))
(file-posted (plist-get file-plist :date))
(file-updated (plist-get file-plist :updated)))
(debug-msg (3 :internal) " %s" file-fullpath)
(debug-msg (3 :internal) " %s" file-plist)
(debug-msg (3 :internal) " %s %s" file-posted file-updated)
(when (and (time-less-p latest-post file-posted) file-posted)
(debug-msg (3 :internal) " Updating latest post time")
(setf latest-post file-posted))
(when (and file-updated (time-less-p latest-update file-updated))
(debug-msg (3 :internal) " Updating latest update time")
(setf latest-update file-updated))))
(puthash cat
(plist-put (plist-put (plist-put cat-plist
:latest-post latest-post)
:latest-update latest-update)
:latest-mod (cond
((null latest-post) latest-update)
((null latest-update) nil)
((time-less-p latest-post
latest-update)
latest-update)
(t latest-post)))
category-plist-hash)
(when (time-less-p last-blog-update latest-post)
(setf last-blog-update latest-post))
(when (time-less-p last-blog-update latest-update)
(setf last-blog-update latest-update))))
(funcall last-post-setter last-blog-update)))
;;; =================================================================
;;; Generating non-ORG/HTML files.
(defun defblog/gen-statics-prep (properties source-directory
gen-directory pub-directory
file-plist-hash cat-plist-hash
category-tags blog-name blog-desc blog-url
last-update generate-xml-sitemap
generate-rss generate-atom generate-htaccess
default-author-name feed-entry-sunset-pred
default-change-freq default-priority)
"Generate XML and other non-ORG/HTML files.