Skip to content

Commit

Permalink
Merge pull request #9852 from hayd/markdown_indent
Browse files Browse the repository at this point in the history
Use 4 space indent in Markdown
  • Loading branch information
ivarne committed Jan 20, 2015
2 parents 6bc53e6 + e23a192 commit 39a498b
Show file tree
Hide file tree
Showing 14 changed files with 598 additions and 599 deletions.
184 changes: 92 additions & 92 deletions base/markdown/Common/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,123 +3,123 @@
# ––––––––––

type Paragraph
content
content
end

Paragraph() = Paragraph([])

function paragraph(stream::IO, md::MD, config::Config)
buffer = IOBuffer()
p = Paragraph()
push!(md, p)
skipwhitespace(stream)
while !eof(stream)
char = read(stream, Char)
if char == '\n' || char == '\r'
if blankline(stream) || parse(stream, md, config, breaking = true)
break
else
write(buffer, ' ')
end
else
write(buffer, char)
buffer = IOBuffer()
p = Paragraph()
push!(md, p)
skipwhitespace(stream)
while !eof(stream)
char = read(stream, Char)
if char == '\n' || char == '\r'
if blankline(stream) || parse(stream, md, config, breaking = true)
break
else
write(buffer, ' ')
end
else
write(buffer, char)
end
end
end
p.content = parseinline(seek(buffer, 0), config)
return true
p.content = parseinline(seek(buffer, 0), config)
return true
end

# –––––––
# Headers
# –––––––

type Header{level}
text
text
end

Header(s, level::Int) = Header{level}(s)
Header(s) = Header(s, 1)

@breaking true ->
function hashheader(stream::IO, md::MD, config::Config)
startswith(stream, "#") || return false
level = 1
while startswith(stream, "#")
level += 1
end
h = readline(stream) |> chomp
h = match(r"\s*(.*)(?<![#\s])", h).captures[1]
buffer = IOBuffer()
print(buffer, h)
if !isempty(h)
push!(md.content, Header(parseinline(seek(buffer, 0), config), level))
return true
else
return false
end
startswith(stream, "#") || return false
level = 1
while startswith(stream, "#")
level += 1
end
h = readline(stream) |> chomp
h = match(r"\s*(.*)(?<![#\s])", h).captures[1]
buffer = IOBuffer()
print(buffer, h)
if !isempty(h)
push!(md.content, Header(parseinline(seek(buffer, 0), config), level))
return true
else
return false
end
end

# ––––
# Code
# ––––

type Code
language::UTF8String
code::UTF8String
language::UTF8String
code::UTF8String
end

Code(code) = Code("", code)

function indentcode(stream::IO, block::MD, config::Config)
withstream(stream) do
buffer = IOBuffer()
while startswith(stream, " ") || startswith(stream, "\t")
write(buffer, readline(stream))
withstream(stream) do
buffer = IOBuffer()
while startswith(stream, " ") || startswith(stream, "\t")
write(buffer, readline(stream))
end
code = takebuf_string(buffer)
!isempty(code) && (push!(block, Code(chomp(code))); return true)
return false
end
code = takebuf_string(buffer)
!isempty(code) && (push!(block, Code(chomp(code))); return true)
return false
end
end

# ––––––
# Quotes
# ––––––

type BlockQuote
content
content
end

BlockQuote() = BlockQuote([])

# TODO: Laziness
@breaking true ->
function blockquote(stream::IO, block::MD, config::Config)
withstream(stream) do
buffer = IOBuffer()
while startswith(stream, ">")
startswith(stream, " ")
write(buffer, readline(stream))
end
md = takebuf_string(buffer)
if !isempty(md)
push!(block, BlockQuote(parse(md, flavor = config).content))
return true
else
return false
withstream(stream) do
buffer = IOBuffer()
while startswith(stream, ">")
startswith(stream, " ")
write(buffer, readline(stream))
end
md = takebuf_string(buffer)
if !isempty(md)
push!(block, BlockQuote(parse(md, flavor = config).content))
return true
else
return false
end
end
end
end

# –––––
# Lists
# –––––

type List
items::Vector{Any}
ordered::Bool
items::Vector{Any}
ordered::Bool

List(x::AbstractVector) = new(x)
List(x::AbstractVector) = new(x)
end

List(xs...) = List([xs...])
Expand All @@ -128,39 +128,39 @@ const bullets = ["* ", "• ", "+ ", "- "]

