@@ -5,69 +5,81 @@ puts each example from the julia file into a code block and adds
55a short html div below with the interactive output.
66=#
77using PlotlyJS
8- using Distributions, Quandl, RDatasets # used in examples
98
10- doc_style = Style (layout= Layout (margin= attr (t= 60 , b= 60 , l= 50 , r= 50 )))
9+ # used in examples
10+ using Distributions, HTTP, DataFrames, RDatasets, Colors, CSV, JSON
11+ using Random, Dates, LinearAlgebra, DelimitedFiles
1112
12- # Read all file names in
13- this_dir = dirname (@__FILE__ )
14- if length (ARGS ) == 0
15- all_file_names = readdir (joinpath (this_dir, " .." , " examples" ))
16- else
17- all_file_names = [endswith (i, " .jl" ) ? i : " $(i) .jl" for i in ARGS ]
18- end
19-
20- nfiles = length (all_file_names)
2113
22- # Check whether files are julia files and select julia files
23- cft (x) = x[end - 2 : end ]== " .jl" ? true : false
24- am_i_julia_file = map (cft, all_file_names)
25- all_julia_files = all_file_names[am_i_julia_file]
26- for i in all_julia_files
27- println (i)
28- include (joinpath (this_dir, " .." , " examples" , i))
29- end
14+ const THIS_DIR = dirname (@__FILE__ )
3015
31- use_style! (doc_style)
3216
3317# Walk through each example in a file and get the markdown from `single_example`
34- function single_file (filename:: String )
18+ function single_example_file (filename:: String )
19+ base_fn = split (filename, " ." )[1 ]
20+ start_example = " ```@example $(base_fn) "
21+ end_example = " ```"
3522 # Open a file to write to
36- open (joinpath (this_dir , " examples" , filename[ 1 : end - 3 ] * " .md" ), " w" ) do outfile
23+ open (joinpath (THIS_DIR , " src " , " examples" , " $(base_fn) .md" ), " w" ) do outfile
3724
38- # Read lines from a files
39- fulltext = open (f-> read (f, String), joinpath (this_dir, " .." , " examples" , filename), " r" )
40- all_lines = split (fulltext, " \n " )
41- l = 1
42- regex = r" ^function ([^_].+?)\( "
43- regex_end = r" ^end$"
25+ write_example (ex) = println (outfile, start_example, " \n " , ex, " \n " , end_example, " \n " )
4426
45- while true
46- # Find next function name (break if none)
47- l = findnext (x -> match (regex, x) != nothing , all_lines, l+ 1 )
48- if l == 0
49- break
50- end
51- # find corresponding end for this function
52- end_l = findnext (x -> match (regex_end, x) != nothing , all_lines, l+ 1 )
27+ fn_h1 = titlecase (replace (base_fn, " _" => " " ))
28+ println (outfile, " # $(fn_h1) \n " )
5329
54- # Pull out function text
55- func_block = join (all_lines[l: end_l], " \n " )
56- fun_name = match (regex, all_lines[l])[1 ]
30+ # Read lines from a files
31+ fulltext = open (
32+ f-> read (f, String),
33+ joinpath (THIS_DIR, " .." , " examples" , filename),
34+ " r"
35+ )
36+ all_lines = split (fulltext, " \n " )
37+ l = 1
38+ regex = r" ^function ([^_].+?)\( "
39+ regex_end = r" ^end$"
5740
58- println (" adding $fun_name " )
41+ # find preamble
42+ if base_fn == " subplots" # special case
43+ preamble = " using PlotlyJS, Dates\n include(\" ../../../examples/line_scatter.jl\" )"
44+ write_example (preamble)
45+ else
46+ first_line = findfirst (x -> match (regex, x) != = nothing , all_lines)
47+ if first_line != = nothing
48+ preamble = strip (join (all_lines[1 : first_line- 1 ], " \n " ))
49+ write_example (preamble)
50+ end
51+ end
5952
60- # Get html block
61- plt = eval (Expr (:call , Symbol (fun_name))). plot
62- relayout! (plt, margin= attr (t= 60 , b= 60 , l= 50 , r= 50 ))
63- html_block = PlotlyJS. html_body (plt)
53+ while true
54+ # Find next function name (break if none)
55+ l = findnext (x -> match (regex, x) != = nothing , all_lines, l+ 1 )
56+ if l == 0 || l === nothing
57+ break
58+ end
59+ # find corresponding end for this function
60+ end_l = findnext (x -> match (regex_end, x) != = nothing , all_lines, l+ 1 )
6461
65- println (outfile, " ```julia\n $func_block \n $(fun_name) ()\n ```\n\n\n $html_block \n\n " )
66- l = end_l
67- end
62+ # Pull out function text
63+ func_block = join (all_lines[l: end_l], " \n " )
64+ fun_name = match (regex, all_lines[l])[1 ]
65+ # println("adding $fun_name")
66+ an_ex = string (func_block, " \n " , fun_name, " ()" )
67+ write_example (an_ex)
68+ l = end_l
69+ end
6870 end # do outfile
6971
7072 return nothing
7173end
7274
73- main () = map (single_file, all_julia_files)
75+ function main ()
76+ # Read all file names in
77+ if length (ARGS ) == 0
78+ all_file_names = readdir (joinpath (THIS_DIR, " .." , " examples" ))
79+ else
80+ all_file_names = [endswith (i, " .jl" ) ? i : " $(i) .jl" for i in ARGS ]
81+ end
82+ all_julia_files = filter (x -> endswith (x, " .jl" ), all_file_names)
83+
84+ foreach (single_example_file, all_julia_files)
85+ end
0 commit comments