-
Notifications
You must be signed in to change notification settings - Fork 132
/
ox-hugo.el
5002 lines (4423 loc) · 242 KB
/
ox-hugo.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
;;; ox-hugo.el --- Hugo Markdown Back-End for Org Export Engine -*- lexical-binding: t -*-
;; Author: Kaushal Modi <kaushal.modi@gmail.com>
;; Matt Price <moptop99@gmail.com>
;; Version: 0.12.1
;; Package-Requires: ((emacs "26.3") (tomelr "0.4.3"))
;; Keywords: Org, markdown, docs
;; URL: https://ox-hugo.scripter.co
;; This file is not part of GNU Emacs.
;; 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 of the License, 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; ox-hugo implements a Markdown back-end for Org exporter. The
;; exported Markdown is compatible with the Hugo static site generator
;; (https://gohugo.io/). This exporter also generates the post
;; front-matter in TOML or YAML.
;; To start using this exporter, add the below to your Emacs config:
;;
;; (with-eval-after-load 'ox
;; (require 'ox-hugo))
;;
;; With the above evaluated, the ox-hugo exporter options will be
;; available in the Org Export Dispatcher. The ox-hugo export
;; commands have bindings beginning with "H" (for Hugo).
;;
;; # Blogging Flows
;;
;; 1. one-post-per-subtree flow :: A single Org file can have multiple
;; Org subtrees which export to individual Hugo posts. Each of
;; those subtrees that has the EXPORT_FILE_NAME property set is
;; called a 'valid Hugo post subtree' in this package and its
;; documentation.
;;
;; 2. one-post-per-file flow :: A single Org file exports to only
;; *one* Hugo post. An Org file intended to be exported by this
;; flow must not have any 'valid Hugo post subtrees', and instead
;; must have the #+title property set.
;;
;; # Commonly used export commands
;;
;; ## For both one-post-per-subtree and one-post-per-file flows
;;
;; - C-c C-e H H -> Export "What I Mean".
;; - If point is in a 'valid Hugo post subtree',
;; export that subtree to a Hugo post in
;; Markdown.
;; - If the file is intended to be exported as a
;; whole (i.e. has the #+title keyword),
;; export the whole Org file to a Hugo post in
;; Markdown.
;;
;; - C-c C-e H A -> Export *all* "What I Mean"
;; - If the Org file has one or more 'valid Hugo
;; post subtrees', export them to Hugo posts in
;; Markdown.
;; - If the file is intended to be exported as a
;; whole (i.e. no 'valid Hugo post subtrees'
;; at all, and has the #+title keyword),
;; export the whole Org file to a Hugo post in
;; Markdown.
;;
;; ## For only the one-post-per-file flow
;;
;; - C-c C-e H h -> Export the Org file to a Hugo post in Markdown.
;; Do M-x customize-group, and select `org-export-hugo' to see the
;; available customization options for this package.
;; See this package's website for more instructions and examples:
;;
;; https://ox-hugo.scripter.co
;;; Code:
(require 'tomelr) ;For `tomelr-encode'
(require 'ox-blackfriday)
(require 'ffap) ;For `ffap-url-regexp'
(require 'ob-core) ;For `org-babel-parse-header-arguments'
;; `org-refile.el' is new in Org 9.4
;; https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=f636cf91b6cbe322eca56e23283f4614548c9d65
(require 'org-refile nil :noerror) ;For `org-get-outline-path'
(require 'org)
(require 'org-id) ;For `org-id-find'
;; For `org-info-emacs-documents', `org-info-other-documents'
;; org-info.el got renamed to ol-info.el in Org version 9.3. Remove
;; below if condition after the minimum emacs dependency is raised to
;; emacs 27.x. The Org version shipped with Emacs 26.3 is 9.1.9.
(if (version< (org-version) "9.3")
(require 'org-info)
(require 'ol-info))
(declare-function org-hugo-pandoc-cite--parse-citations-maybe "ox-hugo-pandoc-cite")
(declare-function org-hugo-pandoc-cite--meta-data-generator "ox-hugo-pandoc-cite")
(require 'ox-hugo-deprecated)
(defvar ffap-url-regexp) ;Silence byte-compiler
;; Using the correct function for getting inherited Org tags.
;; Starting Org 9.2, `org-get-tags' returns all the inherited tags
;; instead of returning only the local tags i.e. only the current
;; heading tags.
;; https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=fbe56f89f75a8979e0ba48001a822518df2c66fe
;; For Org <= 9.1, `org-get-tags' returned a list of tags *only* at
;; the current heading, while `org-get-tags-at' returned inherited
;; tags too.
(with-no-warnings
(if (fboundp #'org--get-local-tags) ;If using Org 9.2+
(defalias 'org-hugo--get-tags 'org-get-tags)
(defalias 'org-hugo--get-tags 'org-get-tags-at)))
;; `org-back-to-heading-or-point-min' was introduced in Org 9.5 in
;; https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=1bdff9f73dc1e7ff625a90e3e61350bdea99f29c.
;; If a user is using a slightly older version of Org (like 9.3),
;; define it.
(unless (fboundp #'org-back-to-heading-or-point-min)
(defun org-back-to-heading-or-point-min (&optional invisible-ok)
"Go back to heading or first point in buffer.
If point is before first heading go to first point in buffer
instead of back to heading."
(if (org-before-first-heading-p)
(goto-char (point-min))
(org-back-to-heading invisible-ok))))
(defvar org-hugo--subtree-coord nil
"Variable to store the current valid Hugo subtree coordinates.
It holds the value returned by
`org-hugo--get-post-subtree-coordinates'.")
(defvar org-hugo--subtree-count 0
"Variable to count of number of subtrees getting exported.
This variable is used when exporting all subtrees in a file.")
(defvar org-hugo--fm nil
"Variable to store the current Hugo post's front-matter string.
This variable is used to cache the original ox-hugo generated
front-matter that's used after Pandoc Citation parsing.")
(defvar org-hugo--fm-yaml nil
"Variable to store the current Hugo post's front-matter string in YAML format.
Pandoc understands meta-data only in YAML format. So when Pandoc
Citations are enabled, Pandoc is handed over the file with this
YAML front-matter.")
(defvar org-hugo--internal-list-separator "\n"
"String used to separate elements in list variables.
Examples are internal variables holding Hugo tags, categories and
keywords.
This variable is for internal use only, and must not be
modified.")
(defvar org-hugo--date-time-regexp (concat "\\`[[:digit:]]\\{4\\}-[[:digit:]]\\{2\\}-[[:digit:]]\\{2\\}"
"\\(?:T[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}"
"\\(?:Z\\|[+-][[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}\\)*\\)*\\'")
"Regexp to match the Hugo time stamp strings.
Reference: https://tools.ietf.org/html/rfc3339#section-5.8
Examples:
2017-07-31
2017-07-31T17:05:38
2017-07-31T17:05:38Z
2017-07-31T17:05:38+04:00
2017-07-31T17:05:38-04:00.")
(defvar org-hugo--trim-pre-marker "<!-- trim-pre -->"
"Special string to mark where whitespace should be trimmed before an element.")
(defvar org-hugo--trim-post-marker "<!-- trim-post -->"
"Special string to mark where whitespace should be trimmed after an element.")
(defvar org-hugo--opened-buffers '()
"List of buffers opened during an export, which will be auto-closed at the end.
An export operation might need to open files for resolving links
pointing to other Org files or temporary buffers for
pre-processing an Org file. Each buffer opened during an Ox-Hugo
export gets added to this list, and they all are auto-closed at
the end of the export in `org-hugo--after-all-exports-function'.")
(defvar org-hugo--disable-after-all-exports-hook nil
"If set, `org-hugo--after-all-exports-function' function is not called.
This variable is set internally by `org-hugo-export-wim-to-md'
when its ALL-SUBTREES arg is set to a non-nil value.
Setting this to non-nil will lead to slow or incorrect
exports. This variable is for internal use only, and must not be
modified.")
(defvar org-hugo--all-subtrees-export--functions-to-silence
'(org-babel-exp-src-block ;Don't print "org-babel-exp process .." messages
write-region ;Don't print "Wrote .." messages
table-generate-source ;Don't print "Generating source..." messages
)
"List of functions to silence in Echo and Messages buffers.
These functions are silenced only when ALL-SUBTREES export is done.")
(defconst org-hugo--preprocess-buffer t
"Enable pre-processing of the current Org buffer.
This variable needs to be non-nil for the support of
cross-subtree Org internal links when using the subtree-based
export flow.")
(defvar org-hugo--preprocessed-buffer nil
"Name of the pre-processed buffer.")
(defconst org-hugo--preprocessed-buffer-dummy-file-suffix ".pre-processed.org"
"Dummy suffix (including file extension) for pre-processed buffers.
Dummy Org file paths are created in
`org-hugo--get-pre-processed-buffer' by appending this variable
to the link targets out of the current subtree scope.")
;;; Obsoletions
(define-obsolete-variable-alias 'org-hugo-default-section-directory 'org-hugo-section "Oct 31, 2018")
(define-obsolete-function-alias 'org-hugo-headline 'org-hugo-heading "Jan 3, 2022")
;;; User-Configurable Variables
(defgroup org-export-hugo nil
"Options for exporting Org mode files to Hugo-compatible Markdown."
:tag "Org Export Hugo"
:group 'org-export
:version "25.2")
(defcustom org-hugo-base-dir nil
"Base directory for Hugo.
Set either this value, or the HUGO_BASE_DIR global property for
export."
:group 'org-export-hugo
:type 'directory)
;;;###autoload (put 'org-hugo-base-dir 'safe-local-variable 'stringp)
(defcustom org-hugo-goldmark t
"Enable Goldmark or Commonmark compatible Markdown export.
When nil, the hacks necessary for Blackfriday Markdown
processing are enabled.
If using Hugo v0.60.0 (released Nov 2019), keep the default
value.
https://github.com/kaushalmodi/ox-hugo/discussions/485."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-goldmark 'safe-local-variable 'booleanp)
(defcustom org-hugo-section "posts"
"Default section for Hugo posts.
This variable is the name of the directory under the \"content/\"
directory where all Hugo posts should go by default."
:group 'org-export-hugo
:type 'directory)
;;;###autoload (put 'org-hugo-section 'safe-local-variable 'stringp)
(defcustom org-hugo-front-matter-format "toml"
"Front-matter format.
This variable can be set to either \"toml\" or \"yaml\"."
:group 'org-export-hugo
:type '(choice
(const :tag "TOML" "toml")
(const :tag "YAML" "yaml")))
;;;###autoload (put 'org-hugo-front-matter-format 'safe-local-variable 'stringp)
(defcustom org-hugo-footer ""
"String to be appended at the end of each Hugo post.
The string needs to be in a Hugo-compatible Markdown format or HTML."
:group 'org-export-hugo
:type 'string)
;;;###autoload (put 'org-hugo-footer 'safe-local-variable 'stringp)
(defcustom org-hugo-preserve-filling t
"When non-nil, text filling done in Org will be retained in Markdown."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-preserve-filling 'safe-local-variable 'booleanp)
(defcustom org-hugo-delete-trailing-ws t
"When non-nil, delete trailing whitespace in Markdown output.
Trailing empty lines at the end of the Markdown output are also deleted.
One might want to set this variable to nil if they want to
preserve the trailing whitespaces in Markdown for the purpose of
forcing line-breaks.
The trailing whitespace deleting is skipped if
`org-export-preserve-breaks' is set to non-nil; either via that
variable or via the OPTIONS keyword \"\\n:t\" (See (org) Export
settings).
\(In below Markdown, underscores are used to represent spaces.)
abc__
def__
Those trailing whitespaces render to \"<br />\" tags in the Hugo
generated HTML. But the same result can also be achived by using the
Org Verse block or Blackfriday hardLineBreak extension."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-delete-trailing-ws 'safe-local-variable 'booleanp)
(defcustom org-hugo-use-code-for-kbd nil
"When non-nil, ~text~ will translate to <kbd>text</kbd>."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-use-code-for-kbd 'safe-local-variable 'booleanp)
(defcustom org-hugo-allow-spaces-in-tags t
"When non-nil, replace double underscores in Org tags with spaces.
See `org-hugo--tag-processing-fn-replace-with-spaces-maybe' for
more information.
This variable affects the Hugo tags and categories (set via Org
tags using the \"@\" prefix)."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-allow-spaces-in-tags 'safe-local-variable 'booleanp)
(defcustom org-hugo-prefer-hyphen-in-tags t
"When non-nil, replace single underscores in Org tags with hyphens.
See `org-hugo--tag-processing-fn-replace-with-hyphens-maybe' for
more information.
This variable affects the Hugo tags and categories (set via Org
tags using the \"@\" prefix)."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-prefer-hyphen-in-tags 'safe-local-variable 'booleanp)
(defcustom org-hugo-tag-processing-functions '(org-hugo--tag-processing-fn-replace-with-spaces-maybe
org-hugo--tag-processing-fn-replace-with-hyphens-maybe)
"List of functions that are called in order to process the Org tags.
Each function has to accept two arguments:
Arg 1: TAG-LIST which is a list of Org tags of the type
\(\"TAG1\" \"TAG2\" ..).
Arg 2: INFO which is a plist holding contextual information.
Each function should then return a list of strings, which would
be processed form of TAG-LIST.
All the functions are called in order, and the output of one
function is fed as the TAG-LIST input of the next called
function.
The `org-hugo--tag-processing-fn-replace-with-spaces-maybe'
function skips any processing and returns its input TAG-LIST as
it is if `org-hugo-allow-spaces-in-tags' is nil.
The `org-hugo--tag-processing-fn-replace-with-hyphens-maybe'
function skips any processing and returns its input TAG-LIST as
it is if `org-hugo-prefer-hyphen-in-tags' is nil."
:group 'org-export-hugo
:type '(repeat (function)))
(defcustom org-hugo-auto-set-lastmod nil
"When non-nil, set the lastmod field in front-matter to current time."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-auto-set-lastmod 'safe-local-variable 'booleanp)
(defcustom org-hugo-suppress-lastmod-period 0.0
"Suppressing period (in seconds) for adding the lastmod front-matter.
The suppressing period is calculated as a delta between the
\"date\" and auto-calculated \"lastmod\" values. This value can
be 0.0 or a positive float.
The default value is 0.0 (seconds), which means that the lastmod
parameter will be added to front-matter even if the post is
modified within just 0.1 seconds after the initial creation of
it (when the \"date\" is set).
If the value is 86400.0, the lastmod parameter will not be added
to the front-matter within 24 hours from the initial exporting.
This variable is effective only if auto-setting of the
\"lastmod\" parameter is enabled i.e. if
`org-hugo-auto-set-lastmod' or `EXPORT_HUGO_AUTO_SET_LASTMOD' is
non-nil."
:group 'org-export-hugo
:type 'float)
;;;###autoload (put 'org-hugo-suppress-lastmod-period 'safe-local-variable 'floatp)
(defcustom org-hugo-export-with-toc nil
"When non-nil, Markdown format TOC will be inserted.
The TOC contains headings with levels up
to`org-export-headline-levels'. When an integer, include levels
up to N in the toc, this may then be different from
`org-export-headline-levels', but it will not be allowed to be
larger than the number of heading levels. When nil, no table of
contents is made.
This option can also be set with the OPTIONS keyword,
e.g. \"toc:nil\", \"toc:t\" or \"toc:3\"."
:group 'org-export-hugo
:type '(choice
(const :tag "No Table of Contents" nil)
(const :tag "Full Table of Contents" t)
(integer :tag "TOC to level")))
;;;###autoload (put 'org-hugo-export-with-toc 'safe-local-variable (lambda (x) (or (booleanp x) (integerp x))))
(defcustom org-hugo-export-with-section-numbers nil
"Configuration for adding section numbers to headings.
When set to `onlytoc', none of the headings will be numbered in
the exported post body, but TOC generation will use the section
numbers.
When set to an integer N, numbering will only happen for
headings whose relative level is higher or equal to N.
When set to any other non-nil value, numbering will happen for
all the headings.
This option can also be set with the OPTIONS keyword,
e.g. \"num:onlytoc\", \"num:nil\", \"num:t\" or \"num:3\"."
:group 'org-export-hugo
:type '(choice
(const :tag "Don't number only in body" onlytoc)
(const :tag "Don't number any heading" nil)
(const :tag "Number all headings" t)
(integer :tag "Number to level")))
;;;###autoload (put 'org-hugo-export-with-section-numbers 'safe-local-variable (lambda (x) (or (booleanp x) (equal 'onlytoc x) (integerp x))))
(defcustom org-hugo-default-static-subdirectory-for-externals "ox-hugo"
"Default sub-directory in Hugo static directory for external files.
If the source path for external files does not contain
\"static\", `ox-hugo` cannot know what directory structure to
create inside the Hugo static directory. So all such files are
copied to this sub-directory inside the Hugo static directory."
:group 'org-export-hugo
:type 'string)
;;;###autoload (put 'org-hugo-default-static-subdirectory-for-externals 'safe-local-variable 'stringp)
(defcustom org-hugo-external-file-extensions-allowed-for-copying
'("jpg" "jpeg" "tiff" "png" "svg" "gif" "bmp"
"mp4"
"pdf" "odt"
"doc" "ppt" "xls"
"docx" "pptx" "xlsx")
"List of external file extensions allowed for copying to Hugo static dir.
If an Org link references a file with one of these extensions,
and if that file is not in the Hugo static directory, that file
is copied over to the static directory.
The auto-copying behavior is disabled if this variable is set to
nil."
:group 'org-export-hugo
:type '(repeat string))
(defcustom org-hugo-export-creator-string
(format "Emacs %s (Org mode%s + ox-hugo)"
emacs-version
(if (fboundp 'org-version)
(concat " " (org-version))
""))
"Information about the creator of the document.
This option can also be set on with the CREATOR keyword."
:group 'org-export-hugo
:type '(string :tag "Creator string"))
;;;###autoload (put 'org-hugo-export-creator-string 'safe-local-variable 'stringp)
(defcustom org-hugo-date-format "%Y-%m-%dT%T%z"
"Date format used for exporting date in front-matter.
Front-matter date parameters: `date', `publishDate',
`expiryDate', `lastmod'.
Note that the date format must match the date specification from
RFC3339. See `org-hugo--date-time-regexp' for reference and
examples of compatible date strings.
Examples of RFC3339-compatible values for this variable:
- %Y-%m-%dT%T%z (default) -> 2017-07-31T17:05:38-04:00
- %Y-%m-%dT%T -> 2017-07-31T17:05:38
- %Y-%m-%d -> 2017-07-31
Note that \"%Y-%m-%dT%T%z\" actually produces a date string like
\"2017-07-31T17:05:38-0400\"; notice the missing colon in the
time-zone portion.
A colon is needed to separate the hours and minutes in the
time-zone as per RFC3339. This gets fixed in the
`org-hugo--format-date' function, so that \"%Y-%m-%dT%T%z\" now
results in a date string like \"2017-07-31T17:05:38-04:00\".
See `format-time-string' to learn about the date format string
expression."
:group 'org-export-hugo
:type 'string)
;;;###autoload (put 'org-hugo-date-format 'safe-local-variable 'stringp)
(defcustom org-hugo-paired-shortcodes ""
"Space-separated string of paired shortcode strings.
Shortcode string convention:
- Begin the string with \"%\" for shortcodes whose content can
contain Markdown, and thus needs to be passed through the
Hugo Markdown processor. The content can also contain HTML.
Example of a paired markdown shortcode:
{{% mdshortcode %}}Content **bold** <i>italics</i>{{% /mdshortcode %}}
- Absence of the \"%\" prefix would imply that the shortcode's
content should not be passed to the Markdown parser. The
content can contain HTML though.
Example of a paired non-markdown (default) shortcode:
{{< myshortcode >}}Content <b>bold</b> <i>italics</i>{{< /myshortcode >}}
For example these shortcode strings:
- %mdshortcode : Paired markdown shortcode
- myshortcode : Paired default shortcode
would be collectively added to this variable as:
\"%mdshortcode myshortcode\"
Hugo shortcodes documentation:
https://gohugo.io/content-management/shortcodes/."
:group 'org-export-hugo
:type 'string)
;;;###autoload (put 'org-hugo-paired-shortcodes 'safe-local-variable 'stringp)
(defcustom org-hugo-link-desc-insert-type nil
"Insert the element type in link descriptions for numbered elements.
String representing the type is inserted for these Org elements
if they are numbered (i.e. both \"#+name\" and \"#+caption\" are
specified for them):
- src-block : \"Code Snippet\"
- table: \"Table\"
- figure: \"Figure\"."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-link-desc-insert-type 'safe-local-variable 'booleanp)
(defcustom org-hugo-container-element ""
"HTML element to use for wrapping top level sections.
Can be set with the in-buffer HTML_CONTAINER property.
When set to \"\", the top level sections are not wrapped in any
HTML element."
:group 'org-export-hugo
:type 'string)
;;;###autoload (put 'org-hugo-container-element 'safe-local-variable 'stringp)
(defcustom org-hugo-special-block-type-properties '(("audio" . (:raw t))
("katex" . (:raw t))
("mark" . (:trim-pre t :trim-post t))
("tikzjax" . (:raw t))
("video" . (:raw t)))
"Alist for storing default properties for special block types.
Each element of the alist is of the form (TYPE . PLIST) where
TYPE is a string holding the special block's type and PLIST is a
property list for that TYPE.
The TYPE string could be any special block type like an HTML
inline or block tag, or name of a Hugo shortcode, or any random
string.
Properties recognized in the PLIST:
- :raw :: When set to t, the contents of the special block as
exported raw i.e. as typed in the Org buffer.
- :trim-pre :: When set to t, the whitespace before the special
block is removed.
- :trim-pre :: When set to t, the whitespace after the special
block is removed.
For the special block types not specified in this variable, the
default behavior is same as if (:raw nil :trim-pre nil :trim-post
nil) plist were associated with them."
:group 'org-export-hugo
:type '(alist :key-type string :value-type (plist :key-type symbol :value-type boolean)))
(defcustom org-hugo-anchor-functions '(org-hugo-get-page-or-bundle-name
org-hugo-get-custom-id
org-hugo-get-heading-slug
org-hugo-get-md5)
"A list of functions for deriving the anchor of current Org heading.
The functions will be run in the order added to this variable
until the first one returns a non-nil value. So the functions in
this list are order-sensitive.
For example, if `org-hugo-get-page-or-bundle-name' is the first
element in this list, the heading's `:EXPORT_FILE_NAME' property
will have the highest precedence in determining the heading's
anchor string.
This variable is used in the `org-hugo--get-anchor' internal
function.
Functions added to this list should have 2 arguments (which could
even be declared as optional):
1. ELEMENT : Org element
2. INFO : General plist used as a communication channel
Some of the inbuilt functions that can be added to this list:
- `org-hugo-get-page-or-bundle-name'
- `org-hugo-get-custom-id'
- `org-hugo-get-heading-slug'
- `org-hugo-get-md5'
- `org-hugo-get-id'"
:group 'org-export-hugo
:type '(repeat function))
(defcustom org-hugo-citations-plist '(:bibliography-section-heading "References")
"Property list for storing default properties for citation exports.
Properties recognized in the PLIST:
- :bibliography-section-heading :: Heading to insert before the bibliography
section.
Auto-detection of bibliography section requires installing the
`citations' package from Melpa and adding `#+cite_export: csl' at
the top of the Org file.
If `:bibliography-section-heading' set to an empty string,
bibliography heading auto-injection is not done."
:group 'org-export-hugo
:type '(plist :key-type symbol :value-type string))
(defcustom org-hugo-info-gnu-software '("3dldf" "8sync"
"a2ps" "acct" "acm" "adns" "alive" "anubis" "apl"
"archimedes" "aris" "artanis" "aspell" "auctex" "autoconf" "autoconf-archive"
"autogen" "automake" "avl"
"ballandpaddle" "barcode" "bash" "bayonne" "bazaar" "bc" "behistun"
"bfd" "binutils" "bison" "bool" "bpel2owfn"
"c-graph" "ccaudio" "ccd2cue" "ccide" "ccrtp" "ccscript" "cflow"
"cgicc" "chess" "cim" "classpath" "classpathx" "clisp" "combine"
"commoncpp" "complexity" "config" "consensus" "coreutils" "cpio" "cppi"
"cssc" "cursynth"
"dap" "datamash" "dc" "ddd" "ddrescue" "dejagnu" "denemo"
"dia" "dico" "diction" "diffutils" "direvent" "djgpp" "dominion"
"dr-geo"
"easejs" "ed" "edma" "electric" "emacs" "emacs-muse" "emms"
"enscript" "epsilon"
"fdisk" "ferret" "findutils" "fisicalab" "foliot" "fontopia" "fontutils"
"freedink" "freefont" "freeipmi" "freetalk" "fribidi"
"g-golf" "gama" "garpd" "gawk" "gcal" "gcc" "gcide"
"gcl" "gcompris" "gdb" "gdbm" "gengen" "gengetopt" "gettext"
"gforth" "ggradebook" "ghostscript" "gift" "gimp" "glean" "global"
"glpk" "glue" "gmediaserver" "gmp" "gnash" "gnat" "gnats"
"gnatsweb" "gnowsys" "gnu-c-manual" "gnu-crypto" "gnu-pw-mgr" "gnuae" "gnuastro"
"gnubatch" "gnubg" "gnubiff" "gnubik" "gnucap" "gnucash" "gnucobol"
"gnucomm" "gnudos" "gnufm" "gnugo" "gnuit" "gnujdoc" "gnujump"
"gnukart" "gnulib" "gnumach" "gnumed" "gnumeric" "gnump3d" "gnun"
"gnunet" "gnupg" "gnupod" "gnuprologjava" "gnuradio" "gnurobots" "gnuschool"
"gnushogi" "gnusound" "gnuspeech" "gnuspool" "gnustandards" "gnustep" "gnutls"
"gnutrition" "gnuzilla" "goptical" "gorm" "gpaint" "gperf" "gprolog"
"grabcomics" "greg" "grep" "gretl" "groff" "grub" "gsasl"
"gsegrafix" "gsl" "gslip" "gsrc" "gss" "gtick" "gtypist"
"guile" "guile-cv" "guile-dbi" "guile-gnome" "guile-ncurses" "guile-opengl"
"guile-rpc" "guile-sdl" "guix" "gurgle" "gv" "gvpe" "gwl" "gxmessage"
"gzip"
"halifax" "health" "hello" "help2man" "hp2xx" "html-info" "httptunnel"
"hurd" "hyperbole"
"icecat" "idutils" "ignuit" "indent" "inetutils" "inklingreader" "intlfonts"
"jacal" "jami" "java-getopt" "jel" "jitter" "jtw" "jwhois"
"kawa" "kopi"
"leg" "less" "libc" "libcdio" "libdbh" "liberty-eiffel" "libextractor"
"libffcall" "libgcrypt" "libiconv" "libidn" "libjit" "libmatheval"
"libmicrohttpd" "libredwg" "librejs" "libsigsegv" "libtasn1" "libtool"
"libunistring" "libxmi" "lightning" "lilypond" "lims" "linux-libre" "liquidwar6"
"lispintro" "lrzsz" "lsh"
"m4" "macchanger" "mailman" "mailutils" "make" "marst" "maverik"
"mc" "mcron" "mcsim" "mdk" "mediagoblin" "melting" "mempool"
"mes" "metaexchange" "metahtml" "metalogic-inference" "mifluz" "mig" "miscfiles"
"mit-scheme" "moe" "motti" "mpc" "mpfr" "mpria" "mtools"
"nana" "nano" "nano-archimedes" "ncurses" "nettle" "network"
"ocrad" "octave" "oleo" "oo-browser" "orgadoc" "osip"
"panorama" "parallel" "parted" "pascal" "patch" "paxutils" "pcb"
"pem" "pexec" "pies" "pipo" "plotutils" "poke" "polyxmass"
"powerguru" "proxyknife" "pspp" "psychosynth" "pth" "pythonwebkit"
"qexo" "quickthreads"
"r" "radius" "rcs" "readline" "recutils" "reftex" "remotecontrol"
"rottlog" "rpge" "rush"
"sather" "scm" "screen" "sed" "serveez" "sharutils" "shepherd"
"shishi" "shmm" "shtool" "sipwitch" "slib" "smalltalk" "social"
"solfege" "spacechart" "spell" "sqltutor" "src-highlite" "ssw" "stalkerfs"
"stow" "stump" "superopt" "swbis" "sysutils"
"taler" "talkfilters" "tar" "termcap" "termutils" "teseq" "teximpatient"
"texinfo" "texmacs" "time" "tramp" "trans-coord" "trueprint"
"unifont" "units" "unrtf" "userv" "uucp"
"vc-dwim" "vcdimager" "vera" "vmgen"
"wb" "wdiff" "websocket4j" "webstump" "wget" "which" "womb"
"xaos" "xboard" "xlogmaster" "xmlat" "xnee" "xorriso"
"zile")
"List of GNU software for Info manual links.
The software list is taken from https://www.gnu.org/software/."
:group 'org-export-hugo
:type '(repeat string))
;;; Define Back-End
(org-export-define-derived-backend 'hugo 'blackfriday ;hugo < blackfriday < md < html
:menu-entry
'(?H "Export to Hugo-compatible Markdown"
((?H "Subtree or File to Md file "
(lambda (a _s v _b)
(org-hugo-export-wim-to-md nil a v)))
(?h "File to Md file"
(lambda (a s v _b)
(org-hugo-export-to-md a s v)))
(?O "Subtree or File to Md file and open "
(lambda (a _s v _b)
(if a
(org-hugo-export-wim-to-md nil :async v)
(org-open-file (org-hugo-export-wim-to-md nil nil v)))))
(?o "File to Md file and open"
(lambda (a s v _b)
(if a
(org-hugo-export-to-md :async s v)
(org-open-file (org-hugo-export-to-md nil s v)))))
(?A "All subtrees (or File) to Md file(s) "
(lambda (a _s v _b)
(org-hugo-export-wim-to-md :all-subtrees a v)))
(?t "File to a temporary Md buffer"
(lambda (a s v _b)
(org-hugo-export-as-md a s v)))))
;;;; translate-alist
:translate-alist '((code . org-hugo-kbd-tags-maybe)
(drawer . org-hugo-drawer)
(example-block . org-hugo-example-block)
(export-block . org-hugo-export-block)
(export-snippet . org-hugo-export-snippet)
(headline . org-hugo-heading)
(inner-template . org-hugo-inner-template)
(inline-src-block . org-hugo-inline-src-block)
(keyword . org-hugo-keyword)
(link . org-hugo-link)
(paragraph . org-hugo-paragraph)
(src-block . org-hugo-src-block)
(special-block . org-hugo-special-block))
:filters-alist '((:filter-body . org-hugo-body-filter))
;;;; options-alist
;; KEY KEYWORD OPTION DEFAULT BEHAVIOR
:options-alist '(;; Variables not setting the front-matter directly
(:with-toc nil "toc" org-hugo-export-with-toc)
(:section-numbers nil "num" org-hugo-export-with-section-numbers)
(:author "AUTHOR" nil user-full-name newline)
(:creator "CREATOR" nil org-hugo-export-creator-string)
(:with-smart-quotes nil "'" nil) ;Hugo/Goldmark does more correct conversion to smart quotes, especially for single quotes.
(:with-special-strings nil "-" nil) ;Hugo/Goldmark does the auto-conversion of "--" -> "–", "---" -> "—" and "..." -> "…"
(:with-sub-superscript nil "^" '{}) ;Require curly braces to be wrapped around text to sub/super-scripted
(:hugo-with-locale "HUGO_WITH_LOCALE" nil nil)
(:hugo-front-matter-format "HUGO_FRONT_MATTER_FORMAT" nil org-hugo-front-matter-format)
(:hugo-level-offset "HUGO_LEVEL_OFFSET" nil "1")
(:hugo-preserve-filling "HUGO_PRESERVE_FILLING" nil org-hugo-preserve-filling) ;Preserve breaks so that text filling in Markdown matches that of Org
(:hugo-delete-trailing-ws "HUGO_DELETE_TRAILING_WS" nil org-hugo-delete-trailing-ws)
(:hugo-section "HUGO_SECTION" nil org-hugo-section)
(:hugo-bundle "HUGO_BUNDLE" nil nil)
(:hugo-base-dir "HUGO_BASE_DIR" nil org-hugo-base-dir)
(:hugo-goldmark "HUGO_GOLDMARK" nil org-hugo-goldmark)
(:hugo-code-fence "HUGO_CODE_FENCE" nil t) ;Prefer to generate triple-backquoted Markdown code blocks by default.
(:hugo-use-code-for-kbd "HUGO_USE_CODE_FOR_KBD" nil org-hugo-use-code-for-kbd)
(:hugo-prefer-hyphen-in-tags "HUGO_PREFER_HYPHEN_IN_TAGS" nil org-hugo-prefer-hyphen-in-tags)
(:hugo-allow-spaces-in-tags "HUGO_ALLOW_SPACES_IN_TAGS" nil org-hugo-allow-spaces-in-tags)
(:hugo-auto-set-lastmod "HUGO_AUTO_SET_LASTMOD" nil org-hugo-auto-set-lastmod)
(:hugo-custom-front-matter "HUGO_CUSTOM_FRONT_MATTER" nil nil space)
(:hugo-blackfriday "HUGO_BLACKFRIDAY" nil nil space) ;Deprecated. See https://github.com/kaushalmodi/ox-hugo/discussions/485.
(:hugo-front-matter-key-replace "HUGO_FRONT_MATTER_KEY_REPLACE" nil nil space)
(:hugo-date-format "HUGO_DATE_FORMAT" nil org-hugo-date-format)
(:hugo-paired-shortcodes "HUGO_PAIRED_SHORTCODES" nil org-hugo-paired-shortcodes space)
(:hugo-pandoc-citations "HUGO_PANDOC_CITATIONS" nil nil)
(:bibliography "BIBLIOGRAPHY" nil nil newline) ;Used in ox-hugo-pandoc-cite
(:html-container "HTML_CONTAINER" nil org-hugo-container-element)
(:html-container-class "HTML_CONTAINER_CLASS" nil "")
;; Front-matter variables
;; https://gohugo.io/content-management/front-matter/#front-matter-variables
;; aliases
(:hugo-aliases "HUGO_ALIASES" nil nil space)
;; audio
(:hugo-audio "HUGO_AUDIO" nil nil)
;; date
;; "date" is parsed from the Org #+date or subtree property EXPORT_HUGO_DATE
(:date "DATE" nil nil)
;; description
(:description "DESCRIPTION" nil nil)
;; draft
;; "draft" value interpreted by the TODO state of a
;; post as Org subtree gets higher precedence.
(:hugo-draft "HUGO_DRAFT" nil nil)
;; expiryDate
(:hugo-expirydate "HUGO_EXPIRYDATE" nil nil)
;; headless (only for Page Bundles - Hugo v0.35+)
(:hugo-headless "HUGO_HEADLESS" nil nil)
;; images
(:hugo-images "HUGO_IMAGES" nil nil newline)
;; isCJKLanguage
(:hugo-iscjklanguage "HUGO_ISCJKLANGUAGE" nil nil)
;; keywords
;; "keywords" is parsed from the Org #+keywords or
;; subtree property EXPORT_KEYWORDS.
(:keywords "KEYWORDS" nil nil newline)
;; layout
(:hugo-layout "HUGO_LAYOUT" nil nil)
;; lastmod
(:hugo-lastmod "HUGO_LASTMOD" nil nil)
;; linkTitle
(:hugo-linktitle "HUGO_LINKTITLE" nil nil)
;; locale (used in Hugo internal templates)
(:hugo-locale "HUGO_LOCALE" nil nil)
;; markup
(:hugo-markup "HUGO_MARKUP" nil nil) ;default is "md"
;; menu
(:hugo-menu "HUGO_MENU" nil nil space)
(:hugo-menu-override "HUGO_MENU_OVERRIDE" nil nil space)
;; outputs
(:hugo-outputs "HUGO_OUTPUTS" nil nil space)
;; publishDate
(:hugo-publishdate "HUGO_PUBLISHDATE" nil nil)
;; series
(:hugo-series "HUGO_SERIES" nil nil newline)
;; slug
(:hugo-slug "HUGO_SLUG" nil nil)
;; taxomonomies - tags, categories
(:hugo-tags "HUGO_TAGS" nil nil newline)
;; #+hugo_tags are used to set the post tags in Org
;; files written for file-based exports. But for
;; subtree-based exports, the EXPORT_HUGO_TAGS
;; property can be used to override inherited tags
;; and Org-style tags.
(:hugo-categories "HUGO_CATEGORIES" nil nil newline)
;; #+hugo_categories are used to set the post
;; categories in Org files written for file-based
;; exports. But for subtree-based exports, the
;; EXPORT_HUGO_CATEGORIES property can be used to
;; override inherited categories and Org-style
;; categories (Org-style tags with "@" prefix).
;; resources
(:hugo-resources "HUGO_RESOURCES" nil nil space)
;; title
;; "title" is parsed from the Org #+title or the subtree heading.
;; type
(:hugo-type "HUGO_TYPE" nil nil)
;; url
(:hugo-url "HUGO_URL" nil nil)
;; videos
(:hugo-videos "HUGO_VIDEOS" nil nil newline)
;; weight
(:hugo-weight "HUGO_WEIGHT" nil nil space)))
;;; Miscellaneous Helper Functions
;;;; Check if a value is non-nil
(defun org-hugo--value-get-true-p (value)
"Return non-nil if VALUE is non-nil.
Return nil if VALUE is nil, \"nil\" or \"\"."
(cond
((or (equal t value)
(equal nil value))
value)
((and (stringp value)
(string= value "nil"))
nil)
(t
;; "" -> nil
;; "t" -> "t"
;; "anything else" -> "anything else"
;; 123 -> nil
(org-string-nw-p value))))
;;;; Check if a boolean plist value is non-nil
(defun org-hugo--plist-get-true-p (info key)
"Return non-nil if KEY in INFO is non-nil.
Return nil if the value of KEY in INFO is nil, \"nil\" or \"\".
This is a special version of `plist-get' used only for keys that
are expected to hold a boolean value.
INFO is a plist used as a communication channel."
(let ((value (plist-get info key)))
;; (message "dbg: org-hugo--plist-get-true-p:: key:%S value:%S" key value)
(org-hugo--value-get-true-p value)))
;;;; Workaround to retain custom parameters in src-block headers post `org-babel-exp-code'
;; http://lists.gnu.org/archive/html/emacs-orgmode/2017-10/msg00300.html
(defun org-hugo--org-babel-exp-code (orig-fun &rest args)
"Return the original code block formatted for export.
ORIG-FUN is the original function `org-babel-exp-code' that this
function is designed to advice using `:around'. ARGS are the
arguments of the ORIG-FUN.
This advice retains the `:hl_lines', `linenos' and
`:front_matter_extra' parameters, if added to any source block.
This parameter is used in `org-hugo-src-block'."
(let* ((param-keys-to-be-retained '(:hl_lines :linenos :front_matter_extra))
(info (car args))
(parameters (nth 2 info))
(ox-hugo-params-str (let ((str ""))
(dolist (param parameters)
(dolist (retain-key param-keys-to-be-retained)
(when (equal retain-key (car param))
(let ((val (cdr param)))
(setq str
(concat str " "
(symbol-name retain-key) " "
(cond
((stringp val)
val)
((numberp val)
(number-to-string val))
(t
(user-error "Invalid value %S assigned to %S"
val retain-key)))))))))
(org-string-nw-p (org-trim str))))
ret)
;; (message "[ox-hugo ob-exp] info: %S" info)
;; (message "[ox-hugo ob-exp] parameters: %S" parameters)
;; (message "[ox-hugo ob-exp] ox-hugo-params-str: %S" ox-hugo-params-str)
(setq ret (apply orig-fun args))
(when ox-hugo-params-str
(let ((case-fold-search t))
(setq ret (replace-regexp-in-string "\\`#\\+begin_src .*"
(format "\\& %s" ox-hugo-params-str) ret))))
;; (message "[ox-hugo ob-exp] ret: %S" ret)
ret))
;;;; Workaround to fix the regression in the behavior of `org-babel--string-to-number'.
;; https://lists.gnu.org/r/emacs-orgmode/2020-02/msg00931.html
(defun org-hugo--org-babel--string-to-number (string)
"If STRING represents a number return its value.
Otherwise return nil.
This function restores the behavior of
`org-babel--string-to-number' to that of before
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=6b2a7cb20b357e730de151522fe4204c96615f98."
(and (string-match-p "\\`-?\\([0-9]\\|\\([1-9]\\|[0-9]*\\.\\)[0-9]*\\)\\'" string)
(string-to-number string)))
(defun org-hugo--org-info-export (path desc format)
"Add support for exporting [[info:..]] links for `hugo' format.
See `org-link-parameters' for details about PATH, DESC and FORMAT."
(let* ((parts (split-string path "#\\|::"))
(manual (car parts))
(node (or (nth 1 parts) "Top"))
(title (format "Emacs Lisp: (info \\\"(%s) %s\\\")" manual node))
(desc (or desc