# Todo: ordered lists, inline formatting
function list(stream::IO, block::MD, config::Config)
withstream(stream) do
skipwhitespace(stream)
startswith(stream, bullets) || return false
the_list = List()
buffer = IOBuffer()
fresh_line = false
while !eof(stream)
if fresh_line
withstream(stream) do
skipwhitespace(stream)
if startswith(stream, bullets)
push!(the_list.items, parseinline(takebuf_string(buffer), config))
buffer = IOBuffer()
else
write(buffer, ' ')
end
startswith(stream, bullets) || return false
the_list = List()
buffer = IOBuffer()
fresh_line = false
else
c = read(stream, Char)
if c == '\n'
eof(stream) && break
next = peek(stream)
if next == '\n'
break
else
fresh_line = true
end
else
write(buffer, c)
while !eof(stream)
if fresh_line
skipwhitespace(stream)
if startswith(stream, bullets)
push!(the_list.items, parseinline(takebuf_string(buffer), config))
buffer = IOBuffer()
else
write(buffer, ' ')
end
fresh_line = false
else
c = read(stream, Char)
if c == '\n'
eof(stream) && break
next = peek(stream)
if next == '\n'
break
else
fresh_line = true
end
else
write(buffer, c)
end
end
end
end
push!(the_list.items, parseinline(takebuf_string(buffer), config))
push!(block, the_list)
return true
end
push!(the_list.items, parseinline(takebuf_string(buffer), config))
push!(block, the_list)
return true
end
end
78 changes: 39 additions & 39 deletions base/markdown/Common/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
# ––––––––

type Italic
text
text
end

@trigger '*' ->
function asterisk_italic(stream::IO)
result = parse_inline_wrapper(stream, "*")
return result == nothing ? nothing : Italic(parseinline(result))
result = parse_inline_wrapper(stream, "*")
return result == nothing ? nothing : Italic(parseinline(result))
end

type Bold
text
text
end

@trigger '*' ->
function asterisk_bold(stream::IO)
result = parse_inline_wrapper(stream, "**")
return result == nothing ? nothing : Bold(parseinline(result))
result = parse_inline_wrapper(stream, "**")
return result == nothing ? nothing : Bold(parseinline(result))
end

# ––––
Expand All @@ -28,50 +28,50 @@ end

@trigger '`' ->
function inline_code(stream::IO)
result = parse_inline_wrapper(stream, "`")
return result == nothing ? nothing : Code(result)
result = parse_inline_wrapper(stream, "`")
return result == nothing ? nothing : Code(result)
end

# ––––––––––––––
# Images & Links
# ––––––––––––––

type Image
url::UTF8String
alt::UTF8String
url::UTF8String
alt::UTF8String
end

@trigger '!' ->
function image(stream::IO)
withstream(stream) do
startswith(stream, "![") || return
alt = readuntil(stream, ']', match = '[')
alt nothing && return
skipwhitespace(stream)
startswith(stream, '(') || return
url = readuntil(stream, ')', match = '(')
url nothing && return
return Image(url, alt)
end
withstream(stream) do
startswith(stream, "![") || return
alt = readuntil(stream, ']', match = '[')
alt nothing && return
skipwhitespace(stream)
startswith(stream, '(') || return
url = readuntil(stream, ')', match = '(')
url nothing && return
return Image(url, alt)
end
end

type Link
text
url::UTF8String
text
url::UTF8String
end

@trigger '[' ->
function link(stream::IO)
withstream(stream) do
startswith(stream, '[') || return
text = readuntil(stream, ']', match = '[')
text nothing && return
skipwhitespace(stream)
startswith(stream, '(') || return
url = readuntil(stream, ')', match = '(')
url nothing && return
return Link(parseinline(text), url)
end
withstream(stream) do
startswith(stream, '[') || return
text = readuntil(stream, ']', match = '[')
text nothing && return
skipwhitespace(stream)
startswith(stream, '(') || return
url = readuntil(stream, ')', match = '(')
url nothing && return
return Link(parseinline(text), url)
end
end

# –––––––––––
Expand All @@ -80,18 +80,18 @@ end

@trigger '-' ->
function en_dash(stream::IO)
if startswith(stream, "--")
return ""
end
if startswith(stream, "--")
return ""
end
end

const escape_chars = "\\`*_#+-.!{[("

@trigger '\\' ->
function escapes(stream::IO)
withstream(stream) do
if startswith(stream, "\\") && !eof(stream) && (c = read(stream, Char)) in escape_chars
return string(c)
withstream(stream) do
if startswith(stream, "\\") && !eof(stream) && (c = read(stream, Char)) in escape_chars
return string(c)
end
end
end
end

0 comments on commit 39a498b

Please sign in to comment.