Browse Source

change #' to # and require ## for regular julia comments, fix #3 (#4)

pull/6/head
Fredrik Ekre 8 years ago committed by GitHub
parent
commit
47ca20d63c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      docs/make.jl
  2. 4
      docs/src/customprocessing.md
  3. 30
      docs/src/fileformat.md
  4. 10
      docs/src/outputformats.jl
  5. 8
      docs/src/outputformats.md
  6. 24
      docs/src/pipeline.md
  7. 116
      examples/example.jl
  8. 32
      src/Literate.jl
  9. 104
      test/runtests.jl

1
docs/make.jl

@ -26,7 +26,6 @@ folder = isempty(travis_tag) ? "latest" : travis_tag
url = "https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/$(folder)/" url = "https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/$(folder)/"
if get(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL", "") == "true" if get(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL", "") == "true"
str = read(joinpath(@__DIR__, "src/outputformats.md"), String) str = read(joinpath(@__DIR__, "src/outputformats.md"), String)
@show url
str = replace(str, "[notebook.ipynb](generated/notebook.ipynb)." => "[notebook.ipynb]($(url)generated/notebook.ipynb).") str = replace(str, "[notebook.ipynb](generated/notebook.ipynb)." => "[notebook.ipynb]($(url)generated/notebook.ipynb).")
write(joinpath(@__DIR__, "src/outputformats.md"), str) write(joinpath(@__DIR__, "src/outputformats.md"), str)
end end

4
docs/src/customprocessing.md

@ -27,8 +27,8 @@ We could of course update our source file before generating the docs, but we cou
instead use a `preprocess` function that splices the date into the source for us. instead use a `preprocess` function that splices the date into the source for us.
Consider the following source file: Consider the following source file:
```julia ```julia
#' # Example # # Example
#' This example was generated DATEOFTODAY # This example was generated DATEOFTODAY
x = 1 // 3 x = 1 // 3
``` ```

30
docs/src/fileformat.md

@ -8,25 +8,27 @@ stay up do date with other changes in your package.
## [**2.1.** Syntax](@id Syntax) ## [**2.1.** Syntax](@id Syntax)
The basic syntax is simple: The basic syntax is simple:
- lines starting with `#'` is treated as markdown, - lines starting with `# ` are treated as markdown,
- all other lines are treated as julia code. - all other lines are treated as julia code.
The reason for using `#'` instead of `#` is that we want to be able to use `#` as comments, !!! note
just as in a regular script. Lets look at a simple example: If you want regular julia comments in the source file use `## ` instead of `# `.
Lets look at a simple example:
```julia ```julia
#' # Rational numbers # # Rational numbers
#' #
#' In julia rational numbers can be constructed with the `//` operator. # In julia rational numbers can be constructed with the `//` operator.
#' Lets define two rational numbers, `x` and `y`: # Lets define two rational numbers, `x` and `y`:
x = 1//3 x = 1//3
y = 2//5 y = 2//5
#' When adding `x` and `y` together we obtain a new rational number: # When adding `x` and `y` together we obtain a new rational number:
z = x + y z = x + y
``` ```
In the lines `#'` we can use regular markdown syntax, for example the `#` In the lines starting with `#` we can use regular markdown syntax, for example the `#`
used for the heading and the backticks for formatting code. The other lines are regular used for the heading and the backticks for formatting code. The other lines are regular
julia code. We note a couple of things: julia code. We note a couple of things:
- The script is valid julia, which means that we can `include` it and the example will run - The script is valid julia, which means that we can `include` it and the example will run
@ -60,11 +62,11 @@ using Documenter. Obviously we don't want to include this in the notebook,
since `@docs` is Documenter syntax that the notebook will not understand. This since `@docs` is Documenter syntax that the notebook will not understand. This
is a case where we can prepend `#md` to those lines: is a case where we can prepend `#md` to those lines:
````julia ````julia
#md #' ```@docs #md # ```@docs
#md #' Literate.markdown #md # Literate.markdown
#md #' Literate.notebook #md # Literate.notebook
#md #' Literate.markdown #md # Literate.markdown
#md #' ``` #md # ```
```` ````
The lines in the example above would be filtered out in the preprocessing step, unless we are The lines in the example above would be filtered out in the preprocessing step, unless we are
generating a markdown file. When generating a markdown file we would simple remove generating a markdown file. When generating a markdown file we would simple remove

