/
tslug.el
296 lines (266 loc) · 8.09 KB
/
tslug.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
;;; tslug.el --- Tests related to slug string derivation -*- lexical-binding: t; -*-
;; Authors: Kaushal Modi <kaushal.modi@gmail.com>
;; 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/>.
;;; Code:
(require 'org-test-lib)
(require 'ox-hugo)
(ert-deftest test-slug/return-nil ()
"Test nil return conditions."
;; Empty title
(should
(equal nil
(org-test-with-parsed-data
"* <point>"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))
;; Heading with EXPORT_FILE_NAME.
(should
(equal nil
(org-test-with-parsed-data
"* Some Heading<point>"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info))))))
(ert-deftest test-slug/ignore-hugo-slug ()
"Test that EXPORT_HUGO_SLUG is not used to derive the slug."
;; EXPORT_FILE_NAME + EXPORT_HUGO_SLUG
(should
(string= "posts/file"
(org-test-with-parsed-data
"* Some Heading<point>
:PROPERTIES:
:EXPORT_FILE_NAME: file
:EXPORT_HUGO_SLUG: slug
:END:"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))
;; EXPORT_FILE_NAME + EXPORT_HUGO_SLUG + EXPORT_HUGO_SECTION
(should
(string= "section/file"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:END:
** Some Heading<point>
:PROPERTIES:
:EXPORT_FILE_NAME: file
:EXPORT_HUGO_SLUG: slug
:END:"
(let ((el (nth 1 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info))))))
(ert-deftest test-slug/export-file-name ()
"Test derivation of the slug from EXPORT_FILE_NAME."
;; Only EXPORT_FILE_NAME
(should
(string= "posts/file"
(org-test-with-parsed-data
"* Some Heading<point>
:PROPERTIES:
:EXPORT_FILE_NAME: file
:END:"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))
;; EXPORT_FILE_NAME + EXPORT_HUGO_SECTION
(should
(string= "section/file"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:EXPORT_OPTIONS: toc:t
:END:
** Some Heading<point>
:PROPERTIES:
:EXPORT_FILE_NAME: file
:END:"
(let ((el (nth 1 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info))))))
;; Leaf bundles
(ert-deftest test-slug/leaf-bundles ()
"Test derivation of the slug leaf bundles."
(should
(string= "posts/leaf"
(org-test-with-parsed-data
"* Some Heading<point>
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: leaf
:EXPORT_FILE_NAME: index
:END:"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))
;; Leaf bundle in a section
(should
(string= "section/leaf"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:END:
** Some Heading<point>
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: leaf
:EXPORT_FILE_NAME: index
:END:"
(let ((el (nth 1 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info))))))
;; Branch bundles
(ert-deftest test-slug/branch-bundles ()
(should
(string= "posts/branch"
(org-test-with-parsed-data
"* Some Heading<point>
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: branch
:EXPORT_FILE_NAME: _index
:END:"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))
;; Inherit :EXPORT_HUGO_BUNDLE
(should
(string= "posts/branch"
(org-test-with-parsed-data
"* Branch bundle
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: branch
:END:
** Landing page<point>
:PROPERTIES:
:EXPORT_FILE_NAME: _index
:END:"
(let ((el (nth 1 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info)))))
;; Page in branch bundle, inheritance
(should
(string= "posts/branch/branch-page"
(org-test-with-parsed-data
"* Branch bundle
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: branch
:END:
** Branch page<point>
:PROPERTIES:
:EXPORT_FILE_NAME: branch-page
:END:"
(let ((el (nth 1 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info)))))
;; Branch bundle in a section
(should
(string= "section/branch"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:END:
** Branch bundle
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: branch
:END:
*** Some Heading<point>
:PROPERTIES:
:EXPORT_FILE_NAME: _index
:END:"
(let ((el (nth 2 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info)))))
;; Branch page in a branch bundle in a section
(should
(string= "section/branch/branch-page"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:END:
** Branch bundle
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: branch
:END:
*** Some Heading<point>
:PROPERTIES:
:EXPORT_FILE_NAME: branch-page
:END:"
(let ((el (nth 2 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info))))))
;; Section
(ert-deftest test-slug/section ()
;; Section keyword
(should
(string= "section/file"
(org-test-with-parsed-data
"#+hugo_section: section
* Some Heading<point>
:PROPERTIES:
:EXPORT_FILE_NAME: file
:END:"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info))))))
;; Section fragments
(ert-deftest test-slug/section-fragments ()
(should
(string= "section/sec2/sec3/file"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:END:
** Section 2
:PROPERTIES:
:EXPORT_HUGO_SECTION_FRAG: sec2
:END:
*** Section 3
:PROPERTIES:
:EXPORT_HUGO_SECTION_FRAG: sec3
:END:
**** Some page<point>
:PROPERTIES:
:EXPORT_FILE_NAME: file
:END:"
(let ((el (nth 3 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info)))))
;; Branch page in a branch bundle in section fragments.
(should
(string= "section/sec2/sec3/branch/branch-page"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:END:
** Section 2
:PROPERTIES:
:EXPORT_HUGO_SECTION_FRAG: sec2
:END:
*** Section 3
:PROPERTIES:
:EXPORT_HUGO_SECTION_FRAG: sec3
:EXPORT_HUGO_BUNDLE: branch
:END:
**** Branch bundle
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: branch
:END:
***** Some page<point>
:PROPERTIES:
:EXPORT_FILE_NAME: branch-page
:END:"
(let ((el (nth 4 (org-element-map tree 'headline #'identity info))))
(org-hugo--heading-get-slug el info))))))
(provide 'tslug)
;; Note: As of 2022-03-17, Org stable or bugfix version's (9.5.2)
;; `org-element-at-point' returns the Org element at point *but*
;; without any of the inherited properties. So `org-element-map' is
;; used where property inheritance needs to be tested (because that
;; does do the prop inheritance as expected!). This issue doesn't
;; exist in the `main' branch version of `org-element-at-point'.
;;
;; To get the 1st heading element: (nth 0 (org-element-map tree 'headline #'identity info))
;; To get the 2nd heading element: (nth 1 (org-element-map tree 'headline #'identity info))
;; ..