Skip to content

Commit c2030ed

Browse files
stevengjJeffBezanson
authored andcommitted
rm newline from end of Markdown text/plain output (#27128)
fixes #27102
1 parent 337ee84 commit c2030ed

File tree

6 files changed

+47
-42
lines changed

6 files changed

+47
-42
lines changed

stdlib/Markdown/src/GitHub/table.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ function term(io::IO, md::Table, columns)
142142
padcells!(cells, md.align, len = ansi_length)
143143
for i = 1:length(cells)
144144
join(io, cells[i], " ")
145-
println(io)
146145
if i == 1
147-
join(io, [""^ansi_length(cells[i][j]) for j = 1:length(cells[1])], " ")
148146
println(io)
147+
join(io, [""^ansi_length(cells[i][j]) for j = 1:length(cells[1])], " ")
149148
end
149+
i < length(cells) && println(io)
150150
end
151151
end
152152

stdlib/Markdown/src/IPython/IPython.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ latex(io::IO, tex::LaTeX) =
3131
latexinline(io::IO, tex::LaTeX) =
3232
print(io, '$', tex.formula, '$')
3333

34-
term(io::IO, tex::LaTeX, cols) = printstyled(io, tex.formula, '\n', color=:magenta)
34+
term(io::IO, tex::LaTeX, cols) = printstyled(io, tex.formula, color=:magenta)
3535
terminline(io::IO, tex::LaTeX) = printstyled(io, tex.formula, color=:magenta)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ wrapped_lines(io::IO, f::Function, args...; width = 80, i = 0) =
3535

3636
function print_wrapped(io::IO, s...; width = 80, pre = "", i = 0)
3737
lines = wrapped_lines(io, s..., width = width, i = i)
38-
println(io, lines[1])
38+
print(io, lines[1])
3939
for line in lines[2:end]
40-
println(io, pre, line)
40+
print(io, '\n', pre, line)
4141
end
4242
length(lines), length(pre) + ansi_length(lines[end])
4343
end
4444

4545
print_wrapped(f::Function, io::IO, args...; kws...) = print_wrapped(io, f, args...; kws...)
46-

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@ function term(io::IO, content::Vector, cols)
99
isempty(content) && return
1010
for md in content[1:end-1]
1111
term(io, md, cols)
12-
println(io)
12+
print(io, '\n', '\n')
1313
end
1414
term(io, content[end], cols)
1515
end
1616

1717
term(io::IO, md::MD, columns = cols(io)) = term(io, md.content, columns)
1818

1919
function term(io::IO, md::Paragraph, columns)
20-
print(io, " "^margin)
21-
print_wrapped(io, width = columns-2margin, pre = " "^margin) do io
20+
print(io, ' '^margin)
21+
print_wrapped(io, width = columns-2margin, pre = ' '^margin) do io
2222
terminline(io, md.content)
2323
end
2424
end
2525

2626
function term(io::IO, md::BlockQuote, columns)
2727
s = sprint(term, md.content, columns - 10; context=io)
28-
for line in split(rstrip(s), "\n")
29-
println(io, " "^margin, "", line)
28+
lines = split(rstrip(s), '\n')
29+
print(io, ' '^margin, '', lines[1])
30+
for i = 2:length(lines)
31+
print(io, '\n', ' '^margin, '', lines[i])
3032
end
3133
end
3234

@@ -42,47 +44,52 @@ function term(io::IO, md::Admonition, columns)
4244
elseif lowercase(md.title) == "tip"
4345
col = :green
4446
end
45-
printstyled(io, " "^margin, ""; color=col, bold=true)
47+
printstyled(io, ' '^margin, ""; color=col, bold=true)
4648
printstyled(io, isempty(md.title) ? md.category : md.title; color=col, bold=true)
47-
printstyled(io, "\n", " "^margin, "", "\n"; color=col, bold=true)
49+
printstyled(io, '\n', ' '^margin, '', '\n'; color=col, bold=true)
4850
s = sprint(term, md.content, columns - 10; context=io)
49-
for line in split(rstrip(s), "\n")
50-
printstyled(io, " "^margin, ""; color=col, bold=true)
51-
println(io, line)
51+
lines = split(rstrip(s), '\n')
52+
for i in eachindex(lines)
53+
printstyled(io, ' '^margin, ''; color=col, bold=true)
54+
print(io, lines[i])
55+
i < lastindex(lines) && println(io)
5256
end
5357
end
5458

5559
function term(io::IO, f::Footnote, columns)
56-
print(io, " "^margin, "")
60+
print(io, ' '^margin, "")
5761
printstyled(io, "[^$(f.id)]", bold=true)
58-
println(io, "\n", " "^margin, "")
62+
println(io, '\n', ' '^margin, '')
5963
s = sprint(term, f.text, columns - 10; context=io)
60-
for line in split(rstrip(s), "\n")
61-
println(io, " "^margin, "", line)
64+
lines = split(rstrip(s), '\n')
65+
for i in eachindex(lines)
66+
print(io, ' '^margin, '', lines[i])
67+
i < lastindex(lines) && println(io)
6268
end
6369
end
6470

6571
function term(io::IO, md::List, columns)
6672
for (i, point) in enumerate(md.items)
67-
print(io, " "^2margin, isordered(md) ? "$(i + md.ordered - 1). " : "")
68-
print_wrapped(io, width = columns-(4margin+2), pre = " "^(2margin+2),
73+
print(io, ' '^2margin, isordered(md) ? "$(i + md.ordered - 1). " : "")
74+
print_wrapped(io, width = columns-(4margin+2), pre = ' '^(2margin+2),
6975
i = 2margin+2) do io
7076
term(io, point, columns - 10)
7177
end
78+
i < lastindex(md.items) && print(io, '\n', '\n')
7279
end
7380
end
7481

7582
function _term_header(io::IO, md, char, columns)
7683
text = terminline_string(io, md.text)
7784
with_output_color(:bold, io) do io
78-
print(io, " "^(margin))
85+
print(io, ' '^margin)
7986
line_no, lastline_width = print_wrapped(io, text,
8087
width=columns - 4margin; pre=" ")
8188
line_width = min(1 + lastline_width, columns)
8289
if line_no > 1
8390
line_width = max(line_width, div(columns, 3))
8491
end
85-
char != ' ' && println(io, " "^(margin), string(char) ^ line_width)
92+
char != ' ' && print(io, '\n', ' '^(margin), char^line_width)
8693
end
8794
end
8895

@@ -96,19 +103,18 @@ end
96103

97104
function term(io::IO, md::Code, columns)
98105
with_output_color(:cyan, io) do io
99-
for line in lines(md.code)
100-
print(io, " "^margin)
101-
println(io, line)
106+
L = lines(md.code)
107+
for i in eachindex(L)
108+
print(io, ' '^margin, L[i])
109+
i < lastindex(L) && println(io)
102110
end
103111
end
104112
end
105113

106-
function term(io::IO, br::LineBreak, columns)
107-
println(io)
108-
end
114+
term(io::IO, br::LineBreak, columns) = nothing # line breaks already printed between subsequent elements
109115

110116
function term(io::IO, br::HorizontalRule, columns)
111-
println(io, " " ^ margin, "" ^ (columns - 2margin))
117+
print(io, ' '^margin, ''^(columns - 2margin))
112118
end
113119

114120
term(io::IO, x, _) = show(io, MIME"text/plain"(), x)
@@ -126,7 +132,7 @@ function terminline(io::IO, content::Vector)
126132
end
127133

128134
function terminline(io::IO, md::AbstractString)
129-
print(io, replace(md, r"[\s\t\n]+" => " "))
135+
print(io, replace(md, r"[\s\t\n]+" => ' '))
130136
end
131137

132138
function terminline(io::IO, md::Bold)

stdlib/Markdown/test/runtests.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ World""" |> plain == "Hello\n\n---\n\nWorld\n"
226226
# Terminal (markdown) output
227227

228228
# multiple whitespace is ignored
229-
@test sprint(term, md"a b") == " a b\n"
230-
@test sprint(term, md"[x](https://julialang.org)") == " x (https://julialang.org)\n"
231-
@test sprint(term, md"[x](@ref)") == " x\n"
232-
@test sprint(term, md"[x](@ref something)") == " x\n"
233-
@test sprint(term, md"![x](https://julialang.org)") == " (Image: x)\n"
229+
@test sprint(term, md"a b") == " a b"
230+
@test sprint(term, md"[x](https://julialang.org)") == " x (https://julialang.org)"
231+
@test sprint(term, md"[x](@ref)") == " x"
232+
@test sprint(term, md"[x](@ref something)") == " x"
233+
@test sprint(term, md"![x](https://julialang.org)") == " (Image: x)"
234234

235235
# enumeration is normalized
236236
let doc = Markdown.parse(
@@ -312,7 +312,7 @@ table = md"""
312312
# mime output
313313
let out =
314314
@test sprint(show, "text/plain", book) ==
315-
" Title\n ≡≡≡≡≡≡≡\n\n Some discussion\n\n │ A quote\n\n Section important\n ===================\n\n Some bolded\n\n • list1\n \n • list2\n \n"
315+
" Title\n ≡≡≡≡≡≡≡\n\n Some discussion\n\n │ A quote\n\n Section important\n ===================\n\n Some bolded\n\n • list1\n\n • list2"
316316
@test sprint(show, "text/markdown", book) ==
317317
"""
318318
# Title
@@ -1073,19 +1073,19 @@ end
10731073
let buf = IOBuffer()
10741074
@test typeof(sprint(Markdown.term, Markdown.parse(" "))) == String
10751075
show(buf, "text/plain", md"*emph*")
1076-
@test String(take!(buf)) == " emph\n"
1076+
@test String(take!(buf)) == " emph"
10771077
show(buf, "text/markdown", md"*emph*")
10781078
@test String(take!(buf)) == "*emph*\n"
10791079
show(IOContext(buf, :color=>true), "text/plain", md"*emph*")
1080-
@test String(take!(buf)) == " \e[4memph\e[24m\n"
1080+
@test String(take!(buf)) == " \e[4memph\e[24m"
10811081
end
10821082

10831083
# table rendering with term #25213
10841084
t = """
10851085
a | b
10861086
:-- | --:
10871087
1 | 2"""
1088-
@test sprint(Markdown.term, Markdown.parse(t), 0) == "a b\n– –\n1 2\n"
1088+
@test sprint(Markdown.term, Markdown.parse(t), 0) == "a b\n– –\n1 2"
10891089

10901090
# test Base.copy
10911091
let

test/docs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ docstring_startswith(d1::DocStr, d2) = docstring_startswith(parsedoc(d1), d2)
3232

3333
@doc "Doc abstract type"
3434
abstract type C74685{T,N} <: AbstractArray{T,N} end
35-
@test repr("text/plain", Docs.doc(C74685))==" Doc abstract type\n"
35+
@test repr("text/plain", Docs.doc(C74685))==" Doc abstract type"
3636
@test string(Docs.doc(C74685))=="Doc abstract type\n"
3737

3838
macro macro_doctest() end

0 commit comments

Comments
 (0)