Skip to content

Commit

Permalink
no paragraph tags when list is tight
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed May 26, 2017
1 parent 11724ff commit ad8c640
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 39 deletions.
13 changes: 12 additions & 1 deletion base/markdown/Common/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

mutable struct Paragraph
content
intightlist::Bool
end

Paragraph() = Paragraph([])
Paragraph(content) = Paragraph(content, false)
Paragraph() = Paragraph([], false)

function paragraph(stream::IO, md::MD)
buffer = IOBuffer()
Expand Down Expand Up @@ -291,13 +293,16 @@ function list(stream::IO, block::MD)
newline = false # For checking if we have two consecutive newlines: end of list.
count = 0 # Count of list items. Used to check if we need to push remaining
# content in `buffer` after leaving the `while` loop.
maybe_isloose = false
isloose = false
while !eof(stream)
if startswith(stream, "\n")
if newline
# Double newline ends the current list.
pushitem!(list, buffer)
break
else
maybe_isloose = true
newline = true
println(buffer)
end
Expand All @@ -319,9 +324,15 @@ function list(stream::IO, block::MD)
print(buffer, readline(stream, chomp=false))
end
end
isloose = maybe_isloose
end
end
count == length(list.items) || pushitem!(list, buffer)
if !isloose
for items in list.items
foreach(x -> isa(x, Paragraph) ? (x.intightlist = true) : nothing, items)
end
end
push!(block, list)
return true
end
Expand Down
6 changes: 5 additions & 1 deletion base/markdown/render/html.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ function html(io::IO, code::Code)
end

function html(io::IO, md::Paragraph)
withtag(io, :p) do
if md.intightlist
htmlinline(io, md.content)
else
withtag(io, :p) do
htmlinline(io, md.content)
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions base/markdown/render/latex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ function latexinline(io::IO, code::Code)
end

function latex(io::IO, md::Paragraph)
for md in md.content
latexinline(io, md)
for content in md.content
latexinline(io, content)
end
println(io)
println(io)
Expand Down
6 changes: 3 additions & 3 deletions base/markdown/render/terminal/formatting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ end
wrapped_lines(f::Function, args...; width = 80, i = 0) =
wrapped_lines(sprint(f, args...), width = width, i = 0)

function print_wrapped(io::IO, s...; width = 80, pre = "", i = 0)
function print_wrapped(io::IO, s...; width = 80, pre = "", i = 0, newline = true)
lines = wrapped_lines(s..., width = width, i = i)
println(io, lines[1])
print(io, lines[1], newline ? "\n" : "")
for line in lines[2:end]
println(io, pre, line)
print(io, pre, line, newline ? "\n" : "")
end
length(lines), length(pre) + ansi_length(lines[end])
end
Expand Down
36 changes: 18 additions & 18 deletions base/markdown/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

include("formatting.jl")

const margin = 2
const MARGIN = 2
cols(io) = displaysize(io)[2]

function term(io::IO, content::Vector, cols)
Expand All @@ -17,44 +17,44 @@ end
term(io::IO, md::MD, columns = cols(io)) = term(io, md.content, columns)

function term(io::IO, md::Paragraph, columns)
print(io, " "^margin)
print_wrapped(io, width = columns-2margin, pre = " "^margin) do io
print(io, " "^MARGIN)
print_wrapped(io, width = columns-2MARGIN, pre = " "^MARGIN, newline = !md.intightlist) do io
terminline(io, md.content)
end
end

function term(io::IO, md::BlockQuote, columns)
s = sprint(term, md.content, columns - 10)
for line in split(rstrip(s), "\n")
println(io, " "^margin, "|", line)
println(io, " "^MARGIN, "|", line)
end
end

function term(io::IO, md::Admonition, columns)
print(io, " "^margin, "| ")
print(io, " "^MARGIN, "| ")
with_output_format(:bold, print, io, isempty(md.title) ? md.category : md.title)
println(io, "\n", " "^margin, "|")
println(io, "\n", " "^MARGIN, "|")
s = sprint(term, md.content, columns - 10)
for line in split(rstrip(s), "\n")
println(io, " "^margin, "|", line)
println(io, " "^MARGIN, "|", line)
end
end

function term(io::IO, f::Footnote, columns)
print(io, " "^margin, "| ")
print(io, " "^MARGIN, "| ")
with_output_format(:bold, print, io, "[^$(f.id)]")
println(io, "\n", " "^margin, "|")
println(io, "\n", " "^MARGIN, "|")
s = sprint(term, f.text, columns - 10)
for line in split(rstrip(s), "\n")
println(io, " "^margin, "|", line)
println(io, " "^MARGIN, "|", line)
end
end