10
docs/src/outputformats.jl

@ -1,12 +1,12 @@
#' # Rational numbers # # Rational numbers
#' #
#' In julia rational numbers can be constructed with the `//` operator. # In julia rational numbers can be constructed with the `//` operator.
#' Lets define two rational numbers, `x` and `y`: # Lets define two rational numbers, `x` and `y`:
x = 1//3 x = 1//3
#- #-
y = 2//5 y = 2//5
#' When adding `x` and `y` together we obtain a new rational number: # When adding `x` and `y` together we obtain a new rational number:
z = x + y z = x + y

8
docs/src/outputformats.md

@ -20,7 +20,7 @@ rm(file)
Markdown.parse(str) Markdown.parse(str)
``` ```
We note that lines starting with `#'` is printed as regular markdown, We note that lines starting with `# ` are printed as regular markdown,
and the code lines have been wrapped in `@example` blocks. We also note that and the code lines have been wrapped in `@example` blocks. We also note that
an `@meta` block have been added, that sets the `EditURL` variable. This is used an `@meta` block have been added, that sets the `EditURL` variable. This is used
by Documenter to redirect the "Edit on GitHub" link for the page, by Documenter to redirect the "Edit on GitHub" link for the page,
@ -38,8 +38,8 @@ Literate.markdown
The (default) notebook output of the source snippet can be seen here: The (default) notebook output of the source snippet can be seen here:
[notebook.ipynb](generated/notebook.ipynb). [notebook.ipynb](generated/notebook.ipynb).
We note that lines starting with `#'` is put in markdown cells, We note that lines starting with `# ` are placed in markdown cells,
and the code lines have been put in code cells. By default the notebook and the code lines have been placed in code cells. By default the notebook
is also executed and output cells populated. The current working directory is also executed and output cells populated. The current working directory
is set to the specified output directory the notebook is executed. is set to the specified output directory the notebook is executed.
Some of the output rendering can be controlled with keyword Some of the output rendering can be controlled with keyword
@ -61,7 +61,7 @@ rm(file)
Markdown.parse(str) Markdown.parse(str)
``` ```
We note that lines starting with `#'` are removed and only the We note that lines starting with `# ` are removed and only the
code lines have been kept. Some of the output rendering can be controlled code lines have been kept. Some of the output rendering can be controlled
with keyword arguments to [`Literate.script`](@ref): with keyword arguments to [`Literate.script`](@ref):

24
docs/src/pipeline.md

