~charles/cdaniels.net

cdaniels.net/build.tcl -rwxr-xr-x 5.0 KiB
dfa22a4eCharles Daniels also match pre 2 months ago
                                                                                
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
#!/usr/bin/env tclsh

package require fileutil

# This is the location where build.tcl lives
variable toplevel [file normalize [file join [info script] ".."]]

# This rewritespec file will be used with gemdown -p.
set rewritespec [file normalize [file join $toplevel "spec.json"]]

# source directory for posts/content
set srcdir [file normalize [file join $toplevel "src"]]

# output directory where generated site should be placed
set outdir [file normalize [file join $toplevel "output"]]

# directories where HTML and GMI versions of the site should go
set htmldir [file normalize [file join $outdir "html"]]
set gmidir [file normalize [file join $outdir "gmi"]]

# path to the tex2img script
set tex2img [file normalize [file join $toplevel "tex2img"]]

puts "running relative to . . . '$toplevel'"
puts "source directory  . . . . '$srcdir'"
puts "output directory  . . . . '$outdir'"
puts "html directory  . . . . . '$htmldir'"
puts "gmi directory . . . . . . '$gmidir'"

# Make output directories and ensure they are empty.
puts -nonewline "creating output folders... "
file delete -force $outdir
file mkdir $htmldir
file mkdir $gmidir
puts "OK"

# Make sure we have gemdown available.
if {[auto_execok gemdown] eq ""} {
	puts "no 'gemdown' in PATH"
	exit 1
}

# We need these for tex2img
if {[auto_execok latexmk] eq ""} {
	puts "no 'latexmk' in PATH"
	exit 1
}
if {[auto_execok pdfcrop] eq ""} {
	puts "no 'pdfcrop' in PATH"
	exit 1
}
if {[auto_execok pdftocairo] eq ""} {
	puts "no 'pdftocairo' in PATH"
	exit 1
}

# Main loop to process all files in the input.
foreach fp [fileutil::findByPattern $srcdir *] {
	puts -nonewline "processing file '$fp'... "
	flush stdout

	# destparent will end up being a relative path fragment that
	# goes in: ${html,gmi}dir/$destparent/$filename.
	#
	# Note the -2 ensure that we don't include the file name, we just
	# want the directory part.
	set destparent ""
	for {set i [llength [file split $srcdir]]} {$i <= [expr [llength [file split $fp]] - 2]} {incr i} {
		set destparent [file join $destparent [lindex [file split $fp] $i]]
	}

	# Ensure $destparent exists in both of the output trees.
	file mkdir [file join $htmldir $destparent]
	file mkdir [file join $gmidir $destparent]

	if [string match *.gmi $fp ] {
		# Copy Gemtext files straight through to the gmi output, but
		# convert them to HTML for the HTML output (duh).
		file copy $fp [file join $gmidir $destparent [file tail $fp]]

		set htmlfile [file join $htmldir $destparent [file rootname [file tail $fp]].html]
		exec -- gemdown --rewrite-spec=$rewritespec html -o $htmlfile --standalone --footer-inject footer.html $fp
	} elseif [string match *.eqn $fp ] {
		set svgfn [file rootname [file tail $fp]].svg
		set jpgfn [file rootname [file tail $fp]].jpg
		exec -ignorestderr $tex2img svg < $fp > [file join $gmidir $destparent $svgfn] 2> /dev/null
		exec -ignorestderr $tex2img jpeg < $fp > [file join $gmidir $destparent $jpgfn] 2> /dev/null
		exec -ignorestderr $tex2img svg < $fp > [file join $htmldir $destparent $svgfn] 2> /dev/null
		exec -ignorestderr $tex2img jpeg < $fp > [file join $htmldir $destparent $jpgfn] 2> /dev/null

	} elseif {![file isdirectory $fp]} {
		# Copy unknown files without doing any special conversion.
		file copy $fp [file join $gmidir $destparent [file tail $fp]]
		file copy $fp [file join $htmldir $destparent [file tail $fp]]
	}

	puts "OK"
}

# Finally, generate the feeds.
puts -nonewline "generating feeds... "
exec -- gemdown --rewrite-spec=$rewritespec feed -t "The Blog of Charles Daniels" -T rss -o [file join $htmldir "feed_http.rss"] --link "http://cdaniels.net" --author-name "Charles Daniels" [file join $srcdir index.gmi]
exec -- gemdown --rewrite-spec=$rewritespec feed -t "The Blog of Charles Daniels" -T atom -o [file join $htmldir "feed_http.atom"] --link "http://cdaniels.net" --author-name "Charles Daniels" [file join $srcdir index.gmi]
exec -- gemdown --rewrite-spec=$rewritespec feed -t "The Blog of Charles Daniels" -T rss -o [file join $gmidir "feed_http.rss"] --link "http://cdaniels.net" --author-name "Charles Daniels" [file join $srcdir index.gmi]
exec -- gemdown --rewrite-spec=$rewritespec feed -t "The Blog of Charles Daniels" -T atom -o [file join $gmidir "feed_http.atom"] --link "http://cdaniels.net" --author-name "Charles Daniels" [file join $srcdir index.gmi]
exec -- gemdown feed -t "The Blog of Charles Daniels" -T rss -o [file join $gmidir "feed_gmi.rss"] --link "gemini://cdaniels.net" --author-name "Charles Daniels" [file join $srcdir index.gmi]
exec -- gemdown feed -t "The Blog of Charles Daniels" -T atom -o [file join $gmidir "feed_gmi.atom"] --link "gemini://cdaniels.net" --author-name "Charles Daniels" [file join $srcdir index.gmi]
exec -- gemdown feed -t "The Blog of Charles Daniels" -T rss -o [file join $htmldir "feed_gmi.rss"] --link "gemini://cdaniels.net" --author-name "Charles Daniels" [file join $srcdir index.gmi]
exec -- gemdown feed -t "The Blog of Charles Daniels" -T atom -o [file join $htmldir "feed_gmi.atom"] --link "gemini://cdaniels.net" --author-name "Charles Daniels" [file join $srcdir index.gmi]
puts "OK"