function term(io::IO, md::List, columns)
for (i, point) in enumerate(md.items)
print(io, " "^2margin, isordered(md) ? "$(i + md.ordered - 1). " : "")
print_wrapped(io, width = columns-(4margin+2), pre = " "^(2margin+2),
i = 2margin+2) do io
print(io, " "^2MARGIN, isordered(md) ? "$(i + md.ordered - 1). " : "")
print_wrapped(io, width = columns-(4MARGIN+2), pre = " "^(2MARGIN+2),
i = 2MARGIN+2) do io
term(io, point, columns - 10)
end
end
Expand All @@ -63,14 +63,14 @@ end
function _term_header(io::IO, md, char, columns)
text = terminline(md.text)
with_output_format(:bold, io) do io
print(io, " "^(2margin), " ")
print(io, " "^(2MARGIN), " ")
line_no, lastline_width = print_wrapped(io, text,
width=columns - 4margin; pre=" ")
width=columns - 4MARGIN; pre=" ")
line_width = min(1 + lastline_width, columns)
if line_no > 1
line_width = max(line_width, div(columns, 3))
end
char != ' ' && println(io, " "^(2margin), string(char) ^ line_width)
char != ' ' && println(io, " "^(2MARGIN), string(char) ^ line_width)
end
end

Expand All @@ -85,7 +85,7 @@ end
function term(io::IO, md::Code, columns)
with_output_format(:cyan, io) do io
for line in lines(md.code)
print(io, " "^margin)
print(io, " "^MARGIN)
println(io, line)
end
end
Expand All @@ -96,7 +96,7 @@ function term(io::IO, br::LineBreak, columns)
end

function term(io::IO, br::HorizontalRule, columns)
println(io, " " ^ margin, "-" ^ (columns - 2margin))
println(io, " " ^ MARGIN, "-" ^ (columns - 2MARGIN))
end

term(io::IO, x, _) = show(io, MIME"text/plain"(), x)
Expand Down
28 changes: 14 additions & 14 deletions test/markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ end
@test md"###### h6" |> html == "<h6>h6</h6>\n"
@test md"####### h7" |> html == "<p>####### h7</p>\n"
@test md" >" |> html == "<blockquote>\n</blockquote>\n"
@test md"1. Hello" |> html == "<ol>\n<li><p>Hello</p>\n</li>\n</ol>\n"
@test md"* World" |> html == "<ul>\n<li><p>World</p>\n</li>\n</ul>\n"
@test md"1. Hello" |> html == "<ol>\n<li>Hello\n</li>\n</ol>\n"
@test md"* World" |> html == "<ul>\n<li>World\n</li>\n</ul>\n"
@test md"# title *blah*" |> html == "<h1>title <em>blah</em></h1>\n"
@test md"## title *blah*" |> html == "<h2>title <em>blah</em></h2>\n"
@test md"<https://julialang.org>" |> html == """<p><a href="https://julialang.org">https://julialang.org</a></p>\n"""
Expand Down Expand Up @@ -326,9 +326,9 @@ let out =
<h2>Section <em>important</em></h2>
<p>Some <strong>bolded</strong></p>
<ul>
<li><p>list1</p>
<li>list1
</li>
<li><p>list2</p>
<li>list2
</li>
</ul>
</div>"""
Expand Down Expand Up @@ -922,7 +922,7 @@ let text =
</li>
</ol>
<ul>
<li><p>one</p>
<li>one
</li>
</ul>
<p>two</p>
Expand All @@ -938,11 +938,11 @@ let text =
</li>
</ul>
<ol>
<li><p>foo</p>
<li>foo
</li>
<li><p>bar</p>
<li>bar
</li>
<li><p>baz</p>
<li>baz
</li>
</ol>
"""
Expand Down Expand Up @@ -1003,21 +1003,21 @@ let text =
let expected =
"""
<ol start="42">
<li><p>foo</p>
<li>foo
</li>
<li><p>bar</p>
<li>bar
</li>
</ol>
<ol>
<li><p>foo</p>
<li>foo
</li>
<li><p>bar</p>
<li>bar
</li>
</ol>
<ul>
<li><p>foo</p>
<li>foo
</li>
<li><p>bar</p>
<li>bar
</li>
</ul>
"""
Expand Down

0 comments on commit ad8c640

Please sign in to comment.