forked from kaushalmodi/ox-hugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathox-hugo.el
4281 lines (3840 loc) · 203 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 -*-
;; Authors: Kaushal Modi <[email protected]>
;; Matt Price <[email protected]>
;; URL: https://ox-hugo.scripter.co
;; Package-Requires: ((emacs "24.4") (org "9.0"))
;; Keywords: Org, markdown, docs
;; Version: 0.7
;;; 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 'ox-blackfriday)
(require 'ffap) ;For `ffap-url-regexp'
(require 'ob-core) ;For `org-babel-parse-header-arguments'
(declare-function org-hugo-pandoc-cite--parse-citations-maybe "ox-hugo-pandoc-cite")
(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
;; headline tags.
;; https://code.orgmode.org/bzg/org-mode/commit/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)))
(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-blackfriday-options
'("taskLists"
"smartypants"
"smartypantsQuotesNBSP"
"angledQuotes"
"fractions"
"smartDashes"
"latexDashes"
"hrefTargetBlank"
"plainIDAnchors"
"extensions"
"extensionsmask")
"Blackfriday option names as used inside Hugo.
Note that these names are case-sensitive.
This is a list of strings.
Stable Hugo version reference:
https://gohugo.io/content-management/formats/#blackfriday-options
Development Hugo version reference:
https://github.com/gohugoio/hugo/blob/master/docs/content/readfiles/bfconfig.md
taskLists
- default: `true'
- Purpose: `false' turns off GitHub-style automatic task/TODO list
generation.
smartypants
- default: `true'
- Purpose: `false' disables smart punctuation substitutions, including
smart quotes, smart dashes, smart fractions, etc. If
`true', it may be fine-tuned with the `angledQuotes',
`fractions', `smartDashes', and `latexDashes' flags.
smartypantsQuotesNBSP
- default: `false'
- Purpose: `true' enables French style Guillemets with non-breaking
space inside the quotes.
angledQuotes
- default: `false'
- Purpose: `true' enables smart, angled double quotes.
Example: \"Hugo\" renders to «Hugo» instead of “Hugo”.
fractions
- default: `true'
- Purpose: `false' disables smart fractions.
- Example: 5/12 renders to 5⁄12(<sup>5</sup>⁄<sub>12</sub>).
- Caveat: Even with \"fractions = false\", Blackfriday still converts
1/2, 1/4, and 3/4 respectively to ½ (½), ¼
(¼) and ¾ (¾), but only these three.
smartDashes
- default: `true'
- Purpose: `false' disables smart dashes; i.e., the conversion of
multiple hyphens into an en-dash or em-dash. If `true',
its behavior can be modified with the `latexDashes' flag.
latexDashes
- default: `true'
- Purpose: `false' disables LaTeX-style smart dashes and selects
conventional smart dashes. Assuming `smartDashes': If
`true', -- is translated into – (–), whereas ---
is translated into — (—). However, spaced single
hyphen between two words is translated into an en dash
e.g., \"12 June - 3 July\" becomes \"12 June – 3
July\" upon rendering.
hrefTargetBlank
- default: `false'
- Purpose: `true' opens external links in a new window or tab.
plainIDAnchors
- default: `true'
- Purpose: `true' renders any heading and footnote IDs without the
document ID.
- Example: renders \"#my-heading\" instead of
\"#my-heading:bec3ed8ba720b970\".
extensions
- default: []
- Purpose: Enable one or more Blackfriday's Markdown extensions (if
they aren't Hugo defaults).
- Example: Include `hardLineBreak' in the list to enable Blackfriday's
EXTENSION_HARD_LINE_BREAK.
extensionsmask
- default: []
- Purpose: Enable one or more of Blackfriday's Markdown extensions (if
they aren't Hugo defaults). Example: Include `autoHeaderIds'
as `false' in the list to disable Blackfriday's
EXTENSION_AUTO_HEADER_IDS
See `org-hugo-blackfriday-extensions' for valid Blackfriday
extensions.")
(defvar org-hugo-blackfriday-extensions
'("noIntraEmphasis"
"tables"
"fencedCode"
"autolink"
"strikethrough"
"laxHtmlBlocks"
"spaceHeaders"
"hardLineBreak"
"tabSizeEight"
"footnotes"
"noEmptyLineBeforeBlock"
"headerIds"
"titleblock"
"autoHeaderIds"
"backslashLineBreak"
"definitionLists"
"joinLines")
"Blackfriday extension names as used inside Hugo.
Note that these names are case-sensitive.
This is a list of strings.
Stable Hugo version reference:
https://gohugo.io/content-management/formats/#blackfriday-extensions
Development Hugo version references:
https://github.com/gohugoio/hugo/blob/master/docs/content/readfiles/bfconfig.md
https://github.com/russross/blackfriday#extensions
https://github.com/russross/blackfriday/blob/master/markdown.go
https://github.com/gohugoio/hugo/blob/master/helpers/content.go
noIntraEmphasis
- default: enabled
- Purpose: The \"_\" character is commonly used inside words when
discussing code, so having Markdown interpret it as an
emphasis command is usually the wrong thing. When enabled,
Blackfriday lets you treat all emphasis markers as normal
characters when they occur inside a word.
tables
- default: enabled
- Purpose: When enabled, tables can be created by drawing them in the
input using the below syntax:
- Example:
Name | Age
--------|------
Bob | 27
Alice | 23
fencedCode
- default: enabled
- Purpose: When enabled, in addition to the normal 4-space indentation
to mark code blocks, you can explicitly mark them and
supply a language (to make syntax highlighting simple).
You can use 3 or more backticks to mark the beginning of
the block, and the same number to mark the end of the
block.
- Example:
```emacs-lisp
(message \"Hello\")
```
autolink
- default: enabled
- Purpose: When enabled, URLs that have not been explicitly marked as
links will be converted into links.
strikethrough
- default: enabled
- Purpose: When enabled, text wrapped with two tildes will be crossed
out.
- Example: ~~crossed-out~~
laxHtmlBlocks
- default: disabled
- Purpose: When enabled, loosen up HTML block parsing rules.
«Needs more information»
spaceHeaders
- default: enabled
- Purpose: When enabled, be strict about prefix header rules.
«Needs more information»
hardLineBreak
- default: disabled
- Purpose: When enabled, newlines in the input translate into line
breaks in the output, like in Org verse blocks.
tabSizeEight
- default: disabled
- Purpose: When enabled, expand tabs to eight spaces instead of four.
footnotes
- default: enabled
- Purpose: When enabled, Pandoc-style footnotes will be supported.
The footnote marker in the text that will become a
superscript text; the footnote definition will be placed in
a list of footnotes at the end of the document.
- Example:
This is a footnote.[^1]
[^1]: the footnote text.
noEmptyLineBeforeBlock
- default: disabled
- Purpose: When enabled, no need to insert an empty line to start a
(code, quote, ordered list, unordered list) block.
headerIds
- default: enabled
- Purpose: When enabled, allow specifying header IDs with {#id}.
titleblock
- default: disabled
- Purpose: When enabled, support Pandoc-style title blocks.
http://pandoc.org/MANUAL.html#extension-pandoc_title_block
autoHeaderIds
- default: enabled
- Purpose: When enabled, auto-create the header ID's from the headline
text.
backslashLineBreak
- default: enabled
- Purpose: When enabled, translate trailing backslashes into line
breaks.
definitionLists
- default: enabled
- Purpose: When enabled, a simple definition list is made of a
single-line term followed by a colon and the definition for
that term.
- Example:
Cat
: Fluffy animal everyone likes
Internet
: Vector of transmission for pictures of cats
Terms must be separated from the previous definition by a
blank line.
joinLines
- default: enabled
- Purpose: When enabled, delete newlines and join the lines. This
behavior is similar to the default behavior in Org.")
(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.")
;;; 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")
(define-obsolete-variable-alias 'org-hugo-default-section-directory 'org-hugo-section "Oct 31, 2018")
(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 headlines 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 headline 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 headlines.
When set to `onlytoc', none of the headlines 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
headlines whose relative level is higher or equal to N.
When set to any other non-nil value, numbering will happen for
all the headlines.
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 headline" nil)
(const :tag "Number all headlines" 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"
"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-langs-no-descr-in-code-fences '()
"List of languages whose descriptors should not be exported to Markdown.
This variable is effective only if the HUGO_CODE_FENCE option is
non-nil (default), AND if none of the Hugo \"highlight\"
shortcode features are needed (see `org-hugo-src-block' for more
information).
If `pygmentsCodeFences' in the Hugo site's config is `true', and
if a language is not supported by Pygments, the HTML of that
fenced code block is not rendered correctly by Hugo. In such
cases, it is better to leave out the language descriptor and
allow the code block to render as an Org example block.
This variable helps get around the above issue which is present
only when using the Pygments syntax highlighter. This issue does
not exist when using the Chroma syntax highlighter. So, setting
this variable to something like (org) is useful only if using the
Pygments syntax highlighter.
It is suggested to instead leave this value at its default value
and use the Chroma syntax highlighter (default) in Hugo v0.28 and
newer."
:group 'org-export-hugo
:type '(repeat symbol))
;;; 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)
(example-block . org-hugo-example-block)
(export-block . org-hugo-export-block)
(export-snippet . org-hugo-export-snippet)
(headline . org-hugo-headline)
(inner-template . org-hugo-inner-template)
(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) ;Don't use smart quotes; that is done automatically by Blackfriday
(:with-special-strings nil "-" nil) ;Don't use special strings for ndash, mdash; that is done automatically by Blackfriday
(: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 nil)
(: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)
(: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
;; 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' and `:front_matter_extra'
parameters, if added to any source block. This parameter is used
in `org-hugo-src-block'.
This advice is added to the ORIG-FUN only while an ox-hugo export
is in progress. See `org-hugo--before-export-function' and
`org-hugo--after-export-function'."
(let* ((param-keys-to-be-retained '(:hl_lines :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))
(defun org-hugo--before-export-function (subtreep)
"Function to be run before an ox-hugo export.
This function is called in the very beginning of
`org-hugo-export-to-md', `org-hugo-export-as-md' and
`org-hugo-publish-to-md'.
SUBTREEP is non-nil for subtree-based exports.
This is an internal function."
(unless subtreep
;; Reset the variables that are used only for subtree exports.
(setq org-hugo--subtree-coord nil))
(advice-add 'org-babel-exp-code :around #'org-hugo--org-babel-exp-code))
(defun org-hugo--after-export-function (info outfile)
"Function to be run after an ox-hugo export.
This function is called in the very end of
`org-hugo-export-to-md', `org-hugo-export-as-md' and
`org-hugo-publish-to-md'.
INFO is a plist used as a communication channel.
OUTFILE is the Org exported file name.
This is an internal function."
(advice-remove 'org-babel-exp-code #'org-hugo--org-babel-exp-code)
(when (and outfile
(org-hugo--pandoc-citations-enabled-p info))
(require 'ox-hugo-pandoc-cite)
(plist-put info :outfile outfile)
(plist-put info :front-matter org-hugo--fm)
(org-hugo-pandoc-cite--parse-citations-maybe info))
(setq org-hugo--fm nil)
(setq org-hugo--fm-yaml nil))
;;;; HTMLized section number for headline
(defun org-hugo--get-headline-number (headline info &optional toc)
"Return htmlized section number for the HEADLINE.
INFO is a plist used as a communication channel.
When the \"num\" export option is `onlytoc', headline number is