Skip to content

Commit

Permalink
Merge pull request #419 from MichaelHatherly/mh/doctests-no-color
Browse files Browse the repository at this point in the history
Disable doctest colors
  • Loading branch information
MichaelHatherly authored Jan 30, 2017
2 parents 7fb10f2 + 1795fb9 commit 9c23e4d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 59 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ os:
- osx

julia:
- 0.4
- 0.5
- nightly

Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.4
julia 0.5
Compat 0.8
DocStringExtensions 0.2
1 change: 0 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
environment:
matrix:
- JULIAVERSION: "julialang/bin/winnt/x64/0.4/julia-0.4-latest-win64.exe"
- JULIAVERSION: "julialang/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
- JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe"

Expand Down
48 changes: 35 additions & 13 deletions src/DocChecks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ function doctest(block::Markdown.Code, meta::Dict, doc::Documents.Document, page
# Define new module or reuse an old one from this page if we have a named doctest.
local name = Utilities.getmatch(matched, 1)
local sym = isempty(name) ? gensym("doctest-") : Symbol("doctest-", name)
local sandbox = get!(page.globals.meta, sym, Module(sym))::Module
local sandbox = get!(page.globals.meta, sym) do
newmod = Module(sym)
eval(newmod, :(eval(x) = Core.eval(current_module(), x)))
eval(newmod, :(eval(m, x) = Core.eval(m, x)))
newmod
end
# Normalise line endings.
local code = replace(block.code, "\r\n", "\n")
if haskey(meta, :DocTestSetup)
Expand Down Expand Up @@ -189,11 +194,13 @@ end
function eval_repl(code, sandbox, meta::Dict, doc::Documents.Document, page)
for (input, output) in repl_splitter(code)
result = Result(code, input, output, meta[:CurrentFile])
for (ex, str) in Utilities.parseblock(input, doc, page)
for (ex, str) in Utilities.parseblock(input, doc, page; keywords = false)
# Input containing a semi-colon gets suppressed in the final output.
result.hide = ends_with_semicolon(str)
(value, success, backtrace, text) = Utilities.withoutput() do
Core.eval(sandbox, ex)
disable_color() do
Core.eval(sandbox, ex)
end
end
result.value = value
print(result.stdout, text)
Expand All @@ -216,7 +223,7 @@ function eval_script(code, sandbox, meta::Dict, doc::Documents.Document, page)
# to mark `input`/`output` separation.
input, output = split(code, "\n# output\n", limit = 2)
result = Result(code, "", output, meta[:CurrentFile])
for (ex, str) in Utilities.parseblock(input, doc, page)
for (ex, str) in Utilities.parseblock(input, doc, page; keywords = false)
(value, success, backtrace, text) = Utilities.withoutput() do
Core.eval(sandbox, ex)
end
Expand Down Expand Up @@ -271,7 +278,9 @@ end

function result_to_string(buf, value)
dis = text_display(buf)
value === nothing || eval(Expr(:call, display, dis, QuoteNode(value)))
value === nothing || disable_color() do
eval(Expr(:call, display, dis, QuoteNode(value)))
end
sanitise(buf)
end

Expand All @@ -281,27 +290,31 @@ else
text_display(buf) = TextDisplay(IOContext(buf, multiline = true, limit = true))
end

funcsym() = CAN_INLINE[] ? :withoutput : :eval
funcsym() = CAN_INLINE[] ? :disable_color : :eval

if isdefined(Base.REPL, :ip_matches_func)
function error_to_string(buf, er, bt)
local fs = funcsym()
# Remove unimportant backtrace info.
local index = findlast(ptr -> Base.REPL.ip_matches_func(ptr, fs), bt)
# Print a REPL-like error message.
print(buf, "ERROR: ")
showerror(buf, er, index == 0 ? bt : bt[1:(index - 1)])
disable_color() do
print(buf, "ERROR: ")
showerror(buf, er, index == 0 ? bt : bt[1:(index - 1)])
end
sanitise(buf)
end
else
# Compat for Julia 0.4.
function error_to_string(buf, er, bt)
local fs = funcsym()
print(buf, "ERROR: ")
try
Base.showerror(buf, er)
finally
Base.show_backtrace(buf, fs, bt, 1:typemax(Int))
disable_color() do
print(buf, "ERROR: ")
try
Base.showerror(buf, er)
finally
Base.show_backtrace(buf, fs, bt, 1:typemax(Int))
end
end
sanitise(buf)
end
Expand Down Expand Up @@ -527,9 +540,18 @@ function linkcheck(link::Base.Markdown.Link, doc::Documents.Document)
end
linkcheck(other, doc::Documents.Document) = true

function disable_color(func)
orig = setcolor!(false)
try
func()
finally
setcolor!(orig)
end
end

const CAN_INLINE = Ref(true)
function __init__()
global setcolor! = eval(Base, :(x -> (_ = have_color; global have_color = x; _)))
CAN_INLINE[] = Base.JLOptions().can_inline == 0 ? false : true
end

Expand Down
46 changes: 6 additions & 40 deletions src/Documenter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ function deploydocs(;
make = () -> run(`mkdocs build`),
)
# Get environment variables.
github_api_key = get(ENV, "GITHUB_API_KEY", "")
documenter_key = get(ENV, "DOCUMENTER_KEY", "")
travis_branch = get(ENV, "TRAVIS_BRANCH", "")
travis_pull_request = get(ENV, "TRAVIS_PULL_REQUEST", "")
Expand All @@ -312,9 +311,7 @@ function deploydocs(;
travis_julia = get(ENV, "TRAVIS_JULIA_VERSION", "")

# Other variables.
sha = readchomp(`git rev-parse --short HEAD`)
ssh_key_file = abspath(joinpath(root, ".documenter.enc"))
has_ssh_key = isfile(ssh_key_file)
sha = readchomp(`git rev-parse --short HEAD`)

# Sanity checks
if !isa(julia, AbstractString)
Expand All @@ -328,12 +325,7 @@ function deploydocs(;
should_deploy =
contains(repo, travis_repo_slug) &&
travis_pull_request == "false" &&
(
# Support token and ssh key deployments.
documenter_key != "" ||
github_api_key != "" ||
has_ssh_key
) &&
documenter_key != "" &&
travis_osname == osname &&
travis_julia == julia &&
(
Expand All @@ -349,10 +341,7 @@ function deploydocs(;
Utilities.debug("TRAVIS_BRANCH = \"$travis_branch\"")
Utilities.debug("TRAVIS_TAG = \"$travis_tag\"")
Utilities.debug("git commit SHA = $sha")
Utilities.debug("GITHUB_API_KEY exists = $(github_api_key != "")")
Utilities.debug("DOCUMENTER_KEY exists = $(documenter_key != "")")
Utilities.debug(".documenter.enc path = $(ssh_key_file)")
Utilities.debug(".documenter.enc exists = $(has_ssh_key)")
Utilities.debug("should_deploy = $should_deploy")
end

Expand Down Expand Up @@ -382,27 +371,13 @@ function deploydocs(;
stable_dir = joinpath(dirname, "stable")
tagged_dir = joinpath(dirname, travis_tag)

keyfile, _ = splitext(ssh_key_file)
keyfile = abspath(joinpath(root, ".documenter"))
target_dir = abspath(target)

# The upstream URL to which we push new content and the ssh decryption commands.
upstream =
if documenter_key != ""
write(keyfile, Compat.String(base64decode(documenter_key)))
chmod(keyfile, 0o600)
"git@$(replace(repo, "github.com/", "github.com:"))"
elseif has_ssh_key
dep_warn("travis-generated SSH keys")
key = getenv(r"encrypted_(.+)_key")
iv = getenv(r"encrypted_(.+)_iv")
success(`openssl aes-256-cbc -K $key -iv $iv -in $keyfile.enc -out $keyfile -d`) ||
error("failed to decrypt SSH key.")
chmod(keyfile, 0o600)
"git@$(replace(repo, "github.com/", "github.com:"))"
else
dep_warn("`GITHUB_API_KEY`")
"https://$github_api_key@$repo"
end
write(keyfile, Compat.String(base64decode(documenter_key)))
chmod(keyfile, 0o600)
upstream = "git@$(replace(repo, "github.com/", "github.com:"))"

# Use a custom SSH config file to avoid overwriting the default user config.
withfile(joinpath(homedir(), ".ssh", "config"),
Expand Down Expand Up @@ -486,15 +461,6 @@ function withfile(func, file::AbstractString, contents::AbstractString)
end
end

dep_warn(msg) = warn(
"""
deploying docs with $msg is deprecated. Please use the new method discussed in:
https://juliadocs.github.io/Documenter.jl/latest/man/hosting.html#SSH-Deploy-Keys-1
"""
)

function getenv(regex::Regex)
for (key, value) in ENV
ismatch(regex, key) && return value
Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Returns a vector of parsed expressions and their corresponding raw strings.
The keyword argument `skip = N` drops the leading `N` lines from the input string.
"""
function parseblock(code::AbstractString, doc, page; skip = 0)
function parseblock(code::AbstractString, doc, page; skip = 0, keywords = true)
# Drop `skip` leading lines from the code block. Needed for deprecated `{docs}` syntax.
code = string(code, '\n')
code = last(split(code, '\n', limit = skip + 1))
Expand All @@ -128,7 +128,7 @@ function parseblock(code::AbstractString, doc, page; skip = 0)
local line = match(r"^(.+)$"m, SubString(code, cursor, strlen)).captures[1]
local keyword = Symbol(strip(line))
(ex, ncursor) =
if haskey(Docs.keywords, keyword)
if keywords && haskey(Docs.keywords, keyword)
(QuoteNode(keyword), cursor + length(line) + offset)
else
try
Expand Down
3 changes: 2 additions & 1 deletion src/Writers/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ it is not included -- it is assumed to be the page title and so does not need to
in the navigation menu twice.
"""
function collect_subsections(page::Documents.Page)
local sections = [], title_found = false
sections = []
title_found = false
for element in page.elements
if isa(element, Base.Markdown.Header) && Utilities.header_level(element) < 3
local toplevel = Utilities.header_level(element) === 1
Expand Down
36 changes: 36 additions & 0 deletions test/examples/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,39 @@ end
julia> @define_show_and_make_object q "abcd"
abcd
```

# Issue418

```jldoctest
julia> f(x::Float64) = x
f (generic function with 1 method)
julia> f("")
ERROR: MethodError: no method matching f(::String)
Closest candidates are:
f(!Matched::Float64) at none:1
```


```jldoctest
julia> a = 1
1
julia> b = 2
2
julia> ex = :(a + b)
:(a + b)
julia> eval(ex)
3
```


```jldoctest
julia> a = 1
1
julia> ans
1
```

0 comments on commit 9c23e4d

Please sign in to comment.