@ -30,15 +30,15 @@ and mark them as either markdown or code according to the rules described in the
[Syntax](@ref Syntax) section. Lets consider the example from the previous section [Syntax](@ref Syntax) section. Lets consider the example from the previous section
with each line categorized: with each line categorized:
``` ```
#' # Rational numbers <- markdown # # Rational numbers <- markdown
#' <- markdown # <- markdown
#' In julia rational numbers can be constructed with the `//` operator. <- markdown # In julia rational numbers can be constructed with the `//` operator. <- markdown
#' Lets define two rational numbers, `x` and `y`: <- markdown # Lets define two rational numbers, `x` and `y`: <- markdown
<- code <- code
x = 1 // 3 <- code x = 1 // 3 <- code
y = 2 // 5 <- code y = 2 // 5 <- code
<- code <- code
#' When adding `x` and `y` together we obtain a new rational number: <- markdown # When adding `x` and `y` together we obtain a new rational number: <- markdown
<- code <- code
z = x + y <- code z = x + y <- code
``` ```
@ -47,21 +47,21 @@ In the next step the lines are grouped into "chunks" of markdown and code.
This is done by simply collecting adjacent lines of the same "type" into This is done by simply collecting adjacent lines of the same "type" into
chunks: chunks:
``` ```
#' # Rational numbers ┐ # # Rational numbers ┐
#' # │
#' In julia rational numbers can be constructed with the `//` operator. │ markdown # In julia rational numbers can be constructed with the `//` operator. │ markdown
#' Lets define two rational numbers, `x` and `y`: ┘ # Lets define two rational numbers, `x` and `y`: ┘
x = 1 // 3 │ x = 1 // 3 │
y = 2 // 5 │ code y = 2 // 5 │ code
#' When adding `x` and `y` together we obtain a new rational number: ] markdown # When adding `x` and `y` together we obtain a new rational number: ] markdown
z = x + y ┘ code z = x + y ┘ code
``` ```
In the last parsing step all empty leading and trailing lines for each chunk In the last parsing step all empty leading and trailing lines for each chunk
are removed, but empty lines *within the same* block are kept. The leading `#' ` are removed, but empty lines *within the same* block are kept. The leading `# `
tokens are also removed from the markdown chunks. Finally we would tokens are also removed from the markdown chunks. Finally we would
end up with the following 4 chunks: end up with the following 4 chunks:
@ -112,7 +112,7 @@ The example above would result in two consecutive code-chunks.
After the parsing it is time to generate the output. What is done in this step is After the parsing it is time to generate the output. What is done in this step is
very different depending on the output target, and it is describe in more detail in very different depending on the output target, and it is describe in more detail in
the Output format sections: [Markdown Output](@ref), [Notebook Output](@ref) and the Output format sections: [Markdown Output](@ref), [Notebook Output](@ref) and
[Script Output](@ref). In short, the following is happening: [Script Output](@ref). Using the default settings, the following is happening:
* Markdown output: markdown chunks are printed as-is, code chunks are put inside * Markdown output: markdown chunks are printed as-is, code chunks are put inside
a code fence (defaults to `@example`-blocks), a code fence (defaults to `@example`-blocks),

116
examples/example.jl

