Skip to content

Commit

Permalink
Better backtick behaviour
Browse files Browse the repository at this point in the history
This extends the backtick syntax in the markdown parser to
generalise the "one tick = code" and "two ticks = math" rule
to "odd ticks = code" and "even ticks = math".

Doing this regains the nesting behaviour of backticks and allows
both inline code and math to contain arbitrary sequences of backticks
so long as enough backticks are used to wrap the code/math.
  • Loading branch information
MichaelHatherly committed Jun 8, 2016
1 parent 11e4031 commit c8629b7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion base/markdown/Common/Common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ include("inline.jl")
@flavor common [list, indentcode, blockquote, hashheader, horizontalrule,
paragraph,

linebreak, escapes, inline_tex, inline_code,
linebreak, escapes, inline_code,
asterisk_bold, asterisk_italic, image, footnote, link]
26 changes: 18 additions & 8 deletions base/markdown/Common/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@ end

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

@trigger '`' ->
function inline_tex(stream::IO, md::MD)
result = parse_inline_wrapper(stream, "``"; rep=true)
return result === nothing ? nothing : LaTeX(result)
withstream(stream) do
ticks = startswith(stream, r"^(`+)")
result = readuntil(stream, ticks)
if result === nothing
nothing
else
result = strip(result)
# An odd number of backticks wrapping the text will produce a `Code` node, while
# an even number will result in a `LaTeX` node. This allows for arbitary
# backtick combinations to be embedded inside the resulting node, i.e.
#
# `a`, ``a``, `` `a` ``, ``` ``a`` ```, ``` `a` ```, etc.
# ^ ^ ^ ^ ^
# C L L C C with C=Code and L=LaTeX.
#
isodd(length(ticks)) ? Code(result) : LaTeX(result)
end
end
end

# ––––––––––––––
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/GitHub/GitHub.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ end
@flavor github [list, indentcode, blockquote, fencedcode, hashheader,
github_table, github_paragraph,

linebreak, escapes, en_dash, inline_tex, inline_code, asterisk_bold,
linebreak, escapes, en_dash, inline_code, asterisk_bold,
asterisk_italic, image, footnote, link]
2 changes: 1 addition & 1 deletion base/markdown/Julia/Julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ include("interp.jl")
@flavor julia [blocktex, blockinterp, hashheader, list, indentcode, fencedcode,
blockquote, github_table, horizontalrule, setextheader, paragraph,

linebreak, escapes, tex, interp, en_dash, inline_tex, inline_code,
linebreak, escapes, tex, interp, en_dash, inline_code,
asterisk_bold, asterisk_italic, image, footnote, link]
54 changes: 54 additions & 0 deletions test/markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,57 @@ let in_dollars =
@test latex_doc == dollars
@test latex_doc == backticks
end

# Nested backticks for inline code and math.

let t_1 = "`code` ``math`` ```code``` ````math```` `````code`````",
t_2 = "`` `math` `` ``` `code` ``code`` ``` ```` `math` ``math`` ```math``` ````",
t_3 = "`` ` `` ``` `` ` `` ` ` ```",
t_4 = """`code
over several
lines` ``math
over several
lines`` ``math with
` some extra ` ` backticks`
``""",
t_5 = "``code at end of string`",
t_6 = "```math at end of string``"
@test Markdown.parse(t_1) == MD(Paragraph([
Code("code"),
" ",
LaTeX("math"),
" ",
Code("code"),
" ",
LaTeX("math"),
" ",
Code("code"),
]))
@test Markdown.parse(t_2) == MD(Paragraph([
LaTeX("`math`"),
" ",
Code("`code` ``code``"),
" ",
LaTeX("`math` ``math`` ```math```"),
]))
@test Markdown.parse(t_3) == MD(Paragraph([
LaTeX("`"),
" ",
Code("`` ` `` ` `"),
]))
@test Markdown.parse(t_4) == MD(Paragraph([
Code("code over several lines"),
" ",
LaTeX("math over several lines"),
" ",
LaTeX("math with ` some extra ` ` backticks`")
]))
@test Markdown.parse(t_5) == MD(Paragraph([
"`",
Code("code at end of string"),
]))
@test Markdown.parse(t_6) == MD(Paragraph([
"`",
LaTeX("math at end of string"),
]))
end

0 comments on commit c8629b7

Please sign in to comment.