forked from TuringLang/turinglang.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-utils.jl
More file actions
335 lines (290 loc) · 9.32 KB
/
Copy pathmake-utils.jl
File metadata and controls
335 lines (290 loc) · 9.32 KB
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
using Base64, Pkg
Pkg.develop(url="https://github.com/TuringLang/TuringTutorials")
using TuringTutorials
## Text Utilities
function find_yaml_header(lines)
yaml = []
yaml_start, yaml_stop = 1, length(lines)
# read out YAML block.
if strip(lines[1]) == "---"
for i = 2:length(lines)
if strip(lines[i]) == "---"
yaml = lines[1:i]
yaml_stop = i
break
end
end
end
return yaml, yaml_start, yaml_stop
end
function remove_yaml(file, key=nothing)
lines = readlines(file, keep=true)
yaml, yaml_start, yaml_stop = find_yaml_header(lines)
if !isempty(yaml)
open(file, "w+") do f
# write back YAML lines.
if key !== nothing
for line in yaml
startswith(line, key * ":") || write(f, line)
end
end
# write back non-YAML lines
for line in lines[yaml_stop + 1 : end]
write(f, line)
end
end
end
return yaml
end
function tidy_api(file)
lines = readlines(file, keep=true)
# Find the ID sections.
for i = 1:(length(lines)-1)
first = lines[i]
second = lines[i+1]
if startswith(first, "<a id=") && startswith(second, "**")
first = replace(first, "\n" => "")
second = replace(second, "\n" => "")
# final_line = replace(first, ">" => ">$second", count = 1)
lines[i] = "### $first $second"
lines[i+1] = ""
end
end
# Write lines back.
open(file, "w+") do f
for line in lines
write(f, line)
end
end
end
## Utilities for Tutorial docs preprocess
"""
fix_header_1
This function is used to add a first-level header into the
markdown file.
There are no H1 titles for documents in tutorials, so we need
this.
"""
function fix_header_1(file)
lines = readlines(file, keep=true)
yaml, yaml_start, yaml_stop = find_yaml_header(lines)
isempty(yaml) && return
for line in lines[yaml_stop + 1 : end]
strip(line) == "" && continue
if startswith(line, "# ") # has a h1 title
return
else # no h1 title
break
end
end
title = "Untitled"
for line in yaml
if startswith(line, "title:")
title = line[7:end] |> strip
break
end
end
title = strip(title, ['"'])
open(file, "w+") do f
for line in yaml
write(f, line)
end
write(f, "\n# $title\n")
# write back non-YAML lines
for line in lines[yaml_stop + 1 : end]
write(f, line)
end
end
end
function fix_image_path(file)
# 
# to
# 
lines = readlines(file, keep=true)
open(file, "w+") do f
for line in lines
line = replace(line, "](figures/" => "](../figures/")
write(f, line)
end
end
end
## Building Utilities
function preprocess_markdown(folder)
@debug "preprocess_markdown" folder
yaml_dict = Dict()
try
for (root, dirs, files) in walkdir(folder)
for file in files
if endswith(file, ".md")
full_path = joinpath(root, file)
yaml_dict[full_path] = remove_yaml(full_path)
end
end
end
catch e
# println("Markdown copy error: $e")
rethrow(e)
end
return yaml_dict
end
function postprocess_markdown(folder, yaml_dict; original = "")
try
for (root, dirs, files) in walkdir(folder)
for file in files
full_path = joinpath(root, file)
original_path = full_path
if length(original) > 0
# original_path = abspath(original, file)
original_path = replace(full_path, folder => original)
# original_path = replace(
# full_path,
# joinpath("docs", "site") => joinpath("docs", "src")
# )
end
if haskey(yaml_dict, original_path)
# println("Original: $original_path => Full path: $full_path")
txt = open(f -> read(f, String), full_path)
open(full_path, "w+") do f
# Add in the yaml block.
for line in yaml_dict[original_path]
write(f, line)
end
# This is needed to replace the hyperlinks Documenter.jl generates
# for the API pages.
txt = replace(txt, "api.md" => "{{site.baseurl}}/docs/library/")
txt = replace(txt, "bijectors.md" => "{{site.baseurl}}/docs/library/bijectors/")
txt = replace(txt, "advancedhmc.md" => "{{site.baseurl}}/docs/library/advancedhmc/")
# Add the rest of the text.
if original_path == full_path
write(f, txt)
else
write(f, replace(txt, ")
end
end
elseif endswith(file, ".md")
println("Original: $original_path")
println("Full: $full_path \n")
end
# Make specific api items headers.
if file == "api.md" && original_path != full_path
tidy_api(full_path)
end
end
end
catch e
# println("Markdown copy error: $e")
rethrow(e)
end
return yaml_dict
end
function cp_by_write(src, dest; force::Bool=false)
if isdir(src)
for (root, dirs, files) in walkdir(src)
new_root = replace(root, src => dest)
mkpath(new_root)
for file in files
src_file = joinpath(root, file)
dest_file = joinpath(new_root, file)
if !ispath(dest_file) || force
@debug "Making $dest_file from $src_file"
open(src_file, "r") do f_in
open(dest_file, "w+") do f_out
write(f_out, read(f_in))
end
end
end
end
end
else
# Assume it's a file
if !ispath(dest) || force
@debug "Making $dest from $src"
open(src, "r") do f_in
open(dest, "w+") do f_out
write(f_out, read(f_in))
end
end
end
end
return dest
end
function with_clean_docs(func, source, target)
src_temp = mktempdir()
cp_by_write(source, src_temp, force=true)
yaml_dict = preprocess_markdown(src_temp)
try
func(src_temp, target)
yaml_dict = postprocess_markdown(target, yaml_dict, original=src_temp)
catch e
rethrow(e)
finally
# Put back the original files in the event of an error.
# cp(src_temp, source_path, force=true)
rm(src_temp, recursive=true)
end
return yaml_dict
end
function copy_tutorial(tutorial_dest_path)
@info "Tutorial destination path $tutorial_dest_path"
ispath(tutorial_dest_path) || mkpath(tutorial_dest_path)
# Make a workspace
tmp_path = tempname()
mkdir(tmp_path)
# Get the path of `TuringTutorials`
md_path = joinpath(dirname(pathof(TuringTutorials)), "..", "markdown")
# Copy the .md versions of all examples.
try
@debug(md_path)
for tutorial_src in readdir(md_path)
# Construct the full path
tutorial_src_path = joinpath(md_path, tutorial_src)
for (dir, subdirs, files) in walkdir(tutorial_src_path)
# Change base directory of source path
dir_new = replace(dir, tutorial_src_path => tmp_path)
# Make the path if it does not exist
mkpath(dir_new)
# Copy over the files ensuring that we have write access
println("Copying $dir to $dir_new")
cp_by_write(dir, dir_new, force=true)
for file in files
dest_path = joinpath(dir_new, file)
if endswith(dest_path, ".md")
# remove_yaml(target_path, "permalink")
fix_header_1(dest_path)
print("fixing image path")
fix_image_path(dest_path)
end
end
end
end
# Copy from temporary workspace to destination
cp(tmp_path, tutorial_dest_path, force=true)
index = joinpath(@__DIR__, "_tutorials/index.md")
cp(index, tutorial_dest_path * "/index.md", force=true)
catch e
rethrow(e)
finally
# Clean up temporary workspace
rm(tmp_path, recursive=true)
end
end
function with_baseurl(func, baseurl, config_path)
jekyll_config = config_path
lines = readlines(jekyll_config, keep=true)
open(jekyll_config, "w+") do f
for line in lines
if startswith(line, "baseurl:")
write(f, "baseurl: \"$(baseurl)\"\n")
continue
end
write(f, line)
end
end
ret = func()
open(jekyll_config, "w+") do f
for line in lines
write(f, line)
end
end
return ret
end