@ -1,43 +1,43 @@
#' # **7.** Example # # **7.** Example
#' #
#' This is an example generated with Literate based on this # This is an example generated with Literate based on this
#' source file: [`example.jl`](@__REPO_ROOT_URL__examples/example.jl). # source file: [`example.jl`](@__REPO_ROOT_URL__examples/example.jl).
#' You are seeing the # You are seeing the
#md #' html-output which Documenter have generated based on a markdown #md # html-output which Documenter have generated based on a markdown
#md #' file generated with Literate. The corresponding notebook #md # file generated with Literate. The corresponding notebook
#md #' can be found here: [`example.ipynb`](@__NBVIEWER_ROOT_URL__generated/example.ipynb), #md # can be found here: [`example.ipynb`](@__NBVIEWER_ROOT_URL__generated/example.ipynb),
#nb #' generated notebook output. The corresponding markdown (html) output #nb # generated notebook output. The corresponding markdown (html) output
#nb #' can be found here: [`example.html`](https://fredrikekre.github.io/Literate.jl/latest/generated/example.html), #nb # can be found here: [`example.html`](https://fredrikekre.github.io/Literate.jl/latest/generated/example.html),
#' and the plain script output can be found here: [`example.jl`](./example.jl). # and the plain script output can be found here: [`example.jl`](./example.jl).
#' It is recommended to have the [source file](@__REPO_ROOT_URL__examples/example.jl) # It is recommended to have the [source file](@__REPO_ROOT_URL__examples/example.jl)
#' available when reading this, to better understand how the syntax in the source file # available when reading this, to better understand how the syntax in the source file
#' corresponds to the output you are seeing. # corresponds to the output you are seeing.
#' ### Basic syntax # ### Basic syntax
#' The basic syntax for Literate is simple, lines starting with `#'` is interpreted # The basic syntax for Literate is simple, lines starting with `# ` is interpreted
#' as markdown, and all the other lines are interpreted as code. Here is some code: # as markdown, and all the other lines are interpreted as code. Here is some code:
x = 1//3 x = 1//3
y = 2//5 y = 2//5
#' In markdown sections we can use markdown syntax. For example, we can # In markdown sections we can use markdown syntax. For example, we can
#' write *text in italic font*, **text in bold font** and use # write *text in italic font*, **text in bold font** and use
#' [links](https://www.youtube.com/watch?v=dQw4w9WgXcQ). # [links](https://www.youtube.com/watch?v=dQw4w9WgXcQ).
#' It is possible to filter out lines depending on the output using the # It is possible to filter out lines depending on the output using the
#' `#md`, `#nb`, `#jl` and `#src` tags (see [Filtering Lines](@ref)): # `#md`, `#nb`, `#jl` and `#src` tags (see [Filtering Lines](@ref)):
#md #' - This line starts with `#md` and is thus only visible in the markdown output. #md # - This line starts with `#md` and is thus only visible in the markdown output.
#nb #' - This line starts with `#nb` and is thus only visible in the notebook output. #nb # - This line starts with `#nb` and is thus only visible in the notebook output.
#jl #' - This line starts with `#jl` and is thus only visible in the notebook output. #jl # - This line starts with `#jl` and is thus only visible in the notebook output.
#src #' - This line starts with `#src` and is thus only visible in the source file. #src # - This line starts with `#src` and is thus only visible in the source file.
#' The source file is parsed in chunks of markdown and code. Starting a line # The source file is parsed in chunks of markdown and code. Starting a line
#' with `#-` manually inserts a chunk break. For example, if we want to # with `#-` manually inserts a chunk break. For example, if we want to
#' display the output of the following operations we may insert `#-` in # display the output of the following operations we may insert `#-` in
#' between. These two code blocks will now end up in different # between. These two code blocks will now end up in different
#' `@example`-blocks in the markdown output, and two different notebook cells # `@example`-blocks in the markdown output, and two different notebook cells
#' in the notebook output. # in the notebook output.
x + y x + y
@ -45,12 +45,12 @@ x + y
x * y x * y
#' ### Output Capturing # ### Output Capturing
#' Code chunks are by default placed in Documenter `@example` blocks in the generated # Code chunks are by default placed in Documenter `@example` blocks in the generated
#' markdown. This means that the output will be captured in a block when Documenter is # markdown. This means that the output will be captured in a block when Documenter is
#' building the docs. In notebooks the output is captured in output cells, if the # building the docs. In notebooks the output is captured in output cells, if the
#' `execute` keyword argument is set to true. Output to `stdout`/`stderr` is also # `execute` keyword argument is set to true. Output to `stdout`/`stderr` is also
#' captured. # captured.
function foo() function foo()
println("This string is printed to stdout.") println("This string is printed to stdout.")
@ -59,26 +59,26 @@ end
foo() foo()
#' ### Custom processing # ### Custom processing
#' #
#' It is possible to give Literate custom pre- and post-processing functions. # It is possible to give Literate custom pre- and post-processing functions.
#' For example, here we insert two placeholders, which we will replace with # For example, here we insert two placeholders, which we will replace with
#' something else at time of generation. We have here replaced our placeholders # something else at time of generation. We have here replaced our placeholders
#' with `z` and `1.0 + 2.0im`: # with `z` and `1.0 + 2.0im`:
MYVARIABLE = MYVALUE MYVARIABLE = MYVALUE
#' ### [Documenter.jl interaction](@id documenter-interaction) # ### [Documenter.jl interaction](@id documenter-interaction)
#' #
#' In the source file it is possible to use Documenter.jl style references, # In the source file it is possible to use Documenter.jl style references,
#' such as `@ref` and `@id`. These will be filtered out in the notebook output. # such as `@ref` and `@id`. These will be filtered out in the notebook output.
#' For example, [here is a link](@ref documenter-interaction), but it is only # For example, [here is a link](@ref documenter-interaction), but it is only
#' visible as a link if you are reading the markdown output. We can also # visible as a link if you are reading the markdown output. We can also
#' use equations: # use equations:
#' #
#' ```math # ```math
#' \int_\Omega \nabla v \cdot \nabla u\ \mathrm{d}\Omega = \int_\Omega v f\ \mathrm{d}\Omega # \int_\Omega \nabla v \cdot \nabla u\ \mathrm{d}\Omega = \int_\Omega v f\ \mathrm{d}\Omega
#' ``` # ```
#' #
#' using Documenters math syntax. Documenters syntax is automatically changed to # using Documenters math syntax. Documenters syntax is automatically changed to
#' `\begin{equation} ... \end{equation}` in the notebook output to display correctly. # `\begin{equation} ... \end{equation}` in the notebook output to display correctly.

