Skip to content
Snippets Groups Projects
Commit 6ebc40df authored by Leif Andersson's avatar Leif Andersson
Browse files

Using include_string instead of include.

parent 0757000a
No related branches found
No related tags found
No related merge requests found
name = "ModJuliaTikz" name = "ModJuliaTikz"
uuid = "03fd23bd-cacd-4584-a80b-c389ae8a36ca" uuid = "03fd23bd-cacd-4584-a80b-c389ae8a36ca"
authors = ["Leif Andersson <leif.andersson@control.lth.se>"] authors = ["Leif Andersson <leif.andersson@control.lth.se>"]
version = "0.1.0" version = "0.2.0"
[compat] [compat]
julia = "1" julia = "1"
......
...@@ -44,9 +44,8 @@ function printcsv(data::Array{Any}, csv="") ...@@ -44,9 +44,8 @@ function printcsv(data::Array{Any}, csv="")
end end
end end
""" """
JuliaTikz(tikzfile,delete=true) JuliaTikz(tikzfile,write_file=false)
Extract and run the Julia part of a combined tikz/Julia file. Extract and run the Julia part of a combined tikz/Julia file.
...@@ -54,20 +53,12 @@ The boundary between the two is the string ...@@ -54,20 +53,12 @@ The boundary between the two is the string
\\endinput Julia <name> \\endinput Julia <name>
where `<name>` is the name that will be given to the Julia file where `<name>` is the name that will be given to a variable `name`
(with .jl appended), and also to a variable `name` in the program, in the program, and also to the Julia file (with .jl appended),
i.e. name = "<name>". if the optional argument `write_file` is true.
The Julia file will be extracted to a temporary directory, which will be
deleted after the run unless the optional argument `delete` is false.
""" """
function JuliaTikz(tikzfile,delete=true) function JuliaTikz(tikzfile,write_file=false)
dir = mktempdir();
filespec = ""; out="";
if !delete
println("Opening " * tikzfile)
end;
open(tikzfile) do file open(tikzfile) do file
res = nothing; res = nothing;
while res === nothing while res === nothing
...@@ -79,28 +70,26 @@ function JuliaTikz(tikzfile,delete=true) ...@@ -79,28 +70,26 @@ function JuliaTikz(tikzfile,delete=true)
end; end;
global name = res.captures[2]; global name = res.captures[2];
filespec = join([dir, name * ".jl"],"/"); global progLines = String[]
out = open(filespec,"w"); push!(progLines,"module " * name * "\n")
if !delete push!(progLines,"import ..ModJuliaTikz: printcsv\n")
println("Writing " * filespec);
end;
line = "module " * name;
println(out, line);
line = "import ..ModJuliaTikz: printcsv";
println(out, line);
while !eof(file) while !eof(file)
println(out,readline(file)); push!(progLines,readline(file;keep=true))
end;
println(out,"end");
close(out);
end; end;
include(filespec); end
push!(progLines,"end\n")
if delete if write_file
rm(dir,recursive=true); dir = mktempdir();
else filespec = join([dir, name * ".jl"],"/");
return filespec; println("Writing new" * filespec)
end; open(filespec,"w") do out
end; for i in eachindex(progLines)
print(out,progLines[i])
end
end
end
include_string(Main,join(progLines),name)
end # JuliaTikz
end; # end module end; # end module
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment