/
developer-guide.html
4115 lines (3741 loc) · 265 KB
/
developer-guide.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
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Lilac Developer Guide</title>
<meta name="author" content="Linus Arver" />
<meta name="generator" content="Org Mode" />
<style>
#content { max-width: 60em; margin: auto; }
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #e6e6e6;
border-radius: 3px;
background-color: #f2f2f2;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: auto;
}
pre.src:before {
display: none;
position: absolute;
top: -8px;
right: 12px;
padding: 3px;
color: #555;
background-color: #f2f2f299;
}
pre.src:hover:before { display: inline; margin-top: 14px;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-authinfo::before { content: 'Authinfo'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { }
</style>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&family=Source+Sans+3:ital,wght@0,200..900;1,200..900&family=Source+Code+Pro:ital,wght@0,200..900;1,200..900">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script src="lilac.js"></script>
<link rel="stylesheet" type="text/css" href="syntax-highlighting.css"/>
<link rel="stylesheet" type="text/css" href="lilac.css" />
<!-- LILAC_HTML_HEAD -->
<script>
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.classList.add("code-highlighted");
target.classList.add("code-highlighted");
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.classList.remove("code-highlighted");
target.classList.remove("code-highlighted");
}
}
// @license-end
</script>
</head>
<body>
<div id="content" class="content">
<h1 class="title">Lilac Developer Guide</h1>
<div id="table-of-contents" role="doc-toc">
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#h-Development-environment--Nix-shell">1. Development environment (Nix shell)</a></li>
<li><a href="#h-Makefile">2. Makefile</a>
<ul>
<li><a href="#h-Linting">2.1. Linting</a>
<ul>
<li><a href="#h-Spell-checker">2.1.1. Spell checker</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#h-Tangling--generating-the-source-code">3. Tangling (generating the source code)</a>
<ul>
<li><a href="#h-Custom-Noweb-delimiters">3.1. Custom Noweb delimiters</a></li>
<li><a href="#h-Allow-evaluation-of-code-blocks">3.2. Allow evaluation of code blocks</a></li>
</ul>
</li>
<li><a href="#h-Weaving--generating-the-HTML">4. Weaving (generating the HTML)</a>
<ul>
<li><a href="#h-Emacs-customizations---lilac-el-">4.1. Emacs customizations (<code>lilac.el</code>)</a></li>
<li><a href="#h-Fix-non-determinism">4.2. Fix non-determinism</a>
<ul>
<li><a href="#h-Do-not-insert-current-time-as-HTML-comment">4.2.1. Do not insert current time as HTML comment</a></li>
<li><a href="#h-Do-not-insert-current-Org-mode-version">4.2.2. Do not insert current Org mode version</a></li>
<li><a href="#h-Do-not-use-random-numbers-for-the-HTML--id--attribute">4.2.3. Do not use random numbers for the HTML <code>id</code> attribute</a></li>
</ul>
</li>
<li><a href="#h-Toplevel-publishing-function---lilac-publish-">4.3. Toplevel publishing function (<code>lilac-publish</code>)</a>
<ul>
<li><a href="#h-Helper-functions">4.3.1. Helper functions</a></li>
</ul>
</li>
<li><a href="#h-Org-modifications">4.4. Org modifications</a>
<ul>
<li><a href="#h-Give-all-source-code-blocks-a----name-------field--HTML-ID">4.4.1. Give all source code blocks a <code>#+name: ...</code> field (HTML ID)</a></li>
<li><a href="#h-Automatic-captions-for-Noweb-source-code-blocks">4.4.2. Automatic captions for Noweb source code blocks</a></li>
<li><a href="#h-Human-readable-UIDs--Headings--aka-headlines">4.4.3. Human-readable UIDs (Headings, aka headlines)</a></li>
</ul>
</li>
<li><a href="#h-HTML-modifications">4.5. HTML modifications</a>
<ul>
<li><a href="#h-Use-human-readable-HTML-IDs-for-source-code-links">4.5.1. Use human-readable HTML IDs for source code links</a></li>
<li><a href="#h-Pretty-source-code-captions">4.5.2. Pretty source code captions</a></li>
<li><a href="#h-Link-noweb-references--link-to-child-block-from-parent-block">4.5.3. Link noweb references (link to child block from parent block)</a></li>
<li><a href="#h-Search-and-replace-hardcoded-things">4.5.4. Search and replace hardcoded things</a>
<ul>
<li><a href="#h-MathJax-with-line-breaking-support">4.5.4.1. MathJax with line breaking support</a></li>
<li><a href="#h-Bibliography--citations">4.5.4.2. Bibliography (citations)</a></li>
<li><a href="#h-Glossary--description-lists">4.5.4.3. Glossary (description lists)</a></li>
</ul>
</li>
<li><a href="#h-Custom-HTML--head--section">4.5.5. Custom HTML "head" section</a></li>
</ul>
</li>
<li><a href="#h-JavaScript">4.6. JavaScript</a>
<ul>
<li><a href="#h-Self-linked-headlines">4.6.1. Self-linked headlines</a></li>
<li><a href="#h-Table-of-contents-sidebar">4.6.2. Table of contents sidebar</a>
<ul>
<li><a href="#h-Delete--Table-of-Contents--text">4.6.2.1. Delete "Table of Contents" text</a></li>
<li><a href="#h-Convert--Table-of-Contents--to-a-sidebar-on-the-left">4.6.2.2. Convert "Table of Contents" to a sidebar on the left</a></li>
<li><a href="#h-Track-the-current-headline">4.6.2.3. Track the current headline</a></li>
</ul>
</li>
<li><a href="#h-Highlight-and-scroll-to-just-clicked-on-item">4.6.3. Highlight and scroll to just-clicked-on item</a></li>
<li><a href="#h-Scroll-to-history-item">4.6.4. Scroll to history item</a></li>
</ul>
</li>
<li><a href="#h-Autogenerate-CSS-for-syntax-highlighting-of-source-code-blocks">4.7. Autogenerate CSS for syntax highlighting of source code blocks</a></li>
<li><a href="#h-Misc-settings">4.8. Misc settings</a>
<ul>
<li><a href="#h-Use-HTML5-export--not-XML--to-un-break-MathJax">4.8.1. Use HTML5 export, not XML (to un-break MathJax)</a></li>
<li><a href="#h-Set-citeproc-styles-folder">4.8.2. Set citeproc styles folder</a></li>
<li><a href="#h-Code-references">4.8.3. Code references</a>
<ul>
<li><a href="#h-Define--CodeHighlightOn--and--CodeHighlightOff">4.8.3.1. Define <code>CodeHighlightOn</code> and <code>CodeHighlightOff</code></a></li>
<li><a href="#h-Do-not-highlight-coderefs">4.8.3.2. Do not highlight coderefs</a></li>
</ul>
</li>
<li><a href="#h-Preserve-leading-whitespace-characters-on-export">4.8.4. Preserve leading whitespace characters on export</a></li>
<li><a href="#h-Set-tab-width-to-4-spaces">4.8.5. Set tab width to 4 spaces</a></li>
<li><a href="#h-Disable-backups">4.8.6. Disable backups</a></li>
<li><a href="#h-Profiling">4.8.7. Profiling</a></li>
</ul>
</li>
<li><a href="#h-Imports">4.9. Imports</a></li>
<li><a href="#h-Additional--hand-tweaked--CSS">4.10. Additional (hand-tweaked) CSS</a>
<ul>
<li><a href="#h-Colors-and-fonts">4.10.1. Colors and fonts</a></li>
<li><a href="#h-General-body-text">4.10.2. General body text</a></li>
<li><a href="#h-Headline-font-sizes">4.10.3. Headline font sizes</a>
<ul>
<li><a href="#h-Title-font">4.10.3.1. Title font</a></li>
</ul>
</li>
<li><a href="#h-Tables">4.10.4. Tables</a></li>
<li><a href="#h-Description-lists">4.10.5. Description lists</a></li>
<li><a href="#h-Images">4.10.6. Images</a></li>
<li><a href="#h-Monospaced">4.10.7. Monospaced</a></li>
<li><a href="#h-Links">4.10.8. Links</a></li>
<li><a href="#h-Source-code-block-body">4.10.9. Source code block body</a></li>
<li><a href="#h-Source-code-block-captions">4.10.10. Source code block captions</a></li>
<li><a href="#h-Links-to-child-source-block-from-parent">4.10.11. Links to child source block from parent</a></li>
<li><a href="#h-Sidenotes">4.10.12. Sidenotes</a></li>
<li><a href="#h-Printer-friendly-styling">4.10.13. Printer-friendly styling</a></li>
<li><a href="#h-Adjustments-for-mobile--touch--screens">4.10.14. Adjustments for mobile (touch) screens</a></li>
</ul>
</li>
<li><a href="#h-Create--lilac-theme--file">4.11. Create <code>lilac.theme</code> file</a></li>
<li><a href="#h-Ignore-woven-HTML-from--git-diff">4.12. Ignore woven HTML from <code>git diff</code></a>
<ul>
<li><a href="#h-git-add--p">4.12.1. <code>git add -p</code></a></li>
</ul>
</li>
<li><a href="#h-gitignore">4.13. gitignore</a></li>
</ul>
</li>
<li><a href="#h-Tests">5. Tests</a>
<ul>
<li><a href="#h-Test-helpers">5.1. Test helpers</a>
<ul>
<li><a href="#h-Avoid-cross-abstraction-Noweb-reference-prefix-collision">5.1.1. Avoid cross-abstraction Noweb reference prefix collision</a></li>
<li><a href="#h-Setup-and-tear-down-fixture-for-HTML-tests">5.1.2. Setup and tear-down fixture for HTML tests</a></li>
</ul>
</li>
<li><a href="#h-Smart-source-code-block-caption-helpers">5.2. Smart source code block caption helpers</a></li>
<li><a href="#h-Link-to-child-block-from-parent-block">5.3. Link to child block from parent block</a>
<ul>
<li><a href="#h-One--same--child-node-block-referenced-in-two-different-parent-blocks">5.3.1. One (same) child node block referenced in two different parent blocks</a></li>
<li><a href="#h-Nested-child-blocks">5.3.2. Nested child blocks</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#h-Glossary">6. Glossary</a></li>
<li><a href="#h-References">7. References</a></li>
</ul>
</div>
</div>
<p>
All source code is generated by <i>tangling</i> this Org file
(<code>developer-guide.org</code>). This file is the single source of truth for basically
everything. Some other things like COPYRIGHT and LICENSE do not come from this
file, but they are exceptions.
</p>
<p>
Tangling is done by loading this file into Emacs and then running
<code>(org-babel-tangle)</code>. This file is also part of the <i>woven</i> HTML documentation
as <code>developer-guide.html</code>, which is referenced by the introductory docs at
<code>README.html</code>. The HTML is generated by invoking <code>(lilac-publish)</code>. The outputs
of both tangling and weaving are checked into version control.
</p>
<p>
The <code>Makefile</code> in this repo is used as the main "driver" for both tangling and
weaving. Typically, you would have a browser pointed to <code>README.html</code> or
<code>developer-guide.html</code> (whichever one you are working on) and refresh it after
editing the corresponding Org file. After every change to the Org file, you can
run <code>make</code> to tangle, weave, and run unit tests.
</p>
<div id="outline-container-h-Development-environment--Nix-shell" class="outline-2">
<h2 id="h-Development-environment--Nix-shell"><span class="section-number-2">1.</span> Development environment (Nix shell)</h2>
<div class="outline-text-2" id="text-h-Development-environment--Nix-shell">
<p>
This is the main development shell and brings in all of our dependencies to
build all of our code. Taken from <a href="https://github.com/tweag/haskell-stack-nix-example/blob/b9383e35416a2b0e21fbc97ed079538f9f395b6a/shell.nix#L1">here</a>. The <code>Makefile</code> is meant to be executed
from within this environment.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><label class="org-src-name">shell.nix</label><span class="lilac-caption-link-symbol"><a href="#shell-nix">🔗</a></span></div><pre class="src src-nix" id="shell-nix"><span class="org-nix-keyword">let</span>
<span class="org-comment"># Nixpkgs snapshot.</span>
<span class="org-nix-attribute">sources</span> = <span class="org-nix-builtin">import</span> <span class="org-nix-constant">./package/nix/sources.nix</span>;
<span class="org-comment"># The final "pkgs" attribute.</span>
<span class="org-nix-attribute">pkgs</span> = <span class="org-nix-builtin">import</span> sources.nixpkgs {};
<span class="org-nix-keyword">in</span>
<span class="org-comment-delimiter"># </span><span class="org-comment">This is our development shell.</span>
pkgs.mkShell ({
<span class="org-nix-attribute">buildInputs</span> = [
<span class="org-comment"># Tangling and weaving for Literate Programming.</span>
pkgs.emacs29-nox
<span class="org-comment"># For evaluation of Python source code blocks.</span>
pkgs.python3Minimal
<span class="org-comment"># Spell checking.</span>
pkgs.typos
<span class="org-comment"># Update Nix dependencies in package/nix/sources.nix.</span>
pkgs.niv
<span class="org-comment"># Misc.</span>
pkgs.git
pkgs.less
];
})
</pre></div></div><p>
For Emacs, we use the "nox" version to avoid GUI dependencies (because we always
invoke Emacs in batch mode in the terminal without ever using it in an
interactive manner).
</p>
</div>
</div>
<div id="outline-container-h-Makefile" class="outline-2">
<h2 id="h-Makefile"><span class="section-number-2">2.</span> Makefile</h2>
<div class="outline-text-2" id="text-h-Makefile">
<p>
We have a top-level <code>Makefile</code> so that we can run some <code>make</code> commands on the
command line. The overall idea is to tangle and weave, while also running any
associated tests.
</p>
<p>
Note that we make use of the fake file <code>tangle</code>, so that we can write the
top-level <code>test</code> rule as <code>test: tangle</code>, which reads more naturally than the
equivalent <code>test: Makefile</code> or <code>test: lilac.el</code>.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><label class="org-src-name">Makefile</label><span class="lilac-caption-link-symbol"><a href="#Makefile">🔗</a></span></div><pre class="src src-makefile" id="Makefile"><span class="org-makefile-targets">all</span>: test weave
<span class="org-makefile-targets">.PHONY</span>: all
<span class="org-makefile-targets">test</span>: tangle
<span class="org-variable-name">LILAC_ROOT</span>=$(<span class="org-variable-name">LILAC_ROOT</span>) emacs --quick --batch --kill --load ert \
--load lilac.el \
--load lilac-tests.el \
--funcall ert-run-tests-batch-and-exit
<span class="org-makefile-targets">.PHONY</span>: test
<span class="lilac-child-link-from-parent"><a href="#__NREF__Makefile-weave">Makefile-weave</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__Makefile-tangle">Makefile-tangle</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__Makefile-run_emacs">Makefile-run_emacs</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__Makefile-lint">Makefile-lint</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__Makefile-update-deps">Makefile-update-deps</a></span>
</pre></div></div><p>
Weaving just depends on the main <code>README.html</code> and <code>developer-guide</code> files being
generated. Before we call <code>(lilac-publish)</code>, we have to first call
<code>(lilac-gen-css-and-exit)</code> because otherwise the source code blocks do not get
any syntax highlighting.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#Makefile">Makefile-weave</a></span><span class="lilac-caption-link-symbol"><a href="#__NREF__Makefile-weave">🔗</a></span></div><pre class="src src-makefile" id="__NREF__Makefile-weave"><span class="org-makefile-targets">weave</span>: lint README.html developer-guide.html
<span class="org-makefile-targets">.PHONY</span>: weave
<span class="org-makefile-targets">README.html</span>: README.org
$(<span class="org-variable-name">call</span> run_emacs,(lilac-publish),$<span class="org-constant"><</span>)
<span class="org-makefile-targets">developer-guide.html</span>: developer-guide.org
$(<span class="org-variable-name">call</span> run_emacs,(lilac-publish),$<span class="org-constant"><</span>)
<span class="org-makefile-targets">syntax-highlighting.css</span>:
$(<span class="org-variable-name">call</span> run_emacs_nobatch,(lilac-gen-css-and-exit),)
<span class="org-makefile-targets">.PHONY</span>: syntax-highlighting.css
</pre></div></div><p>
Tangling is pretty straightforward — we just need to call
<code>(org-babel-tangle)</code> on <code>developer-guide.org</code> (the <code>README.org</code> does not contain
any code we need to run to make this work). This generates a number of files,
such as the <code>Makefile</code> and <code>shell.nix</code>.
</p>
<p>
The key here is to enumerate these generated files, because we need to tell the
<code>make</code> utility that it should run the rule if <code>developer-guide.org</code> has a newer
modification timestamp than any of the generated files. Technically speaking,
because all of the tangled files are tangled together at once with
<code>(org-babel-tangle)</code>, we could just list one of them such as <code>Makefile</code> (instead
of enumerating all of them). However we still enumerate them all here for
completeness.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#Makefile">Makefile-tangle</a></span><span class="lilac-caption-link-symbol"><a href="#__NREF__Makefile-tangle">🔗</a></span></div><pre class="src src-makefile" id="__NREF__Makefile-tangle"><span class="org-comment-delimiter"># </span><span class="org-comment">tangled_output are all files that are generated by tangling developer-guide.org.</span>
<span class="org-variable-name">tangled_output</span> = \
citations-developer-guide.bib \
lilac.css \
lilac.el \
lilac-tests.el \
lilac.js \
lilac.theme \
.gitattributes \
.gitignore \
Makefile \
shell.nix
<span class="org-makefile-targets">tangle $(</span><span class="org-variable-name"><span class="org-makefile-targets">tangled_output</span></span><span class="org-makefile-targets">) &</span>: developer-guide.org
<span class="org-makefile-space"> #</span><span class="org-comment-delimiter"> </span><span class="org-comment">Generate the toplevel Makefile (this file) and others as described in</span>
<span class="org-makefile-space"> #</span><span class="org-comment-delimiter"> </span><span class="org-comment">tangled_output. In a way this bootstraps the whole literate-programming</span>
<span class="org-makefile-space"> #</span><span class="org-comment-delimiter"> </span><span class="org-comment">pipeline.</span>
$(<span class="org-variable-name">call</span> run_emacs,(org-babel-tangle),developer-guide.org)
touch tangle
</pre></div></div><p>
The <code>run_emacs</code> function is used for both weaving and tangling. The main thing
of interest here is that it loads the <code>lilac.el</code> (tangled) file before
evaluating the given expression.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#Makefile">Makefile-run_emacs</a></span><span class="lilac-caption-link-symbol"><a href="#__NREF__Makefile-run_emacs">🔗</a></span></div><pre class="src src-makefile" id="__NREF__Makefile-run_emacs">define run_emacs
<span class="org-variable-name">LILAC_ROOT</span>=$(<span class="org-variable-name">LILAC_ROOT</span>) emacs $(<span class="org-variable-name">2</span>) --quick --batch --kill \
--load $(<span class="org-variable-name">LILAC_ROOT</span>)/lilac.el --eval=<span class="org-string">"$(</span><span class="org-string"><span class="org-variable-name">1</span></span><span class="org-string">)"</span>
endef
define run_emacs_nobatch
<span class="org-variable-name">LILAC_ROOT</span>=$(<span class="org-variable-name">LILAC_ROOT</span>) emacs $(<span class="org-variable-name">2</span>) --quick --kill \
--load $(<span class="org-variable-name">LILAC_ROOT</span>)/lilac.el --eval=<span class="org-string">"$(</span><span class="org-string"><span class="org-variable-name">1</span></span><span class="org-string">)"</span>
endef
<span class="org-variable-name">LILAC_ROOT</span> := $(<span class="org-variable-name">shell</span> git rev-parse --show-toplevel)
</pre></div></div><p>
We use <a href="https://github.com/nmattia/niv"><code>niv</code></a> to update the dependencies sourced by <a href="#shell-nix"><code>shell.nix</code></a>. Niv uses two
sources of truth: the niv repository itself on GitHub, and a branch of nixpkgs.
The former tracks the <code>master</code> branch, and the latter tracks the stable channels
(example: <code>nixos-23.11</code> branch). Whenever we run <code>niv update</code>, <code>niv</code> will update
the HEAD commit SHA of these branches.
</p>
<p>
One problem with the nixpkgs stable channel is that it will eventually become
obsolete as newer stable channels get created. So we have to manually track
these channels ourselves.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#Makefile">Makefile-update-deps</a></span><span class="lilac-caption-link-symbol"><a href="#__NREF__Makefile-update-deps">🔗</a></span></div><pre class="src src-makefile" id="__NREF__Makefile-update-deps"><span class="org-variable-name">nixpkgs_stable_channel</span> := nixos-23.11
<span class="org-makefile-targets">update-deps</span>: package/nix/sources.json package/nix/sources.nix
cd package && niv update nixpkgs --branch $(<span class="org-variable-name">nixpkgs_stable_channel</span>)
cd package && niv update
touch update-deps
</pre></div></div>
</div>
<div id="outline-container-h-Linting" class="outline-3">
<h3 id="h-Linting"><span class="section-number-3">2.1.</span> Linting</h3>
<div class="outline-text-3" id="text-h-Linting">
</div>
<div id="outline-container-h-Spell-checker" class="outline-4">
<h4 id="h-Spell-checker"><span class="section-number-4">2.1.1.</span> Spell checker</h4>
<div class="outline-text-4" id="text-h-Spell-checker">
<p>
We use <a href="https://crates.io/crates/typos-cli">typos-cli</a> to check for spelling errors. Below we configure it to only
check the original source material — Org files.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><label class="org-src-name">_typos.toml</label><span class="lilac-caption-link-symbol"><a href="#typos-toml">🔗</a></span></div><pre class="src src-toml" id="typos-toml">[<span class="org-type">files</span>]
<span class="org-variable-name">extend-exclude</span> = [
<span class="org-string">"*.html"</span>,
<span class="org-string">"deps/*"</span>,
]
</pre></div></div><p>
Here we have the Makefile rules for linting, which include this spellchecker.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#Makefile">Makefile-lint</a></span><span class="lilac-caption-link-symbol"><a href="#__NREF__Makefile-lint">🔗</a></span></div><pre class="src src-makefile" id="__NREF__Makefile-lint"><span class="org-makefile-targets">lint</span>: spellcheck
<span class="org-makefile-targets">.PHONY</span>: lint
<span class="org-makefile-targets">spellcheck</span>: README.org developer-guide.org
typos
<span class="org-makefile-targets">.PHONY</span>: spellcheck
</pre></div></div>
</div>
</div>
</div>
</div>
<div id="outline-container-h-Tangling--generating-the-source-code" class="outline-2">
<h2 id="h-Tangling--generating-the-source-code"><span class="section-number-2">3.</span> Tangling (generating the source code)</h2>
<div class="outline-text-2" id="text-h-Tangling--generating-the-source-code">
<p>
Tangling is simply the act of collecting the <code>#+begin_src ... #+end_src</code> blocks
and arranging them into the various target (source code) files. Every source
code block is given a unique name.
</p>
<p>
We simply tangle the <code>developer-guide.org</code> file to get all the code we need.
</p>
<p>
The only thing that we customize in terms of tangling is how we define Noweb
references inside source code blocks.
</p>
</div>
<div id="outline-container-h-Custom-Noweb-delimiters" class="outline-3">
<h3 id="h-Custom-Noweb-delimiters"><span class="section-number-3">3.1.</span> Custom Noweb delimiters</h3>
<div class="outline-text-3" id="text-h-Custom-Noweb-delimiters">
<p>
By default, Org mode requires Noweb source code references to be delimited with
double angled brackets (<code><<</code> and <code>>></code>). This presents two problems:
</p>
<ol class="org-ol">
<li>The biggest problem with this is that it interferes with syntax highlighting,
especially because different programming languages treat these brackets in a
different manner (it may be a particular type of token in one language, but a
different one in another).</li>
<li>It's also slightly annoying because when searching inside an editor such as
Vim (or even Emacs with the Org mode on), the angle brackets are not
considered part of the word being searched. This means that if the source
code block shares the same name as another (unrelated) string, the search
results for both are combined into one.</li>
</ol>
<p>
To avoid these problems, we simply make Noweb references use a <code>__NREF__</code>
prefix. We also define a suffix group such that the reference can take an
argument to the code block itself. See the example <a href="README.org#h-Monoblock--pass-argument-to-source-code-block">here</a>.
</p>
<p>
Note that because this is a customization, we cannot run <code>C-c C-v t</code> to tangle
code blocks properly in an interactive manner from an Emacs editing session of
this Org file, without first evaluating the bits of code in the following code
block.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#lilac-el">lilac_dot_el-custom-noweb-delimiters</a></span><span class="lilac-caption-link-symbol"><a href="#__NREF__lilac_dot_el-custom-noweb-delimiters">🔗</a></span></div><pre class="src src-emacs-lisp" id="__NREF__lilac_dot_el-custom-noweb-delimiters">(<span class="org-keyword">setq</span> org-babel-noweb-wrap-start <span class="org-string">"__NREF__"</span>)
(<span class="org-keyword">setq</span> org-babel-noweb-wrap-end <span class="org-string">""</span>)
(<span class="org-keyword">defun</span> <span class="org-function-name">lilac-nref-rx</span> (match-optional-params)
(rx-to-string
(lilac-nref-rx-primitive match-optional-params)))
(<span class="org-keyword">defun</span> <span class="org-function-name">lilac-nref-rx-primitive</span> (match-optional-params)
(<span class="org-keyword">if</span> match-optional-params
`(group
<span class="org-string">"__NREF__"</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">Noweb reference must start with a letter...</span>
(any alpha)
<span class="org-comment-delimiter">;; </span><span class="org-comment">...and must be followed by</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">letters,numbers,dashes,underscores,periods...</span>
(* (<span class="org-keyword">or</span> (any alnum) <span class="org-string">"-"</span> <span class="org-string">"_"</span> <span class="org-string">"."</span>))
<span class="org-comment-delimiter">;; </span><span class="org-comment">...and may terminate with a "(...)" where the "..." may be an empty</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">string, or some other argument.</span>
(* (<span class="org-keyword">or</span> <span class="org-string">"()"</span>
(<span class="org-keyword">and</span> <span class="org-string">"("</span>
(* (not <span class="org-string">")"</span>))
<span class="org-string">")"</span>))))
`(group
<span class="org-string">"__NREF__"</span>
(any alpha)
(* (<span class="org-keyword">or</span> (any alnum) <span class="org-string">"-"</span> <span class="org-string">"_"</span> <span class="org-string">"."</span>)))))
<span class="org-comment-delimiter">;; </span><span class="org-comment">Customize noweb delimiters. Unlike traditional << and >> delimiters, we just</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">use the "__NREF__" prefix as our only delimiter. This has the advantage of</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">being encoded the same way into HTML, which makes our HTML modifications</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">easier and more consistent across different source code languages.</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">See https://emacs.stackexchange.com/a/73720/13006.</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">org-babel-noweb-wrap</span> (<span class="org-type">&optional</span> regexp)
<span class="org-doc">"Return regexp matching a Noweb reference.</span>
<span class="org-doc">Match any reference, or only those matching REGEXP, if non-nil.</span>
<span class="org-doc">When matching, reference is stored in match group 1."</span>
(lilac-nref-rx t))
</pre></div></div>
</div>
</div>
<div id="outline-container-h-Allow-evaluation-of-code-blocks" class="outline-3">
<h3 id="h-Allow-evaluation-of-code-blocks"><span class="section-number-3">3.2.</span> Allow evaluation of code blocks</h3>
<div class="outline-text-3" id="text-h-Allow-evaluation-of-code-blocks">
<p>
Some code blocks are generated by evaluating code in other blocks. This way, you
can use all the power of Org mode (as well as any supported programming
language) as a kind of meta-programming system.
</p>
<p>
Orgmode by default disables automatic evaluation of source code blocks, because
it is a big security risk. For us, we know that we want to allow code
evaluation, so we disable the evaluation confirmation check. This way,
evaluation can still work even in batch mode.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#lilac-el">lilac_dot_el-allow-code-evaluation</a></span>(1/2) <span class="lilac-caption-link-symbol"><a href="#__NREF__lilac_dot_el-allow-code-evaluation-1">🔗</a></span></div><pre class="src src-emacs-lisp" id="__NREF__lilac_dot_el-allow-code-evaluation-1">(<span class="org-keyword">setq</span> org-confirm-babel-evaluate nil)
</pre></div></div><p>
The following is needed to evaluate the example for source code block evaluation
in <a href="README.html#h-Monoblock--evaluation-result-s-value">Monoblock (evaluation result's value)</a>, which evaluates Python. We don't
need to add in Emacs lisp here because it's already supported by default (which
we take advantage of in <a href="#h-Create--lilac-theme--file">4.11</a>).
</p>
<p>
In addition to Python, we add in some additional languages that might be of use.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#lilac-el">lilac_dot_el-allow-code-evaluation</a></span>(2/2) <span class="lilac-caption-link-symbol"><a href="#__NREF__lilac_dot_el-allow-code-evaluation-2">🔗</a></span></div><pre class="src src-emacs-lisp" id="__NREF__lilac_dot_el-allow-code-evaluation-2">(org-babel-do-load-languages 'org-babel-load-languages
(append org-babel-load-languages
'((awk . t)
(C . t)
(calc . t)
(clojure . t)
(java . t)
(js . t)
(latex . t)
(lisp . t)
(lua . t)
(perl . t)
(python . t)
(R . t)
(ruby . t)
(scheme . t)
(sed . t)
(shell . t))))
</pre></div></div>
</div>
</div>
</div>
<div id="outline-container-h-Weaving--generating-the-HTML" class="outline-2">
<h2 id="h-Weaving--generating-the-HTML"><span class="section-number-2">4.</span> Weaving (generating the HTML)</h2>
<div class="outline-text-2" id="text-h-Weaving--generating-the-HTML">
<p>
Weaving is conceptually simpler than tangling because there is no extra step —
the output is an HTML page and that is something that we can use directly
(unlike program source code, which may require additional compilation into a
binary, depending on the language). We limit ourselves to HTML output because
HTML support is ubiquitous; plus we don't have to worry about page breaks such
as in PDF output.
</p>
<p>
Although weaving is conceptually simple, most of the code in <a href="lilac.el"><code>lilac.el</code></a> have to
do with weaving because the default infrastructure that ships with Org mode is
too rigid for our needs. For example, we make heavy use of Noweb-style
<a href="#citeproc_bib_item_1">[1]</a> references, but also add in extensive HTML links to allow
the reader to jump around code easily because Org does not cross-link these
references by default.
</p>
<p>
Weaving currently requires the following dependencies:
</p>
<table>
<colgroup>
<col class="org-left">
<col class="org-left">
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Dependency</th>
<th scope="col" class="org-left">Why</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><a href="https://www.gnu.org/software/make/">GNU Make</a></td>
<td class="org-left">to run "make"</td>
</tr>
<tr>
<td class="org-left"><a href="https://www.gnu.org/software/emacs/">GNU Emacs</a></td>
<td class="org-left">for tangling and weaving</td>
</tr>
</tbody>
</table>
<p>
Note that all of the above can be brought in by using the <a href="https://github.com/NixOS/nix">Nix package manager</a>.
This is why we provide a <a href="#shell-nix"><code>shell.nix</code></a> <a href="shell.nix">file</a> in this repo.
</p>
</div>
<div id="outline-container-h-Emacs-customizations---lilac-el-" class="outline-3">
<h3 id="h-Emacs-customizations---lilac-el-"><span class="section-number-3">4.1.</span> Emacs customizations (<code>lilac.el</code>)</h3>
<div class="outline-text-3" id="text-h-Emacs-customizations---lilac-el-">
<p>
Below is the overall structure of <a href="lilac.el"><code>lilac.el</code></a>. The <code>gc-cons-threshold</code>
setting is to prevent <code>emacs</code> from entering garbage collection pauses, because
we invoke <code>emacs</code> from the command line in a non-interactive manner.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><label class="org-src-name">lilac.el</label><span class="lilac-caption-link-symbol"><a href="#lilac-el">🔗</a></span></div><pre class="src src-emacs-lisp" id="lilac-el"><span class="org-comment-delimiter">; </span><span class="org-comment">Set garbage-collection threshold to 16 GiB.</span>
(<span class="org-keyword">setq</span> gc-cons-threshold #x400000000)
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-imports">lilac_dot_el-imports</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-fix-nondeterminism-1">lilac_dot_el-fix-nondeterminism</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-custom-noweb-delimiters">lilac_dot_el-custom-noweb-delimiters</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-allow-code-evaluation-1">lilac_dot_el-allow-code-evaluation</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-lilac-publish-1">lilac_dot_el-lilac-publish</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-lilac-publish-helpers-1">lilac_dot_el-lilac-publish-helpers</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-search-replace-1">lilac_dot_el-search-replace</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-autogenerate-css">lilac_dot_el-autogenerate-css</a></span>
<span class="lilac-child-link-from-parent"><a href="#__NREF__lilac_dot_el-misc-1">lilac_dot_el-misc</a></span>
(<span class="org-keyword">provide</span> '<span class="org-constant">lilac</span>)
</pre></div></div>
</div>
</div>
<div id="outline-container-h-Fix-non-determinism" class="outline-3">
<h3 id="h-Fix-non-determinism"><span class="section-number-3">4.2.</span> Fix non-determinism</h3>
<div class="outline-text-3" id="text-h-Fix-non-determinism">
<p>
Nondeterminism is problematic because it results in a different HTML file
every time we run <code>org-babel-tangle</code>, <i>even if the Org files have not changed</i>.
Here we take care to set things right so that we can have reprducible, stable
HTML output.
</p>
</div>
<div id="outline-container-h-Do-not-insert-current-time-as-HTML-comment" class="outline-4">
<h4 id="h-Do-not-insert-current-time-as-HTML-comment"><span class="section-number-4">4.2.1.</span> Do not insert current time as HTML comment</h4>
<div class="outline-text-4" id="text-h-Do-not-insert-current-time-as-HTML-comment">
<p>
Org mode also injects an HTML comment (not visible to the user) to record the
time that the HTML was generated. We disable this because it breaks
deterministic output. See <a href="https://emacs.stackexchange.com/questions/50117/how-to-disable-commented-date-in-org-mode-html-export">this link</a> for more info.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#lilac-el">lilac_dot_el-fix-nondeterminism</a></span>(1/3) <span class="lilac-caption-link-symbol"><a href="#__NREF__lilac_dot_el-fix-nondeterminism-1">🔗</a></span></div><pre class="src src-emacs-lisp" id="__NREF__lilac_dot_el-fix-nondeterminism-1">(<span class="org-keyword">setq</span> org-export-time-stamp-file nil)
</pre></div></div>
</div>
</div>
<div id="outline-container-h-Do-not-insert-current-Org-mode-version" class="outline-4">
<h4 id="h-Do-not-insert-current-Org-mode-version"><span class="section-number-4">4.2.2.</span> Do not insert current Org mode version</h4>
<div class="outline-text-4" id="text-h-Do-not-insert-current-Org-mode-version">
<p>
By default Org mode appends visible metadata at the bottom of the HTML document,
including the Org version used to generate the document. We suppress this
information.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#lilac-el">lilac_dot_el-fix-nondeterminism</a></span>(2/3) <span class="lilac-caption-link-symbol"><a href="#__NREF__lilac_dot_el-fix-nondeterminism-2">🔗</a></span></div><pre class="src src-emacs-lisp" id="__NREF__lilac_dot_el-fix-nondeterminism-2">(<span class="org-keyword">setq</span> org-html-postamble nil)
</pre></div></div>
</div>
</div>
<div id="outline-container-h-Do-not-use-random-numbers-for-the-HTML--id--attribute" class="outline-4">
<h4 id="h-Do-not-use-random-numbers-for-the-HTML--id--attribute"><span class="section-number-4">4.2.3.</span> Do not use random numbers for the HTML <code>id</code> attribute</h4>
<div class="outline-text-4" id="text-h-Do-not-use-random-numbers-for-the-HTML--id--attribute">
<p>
Stop randomized IDs from being generated every time. Instead count from 0 and
work our way up.
</p>
<p>
See <a href="https://www.reddit.com/r/orgmode/comments/aagmfh/comment/hk6upbf">https://www.reddit.com/r/orgmode/comments/aagmfh/comment/hk6upbf</a>.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#lilac-el">lilac_dot_el-fix-nondeterminism</a></span>(3/3) <span class="lilac-caption-link-symbol"><a href="#__NREF__lilac_dot_el-fix-nondeterminism-3">🔗</a></span></div><pre class="src src-emacs-lisp" id="__NREF__lilac_dot_el-fix-nondeterminism-3">(<span class="org-keyword">defun</span> <span class="org-function-name">org-export-deterministic-reference</span> (references)
(<span class="org-keyword">let</span> ((new (length references)))
(<span class="org-keyword">while</span> (rassq new references) (<span class="org-keyword">setq</span> new (1+ new)))
new))
(advice-add #'org-export-new-reference
<span class="org-builtin">:override</span> #'org-export-deterministic-reference)
</pre></div></div>
</div>
</div>
</div>
<div id="outline-container-h-Toplevel-publishing-function---lilac-publish-" class="outline-3">
<h3 id="h-Toplevel-publishing-function---lilac-publish-"><span class="section-number-3">4.3.</span> Toplevel publishing function (<code>lilac-publish</code>)</h3>
<div class="outline-text-3" id="text-h-Toplevel-publishing-function---lilac-publish-">
<p>
The toplevel function is <code>lilac-publish</code>. This actually publishes to HTML twice,
with two separate calls to <code>org-html-export-to-html</code>. The reason we publish
twice is because we need to examine the HTML output twice in order to build up a
database of parent/child source code block links (which is then used to link
between these parent/child source code blocks).
</p>
<p>
Also note that we do some modifications to the Org buffer directly before
exporting to HTML. The main reason is so that the source code blocks that are
named <code>__NREF__...</code> get an automatic <code>#+caption: ...</code> text to go along with it
(because for these Noweb-style blocks, the captions should always look uniform).
</p>
<p>
The full code listing for <code>lilac-publish</code> is below.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><label class="org-src-name">lilac-publish</label><span class="lilac-caption-link-symbol"><a href="#lilac-publish">🔗</a></span></div><pre class="src src-emacs-lisp" id="lilac-publish">(<span class="org-keyword">defun</span> <span class="org-function-name">lilac-publish</span> ()
(<span class="org-keyword">interactive</span>)
(<span class="org-keyword">setq</span> org-html-htmlize-output-type 'css)
(<span class="org-keyword">setq</span> org-export-before-parsing-hook
'(lilac-UID-for-all-src-blocks
lilac-insert-noweb-source-code-block-captions
lilac-UID-for-all-headlines))
(<span class="org-keyword">setq</span> org-export-filter-src-block-functions
'(lilac-populate-child-HTML_ID-hash-table
lilac-populate-org_id-human_id-hash-table))
(org-html-export-to-html)
(clrhash lilac-polyblock-names-totals)
(<span class="org-keyword">setq</span> org-export-filter-src-block-functions
'(lilac-link-to-children-from-parent-body
lilac-prettify-source-code-captions))
(<span class="org-keyword">setq</span> org-export-filter-final-output-functions
'(lilac-replace-org_ids-with-human_ids))
(org-html-export-to-html)
(clrhash lilac-polyblock-names)
(clrhash lilac-polyblock-names-totals)
(clrhash lilac-org_id-human_id-hash-table)
(clrhash lilac-human_id-count-hash-table)
(clrhash lilac-human_id-org_id-hash-table)
(lilac-replace-from-to-html
<span class="org-string">"<h2>Table of Contents</h2>"</span>
<span class="org-string">""</span>)
(lilac-replace-from-to-html
<span class="org-string">"mathjax@3/es5/tex-mml-chtml.js\">"</span>
<span class="org-string">"mathjax@4.0.0-beta.4/tex-mml-chtml.js\">"</span>)
(lilac-replace-from-to-html
<span class="org-string">".csl-right-inline{margin: 0 0 0 1em;}<"</span>
<span class="org-string">".csl-right-inline{margin: 0 0 0 2em;}<"</span>)
(lilac-replace-from-to-html
<span class="org-string">"\"csl-entry\"><a </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">id=\"[</span><span class="org-string"><span class="org-negation-char">^</span></span><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>"</span>
<span class="org-string">"\"csl-entry\" \\1>"</span>
t)
(lilac-replace-from-to-html
<span class="org-string">"<dt>"</span>
<span class="org-string">"<div class=\"lilac-description-list-entry\"><dt>"</span>
t)
(lilac-replace-from-to-html
<span class="org-string">"\"lilac-description-list-entry\"><dt><a </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">id=\"[</span><span class="org-string"><span class="org-negation-char">^</span></span><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>"</span>
<span class="org-string">"\"lilac-description-list-entry\" \\1><dt>"</span>
t)
(lilac-replace-from-to-html
<span class="org-string">"</dd>"</span>
<span class="org-string">"</dd></div>"</span>)
(<span class="org-keyword">if</span> (boundp 'lilac-html-head)
(lilac-replace-from-to-html
<span class="org-string">"<!-- LILAC_HTML_HEAD -->"</span>
lilac-html-head)))
</pre></div></div><p>
Now let's go through <code>lilac-publish</code> in detail.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#lilac-el">lilac_dot_el-lilac-publish</a></span>(1/7) <span class="lilac-caption-link-symbol"><a href="#__NREF__lilac_dot_el-lilac-publish-1">🔗</a></span></div><pre class="src src-emacs-lisp" id="__NREF__lilac_dot_el-lilac-publish-1">(<span class="org-keyword">defun</span> <span class="org-function-name">lilac-publish</span> ()
(<span class="org-keyword">interactive</span>)
(<span class="org-keyword">setq</span> org-html-htmlize-output-type 'css)
</pre></div></div><p>
Do not hardcode colors into the HTML. Instead refer to CSS class names, to be
stylized by an external CSS file.
</p>
<div class="org-src-container"><div class="lilac-pre-with-caption"><div class="lilac-caption"><span class="lilac-caption-parent-link"><a href="#lilac-el">lilac_dot_el-lilac-publish</a></span>(2/7) <span class="lilac-caption-link-symbol"><a href="#__NREF__lilac_dot_el-lilac-publish-2">🔗</a></span></div><pre class="src src-emacs-lisp" id="__NREF__lilac_dot_el-lilac-publish-2"> (<span class="org-keyword">setq</span> org-export-before-parsing-hook
'(lilac-UID-for-all-src-blocks
lilac-insert-noweb-source-code-block-captions
lilac-UID-for-all-headlines))
</pre></div></div><p>
Here we modify the Org mode buffer, by using <code>org-export-before-parsing-hook</code>.
This takes a list of functions that are free to modify the Org mode buffer
before each Org element in the buffer gets converted into HTML.
</p>
<p>
As for the actual modifications, see:
</p>
<ul class="org-ul">
<li><code>lilac-UID-for-all-src-blocks</code> (<a href="#h-Give-all-source-code-blocks-a----name-------field--HTML-ID">4.4.1</a>)</li>
<li><code>lilac-insert-noweb-source-code-block-captions</code> (<a href="#h-Automatic-captions-for-Noweb-source-code-blocks">4.4.2</a>)</li>
<li><code>lilac-UID-for-all-headlines</code> (<a href="#h-Human-readable-UIDs--Headings--aka-headlines">4.4.3</a>)</li>
</ul>
<p>
In brief, the <code>lilac-UID-for-all-*</code> functions make it so that the links to
headlines and source code blocks are both deterministic and human-readable. The
<code>lilac-insert-noweb-source-code-block-captions</code> function
</p>
<p>
Now we start modifying the HTML.
</p>
<p>
This is useful for adding in final tweaks to the HTML that is difficult to