Skip to content

Commit 1129e82

Browse files
authored
Insert hlines in show with MD objects (#55116)
Fixes #54486.
1 parent b99e411 commit 1129e82

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

stdlib/Markdown/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ version = "1.11.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
7-
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
87
JuliaSyntaxHighlighting = "dc6e5ff7-fb65-4e79-a425-ec3bc9c03011"
8+
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
99

1010
[extras]
1111
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

stdlib/Markdown/src/render/terminal/formatting.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,17 @@ function wraplines(content::Union{Annot, SubString{<:Annot}}, width::Integer = 8
6666
end
6767
lines
6868
end
69+
70+
# Print horizontal lines between each docstring if there are multiple docs
71+
function insert_hlines(docs)
72+
if !isa(docs, MD) || !haskey(docs.meta, :results) || isempty(docs.meta[:results])
73+
return docs
74+
end
75+
docs = docs::MD
76+
v = Any[]
77+
for (n, doc) in enumerate(docs.content)
78+
push!(v, doc)
79+
n == length(docs.content) || push!(v, HorizontalRule())
80+
end
81+
return MD(v)
82+
end

stdlib/Markdown/src/render/terminal/render.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ function term(io::IO, content::Vector, cols)
1313
term(io, content[end], cols)
1414
end
1515

16-
term(io::IO, md::MD, columns = cols(io)) = term(io, md.content, columns)
16+
function term(io::IO, md::MD, columns = cols(io))
17+
md = insert_hlines(md)
18+
return term(io, md.content, columns)
19+
end
1720

1821
function term(io::IO, md::Paragraph, columns)
1922
lines = wraplines(annotprint(terminline, md.content), columns-2margin)

stdlib/Markdown/test/runtests.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

33
using Test, Markdown, StyledStrings
4-
import Markdown: MD, Paragraph, Header, Italic, Bold, LineBreak, plain, term, html, rst, Table, Code, LaTeX, Footnote
4+
import Markdown: MD, Paragraph, Header, Italic, Bold, LineBreak, insert_hlines, plain, term, html, rst, Table, Code, LaTeX, Footnote
55
import Base: show
66

77
# Basics
@@ -1301,3 +1301,10 @@ end
13011301
@testset "Docstrings" begin
13021302
@test isempty(Docs.undocumented_names(Markdown))
13031303
end
1304+
1305+
@testset "Non-Markdown" begin
1306+
# https://github.com/JuliaLang/julia/issues/37765
1307+
@test isa(insert_hlines(Text("foo")), Text)
1308+
# https://github.com/JuliaLang/julia/issues/37757
1309+
@test insert_hlines(nothing) === nothing
1310+
end

stdlib/REPL/src/docview.jl

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using Unicode: normalize
2323
function helpmode(io::IO, line::AbstractString, mod::Module=Main)
2424
internal_accesses = Set{Pair{Module,Symbol}}()
2525
quote
26-
docs = $REPL.insert_hlines($(REPL._helpmode(io, line, mod, internal_accesses)))
26+
docs = $Markdown.insert_hlines($(REPL._helpmode(io, line, mod, internal_accesses)))
2727
$REPL.insert_internal_warning(docs, $internal_accesses)
2828
end
2929
end
@@ -76,20 +76,6 @@ function _helpmode(io::IO, line::AbstractString, mod::Module=Main, internal_acce
7676
end
7777
_helpmode(line::AbstractString, mod::Module=Main) = _helpmode(stdout, line, mod)
7878

79-
# Print horizontal lines between each docstring if there are multiple docs
80-
function insert_hlines(docs)
81-
if !isa(docs, Markdown.MD) || !haskey(docs.meta, :results) || isempty(docs.meta[:results])
82-
return docs
83-
end
84-
docs = docs::Markdown.MD
85-
v = Any[]
86-
for (n, doc) in enumerate(docs.content)
87-
push!(v, doc)
88-
n == length(docs.content) || push!(v, Markdown.HorizontalRule())
89-
end
90-
return Markdown.MD(v)
91-
end
92-
9379
function formatdoc(d::DocStr)
9480
buffer = IOBuffer()
9581
for part in d.text

stdlib/REPL/test/docview.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ end
2828
@test occursin("Couldn't find 'mutable s'", str)
2929
end
3030

31-
@testset "Non-Markdown" begin
32-
# https://github.com/JuliaLang/julia/issues/37765
33-
@test isa(REPL.insert_hlines(Markdown.Text("foo")), Markdown.Text)
34-
# https://github.com/JuliaLang/julia/issues/37757
35-
@test REPL.insert_hlines(nothing) === nothing
36-
end
37-
3831
@testset "Check @var_str also completes to var\"\" in REPL.doc_completions()" begin
3932
checks = ["var", "raw", "r"]
4033
symbols = "@" .* checks .* "_str"

0 commit comments

Comments
 (0)