Skip to content

The inline code lexer now copes with internal backticks. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/pygments_markdown_lexer/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Markdown(object):
Markup = Keyword
Heading = Generic.Heading
SubHeading = Generic.Heading
InlineCode = Token.Literal.String.Backtick
CodeBlock = Comment.Preproc
HtmlSingle = Comment.Single
HtmlBlock = Comment.Preproc
Expand Down Expand Up @@ -112,7 +113,8 @@ class MarkdownLexer(RegexLexer):
(r'&', Text),

# Inline code
(r'``?', Markdown.Markup, state('literal')),
(r'(`+) ?(.*?) ?(\1)',
bygroups(Markdown.Markup, Markdown.InlineCode, Markdown.Markup)),

# Emphasis
(r'_?_[ \n]', Text), # whitespace escape
Expand Down Expand Up @@ -141,10 +143,6 @@ class MarkdownLexer(RegexLexer):
(r'[a-zA-Z0-9]+', Text), # optimize normal words a little
(r'.', Text), # default fallback
],
state('literal'): [
(r'[^`]+', String.Backtick),
(r'(?<!\\)``?' + end_string_suffix, Markdown.Markup, state('#pop')),
],
state('htmlblock'): [ # TODO: delegate to HTML lexer
(r'^</[^>]+>\n', Markdown.HtmlBlock, state('#pop')),
(r'.*\n', Markdown.HtmlBlock), # slurp boring text
Expand Down
8 changes: 8 additions & 0 deletions src/tests/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,11 @@ def test_lists_and_blockquotes():
(Markdown.Markup, '\t' + lead + ' '),
(Text, 'Lorem ipsum.\n'),
)

def test_inline_code():
check(
(Markdown.Markup, r'``'),
(Markdown.InlineCode, 'There is a literal backtick (`) here.'),
(Markdown.Markup, r'``'),
(Text, '\n'),
)