From 6ebc40df96891a8c414cbd09c30450541152b913 Mon Sep 17 00:00:00 2001 From: Leif Andersson <leif.andersson@control.lth.se> Date: Sat, 27 Jan 2024 15:25:17 +0100 Subject: [PATCH] Using include_string instead of include. --- Project.toml | 2 +- src/ModJuliaTikz.jl | 63 +++++++++++++++++++-------------------------- 2 files changed, 27 insertions(+), 38 deletions(-) diff --git a/Project.toml b/Project.toml index f2afa53..f372625 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModJuliaTikz" uuid = "03fd23bd-cacd-4584-a80b-c389ae8a36ca" authors = ["Leif Andersson <leif.andersson@control.lth.se>"] -version = "0.1.0" +version = "0.2.0" [compat] julia = "1" diff --git a/src/ModJuliaTikz.jl b/src/ModJuliaTikz.jl index 6e78427..dc63484 100644 --- a/src/ModJuliaTikz.jl +++ b/src/ModJuliaTikz.jl @@ -44,9 +44,8 @@ function printcsv(data::Array{Any}, csv="") end end - """ - JuliaTikz(tikzfile,delete=true) + JuliaTikz(tikzfile,write_file=false) Extract and run the Julia part of a combined tikz/Julia file. @@ -54,20 +53,12 @@ The boundary between the two is the string \\endinput Julia <name> -where `<name>` is the name that will be given to the Julia file -(with .jl appended), and also to a variable `name` in the program, -i.e. name = "<name>". - -The Julia file will be extracted to a temporary directory, which will be -deleted after the run unless the optional argument `delete` is false. - +where `<name>` is the name that will be given to a variable `name` +in the program, and also to the Julia file (with .jl appended), + if the optional argument `write_file` is true. """ -function JuliaTikz(tikzfile,delete=true) - dir = mktempdir(); - filespec = ""; out=""; - if !delete - println("Opening " * tikzfile) - end; +function JuliaTikz(tikzfile,write_file=false) + open(tikzfile) do file res = nothing; while res === nothing @@ -79,28 +70,26 @@ function JuliaTikz(tikzfile,delete=true) end; global name = res.captures[2]; - filespec = join([dir, name * ".jl"],"/"); - out = open(filespec,"w"); - if !delete - println("Writing " * filespec); - end; - line = "module " * name; - println(out, line); - line = "import ..ModJuliaTikz: printcsv"; - println(out, line); - while !eof(file) - println(out,readline(file)); + global progLines = String[] + push!(progLines,"module " * name * "\n") + push!(progLines,"import ..ModJuliaTikz: printcsv\n") + while !eof(file) + push!(progLines,readline(file;keep=true)) end; - println(out,"end"); - close(out); - end; - include(filespec); - - if delete - rm(dir,recursive=true); - else - return filespec; - end; -end; + end + push!(progLines,"end\n") + if write_file + dir = mktempdir(); + filespec = join([dir, name * ".jl"],"/"); + println("Writing new" * filespec) + open(filespec,"w") do out + for i in eachindex(progLines) + print(out,progLines[i]) + end + end + end + + include_string(Main,join(progLines),name) +end # JuliaTikz end; # end module -- GitLab