-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.html
998 lines (857 loc) · 72.2 KB
/
code.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 2022-07-23 Sat 05:11 -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Code</title>
<meta name="generator" content="Org mode">
<meta name="author" content="ivanaf">
<link rel="stylesheet" type="text/css" href="css/org.css"/>
<link rel="icon" href="ico/favicon.ico" type="image/x- icon">
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2020 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div class="head">
<div class="title">
<p>
<a href="index.html">Ivanaf</a>
</p>
</div>
<menu>
<ul class="org-ul">
<li><a href="index.html">Home</a></li>
<li><a href="journal.html">Journal</a></li>
<li><a href="about.html">About</a></li>
<li><a href="resume.html">Resume</a></li>
<li><a href="portfolio.html">Portfolio</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="projects_ideas.html">Messy Ideas</a></li>
</ul>
</menu>
</div>
<p>
</p><h1>
Code
</h1><p>
</p>
<p>
<span class=page-date> <small>
2019-05-24, updated 2022-07-23 — <a href='journal.html#tech' class='tech tagbutton'>tech</a> <a href='journal.html#code' class='code tagbutton'>code</a> <a href='journal.html#blog' class='blog tagbutton'>blog</a>   <a href="index.html">⇦index</a> – <a href="index.html">index⇨</a>
</small> </span>
</p>
<p>
This is the code that runs this blog! I might as well have it here as well!
</p>
<div class="org-src-container">
<pre class="src src-elisp">#! /bin/sh
<span class="org-string">":"</span><span class="org-comment-delimiter">; </span><span class="org-comment">exec emacs --no-site-file --script "$0" "$@" # -*-emacs-lisp-*- </span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">The above line is bash trickery https://stackoverflow.com/questions/6238331/emacs-shell-scripts-how-to-put-initial-options-into-the-script#6259330</span>
(<span class="org-keyword">require</span> '<span class="org-constant">org</span>)
<span class="org-comment-delimiter">;; </span><span class="org-comment">based on http://pragmaticemacs.com/emacs/export-org-mode-headlines-to-separate-files/</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">export headlines to separate files</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">http://emacs.stackexchange.com/questions/2259/how-to-export-top-level-headings-of-org-mode-buffer-to-separate-files</span>
<span class="org-comment-delimiter">;;; </span><span class="org-comment">Begin config</span>
(<span class="org-keyword">setq</span> org-export-head--html-postamble
<span class="org-string">"<p class=\"author\">Author: Ivan Tadeu Ferreira Antunes Filho</p></span>
<span class="org-string"><p class=\"date\">Date: %T</p></span>
<span class="org-string"><p class=\"author\">Github: <a href=\"https://github.com/itf/\">github.com/itf</a></p></span>
<span class="org-string"><p class=\"creator\">Made with %c and <a href=\"https://github.com/itf/org-export-head\">Org export head</a> </p>"</span>)
(<span class="org-keyword">setq</span> org-export-head-tags-page <span class="org-string">"Journal"</span>) <span class="org-comment-delimiter">; </span><span class="org-comment">used for the tags to link to a page</span>
(<span class="org-keyword">setq</span> org-export-head-using-video-links t) <span class="org-comment-delimiter">;</span><span class="org-comment">helper function to add [[video:path]] links</span>
(<span class="org-keyword">setq</span> org-export-head-using-inline-js t) <span class="org-comment-delimiter">;</span><span class="org-comment">helper function to add inline-js to babel</span>
(<span class="org-keyword">setq</span> org-export-head-link-files-to-export (list <span class="org-string">"file"</span> <span class="org-string">"video"</span>)) <span class="org-comment-delimiter">;;</span><span class="org-comment">Link types whose paths are gonna be copied with hard links, example[[video:path]]</span>
<span class="org-comment-delimiter">;</span><span class="org-comment">useful if using custom links</span>
(<span class="org-keyword">setq</span> org-export-head-click-toc-h2 t) <span class="org-comment-delimiter">;; </span><span class="org-comment">Make the header of TOC clickable, so you can write CSS for click to display</span>
(<span class="org-keyword">setq</span> org-export-head-resize-images t)
(<span class="org-keyword">setq</span> org-export-head-resize-images-width 640)
(<span class="org-keyword">setq</span> org-export-head-resize-images-height 640)
(<span class="org-keyword">setq</span> org-export-head-resize-images-max-size 300000)<span class="org-comment-delimiter">;</span><span class="org-comment">0.3mb</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">End config</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--run-on-temp-copy-buffer</span> (function-to-run <span class="org-type">&rest</span> args)
<span class="org-doc">"Runs function on a temp buffer with the contents of the original buffer"</span>
(<span class="org-keyword">save-excursion</span>
(<span class="org-keyword">let</span> ((temp-buffer (generate-new-buffer <span class="org-string">"tmp"</span>)))
(copy-to-buffer temp-buffer (point-min) (point-max))
(<span class="org-keyword">with-current-buffer</span> temp-buffer
(org-mode)
(outline-show-all)
(apply function-to-run args))
(kill-buffer temp-buffer))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head</span> (<span class="org-type">&optional</span> directory-name backend reexport)
<span class="org-doc">"Updates the hashes and reexport all changed headings if reexport is nil.</span>
<span class="org-doc">Reexports all headings if reexport is non-nil"</span>
(<span class="org-keyword">interactive</span>)
(<span class="org-keyword">let</span> ((directory-name (<span class="org-keyword">or</span> directory-name (read-directory-name <span class="org-string">"Directory:"</span>)))
(backend (<span class="org-keyword">or</span> backend <span class="org-string">"html"</span>)))
(make-directory directory-name t)
(org-export-head--run-on-temp-copy-buffer #'org-export-head--modify-buffer-ast directory-name backend reexport)
(org-export-head--update-hashes)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head-reexport</span> (<span class="org-type">&optional</span> directory-name backend)
<span class="org-doc">"Reexports all the headings"</span>
(<span class="org-keyword">interactive</span>)
(org-export-head directory-name backend t))
<span class="org-comment-delimiter">;;</span><span class="org-comment">TODO this function should return a list of hashes to update the original buffer</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--modify-buffer-ast</span> (directory-path backend reexport)
<span class="org-doc">"Export all subtrees that are *not* tagged with :noexport: to</span>
<span class="org-doc">separate files.</span>
<span class="org-doc">Subtrees that do not have the :EXPORT_FILE_NAME: property set</span>
<span class="org-doc">are exported to a filename derived from the headline text."</span>
(org-export-head--update-hashes)
(org-export-expand-include-keyword)
<span class="org-comment-delimiter">;; </span><span class="org-comment">Create summaries before deleting the posts</span>
(org-export-head--create-summaries)
(org-export-head--create-entry-tags) <span class="org-comment-delimiter">;; </span><span class="org-comment">creates tag lists</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">Delete content that has already been exported and set it to noreexport</span>
(<span class="org-keyword">if</span> (not reexport)
(org-export-head--delete-noreexport))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Get headlines, and generate macros (previous post, etc)</span>
(<span class="org-keyword">let*</span> ((headlines-hash-list (org-export-head--get-headlines))
(headlines-hash (car headlines-hash-list))
(headlines-list (cdr headlines-hash-list))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Insert extra things in the headlines-hash to be used for fixing the macros</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">To define new headline-level macros, add extra functions here</span>
(headlines-hash (org-export-head--insert-next-previous-headline headlines-hash headlines-list))
(headlines-hash (org-export-head--add-title-macro headlines-hash headlines-list))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Now we get global macros such as the index and the reversed index</span>
(global-macros (org-export-head--generate-index-alist headlines-list headlines-hash))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Now we get the templates. At the moment it is only the header</span>
(header (org-export-head--get-content-subtree-match <span class="org-string">"header"</span>))
<span class="org-comment-delimiter">;;</span><span class="org-comment">And now the footer, for example, for comments</span>
(footer (org-export-head--get-content-subtree-match <span class="org-string">"footer"</span>)))
<span class="org-comment-delimiter">;;</span><span class="org-comment">For each not noexport/noreexport headline apply the template, i.e. copy contents</span>
(org-export-head--run-on-each-heading
#'(lambda ()
(org-export-head--insert-on-header header)
(org-export-head--insert-on-footer footer))
<span class="org-string">"-noexport-noreexport"</span>)
<span class="org-comment-delimiter">;;</span><span class="org-comment">After applying the template we replace the macros on all places</span>
(org-export-head--run-on-each-heading
#'(lambda ()
(<span class="org-keyword">let*</span> ((headline-name (org-export-head--headline))
(headline-alist (gethash headline-name headlines-hash nil))
(macro-alist (append headline-alist global-macros))) <span class="org-comment-delimiter">;;</span><span class="org-comment">in reverse order so that headline properties can overshadow these</span>
(org-export-head--replace-headline-macros macro-alist headlines-list headlines-hash)))
<span class="org-string">"-noexport-noreexport"</span>)
<span class="org-comment-delimiter">;;</span><span class="org-comment">Get the parser tree and the headlines that will become files</span>
(<span class="org-keyword">let*</span> ((ast (org-element-parse-buffer)))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Fix links -- order is important. First external than fuzzy links</span>
(org-element-map ast 'link
(<span class="org-keyword">lambda</span> (link)
(<span class="org-keyword">let*</span> ((link (<span class="org-keyword">or</span> (org-export-head--fix-file-external-link-ast directory-path link) link))
(link (<span class="org-keyword">or</span> (org-export-head--fix-local-link-ast headlines-hash link) link))))))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Convert the buffer to contain the new AST, </span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">this is needed because the exporter expects the content to be in a buffer</span>
(erase-buffer)
(insert (org-element-interpret-data ast))
(outline-show-all)
<span class="org-comment-delimiter">;;</span><span class="org-comment">Finally export all the headers</span>
(org-export-head-export-headers directory-path backend))))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Not everything can be done using the AST, sadly.</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">Org element has no support for adding custom properties to headlines</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">Nor does it have a nice interface to grab the contents without the property drawer</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">Ideally everything would be done using the AST and org-element, since it is </span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">Less prone to writting bugs when using it. </span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">So right now it is only used for fixing links</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">START OF NON AST (non org-element) SESSION</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--run-on-each-heading</span>(fn match <span class="org-type">&rest</span> args)
<span class="org-doc">"Puts the point on each heading and runs the function. Needed for exporting all headings</span>
<span class="org-doc"> from http://pragmaticemacs.com/emacs/export-org-mode-headlines-to-separate-files/"</span>
(<span class="org-keyword">save-excursion</span>
(goto-char (point-min))
(goto-char (re-search-forward <span class="org-string">"^*"</span>))
(set-mark (line-beginning-position))
(goto-char (point-max))
(org-map-entries
(<span class="org-keyword">lambda</span> ()
(apply fn args))
match 'region-start-level)
(deactivate-mark)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head-export-headers</span> (directory-name backend)
<span class="org-doc">"Exports each heading to directory-name using backend"</span>
(<span class="org-keyword">if</span> (equal backend <span class="org-string">"html"</span>)
(org-export-head--run-on-each-heading
#'(lambda ()
(org-set-property
<span class="org-string">"EXPORT_FILE_NAME"</span>
(concat directory-name (org-export-head--escaped-headline)))
(deactivate-mark)
(<span class="org-keyword">let</span> ((org-html-postamble org-export-head--html-postamble))
(<span class="org-keyword">cl-letf</span> (((symbol-function 'org-export-get-reference) (symbol-function 'org-export-get-reference-custom)))
(<span class="org-keyword">if</span> org-export-head-click-toc-h2
(<span class="org-keyword">cl-letf</span> (((symbol-function 'org-html-toc) (symbol-function 'org-export-head--custom-toc)))
(org-html-export-to-html nil t)))))
(set-buffer-modified-p t)) <span class="org-string">"-noexport-noreexport"</span>))
(<span class="org-keyword">if</span> (equal backend <span class="org-string">"pdf"</span>)
(org-export-head--run-on-each-heading
#'(lambda ()
(org-set-property
<span class="org-string">"EXPORT_FILE_NAME"</span>
(concat directory-name (org-export-head--escaped-headline)))
(deactivate-mark)
(org-latex-export-to-pdf nil t)
(set-buffer-modified-p t)) <span class="org-string">"-noexport-noreexport"</span>)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--goto-header</span>(<span class="org-type">&optional</span> no-new-line)
<span class="org-doc">"Puts point after property-block if it exists, in an empty line</span>
<span class="org-doc"> by creating a new line, unless no-new-line is non nil and returns point"</span>
(<span class="org-keyword">interactive</span>)
(org-back-to-heading t)
(<span class="org-keyword">let*</span> ((beg-end (org-get-property-block))
(end (cdr beg-end)))
(goto-char (<span class="org-keyword">or</span> end (point))))
(goto-char (point-at-bol 2)) <span class="org-comment-delimiter">;;</span><span class="org-comment">Advance one line</span>
(<span class="org-keyword">if</span> (not no-new-line)
(<span class="org-keyword">progn</span>
(newline)
(goto-char (point-at-bol 0)))) <span class="org-comment-delimiter">;;</span><span class="org-comment">Go back one line</span>
(point))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--goto-footer</span>(<span class="org-type">&optional</span> no-new-line)
<span class="org-doc">"Puts point at end of ubtree and returns point"</span>
(<span class="org-keyword">interactive</span>)
(org-end-of-subtree)
(<span class="org-keyword">if</span> (not no-new-line)
(<span class="org-keyword">progn</span>
(newline)))
(point))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--get-content-subtree-at-point</span>()
(<span class="org-keyword">interactive</span>)
<span class="org-string">"Gets the content of the subtree at point, performing the necessary includes</span>
<span class="org-string">to check if the hash has changed"</span>
(<span class="org-keyword">save-excursion</span>
(deactivate-mark t)
(<span class="org-keyword">let*</span> ((start (org-export-head--goto-header t))
(end (org-end-of-subtree t))
(buffer (current-buffer))
(content (buffer-substring start end))
(include-re <span class="org-string">"^[ \t]*#\\+INCLUDE:"</span>))
(<span class="org-keyword">if</span> (string-match include-re content)
(<span class="org-keyword">with-temp-buffer</span>
(insert content)
(org-mode)
(org-export-expand-include-keyword)
(buffer-string))
content))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--get-summary-at-point</span>(<span class="org-type">&optional</span> n-paragraphs n-chars)
<span class="org-doc">"Gets the summary of the subtree at point"</span>
(<span class="org-keyword">interactive</span>)
(<span class="org-keyword">save-excursion</span>
(deactivate-mark t)
(<span class="org-keyword">let*</span> ((n-paragraphs (<span class="org-keyword">or</span> n-paragraphs 1))
(n-chars (<span class="org-keyword">or</span> n-chars 200))
(start (org-export-head--goto-header t))
(endmax (<span class="org-keyword">save-excursion</span> (org-end-of-subtree t)))
(endparagraph
(<span class="org-keyword">save-excursion</span>
(<span class="org-keyword">dotimes</span> (i n-paragraphs)
(org-forward-paragraph))
(- (point) 1)))
(end (min endmax endparagraph (+ start n-chars))))
(buffer-substring start end))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--create-summaries</span>()
<span class="org-doc">"Creates summary for all the headings"</span>
(org-export-head--run-on-each-heading
#'(lambda()
(<span class="org-keyword">let*</span> ((summary (org-entry-get-with-inheritance <span class="org-string">"SUMMARY"</span>))
(summary (<span class="org-keyword">or</span> summary (org-export-head--get-summary-at-point)))
(summary (replace-regexp-in-string <span class="org-string">"\n"</span> <span class="org-string">" "</span> summary)))
(<span class="org-keyword">if</span> summary
(org-set-property <span class="org-string">"SUMMARY"</span> summary))))
<span class="org-string">"-noexport"</span>))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--create-entry-tags</span>()
<span class="org-doc">"Creates list of tags with link"</span>
(org-export-head--run-on-each-heading
#'(lambda()
(<span class="org-keyword">let*</span> ((entry-tags (assoc <span class="org-string">"ALLTAGS"</span> (org-entry-properties)))
(entry-tags (<span class="org-keyword">when</span> entry-tags (delete <span class="org-string">""</span> (split-string (cdr entry-tags) <span class="org-string">":"</span>))))
(url (org-export-head--escape org-export-head-tags-page))
(tags-text-url <span class="org-string">""</span>)
(tags-text <span class="org-string">""</span>)
(tags-buttons-front <span class="org-string">""</span>)
(tags-buttons <span class="org-string">""</span>))
(<span class="org-keyword">dolist</span> (tag entry-tags)
(<span class="org-keyword">unless</span> (<span class="org-keyword">or</span> (string= tag <span class="org-string">"reexport"</span>) (string= tag <span class="org-string">"noexport"</span>) (string= tag <span class="org-string">"noreexport"</span>))
(<span class="org-keyword">setq</span> tags-text-url (concat tags-text-url <span class="org-string">"[[file:"</span> url <span class="org-string">".org::#"</span> tag <span class="org-string">"][#"</span>tag<span class="org-string">"]] "</span>))
(<span class="org-keyword">setq</span> tags-text (concat tags-text tag <span class="org-string">" "</span>))
(<span class="org-keyword">setq</span> tags-buttons-front (concat tags-buttons-front <span class="org-string">"@@html:<a href='#"</span> tag <span class="org-string">"' class='"</span> tag <span class="org-string">" tagbutton'>"</span> tag <span class="org-string">"</a>@@ "</span>))
(<span class="org-keyword">setq</span> tags-buttons (concat tags-buttons <span class="org-string">"@@html:<a href='"</span> url <span class="org-string">".html#"</span> tag <span class="org-string">"' class='"</span> tag <span class="org-string">" tagbutton'>"</span> tag <span class="org-string">"</a>@@ "</span>))))
(org-set-property <span class="org-string">"TAGSURL"</span> tags-text-url)
(org-set-property <span class="org-string">"TAGSFRONT"</span> tags-buttons-front)
(org-set-property <span class="org-string">"TAGSBUTTONS"</span> tags-buttons)
(org-set-property <span class="org-string">"TAGSTEXT"</span> tags-text)))
<span class="org-string">"-noexport"</span>))
<span class="org-comment-delimiter">;;; </span><span class="org-comment">HASH code</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">Idea from https://emacs.stackexchange.com/a/39376/20165</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--update-hashes</span>()
<span class="org-doc">"Updates the hashes of all the headings"</span>
(org-export-head--run-on-each-heading
#'(lambda()
(<span class="org-keyword">let</span> ((new-hash (format <span class="org-string">"%s"</span> (org-export-head-get-hash-value-content)))
(old-hash (org-entry-get-with-inheritance <span class="org-string">"HASH"</span>))
(older-hash (org-entry-get-with-inheritance <span class="org-string">"PREVIOUS-HASH"</span>)))
(<span class="org-keyword">if</span> (not old-hash)
(<span class="org-keyword">progn</span>
(org-set-property <span class="org-string">"CREATION-DATE"</span> (format-time-string <span class="org-string">"%Y-%m-%d"</span>))))
<span class="org-comment-delimiter">;;</span><span class="org-comment">If there was a change made</span>
(<span class="org-keyword">if</span> (not (equal new-hash old-hash))
(<span class="org-keyword">progn</span>
(org-set-property <span class="org-string">"MODIFICATION-DATE"</span> (format-time-string <span class="org-string">"%Y-%m-%d"</span>))
(org-set-property <span class="org-string">"HASH"</span> new-hash)))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Setting property is expensive</span>
(<span class="org-keyword">if</span> (not (equal old-hash older-hash))
(org-set-property <span class="org-string">"PREVIOUS-HASH"</span> (<span class="org-keyword">or</span> old-hash <span class="org-string">""</span>)))))
<span class="org-string">"-noexport"</span>))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head-get-hash-value-content</span>()
<span class="org-doc">"Gets the hash of the subtree at point"</span>
(org-export-head-hash-function (org-export-head--get-content-subtree-at-point)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head-hash-function</span>(text)
<span class="org-doc">"Function to calculate the hash of text.</span>
<span class="org-doc">Can be changed to something such as (length text) to run even faster.</span>
<span class="org-doc">Shouldn't rally affect the time to export unless your file contains over 100 thousand lines of text"</span>
(md5 text))
<span class="org-comment-delimiter">;;;</span><span class="org-comment">END HASH CODE</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--delete-noreexport</span>()
<span class="org-doc">"Faster export by deleting things that won't be exported so we don't process them and their links"</span>
(org-export-head--run-on-each-heading
#'(lambda()
(<span class="org-keyword">let</span> ((old-hash (org-entry-get-with-inheritance <span class="org-string">"PREVIOUS-HASH"</span>))
(new-hash (org-entry-get-with-inheritance <span class="org-string">"HASH"</span>)))
<span class="org-comment-delimiter">;;</span><span class="org-comment">If there was a change made</span>
(<span class="org-keyword">if</span> (equal new-hash old-hash)
(<span class="org-keyword">progn</span>
(org-toggle-tag <span class="org-string">"noreexport"</span> 'on)
<span class="org-comment-delimiter">;;</span><span class="org-comment">faster export by deleting noexport things before processing</span>
(org-export-head--erase-content-subtree)))))
<span class="org-string">"-noexport-reexport"</span>))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--erase-content-subtree</span>()
(<span class="org-keyword">save-excursion</span>
(<span class="org-keyword">let</span> ((start (org-export-head--goto-header t))
(end (org-end-of-subtree)))
(delete-region start end))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--get-headlines</span> ()
<span class="org-doc">"Returns a tuple that contains a hashtable of headline name to Alist of headline properties</span>
<span class="org-doc">As well as a list of the headline names"</span>
(message <span class="org-string">"1"</span>)
(<span class="org-keyword">flet</span> ((make-hash ()
(make-hash-table <span class="org-builtin">:test</span> 'equal))
(add-to-hash (hashtable)
(puthash (org-export-head--headline) (org-entry-properties) hashtable)))
(<span class="org-keyword">let</span> ((headlines-hash (make-hash))
(headlines-list ()))
(org-export-head--run-on-each-heading
#'(lambda()
(add-to-hash headlines-hash)
(<span class="org-keyword">setq</span> headlines-list (cons (org-export-head--headline) headlines-list)))
<span class="org-string">"-noexport"</span>)
(cons headlines-hash headlines-list))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--headline</span> ()
<span class="org-doc">"Gets the headline title if point is at the headline"</span>
(nth 4 (org-heading-components)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--escaped-headline</span> ()
(org-export-head--escape (org-export-head--headline)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--replace-headline-macros</span>(macro-alist headlines-list headlines-hash)
<span class="org-doc">"Replace macros of the type They can contain information such as date</span>
<span class="org-doc">or previous and next post.</span>
<span class="org-doc">Any headline property can be used as a macro of this type."</span>
(<span class="org-keyword">save-excursion</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">Let's find the end of the headline as a marker, since it can move</span>
(<span class="org-keyword">let</span> ((subtree-end-marker (<span class="org-keyword">save-excursion</span> (org-end-of-subtree) (point-marker))))
<span class="org-comment-delimiter">;; </span><span class="org-comment">End of subtree might change because of macro expansion, so it is recalculated.</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">Macros might be substituted for something smaller, so we move the point on to the left at the end.</span>
(<span class="org-keyword">while</span> (re-search-forward <span class="org-string">"\\#\\#\\#</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">(</span></span><span class="org-string">[-A-Za-z_]+</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">)</span></span><span class="org-string">\\#\\#\\#"</span> (marker-position subtree-end-marker) t)
(<span class="org-keyword">unless</span> (org-in-src-block-p)
(<span class="org-keyword">let*</span> ((macro (match-string-no-properties 1))
(macro-subs (cdr (assoc macro macro-alist))))
(<span class="org-keyword">if</span> macro-subs
(replace-match macro-subs t t)
(replace-match <span class="org-string">""</span>))
(backward-char)))))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--get-content-subtree-match</span>(match)
<span class="org-doc">"Get content of the subtree that matches \"match\" </span>
<span class="org-doc">Where match is a tag or -tag or combination of them."</span>
(<span class="org-keyword">save-excursion</span>
(<span class="org-keyword">let</span> ((content <span class="org-string">""</span>))
(org-export-head--run-on-each-heading
#'(lambda()
(<span class="org-keyword">setq</span> content (concat content (org-export-head--get-content-subtree-at-point))))
match)
content)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--insert-on-header</span> (text)
<span class="org-doc">"Insert text on the header of the subtree, but after the property box"</span>
(<span class="org-keyword">save-excursion</span>
(org-export-head--goto-header)
(insert text)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--insert-on-footer</span> (text)
<span class="org-doc">"Insert text on the footer (end) of the subtree"</span>
(<span class="org-keyword">save-excursion</span>
(org-export-head--goto-footer)
(insert text)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--wrap-in-div-class</span> (text classes)
<span class="org-doc">"Wraps the text in a div with classes equal to the list of classes"</span>
(<span class="org-keyword">let</span> ((classes-str (string-join classes <span class="org-string">" "</span>)))
(concat (org-export-head--div-class-start classes) text (org-export-head--div-class-end classes))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--div-class-start</span> (classes)
<span class="org-doc">"Wraps the text in a div with classes equal to the list of classes"</span>
(<span class="org-keyword">let</span> ((classes-str (string-join classes <span class="org-string">" "</span>)))
(concat <span class="org-string">"@@html:<div class=\""</span>classes-str<span class="org-string">" post\">@@"</span> )))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--div-end</span> ()
<span class="org-doc">"Wrapes the text in a div with classes equal to the list of classes"</span>
<span class="org-string">"@@html:</div>@@"</span>)
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--generate-index-alist</span> (headlines-list headlines-hash)
<span class="org-doc">"Geneates an org list with all the different index options of the website and inserts it in an alist"</span>
(<span class="org-keyword">let</span> ((index <span class="org-string">""</span>)
(reverse-index <span class="org-string">""</span>)
(index-with-dates <span class="org-string">""</span>)
(index-with-summaries <span class="org-string">""</span>)
(all-tags <span class="org-string">"\n"</span>)
(all-tags-reverse <span class="org-string">"\n"</span>)
(all-tags-with-dates <span class="org-string">"\n"</span>)
(all-tags-with-summaries <span class="org-string">"\n"</span>)
(index-with-summaries-tags <span class="org-string">""</span>)
(index-with-tags <span class="org-string">""</span>)
(index-tags <span class="org-string">""</span>)
(tags ())
(tags-indexes ())
(tags-buttons ()))
(<span class="org-keyword">cl-flet</span> ((class-start(tags) (org-export-head--div-class-start tags))
(class-end() (org-export-head--div-end)))
(<span class="org-keyword">dolist</span> (headline-name headlines-list)
(<span class="org-keyword">let*</span> ((headline-alist (gethash headline-name headlines-hash nil))
(entry-tags (assoc <span class="org-string">"ALLTAGS"</span> headline-alist))
(entry-tags (<span class="org-keyword">when</span> entry-tags (split-string (cdr entry-tags) <span class="org-string">":"</span>)))
(entry-tags (delete <span class="org-string">""</span> entry-tags))
(entry-tags (delete <span class="org-string">"noexport"</span> entry-tags))
(entry-tags (delete <span class="org-string">"reexport"</span> entry-tags))
(entry-tags (delete <span class="org-string">"noreexport"</span> entry-tags))
(creation-date (cdr (assoc <span class="org-string">"CREATION-DATE"</span> headline-alist)))
(modification-date (cdr (assoc <span class="org-string">"MODIFICATION-DATE"</span> headline-alist)))
(summary (string-trim (cdr (assoc <span class="org-string">"SUMMARY"</span> headline-alist))))
(summary-html (concat <span class="org-string">""</span> (<span class="org-keyword">unless</span> (= (length summary) 0)
(concat <span class="org-string">" @@html:<br>@@"</span> summary))))
(tags-buttons (cdr (assoc <span class="org-string">"TAGSFRONT"</span> headline-alist)))
(headline-link (concat <span class="org-string">"- "</span> (class-start entry-tags) <span class="org-string">" @@html:<b>@@[["</span>headline-name<span class="org-string">"]["</span>headline-name<span class="org-string">"]]@@html:</b>@@"</span>))
(index-entry (concat headline-link (class-end)))
(date (concat <span class="org-string">"@@html:<span class=\"page-date\">@@"</span>
<span class="org-string">" ("</span> creation-date<span class="org-string">", updated "</span> modification-date <span class="org-string">")"</span>
<span class="org-string">"@@html:</span>@@"</span> ))
(index-entry-with-date (concat headline-link date (class-end)))
(index-entry-with-summary
(concat headline-link date <span class="org-string">" "</span> summary-html (class-end)))
(index-entry-with-summary-tags
(concat headline-link date <span class="org-string">" "</span> tags-buttons summary-html (class-end)))
(index-entry-with-tags
(concat headline-link date <span class="org-string">" "</span> tags-buttons (class-end))))
(<span class="org-keyword">setq</span> index (concat index index-entry <span class="org-string">"\n"</span>))
(<span class="org-keyword">setq</span> reverse-index (concat index-entry <span class="org-string">"\n"</span> reverse-index) )
(<span class="org-keyword">setq</span> index-with-dates (concat index-with-dates index-entry-with-date <span class="org-string">"\n"</span>))
(<span class="org-keyword">setq</span> index-with-summaries (concat index-with-summaries index-entry-with-summary <span class="org-string">"\n"</span>))
(<span class="org-keyword">when</span> entry-tags
(<span class="org-keyword">setq</span> index-with-summaries-tags (concat index-with-summaries-tags index-entry-with-summary-tags <span class="org-string">"\n"</span>))
(<span class="org-keyword">setq</span> index-with-tags (concat index-with-tags index-entry-with-tags <span class="org-string">"\n"</span>)))
(<span class="org-keyword">dolist</span> (tag entry-tags)
(<span class="org-keyword">if</span> (not (member tag tags))
(<span class="org-keyword">setq</span> tags (cons tag tags)))
(<span class="org-keyword">dolist</span> (suffix '(<span class="org-string">""</span> <span class="org-string">"-reverse"</span> <span class="org-string">"-with-dates"</span> <span class="org-string">"-with-summaries"</span> <span class="org-string">"-with-summaries-tags"</span> <span class="org-string">"-with-tags"</span>))
<span class="org-comment-delimiter">;; </span><span class="org-comment">Initialize tags lists</span>
(<span class="org-keyword">let</span> ((tag-index-name (upcase (concat tag suffix))))
(<span class="org-keyword">unless</span> (assoc tag-index-name tags-indexes)
(<span class="org-keyword">setq</span> tags-indexes (cons `(,tag-index-name . <span class="org-string">""</span>) tags-indexes)))))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Add tag indexes to list</span>
(<span class="org-keyword">let*</span> ((tag (upcase tag))
(tag-reverse (upcase (concat tag <span class="org-string">"-reverse"</span>)))
(tag-with-dates (upcase (concat tag <span class="org-string">"-with-dates"</span>)))
(tag-with-summaries (upcase (concat tag <span class="org-string">"-with-summaries"</span>)))
(tag-with-summaries-tags (upcase (concat tag <span class="org-string">"-with-summaries-tags"</span>)))
(tag-with-tags (upcase (concat tag <span class="org-string">"-with-tags"</span>)))
(tag-assoc (assoc tag tags-indexes))
(tag-assoc-reverse (assoc tag-reverse tags-indexes))
(tag-assoc-with-dates (assoc tag-with-dates tags-indexes))
(tag-assoc-with-summaries (assoc tag-with-summaries tags-indexes))
(tag-assoc-with-summaries-tags (assoc tag-with-summaries-tags tags-indexes))
(tag-assoc-with-tags (assoc tag-with-tags tags-indexes))
(tag-index (cdr tag-assoc))
(tag-index-reverse (cdr tag-assoc-reverse))
(tag-index-with-dates (cdr tag-assoc-with-dates))
(tag-index-with-summaries (cdr tag-assoc-with-summaries))
(tag-index-with-summaries-tags (cdr tag-assoc-with-summaries-tags))
(tag-index-with-tags (cdr tag-assoc-with-tags)))
(<span class="org-keyword">setf</span> (cdr tag-assoc) (concat tag-index index-entry <span class="org-string">"\n"</span>))
(<span class="org-keyword">setf</span> (cdr tag-assoc-reverse) (concat index-entry <span class="org-string">"\n"</span> tag-index-reverse ))
(<span class="org-keyword">setf</span> (cdr tag-assoc-with-dates) (concat tag-index-with-dates index-entry-with-date <span class="org-string">"\n"</span>))
(<span class="org-keyword">setf</span> (cdr tag-assoc-with-summaries) (concat tag-index-with-summaries index-entry-with-summary <span class="org-string">"\n"</span>))
(<span class="org-keyword">setf</span> (cdr tag-assoc-with-summaries-tags) (concat tag-index-with-summaries-tags index-entry-with-summary-tags <span class="org-string">"\n"</span>))
(<span class="org-keyword">setf</span> (cdr tag-assoc-with-tags) (concat tag-index-with-tags index-entry-with-tags <span class="org-string">"\n"</span>))
(<span class="org-keyword">setf</span> (cdr tag-assoc-with-summaries-tags) (concat tag-index-with-summaries-tags index-entry-with-summary-tags <span class="org-string">"\n"</span>))))))
<span class="org-comment-delimiter">;; </span><span class="org-comment">Now we create an index for all tags, to be able to have tag pages</span>
(<span class="org-keyword">setq</span> tags (cl-sort tags 'string-lessp <span class="org-builtin">:key</span> 'downcase)) <span class="org-comment-delimiter">;</span><span class="org-comment">first sort them</span>
(<span class="org-keyword">dolist</span> (tag tags)
(<span class="org-keyword">unless</span> (<span class="org-keyword">or</span> (string= tag <span class="org-string">"reexport"</span>) (string= tag <span class="org-string">"noexport"</span>) (string= tag <span class="org-string">"noreexport"</span>))
(<span class="org-keyword">let*</span> ((tag-reverse (upcase (concat tag <span class="org-string">"-reverse"</span>)))
(tag-with-dates (upcase (concat tag <span class="org-string">"-with-dates"</span>)))
(tag-with-summaries (upcase (concat tag <span class="org-string">"-with-summaries"</span>)))
(tag-with-summaries-list (cdr (assoc tag-with-summaries tags-indexes)))
(tag-list (cdr (assoc tag tags-indexes)))
(tag-reverse-list (cdr (assoc tag-reverse tags-indexes)))
(tag-with-dates-list (cdr (assoc tag-with-dates tags-indexes))))
(<span class="org-keyword">setq</span> all-tags (concat all-tags <span class="org-string">"** "</span> tag <span class="org-string">"\n"</span> tag-list))
(<span class="org-keyword">setq</span> all-tags-reverse (concat all-tags-reverse <span class="org-string">"** "</span> tag <span class="org-string">"\n"</span> tag-reverse-list))
(<span class="org-keyword">setq</span> all-tags-with-dates (concat all-tags-with-dates <span class="org-string">"** "</span> tag <span class="org-string">"\n"</span> tag-with-dates-list))
(<span class="org-keyword">setq</span> all-tags-with-summaries (concat all-tags-with-summaries <span class="org-string">"** "</span> tag <span class="org-string">"\n"</span> tag-with-summaries-list)))))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Kinda bad, generates the tag buttons for each tag. Ideally should be done only if requested</span>
(<span class="org-keyword">dolist</span> (tag tags)
(<span class="org-keyword">unless</span> (<span class="org-keyword">or</span> (string= tag <span class="org-string">"reexport"</span>) (string= tag <span class="org-string">"noexport"</span>) (string= tag <span class="org-string">"noreexport"</span>))
(<span class="org-keyword">setq</span> tags-buttons (cons `(,(upcase (concat tag <span class="org-string">"-tags"</span>)) . ,(org-export--get-tag-buttons tag)) tags-buttons))))
(<span class="org-keyword">setq</span> index-tags (org-export--get-tag-buttons))
(append
(list (cons <span class="org-string">"INDEX"</span> index) (cons <span class="org-string">"INDEX-TAGS"</span> index-tags) (cons <span class="org-string">"INDEX-REVERSE"</span> reverse-index) (cons <span class="org-string">"INDEX-WITH-DATES"</span> index-with-dates) (cons <span class="org-string">"INDEX-WITH-SUMMARIES"</span> index-with-summaries) (cons <span class="org-string">"INDEX-WITH-SUMMARIES-TAGS"</span> index-with-summaries-tags)
(cons <span class="org-string">"INDEX-WITH-TAGS"</span> index-with-tags)
(cons <span class="org-string">"ALL-TAGS"</span> all-tags) (cons <span class="org-string">"ALL-TAGS-REVERSE"</span> all-tags-reverse) (cons <span class="org-string">"ALL-TAGS-WITH-DATES"</span> all-tags-with-dates) (cons <span class="org-string">"ALL-TAGS-WITH-SUMMARIES"</span> all-tags-with-summaries))
tags-indexes tags-buttons))))
<span class="org-comment-delimiter">;;</span><span class="org-comment">END OF NON AST (non org-element) SESSION</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--fix-local-link-ast</span> (headlines link)
<span class="org-doc">"Fixes fuzzy links to headlines, so the they point to new files"</span>
(<span class="org-keyword">flet</span> ((get-hash (element set)
(gethash element set nil)))
(<span class="org-keyword">when</span> (string= (org-element-property <span class="org-builtin">:type</span> link) <span class="org-string">"fuzzy"</span>)
(<span class="org-keyword">let*</span> ((path (org-element-property <span class="org-builtin">:path</span> link))
(new-path (get-hash path headlines)))
(<span class="org-keyword">if</span> new-path
(<span class="org-keyword">let</span> ((link-copy (org-element-copy link)))
(apply #'org-element-adopt-elements link-copy (org-element-contents link))
(org-element-put-property link-copy <span class="org-builtin">:type</span> <span class="org-string">"file"</span>)
(org-element-put-property link-copy <span class="org-builtin">:path</span> (concat (org-export-head--escape path) <span class="org-string">".org"</span>))
(org-element-set-element link link-copy))
<span class="org-comment-delimiter">;; </span><span class="org-comment">else: need to check if the link is linking to a subheadline</span>
(<span class="org-keyword">save-excursion</span>
(org-link-search path)
<span class="org-comment-delimiter">;; </span><span class="org-comment">If the same heading contains multiple subheadings, each with the same name; it will simply link to the first one</span>
(<span class="org-keyword">let</span> ((link-copy (org-element-copy link)))
(apply #'org-element-adopt-elements link-copy (org-element-contents link))
(org-element-put-property link-copy <span class="org-builtin">:type</span> <span class="org-string">"file"</span>)
(org-element-put-property link-copy <span class="org-builtin">:path</span> (concat (org-export-head--escape (org-find-top-headline)) <span class="org-string">".org"</span>))
(org-element-put-property link-copy <span class="org-builtin">:search-option</span> (concat <span class="org-string">"#"</span> (org-export-head--escape path)))
(org-element-set-element link link-copy))
))))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--fix-file-external-link-ast</span> (directory-path link)
<span class="org-doc">"Creates hard links to the external files in the output directory</span>
<span class="org-doc">Only modifies links if file exists"</span>
(<span class="org-keyword">when</span> (<span class="org-keyword">and</span> (member (org-element-property <span class="org-builtin">:type</span> link) org-export-head-link-files-to-export) (file-exists-p (org-element-property <span class="org-builtin">:path</span> link)))
(<span class="org-keyword">let*</span> ((path (org-element-property <span class="org-builtin">:path</span> link))
(link-copy (org-element-copy link))
(extension (file-name-extension path))
(img-extensions '(<span class="org-string">"jpg"</span> <span class="org-string">"tiff"</span> <span class="org-string">"png"</span> <span class="org-string">"bmp"</span>))
(link-description (org-element-contents link))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Removes ../ from the releative path of the file to force it to be moved to a subfolder</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">of the current dir. This causes some file conflits in edge cases</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">e.g: ../images and ../../images will map to the same place. This should be rare in normal usage</span>
(new-relative-path
(concat <span class="org-string">"./"</span> (file-name-extension path) <span class="org-string">"/"</span> (file-name-nondirectory path)))
(new-hard-link-path (concat directory-path new-relative-path))
(new-hard-link-directory (file-name-directory new-hard-link-path)))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Create hard link folder</span>
(make-directory new-hard-link-directory t)
<span class="org-comment-delimiter">;;</span><span class="org-comment">Create hard link, not replacing if it already exists, catching error if file does not exist</span>
(<span class="org-keyword">let</span> ((new-file
(<span class="org-keyword">condition-case</span> nil
(add-name-to-file path new-hard-link-path nil)
(<span class="org-warning">error</span> nil))))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Fix the AST</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">If image, remove description so it will become a real image instead of a link</span>
(<span class="org-keyword">if</span> (not (member extension img-extensions))
(apply #'org-element-adopt-elements link-copy link-description)
(<span class="org-keyword">if</span> org-export-head-resize-images
(<span class="org-keyword">let</span> ((new-description (org-export-head--resize-image new-hard-link-path org-export-head-resize-images-width org-export-head-resize-images-height org-export-head-resize-images-max-size)))
(org-element-adopt-elements link-copy (concat <span class="org-string">"file:"</span>(org-export-head--replace-in-string directory-path <span class="org-string">""</span> new-description))))
))
(org-element-put-property link-copy <span class="org-builtin">:path</span> new-relative-path)
(org-element-set-element link link-copy)
new-file
))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--replace-in-string</span> (what with in)
<span class="org-doc">"https://stackoverflow.com/a/17325791/5881930"</span>
(replace-regexp-in-string (regexp-quote what) with in nil 'literal))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--resize-image</span> (path width height max-bytes)
<span class="org-doc">"resizes path to width height, keeping proportions, if file larger than max bytes"</span>
(<span class="org-keyword">let*</span> ((swidth (number-to-string width))
(sheight (number-to-string height))
(resized-path (concat (file-name-sans-extension path) swidth <span class="org-string">"x"</span> sheight <span class="org-string">"."</span> (file-name-extension path)))
(size-bytes (file-attribute-size (file-attributes path))))
(<span class="org-keyword">if</span> (> size-bytes max-bytes)
(<span class="org-keyword">progn</span> (<span class="org-keyword">if</span> (not (file-exists-p resized-path))
(call-process <span class="org-string">"convert"</span> nil t nil path
<span class="org-string">"-resize"</span>
(concat swidth <span class="org-string">"x"</span> sheight<span class="org-string">">"</span>)
resized-path))
resized-path)
path)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--insert-next-previous-headline</span>(headlines-hash headlines-list)
<span class="org-doc">"Decides what is the next and the previous post and create macro"</span>
(<span class="org-keyword">let*</span> ((temp-list (cons nil headlines-list))
(len (length headlines-list)))
(<span class="org-keyword">dotimes</span> (i len)
(<span class="org-keyword">let*</span> ((previous (nth 0 temp-list))
(headline-name (nth 1 temp-list))
(next (nth 2 temp-list))
(headline (gethash headline-name headlines-hash nil))
(new-properties
(list (cons <span class="org-string">"PREVIOUS"</span> (<span class="org-keyword">or</span> next <span class="org-string">"index"</span>))
(cons <span class="org-string">"NEXT"</span> (<span class="org-keyword">or</span> previous <span class="org-string">"index"</span>))))
(headline (append headline new-properties))) <span class="org-comment-delimiter">;; </span><span class="org-comment">In reverse order, to allow headline properties to shadow this.</span>
(puthash headline-name headline headlines-hash))
(<span class="org-keyword">setq</span> temp-list (cdr temp-list))))
headlines-hash)
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--add-title-macro</span>(headlines-hash headlines-list)
<span class="org-doc">"Creates title macro"</span>
(<span class="org-keyword">let*</span> ((temp-list (cons nil headlines-list))
(len (length headlines-list)))
(<span class="org-keyword">dotimes</span> (i len)
(<span class="org-keyword">let*</span> ((headline-name (nth 1 temp-list))
(headline (gethash headline-name headlines-hash nil))
(new-properties
(list (cons <span class="org-string">"TITLE"</span> headline-name)))
(headline (append headline new-properties))) <span class="org-comment-delimiter">;; </span><span class="org-comment">In reverse order, to allow headline properties to shadow this.</span>
(puthash headline-name headline headlines-hash))
(<span class="org-keyword">setq</span> temp-list (cdr temp-list))))
headlines-hash)
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--headline-to-file</span>(headline-name)
<span class="org-doc">"Generate the file name of the headline"</span>
(concat (org-export-head--escape headline-name) <span class="org-string">".org"</span>))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--escape</span>(text)
(<span class="org-keyword">when</span> text
(<span class="org-keyword">let*</span> ((text (replace-regexp-in-string <span class="org-string">" "</span> <span class="org-string">"_"</span> text))
(text (replace-regexp-in-string <span class="org-string">"/"</span> <span class="org-string">"-"</span> text))
(text (replace-regexp-in-string <span class="org-string">"[</span><span class="org-string"><span class="org-negation-char">^</span></span><span class="org-string">[:alnum:]-_]"</span> <span class="org-string">""</span> (downcase text))))
text)))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Nice export headings http://ivanmalison.github.io/dotfiles/#usemyowndefaultnamingschemefororgheadings</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">imalison:org-get-raw-value</span> (item)
(<span class="org-keyword">when</span> (listp item)
(<span class="org-keyword">let*</span> ((property-list (cadr item)))
(<span class="org-keyword">when</span> property-list (plist-get property-list <span class="org-builtin">:raw-value</span>)))))
(<span class="org-keyword">defun</span> <span class="org-function-name">imalison:generate-name</span> (datum cache)
(<span class="org-keyword">let</span> ((raw-value (imalison:org-get-raw-value datum)))
(<span class="org-keyword">if</span> raw-value
(org-export-head--escape raw-value)
<span class="org-comment-delimiter">;; </span><span class="org-comment">This is the default implementation from org</span>
(<span class="org-keyword">let</span> ((type (org-element-type datum)))
(format <span class="org-string">"org%s%d"</span>
(<span class="org-keyword">if</span> type
(replace-regexp-in-string <span class="org-string">"-"</span> <span class="org-string">""</span> (symbol-name type))
<span class="org-string">"secondarystring"</span>)
(<span class="org-keyword">incf</span> (gethash type cache 0)))))))
<span class="org-comment-delimiter">;</span><span class="org-comment">(use-package ox)</span>
<span class="org-comment-delimiter">; </span><span class="org-comment">:defer t</span>
<span class="org-comment-delimiter">; </span><span class="org-comment">:config</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-get-reference-custom</span> (datum info)
<span class="org-doc">"Return a unique reference for DATUM, as a string.</span>
<span class="org-doc">DATUM is either an element or an object. INFO is the current</span>
<span class="org-doc">export state, as a plist. Returned reference consists of</span>
<span class="org-doc">alphanumeric characters only."</span>
(<span class="org-keyword">let</span> ((type (org-element-type datum))
(cache (<span class="org-keyword">or</span> (plist-get info <span class="org-builtin">:internal-references</span>)
(<span class="org-keyword">let</span> ((h (make-hash-table <span class="org-builtin">:test</span> #'eq)))
(plist-put info <span class="org-builtin">:internal-references</span> h)
h)))
(reverse-cache (<span class="org-keyword">or</span> (plist-get info <span class="org-builtin">:taken-internal-references</span>)
(<span class="org-keyword">let</span> ((h (make-hash-table <span class="org-builtin">:test</span> 'equal)))
(plist-put info <span class="org-builtin">:taken-internal-references</span> h)
h))))
(<span class="org-keyword">or</span> (gethash datum cache)
(<span class="org-keyword">let*</span> ((name (imalison:generate-name datum cache))
(number (+ 1 (gethash name reverse-cache -1)))
(new-name (format <span class="org-string">"%s%s"</span> name (<span class="org-keyword">if</span> (< 0 number) (format <span class="org-string">"%s%s"</span> <span class="org-string">"."</span> number) <span class="org-string">""</span>))))
(puthash name number reverse-cache)
(puthash datum new-name cache)
new-name))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head-other-file</span> (file directory-name <span class="org-type">&optional</span> reexport)
<span class="org-doc">"Main function of this script"</span>
(find-file file)
(make-directory <span class="org-string">"../.emacs-saves"</span> t)
(<span class="org-keyword">let</span> ((backup-directory-alist `((<span class="org-string">"."</span> . <span class="org-string">"../.emacs-saves/"</span>)))
(auto-save-file-name-transforms `((<span class="org-string">".*"</span> <span class="org-string">"../.emacs-saves/"</span> t))))
(<span class="org-keyword">require</span> '<span class="org-constant">ox</span>)
(<span class="org-keyword">require</span> '<span class="org-constant">cl</span>)
(<span class="org-keyword">require</span> '<span class="org-constant">subr-x</span>)
(<span class="org-keyword">setq</span> org-src-fontify-natively t)
(<span class="org-keyword">require</span> '<span class="org-constant">package</span>)
(add-to-list 'package-archives '(<span class="org-string">"melpa"</span> . <span class="org-string">"http://melpa.org/packages/"</span>))
(<span class="org-keyword">setq</span> package-load-list '((htmlize t)))
(package-initialize)
(<span class="org-keyword">unless</span> (package-installed-p 'htmlize)
(package-refresh-contents)
(package-install 'htmlize))
(<span class="org-keyword">require</span> '<span class="org-constant">htmlize</span>)
(<span class="org-keyword">setq</span> org-export-htmlize-output-type 'css)
(<span class="org-keyword">setq</span> org-html-htmlize-output-type 'css)
(<span class="org-keyword">setq</span> org-confirm-babel-evaluate nil)
(org-mode)
(transient-mark-mode) <span class="org-comment-delimiter">;</span><span class="org-comment">necessary for using org-map-entries</span>
(outline-show-all)
(org-export-head (concat directory-name <span class="org-string">"/"</span>) nil reexport)
(save-buffer)))
<span class="org-comment-delimiter">;;</span><span class="org-comment">Inpired by https://emacs.stackexchange.com/questions/51251/org-mode-html-export-table-of-contents-without-h2</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">from ox-html.el</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head--custom-toc</span>(depth info <span class="org-type">&optional</span> scope)
(<span class="org-keyword">let</span> ((toc-entries
(mapcar (<span class="org-keyword">lambda</span> (headline)
(cons (org-html--format-toc-headline headline info)
(org-export-get-relative-level headline info)))
(org-export-collect-headlines info depth scope))))
(<span class="org-keyword">when</span> toc-entries
(<span class="org-keyword">let</span> ((toc (concat <span class="org-string">"<nav id=\"table-of-contents\">\n"</span>
<span class="org-string">"<input id=\"toggle-toc\" style=\"display: none; visibility: hidden;\" type=\"checkbox\">\n"</span>
<span class="org-string">"<label for=\"toggle-toc\">\n <h2> <b> Table of Contents </b> </h2>\n </label>\n"</span>
<span class="org-string">"<div id=\"text-table-of-contents\">"</span>
(org-html--toc-text toc-entries)
<span class="org-string">"</div>\n"</span>
<span class="org-string">"</nav> \n"</span>)))
toc))))
<span class="org-comment-delimiter">;;</span><span class="org-comment">by jkitcking https://stackoverflow.com/a/27527252/5881930</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export--get-tag-counts</span> (<span class="org-type">&optional</span> match)
(<span class="org-keyword">let*</span> ((all-tags '())
(ignore (<span class="org-keyword">or</span> match <span class="org-string">""</span>))
(match (concat (<span class="org-keyword">or</span> match <span class="org-string">""</span>) <span class="org-string">"-noexport"</span>)))
(org-map-entries
(<span class="org-keyword">lambda</span> ()
(<span class="org-keyword">let</span> ((tag-string (car (last (org-heading-components)))))
(<span class="org-keyword">when</span> tag-string
(<span class="org-keyword">setq</span> all-tags
(append all-tags (split-string tag-string <span class="org-string">":"</span> t)))))) <span class="org-warning">match)</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">now get counts</span>
(remove nil (<span class="org-keyword">loop</span> for tag in (remove-duplicates all-tags
<span class="org-builtin">:test</span> (<span class="org-keyword">lambda</span> (x y) (<span class="org-keyword">or</span> (null y) (equal x y)))
<span class="org-builtin">:from-end</span> t)
collect
(<span class="org-keyword">unless</span> (<span class="org-keyword">or</span> (string= tag <span class="org-string">"reexport"</span>) (string= tag <span class="org-string">"noexport"</span>) (string= tag <span class="org-string">"noreexport"</span>) (string= tag ignore))
(cons tag (cl-count tag all-tags <span class="org-builtin">:test</span> 'string=)))))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export--downcase-car</span>(x)
(downcase (car x)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export--get-tag-buttons</span> (<span class="org-type">&optional</span> match num)
<span class="org-doc">"Gets the tag buttons for the pages that match 'match'"</span>
(<span class="org-keyword">let*</span> ((tags-counts (org-export--get-tag-counts match))
(tags-counts (cl-sort tags-counts 'string-lessp <span class="org-builtin">:key</span> 'org-export--downcase-car)) <span class="org-comment-delimiter">;</span><span class="org-comment">first sort them</span>
(tags-buttons <span class="org-string">""</span>))
(<span class="org-keyword">dolist</span> (tag-tupl tags-counts)
(<span class="org-keyword">let</span> ((tag (car tag-tupl))
(tagN (number-to-string (cdr tag-tupl))))
(<span class="org-keyword">if</span> num
(<span class="org-keyword">setq</span> tags-buttons (concat tags-buttons <span class="org-string">"@@html:<a href='#"</span> tag <span class="org-string">"' class='tagbutton tagindex "</span> tag<span class="org-string">"'>"</span> tag <span class="org-string">" "</span>tagN <span class="org-string">"</a>@@ "</span>))
(<span class="org-keyword">setq</span> tags-buttons (concat tags-buttons <span class="org-string">"@@html:<a href='#"</span> tag <span class="org-string">"' class='tagbutton tagindex "</span> tag <span class="org-string">"'>"</span> tag <span class="org-string">"</a>@@ "</span>)))))
tags-buttons))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head-refile-subtree-at-point</span>()
(<span class="org-keyword">interactive</span>)
<span class="org-string">"Cuts the subtree leaving the header"</span>
(<span class="org-keyword">save-excursion</span>
(deactivate-mark t)
(<span class="org-keyword">let*</span> ((start (org-export-head--goto-header t))
(end (org-end-of-subtree t))
(buffer (current-buffer))
(content (buffer-substring start end)))
(<span class="org-keyword">let*</span> ((file-name (read-file-name <span class="org-string">"Move subtree to file:"</span> nil nil 'confirm (concat (nth 4 (org-heading-components)) <span class="org-string">".org"</span>))))
(<span class="org-keyword">with-temp-buffer</span>
(insert content)
(write-file file-name))
(<span class="org-keyword">let</span> ((relative-file-name (file-relative-name file-name)))
(insert (concat <span class="org-string">"#+INCLUDE \""</span> relative-file-name <span class="org-string">"\"\n"</span>))
(insert (concat <span class="org-string">"@@comment: [[file:"</span> relative-file-name <span class="org-string">"]] @@"</span>))
(delete-region start end)
content)))))
<span class="org-comment-delimiter">;;; </span><span class="org-comment">START utils</span>
<span class="org-comment-delimiter">;;</span><span class="org-comment">Add video links</span>
(<span class="org-keyword">if</span> org-export-head-using-video-links
(<span class="org-keyword">progn</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-head-export-video</span> (path desc format)
<span class="org-doc">"Format video links for export."</span>
(<span class="org-keyword">cl-case</span> format
(html (concat <span class="org-string">"<video controls></span>
<span class="org-string"> <source src=\""</span>path<span class="org-string">"\"></span>
<span class="org-string"> Sorry, your browser doesn't support embedded videos.</span>
<span class="org-string"></video>"</span> ))
(latex (format <span class="org-string">"\\href{%s}{%s}"</span> path (<span class="org-keyword">or</span> desc path)))
(otherwise path)))
(org-link-set-parameters <span class="org-string">"video"</span> <span class="org-builtin">:export</span> 'org-export-head-export-video)))
(<span class="org-keyword">if</span> org-export-head-using-inline-js
(<span class="org-keyword">progn</span>
(add-to-list 'org-src-lang-modes '(<span class="org-string">"inline-js"</span> . javascript)) <span class="org-comment-delimiter">;; </span><span class="org-comment">js2 if you're fancy</span>
(<span class="org-keyword">defvar</span> <span class="org-variable-name">org-babel-default-header-args:inline-js</span>
'((<span class="org-builtin">:results</span> . <span class="org-string">"html"</span>)
(<span class="org-builtin">:exports</span> . <span class="org-string">"results"</span>)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-babel-execute:inline-js</span> (body _params)
(format <span class="org-string">"<script type=\"text/javascript\">\n%s\n</script>"</span> body))))
(<span class="org-keyword">let</span> ((file (elt argv 0))
(dir (elt argv 1))
(reexport (elt argv 2)))
(<span class="org-keyword">if</span> (<span class="org-keyword">or</span> (not file) (not dir))
(message <span class="org-string">"usage FILE DIR [export]"</span>)
(message <span class="org-string">"Exportinf %s to %s"</span> file dir)
(org-export-head-other-file file dir reexport)))
</pre>
</div>
</div></div>
<br>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'ivanaf'; // Required - Replace '<example>' with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
var showComments = function() {
var button = document.getElementById('comment-button')
button.style.display = 'none'
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
};
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<button id="comment-button" onclick="showComments()">Show comments</button>
</div>
<div><div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Ivan Tadeu Ferreira Antunes Filho</p>
<p class="date">Date: 2022-07-23 Sat 05:11</p>
<p class="author">Github: <a href="https://github.com/itf/">github.com/itf</a></p>
<p class="creator">Made with <a href="https://www.gnu.org/software/emacs/">Emacs</a> 27.1 (<a href="https://orgmode.org">Org</a> mode 9.3) and <a href="https://github.com/itf/org-export-head">Org export head</a> </p>
</div>
</body>
</html>