-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmakedoc.jl
312 lines (266 loc) · 10.7 KB
/
makedoc.jl
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
# To build the documentation:
# - julia --project="." make.jl
# - empty!(ARGS); include("make.jl")
# To build the documentation without running the tests:
# - julia --project="." make.jl preview
# - push!(ARGS,"preview"); include("make.jl")
# !!! note "An optional title"
# 4 spaces idented
# note, tip, warning, danger, compat
# Format notes:
# # A markdown H1 title
# A non-code markdown normal line
## A comment within the code chunk
#src: line exclusive to the source code and thus filtered out unconditionally
using Pkg
cd(@__DIR__)
Pkg.activate(".")
#Pkg.resolve()
Pkg.instantiate()
#Pkg.add(["Documenter", "Literate", "Glob", "DataFrames", "OdsIO"])
using Test, DataStructures, Documenter, Literate, Glob, DataFrames, OdsIO
#using DocumenterMarkdown # non supported, it block Documenter to older versions
const LESSONS_ROOTDIR = joinpath(@__DIR__, "lessonsSources")
# Important: If some lesson is removed but the md file is left, this may still be used by Documenter
const LESSONS_ROOTDIR_TMP = joinpath(@__DIR__, "lessonsSources_tmp")
# Where to save the lessons before they are preprocessed
MAKE_PDF = false
LESSONS_SUBDIR = OrderedDict(
"INTRO - Introduction to the course, Julia and ML" => "00_-_INTRO_-_Introduction_julia_ml",
"JULIA1 - Basic Julia programming" => "01_-_JULIA1_-_Basic_Julia_programming",
"JULIA2 - Scientific programming with Julia" => "02_-_JULIA2_-_Scientific_programming_with_Julia",
"ML1 - Introduction to Machine Learning" => "03_-_ML1_-_Introduction_to_Machine_Learning",
"NN - Neural Networks" => "04_-_NN_-_Neural_Networks",
"DT - Decision trees based algorithms [DRAFT]" => "05_-_DT_-_Decision_trees_based_algorithms"
)
# Utility functions.....
function link_example(content)
edit_url = match(r"EditURL = \"(.+?)\"", content)[1]
footer = match(r"^(---\n\n\*This page was generated using)"m, content)[1]
content = replace(
content, footer => "[View this file on Github]($(edit_url)).\n\n" * footer
)
return content
end
"""
include_sandbox(filename)
Include the `filename` in a temporary module that acts as a sandbox. (Ensuring
no constants or functions leak into other files.)
"""
function include_sandbox(filename)
mod = @eval module $(gensym()) end
return Base.include(mod, filename)
end
function makeList(rootDir,subDirList)
outArray = []
for l in sort(collect(subDirList), by=x->x[2])
#println(l)
lessonName = l[1]
lessonDir = l[2]
lessonName = replace(lessonName,"_"=>" ")
dirArray =[]
for file in filter(file -> endswith(file, ".md"), sort(readdir(joinpath(rootDir,lessonDir))))
displayFilename = replace(file,".md"=>"")
displayFilename = replace(displayFilename,"_"=>" ")
push!(dirArray,displayFilename=>joinpath(lessonDir,file))
end
push!(outArray,lessonName=>dirArray)
end
return outArray
end
"""
rdir(string,match)
Return a vector of all files (full paths) of a given directory recursivelly taht matches `match`, recursivelly.
# example
filenames = getindex.(splitdir.(rdir(LESSONS_ROOTDIR,"*.jl")),2) #get all md filenames
"""
function rdir(dir::AbstractString, pat::Glob.FilenameMatch)
result = String[]
for (root, dirs, files) in walkdir(dir)
append!(result, filter!(f -> occursin(pat, f), joinpath.(root, files)))
end
return result
end
rdir(dir::AbstractString, pat::AbstractString) = rdir(dir, Glob.FilenameMatch(pat))
function literate_directory(dir)
# Removing old compiled md files...
#for filename in filter(file -> endswith(file, ".md"), readdir(dir))
# rm(joinpath(dir,filename))
#end
for filename in filter(file -> endswith(file, ".jl"), readdir(dir))
filenameNoPath = filename
filename = joinpath(dir,filename)
# if the md file exist, let's delete it first...
filenameMD = replace(filename,".jl" => ".md")
if isfile(filenameMD)
rm(filenameMD)
end
# `include` the file to test it before `#src` lines are removed. It is
# in a testset to isolate local variables between files.
if ! ("preview" in ARGS)
@testset "$(filename)" begin
println(filename)
include_sandbox(filename)
end
Literate.markdown(
filename,
dir;
documenter = true,
postprocess = link_example,
# default is @example -> evaluated by documenter at the end of the block
codefence = "```@repl $filenameNoPath" => "```"
)
else
Literate.markdown(
filename,
dir;
documenter = true,
postprocess = link_example,
codefence = "```text" => "```"
)
end
end
return nothing
end
function preprocess(rootDir)
cd(@__DIR__)
Pkg.activate(".")
#rootDir = LESSONS_ROOTDIR
files = rdir(rootDir,"*.md")
videos = ods_read(joinpath(@__DIR__,"videosList.ods");sheetName="videos",retType="DataFrame")
for file in files
#file = files[4]
origContent = read(file,String)
outContent = ""
filename = splitdir(file)[2]
segmentVideos = videos[videos.host_filename .== filename,:]
#openVideosFlag = filename == "0001_-_Course_presentation.md" ? "open" : ""
if size(segmentVideos,1) > 0
outContent *= """
```@raw html
<div id="ytb-videos">
<span style=font-weight:bold;>Videos related to this segment (click the title to watch)</span>
"""
for (i, video) in enumerate(eachrow(segmentVideos))
#video = segmentVideos[1,:]
openVideosFlag = (i == 1) ? "open" : ""
outContent *= """
<details $(openVideosFlag)><summary>$(video.lesson_short_name) - $(video.segment_id)$(video.part_id): $(video.part_name) ($(video.minutes):$(video.seconds))</summary>
<div class="container ytb-container">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/$(video.vid)" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0"></iframe>
</div>
</div>
</details>
"""
end # end of each video
outContent *= """
</div>
```
------
"""
end # end of if there are videos
outContent *= origContent
if (filename != "index.md")
commentCode = """
```@raw html
<script src="https://utteranc.es/client.js"
repo="sylvaticus/SPMLJ"
issue-term="title"
label="💬 website_comment"
theme="github-dark"
crossorigin="anonymous"
async>
</script>
```
"""
addThisCode1 = """
```@raw html
<div class="addthis_inline_share_toolbox"></div>
```
"""
addThisCode2 = """
```@raw html
<!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-6256c971c4f745bc"></script>
```
"""
# https://crowdsignal.com/support/rating-widget/
ratingCode1 = """
```@raw html
<div id="pd_rating_holder_8962705"></div>
<script type="text/javascript">
const pageURL = window.location.href;
PDRTJS_settings_8962705 = {
"id" : "8962705",
"unique_id" : "$(file)",
"title" : "$(filename)",
"permalink" : pageURL
};
</script>
```
"""
ratingCode2 = """
```@raw html
<script type="text/javascript" charset="utf-8" src="https://polldaddy.com/js/rating/rating.js"></script>
```
"""
outContent *= "\n---------\n"
outContent *= ratingCode1
outContent *= addThisCode1
outContent *= "\n---------\n"
outContent *= commentCode
outContent *= ratingCode2
outContent *= addThisCode2
end
write(file,outContent)
end # end for each file
end #end preprocess function
# ------------------------------------------------------------------------------
# Saving the unmodified source to a temp directory
cp(LESSONS_ROOTDIR, LESSONS_ROOTDIR_TMP; force=true)
println("Starting literating tutorials (.jl --> .md)...")
literate_directory.(map(lsubdir->joinpath(LESSONS_ROOTDIR ,lsubdir),values(LESSONS_SUBDIR)))
if MAKE_PDF
@error "MD->PDF generation is disabled because DocumenterMarkdown is not maintained and it blocks Documenter to older versions"
println("Starting making PDF...")
makedocs(sitename="SPMLJ - Introduction to Scientific Programming and Machine Learning with Julia",
authors = "Antonello Lobianco",
pages = [
"Index" => "index.md",
"Lessons" => makeList(LESSONS_ROOTDIR,LESSONS_SUBDIR),
],
format = Markdown(),
source = "lessonsSources", # Attention here !!!!!!!!!!!
build = "buildedDoc_PDF",
)
end
println("Starting preprocessing markdown pages...")
preprocess(LESSONS_ROOTDIR)
println("Starting making the documentation...")
makedocs(sitename="SPMLJ",
authors = "Antonello Lobianco",
pages = [
"Index" => "index.md",
"Lessons" => makeList(LESSONS_ROOTDIR,LESSONS_SUBDIR),
],
format = Documenter.HTML(
prettyurls = false,
analytics = "G-Q39LHCRBB6",
assets = ["assets/custom.css"],
),
#strict = true,
warnonly = true,
#doctest = false
source = "lessonsSources", # Attention here !!!!!!!!!!!
build = "buildedDoc",
#preprocess = preprocess
)
# Copying back the unmodified source
cp(LESSONS_ROOTDIR_TMP, LESSONS_ROOTDIR; force=true)
println("Starting deploying the documentation...")
deploydocs(
repo = "github.com/sylvaticus/SPMLJ.git",
devbranch = "main",
target = "buildedDoc"
)