1
1
"""
2
2
makedemos(source::String;
3
3
root = "<current-directory>",
4
- destination = "democards",
5
4
src = "src",
6
5
build = "build",
7
6
branch = "gh-pages",
@@ -18,10 +17,10 @@ Processing pipeline:
18
17
4. save/copy cover images
19
18
5. generate postprocess callback function, which includes url-redirection.
20
19
21
- !!! note By default, the source demo files are read, processed and save to `docs/src/democards`, so
22
- if you put all source demo files in `docs/src`, there will be a duplication of files and assets.
23
- Thus, it's recommended to not put your original page folder in `docs/src`, and it's prefered to
24
- git ignore `docs/src/democards`
20
+ !!! warning
21
+ By default, `makedemos` generates all the necessary files to `docs/src/ `, this means that the
22
+ data you pass to `makedemos` should not be placed at `docs/src/`. A recommendation is to
23
+ put folders in `docs/`. For example, `docs/quickstart` is a good choice.
25
24
26
25
# Inputs
27
26
@@ -37,8 +36,8 @@ Processing pipeline:
37
36
38
37
# Keywords
39
38
40
- * `root::String`: should be equal to `Documenter`'s setting. By default it's `"docs"`.
41
- * `destination::String`: The folder name in generated documentation. By default it's `"democards"` .
39
+ * `root::String`: should be equal to `Documenter`'s setting. Typically it is `"docs"` if this
40
+ function is called in `docs/make.jl` file .
42
41
* `src::String`: should be equal to `Documenter`'s setting. By default it's `"src"`.
43
42
* `build::String`: should be equal to `Documenter`'s setting. By default it's `"build"`.
44
43
* `edit_branch::String`: should be equal to `Documenter`'s setting. By default it's `"master"`.
@@ -85,13 +84,20 @@ For more details of how `config.json` is configured, please check [`DemoCards.De
85
84
"""
86
85
function makedemos (source:: String , templates:: Union{Dict, Nothing} = nothing ;
87
86
root:: String = Base. source_dir (),
88
- destination:: String = " democards" ,
89
87
src:: String = " src" ,
90
88
build:: String = " build" ,
91
89
branch:: String = " gh-pages" ,
92
90
edit_branch:: String = " master" ,
93
91
credit = true )
94
92
93
+ if ! (basename (pwd ()) == " docs" || basename (root) == " docs" || root == preview_build_dir ())
94
+ # special cases that warnings are not printed:
95
+ # 1. called from `docs/make.jl`
96
+ # 2. called from `preview_demos`
97
+ # 3. pwd() in `docs` -- REPL
98
+ @warn " Suspicious `root` setting: typically `makedemos` should be called from `docs/make.jl`. " pwd= pwd () root
99
+ end
100
+
95
101
root = abspath (root)
96
102
page_root = abspath (root, source)
97
103
@@ -102,6 +108,16 @@ function makedemos(source::String, templates::Union{Dict, Nothing} = nothing;
102
108
103
109
page = DemoPage (page_root)
104
110
111
+ relative_root = basename (page)
112
+
113
+ # Where files are generated by DemoCards for Documenter.makedocs
114
+ # e.g "$PROJECT_ROOT/docs/src/quickstart"
115
+ absolute_root = abspath (root, src, relative_root)
116
+
117
+ if page_root == absolute_root
118
+ throw (ArgumentError (" Demo page $page_root should not be placed at \" docs/$src \" folder." ))
119
+ end
120
+
105
121
config_file = joinpath (page. root, config_filename)
106
122
if ! isnothing (templates)
107
123
Base. depwarn (
@@ -121,13 +137,6 @@ function makedemos(source::String, templates::Union{Dict, Nothing} = nothing;
121
137
theme_assets = nothing
122
138
end
123
139
124
- relative_root = joinpath (destination, basename (page))
125
-
126
- # Where files are generated by DemoCards for Documenter.makedocs
127
- # e.g "$PROJECT_ROOT/docs/src/democards/quickstart"
128
- absolute_root = joinpath (root, src, relative_root)
129
- isdir (absolute_root) && rm (absolute_root; force= true , recursive= true )
130
-
131
140
# we can directly pass it to Documenter.makedocs
132
141
if isnothing (templates)
133
142
out_path = walkpage (page; flatten= false ) do item
@@ -137,37 +146,75 @@ function makedemos(source::String, templates::Union{Dict, Nothing} = nothing;
137
146
out_path = joinpath (relative_root, " index.md" )
138
147
end
139
148
140
- @info " SetupDemoCardsDirectory: setting up $(source) directory."
141
- rm (absolute_root; force= true , recursive= true )
142
- mkpath (absolute_root)
143
- # hard coded "covers" should be consistant to card template
144
- isnothing (templates) || mkpath (joinpath (absolute_root, " covers" ))
145
-
146
- # make a copy before pipeline because `save_democards` modifies card path
147
- source_files = [x. path for x in walkpage (page)[2 ]]
148
-
149
- # pipeline
150
- # for themeless version, we don't need to generate covers and index page
151
- copy_assets (absolute_root, page)
152
- # WARNING: julia cards are reconfigured here
153
- save_democards (absolute_root, page;
154
- project_root = root,
155
- src = src,
156
- credit = credit,
157
- nbviewer_root_url = get_nbviewer_root_url (branch))
158
- isnothing (templates) || save_cover (joinpath (absolute_root, " covers" ), page)
159
- isnothing (templates) || generate (joinpath (absolute_root, " index.md" ), page, templates)
160
-
161
- # pipeline: generate postprocess callback function
162
- postprocess_cb = ()-> begin
163
- @info " Redirect URL: redirect docs-edit-link for demos in $(source) directory."
164
- isnothing (templates) || push! (source_files, joinpath (page_root, " index.md" ))
165
- foreach (source_files) do source_file
166
- redirect_link (source_file, source, root, destination, src, build, edit_branch)
149
+ @info " SetupDemoCardsDirectory: setting up \" $(source) \" directory."
150
+ if isdir (absolute_root)
151
+ # a typical and probably safe case -- that we're still in docs/ folder
152
+ trigger_prompt = ! endswith (dirname (absolute_root), joinpath (" docs" , " src" ))
153
+
154
+ if trigger_prompt
155
+ @warn " DemoCards build dir $absolute_root already exists.\n This should only happen when you are using DemoCards incorrectly."
156
+ # Should never reach this in CI environment
157
+ clean_builddir = input_bool (" It might contain important data, do you still want to remove it?" )
158
+ else
159
+ clean_builddir = true
160
+ end
161
+ if clean_builddir
162
+ @info " Remove DemoCards build dir: $absolute_root "
163
+ rm (absolute_root; force= true , recursive= true )
164
+ else
165
+ error (" Stopped demos build." )
167
166
end
168
167
end
169
168
170
- return out_path, postprocess_cb, theme_assets
169
+ mkpath (absolute_root)
170
+ try
171
+ # hard coded "covers" should be consistant to card template
172
+ isnothing (templates) || mkpath (joinpath (absolute_root, " covers" ))
173
+
174
+ # make a copy before pipeline because `save_democards` modifies card path
175
+ source_files = [x. path for x in walkpage (page)[2 ]]
176
+
177
+ # pipeline
178
+ # for themeless version, we don't need to generate covers and index page
179
+ copy_assets (absolute_root, page)
180
+ # WARNING: julia cards are reconfigured here
181
+ save_democards (absolute_root, page;
182
+ project_root = root,
183
+ src = src,
184
+ credit = credit,
185
+ nbviewer_root_url = get_nbviewer_root_url (branch))
186
+ isnothing (templates) || save_cover (joinpath (absolute_root, " covers" ), page)
187
+ isnothing (templates) || generate (joinpath (absolute_root, " index.md" ), page, templates)
188
+
189
+ # pipeline: generate postprocess callback function
190
+ postprocess_cb = ()-> begin
191
+ @info " Redirect page URL: redirect docs-edit-link for demos in \" $(source) \" directory."
192
+ isnothing (templates) || push! (source_files, joinpath (page_root, " index.md" ))
193
+ foreach (source_files) do source_file
194
+ redirect_link (source_file, source, root, src, build, edit_branch)
195
+ end
196
+
197
+ @info " Clean up DemoCards build dir: \" $source \" "
198
+ rm (absolute_root; force= true , recursive= true )
199
+
200
+ if ! isnothing (theme_assets)
201
+ assets_path = abspath (root, src, theme_assets)
202
+ rm (assets_path, force= true )
203
+ if (isempty (readdir (dirname (assets_path))))
204
+ rm (dirname (assets_path))
205
+ end
206
+ end
207
+ end
208
+
209
+ return out_path, postprocess_cb, theme_assets
210
+
211
+ catch err
212
+ # clean up build dir if anything wrong happens here so that we _hopefully_ never trigger the
213
+ # user prompt logic before the build process.
214
+ rm (absolute_root; force= true , recursive= true )
215
+ @error " Errors when building demo dir" pwd= pwd () source root src
216
+ rethrow (err)
217
+ end
171
218
end
172
219
173
220
function generate (file:: String , page:: DemoPage , args... )
@@ -347,13 +394,13 @@ end
347
394
# ## postprocess
348
395
349
396
"""
350
- redirect_link(src_file, source, root, destination, src, build, edit_branch)
397
+ redirect_link(src_file, source, root, src, build, edit_branch)
351
398
352
399
Redirect the "Edit On GitHub" link of generated demo files to its original url, without
353
400
this a 404 error is expected.
354
401
"""
355
- function redirect_link (source_file, source, root, destination, src, build, edit_branch)
356
- build_file = get_build_file (source_file, source, destination, build)
402
+ function redirect_link (source_file, source, root, src, build, edit_branch)
403
+ build_file = get_build_file (source_file, source, build)
357
404
if ! isfile (build_file)
358
405
@warn " $build_file doesn't exists, skip"
359
406
return nothing
@@ -370,16 +417,16 @@ function redirect_link(source_file, source, root, destination, src, build, edit_
370
417
isnothing (m) && return nothing
371
418
build_url = m. captures[1 ]
372
419
373
- src_url = get_source_url (build_url, source, basename (source_file), src, destination )
420
+ src_url = get_source_url (build_url, source, basename (source_file), src)
374
421
new_contents = replace (contents, build_url=> src_url)
375
422
end
376
423
write (build_file, new_contents)
377
424
end
378
425
379
- function get_source_url (build_url, source, cardname, src, destination )
426
+ function get_source_url (build_url, source, cardname, src)
380
427
# given input:
381
428
# - projct_root: "$REPO/blob/$edit_branch"
382
- # - build_root: "$projct_root/$docs_root/$src/$destination "
429
+ # - build_root: "$projct_root/$docs_root/$src"
383
430
# - build_dir: "$build_root/$prefix/$page/$section/$subsection"
384
431
# - build_url: "$build_dir/$cardfile"
385
432
# example of build_url:
@@ -393,28 +440,27 @@ function get_source_url(build_url, source, cardname, src, destination)
393
440
source = replace (source, Base. Filesystem. path_separator => " /" )
394
441
395
442
repo, path = strip .(split (build_url, " /blob/" ; limit= 2 ), ' /' )
396
- root_to_subsection = replace (splitdir (path)[1 ], " $(src) /$(destination) / " => " " ; count= 1 )
443
+ root_to_subsection = replace (splitdir (path)[1 ], " $(src) /" => " " ; count= 1 )
397
444
root_to_subsection = replace (root_to_subsection, " /$(basename (source)) /" => " /$(source) /" ; count= 1 )
398
445
src_url = join ([repo, " blob" , root_to_subsection, cardname], " /" )
399
446
400
447
return src_url
401
448
end
402
449
403
- function get_build_file (source_file, source, destination, build)
450
+ function get_build_file (source_file, source, build)
404
451
# given inputs:
405
452
# - source_file: "$source_root/$prefix/$page/$section/$subsection/$card.md"
406
453
# - source: "$prefix/$page
407
- # - destination: "democards"
408
454
# - build: "build"
409
455
# we need to generate:
410
- # - build_root: "$source_root/$build/$destination "
456
+ # - build_root: "$source_root/$build"
411
457
# - build_dir: "$build_root/$page/$section/$subsection
412
458
# - build_file: "$build_dir/$card.html" or "$build_dir/$card/index.html"
413
459
414
460
sep = Base. Filesystem. path_separator
415
461
# add trailing / to avoid incorrect substring match
416
462
source_root = first (split (source_file, source * sep; limit= 2 ))
417
- build_root = joinpath (source_root, build, destination )
463
+ build_root = joinpath (source_root, build)
418
464
prefix, page = splitdir (source)
419
465
420
466
source_dir, name = splitdir (source_file)
0 commit comments