32
src/Literate.jl

@ -11,7 +11,7 @@ import .Documenter
# # Some simple rules: # # Some simple rules:
# #
# * All lines starting with `#'` are considered markdown, everything else is considered code # * All lines starting with `# ` are considered markdown, everything else is considered code
# * The file is parsed in "chunks" of code and markdown. A new chunk is created when the # * The file is parsed in "chunks" of code and markdown. A new chunk is created when the
# lines switch context from markdown to code and vice versa. # lines switch context from markdown to code and vice versa.
# * Lines starting with `#-` can be used to start a new chunk. # * Lines starting with `#-` can be used to start a new chunk.
@ -33,28 +33,32 @@ mutable struct CodeChunk <: Chunk
end end
CodeChunk() = CodeChunk(String[], false) CodeChunk() = CodeChunk(String[], false)
ismdline(line) = (line == "#" || startswith(line, "# ")) && !startswith(line, "##")
function parse(content; allow_continued = true) function parse(content; allow_continued = true)
lines = collect(eachline(IOBuffer(content))) lines = collect(eachline(IOBuffer(content)))
chunks = Chunk[] chunks = Chunk[]
push!(chunks, startswith(lines[1], "#'") ? MDChunk() : CodeChunk()) push!(chunks, ismdline(rstrip(lines[1])) ? MDChunk() : CodeChunk())
for line in lines for line in lines
line = rstrip(line) line = rstrip(line)
if startswith(line, "#-") # new chunk if startswith(line, "#-") # new chunk
# assume same as last chunk, will be cleaned up otherwise # assume same as last chunk, will be cleaned up otherwise
push!(chunks, typeof(chunks[end])()) push!(chunks, typeof(chunks[end])())
elseif startswith(line, "#'") # markdown elseif ismdline(line) # markdown
if !(chunks[end] isa MDChunk) if !(chunks[end] isa MDChunk)
push!(chunks, MDChunk()) push!(chunks, MDChunk())
end end
# remove "#' " and "#'\n" # remove "# " and "#\n"
line = replace(replace(line, r"^#' " => ""), r"^#'$" => "") line = replace(replace(line, r"^# " => ""), r"^#$" => "")
push!(chunks[end].lines, line) push!(chunks[end].lines, line)
else # code else # code
if !(chunks[end] isa CodeChunk) if !(chunks[end] isa CodeChunk)
push!(chunks, CodeChunk()) push!(chunks, CodeChunk())
end end
# remove "## " and "##\n"
line = replace(replace(line, r"^## " => "# "), r"^##$" => "#")
push!(chunks[end].lines, line) push!(chunks[end].lines, line)
end end
end end
@ -95,9 +99,9 @@ function parse(content; allow_continued = true)
@assert !isempty(merged_chunks) @assert !isempty(merged_chunks)
if isa(chunk, CodeChunk) if isa(chunk, CodeChunk)
append!(merged_chunks[end].lines, chunk.lines) append!(merged_chunks[end].lines, chunk.lines)
else # need to put back #' else # need to put back "#"
for line in chunk.lines for line in chunk.lines
push!(merged_chunks[end].lines, rstrip("#' " * line)) push!(merged_chunks[end].lines, rstrip("# " * line))
end end
end end
else else
@ -127,12 +131,12 @@ function replace_default(content, sym;
if sym === :jl if sym === :jl
content *= """ content *= """
#- #-
# This file was generated using Literate.jl, https://github.com/fredrikekre/Literate.jl ## This file was generated using Literate.jl, https://github.com/fredrikekre/Literate.jl
""" """
else else
content *= """ content *= """
#- #-
#' *This $(sym === :md ? "page" : "notebook") was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).* # *This $(sym === :md ? "page" : "notebook") was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*
""" """
end end
end end
@ -217,7 +221,7 @@ Keyword arguments:
- `documenter`: boolean that says if the source contains Documenter.jl specific things - `documenter`: boolean that says if the source contains Documenter.jl specific things
to filter out during script generation. Defaults to `true`. See the the manual to filter out during script generation. Defaults to `true`. See the the manual
section on [Interaction with Documenter](@ref Interaction-with-Documenter). section on [Interaction with Documenter](@ref Interaction-with-Documenter).
- `keep_comments`: boolean that, if set to `true`, keeps markdown lines (`#'`) - `keep_comments`: boolean that, if set to `true`, keeps markdown lines
as comments in the output script. Defaults to `false`. as comments in the output script. Defaults to `false`.
""" """
function script(inputfile, outputdir; preprocess = identity, postprocess = identity, function script(inputfile, outputdir; preprocess = identity, postprocess = identity,
@ -248,7 +252,7 @@ function script(inputfile, outputdir; preprocess = identity, postprocess = ident
write(ioscript, '\n') # add a newline between each chunk write(ioscript, '\n') # add a newline between each chunk
elseif isa(chunk, MDChunk) && keep_comments elseif isa(chunk, MDChunk) && keep_comments
for line in chunk.lines for line in chunk.lines
write(ioscript, "#' ", line, '\n') write(ioscript, rstrip("# " * line * '\n'))
end end
write(ioscript, '\n') # add a newline between each chunk write(ioscript, '\n') # add a newline between each chunk
end end
@ -314,9 +318,9 @@ function markdown(inputfile, outputdir; preprocess = identity, postprocess = ide
repo = get(ENV, "TRAVIS_REPO_SLUG", "") repo = get(ENV, "TRAVIS_REPO_SLUG", "")
pkg = first(split(last(split(repo, '/')), '.')) pkg = first(split(last(split(repo, '/')), '.'))
content = """ content = """
#' ```@meta # ```@meta
#' EditURL = "@__REPO_ROOT_URL__$(replace(relpath(inputfile, Pkg.dir(pkg)), "\\" => "/"))" # EditURL = "@__REPO_ROOT_URL__$(replace(relpath(inputfile, Pkg.dir(pkg)), "\\" => "/"))"
#' ``` # ```
""" * content """ * content
end end

104
test/runtests.jl

@ -26,27 +26,27 @@ end
@testset "Literate.parse" begin @testset "Literate.parse" begin
content = """ content = """
#' Line 1 # Line 1
Line 2 Line 2
#' Line 3 # Line 3
#' #
#' Line 5 # Line 5
Line 6 Line 6
Line 8 Line 8
#' Line 9 # Line 9
#- #-
#' Line 11 # Line 11
Line 12 Line 12
#- #-
Line 14 Line 14
#' Line 15 # Line 15
#----------------- #-----------------
#' Line 17 # Line 17
Line 18 Line 18
#----------------- #-----------------
Line 20 Line 20
#' Line 21 # Line 21
Line 22 Line 22
Line 23 Line 23
Line 24 Line 24
@ -58,7 +58,7 @@ end
#- #-
Line 31 Line 31
Line 32 Line 32
#' Line 33 # Line 33
Line 34 Line 34
#- #-
Line 36 Line 36
@ -69,18 +69,23 @@ end
#- #-
Line 42 Line 42
Line 43 Line 43
#' Line 44 # Line 44
Line 45 Line 45
#' Line 46 # Line 46
Line 47 Line 47
#' Line 48 # Line 48
#Line 49 #Line 49
Line 50 Line 50
#' #
#' #
#' Line 53 # Line 53
#' #
#' #
#-
## Line 57
Line 58
## Line 59
##Line 60
""" """
expected_chunks = Chunk[ expected_chunks = Chunk[
MDChunk(["Line 1"]), MDChunk(["Line 1"]),
@ -113,6 +118,7 @@ end
MDChunk(["Line 48"]), MDChunk(["Line 48"]),
CodeChunk(["#Line 49", "Line 50"], false), CodeChunk(["#Line 49", "Line 50"], false),
MDChunk(["Line 53"]), MDChunk(["Line 53"]),
CodeChunk(["# Line 57", "Line 58", "# Line 59", "##Line 60"], false)
] ]
parsed_chunks = Literate.parse(content) parsed_chunks = Literate.parse(content)
compare_chunks(parsed_chunks, expected_chunks) compare_chunks(parsed_chunks, expected_chunks)
@ -125,8 +131,8 @@ end
foreach(x-> println(io, x), c.lines) foreach(x-> println(io, x), c.lines)
foreach(x-> println(iows, x, " "), c.lines) foreach(x-> println(iows, x, " "), c.lines)
else else
foreach(x -> println(io, "#' ", x), c.lines) foreach(x -> println(io, "# ", x), c.lines)
foreach(x -> println(iows, "#' ", x, " "), c.lines) foreach(x -> println(iows, "# ", x, " "), c.lines)
end end
println(io, "#-") println(io, "#-")
println(iows, "#-") println(iows, "#-")
@ -138,40 +144,40 @@ end
end # testset parser end # testset parser
content = """ content = """
#' # [Example](@id example-id) # # [Example](@id example-id)
#' [foo](@ref), [bar](@ref bbaarr) # [foo](@ref), [bar](@ref bbaarr)
x = 1 x = 1
#md #' Only markdown #md # Only markdown
#md x + 1 #md x + 1
#nb #' Only notebook #nb # Only notebook
#nb x + 2 #nb x + 2
#jl #' Only script #jl # Only script
#jl x + 3 #jl x + 3
#src #' Source code only #src # Source code only
Source code only #src Source code only #src
# #' Comment ## # Comment
# another comment ## another comment
#- #-
for i in 1:10 for i in 1:10
print(i) print(i)
#' some markdown in a code block # some markdown in a code block
end end
#' name: @__NAME__
#' Link to repo root: @__REPO_ROOT_URL__
#' Link to nbviewer: @__NBVIEWER_ROOT_URL__
# name: @__NAME__ # name: @__NAME__
# Link to repo root: @__REPO_ROOT_URL__ # Link to repo root: @__REPO_ROOT_URL__
# Link to nbviewer: @__NBVIEWER_ROOT_URL__ # Link to nbviewer: @__NBVIEWER_ROOT_URL__
## name: @__NAME__
#' PLACEHOLDER1 ## Link to repo root: @__REPO_ROOT_URL__
#' PLACEHOLDER2 ## Link to nbviewer: @__NBVIEWER_ROOT_URL__
# PLACEHOLDER3
# PLACEHOLDER4 # PLACEHOLDER1
# PLACEHOLDER2
#' Some math: ## PLACEHOLDER3
#' ```math ## PLACEHOLDER4
#' \\int f(x) dx
#' ``` # Some math:
# ```math
# \\int f(x) dx
# ```
""" """
@testset "Literate.script" begin @testset "Literate.script" begin
@ -192,7 +198,7 @@ content = """
x = 1 x = 1
x + 3 x + 3
# #' Comment # # Comment
# another comment # another comment
for i in 1:10 for i in 1:10
@ -244,9 +250,9 @@ content = """
# keep_comments # keep_comments
Literate.script(inputfile, outdir, keep_comments = true) Literate.script(inputfile, outdir, keep_comments = true)
script = read(joinpath(outdir, "inputfile.jl"), String) script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("#' # Example", script) @test occursin("# # Example", script)
@test occursin("#' foo, bar", script) @test occursin("# foo, bar", script)
@test occursin("#' \\int f(x) dx", script) @test occursin("# \\int f(x) dx", script)
end end
end end
end end
@ -281,7 +287,7 @@ end
```@example inputfile ```@example inputfile
x + 1 x + 1
# #' Comment # # Comment
# another comment # another comment
``` ```
@ -416,7 +422,7 @@ end
""" """
"source": [ "source": [
"x + 2\\n", "x + 2\\n",
"# #' Comment\\n", "# # Comment\\n",
"# another comment" "# another comment"
] ]
""", """,
@ -425,7 +431,7 @@ end
"source": [ "source": [
"for i in 1:10\\n", "for i in 1:10\\n",
" print(i)\\n", " print(i)\\n",
"#' some markdown in a code block\\n", "# some markdown in a code block\\n",
"end" "end"
] ]
""", """,

Loading…
Cancel
Save