Skip to content

Commit

Permalink
Merge pull request #10483 from hayd/hr
Browse files Browse the repository at this point in the history
enh add HorizontalRule to markdown
  • Loading branch information
MikeInnes committed Mar 14, 2015
2 parents 6fbde92 + 6ebee73 commit 6856f9d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 6 deletions.
7 changes: 4 additions & 3 deletions base/markdown/Common/Common.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
include("block.jl")
include("inline.jl")

@flavor common [list, indentcode, blockquote, hashheader, paragraph,
@flavor common [list, indentcode, blockquote, hashheader, horizontalrule,
paragraph,

linebreak, escapes, en_dash, inline_code, asterisk_bold,
asterisk_italic, image, link]
linebreak, escapes, en_dash, inline_code,
asterisk_bold, asterisk_italic, image, link]
27 changes: 27 additions & 0 deletions base/markdown/Common/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,30 @@ function list(stream::IO, block::MD, config::Config)
return true
end
end

# ––––––––––––––
# HorizontalRule
# ––––––––––––––

type HorizontalRule
end

function horizontalrule(stream::IO, block::MD, config::Config)
withstream(stream) do
n, rule = 0, ' '
while !eof(stream)
char = read(stream, Char)
char == '\n' && break
isspace(char) && continue
if n==0 || char==rule
rule = char
n += 1
else
return false
end
end
is_hr = (n 3 && rule in "*-")
is_hr && push!(block, HorizontalRule())
return is_hr
end
end
6 changes: 3 additions & 3 deletions base/markdown/Julia/Julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ We start by borrowing GitHub's `fencedcode` extension – more to follow.
include("interp.jl")

@flavor julia [blocktex, blockinterp, hashheader, list, indentcode, fencedcode,
blockquote, github_table, paragraph,
blockquote, github_table, horizontalrule, paragraph,

linebreak, escapes, latex, interp, en_dash, inline_code, asterisk_bold,
asterisk_italic, image, link]
linebreak, escapes, latex, interp, en_dash, inline_code,
asterisk_bold, asterisk_italic, image, link]
4 changes: 4 additions & 0 deletions base/markdown/render/html.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ function html(io::IO, md::List)
end
end

function html(io::IO, md::HorizontalRule)
tag(io, :hr)
end

html(io::IO, x) = tohtml(io, x)

# Inline elements
Expand Down
4 changes: 4 additions & 0 deletions base/markdown/render/latex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ function writemime(io::IO, ::MIME"text/latex", md::List)
end
end

function writemime(io::IO, ::MIME"text/latex", md::HorizontalRule)
println(io, "\\rule{\\textwidth}{1pt}")
end

# Inline elements

function writemime(io::IO, ::MIME"text/latex", md::Plain)
Expand Down
4 changes: 4 additions & 0 deletions base/markdown/render/plain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function plain(io::IO, list::List)
end
end

function plain(io::IO, md::HorizontalRule)
println(io, "" ^ 3)
end

plain(io::IO, x) = tohtml(io, x)

# Inline elements
Expand Down
4 changes: 4 additions & 0 deletions base/markdown/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ function term(io::IO, br::LineBreak, columns)
println(io)
end

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

term(io::IO, x, _) = writemime(io, MIME"text/plain"(), x)

# Inline Content
Expand Down
8 changes: 8 additions & 0 deletions test/markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ foo
@test md"#title" |> plain == "# title\n"
@test md"## section" |> plain == "## section\n"
@test md"## section `foo`" |> plain == "## section `foo`\n"
@test md"""Hello
---
World""" |> plain == "Hello\n\n–––\n\nWorld\n"

# HTML output

Expand All @@ -50,6 +54,10 @@ foo
@test md"* World" |> html == "<ul><li>World</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"""Hello
---
World""" |> html == "<p>Hello</p>\n<hr />\n<p>World</p>\n"

# Interpolation / Custom types

Expand Down

0 comments on commit 6856f9d

Please sign in to comment.