Skip to content

Commit

Permalink
Implement GitHub-style syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Dec 18, 2023
1 parent 8d2b4d1 commit a679a9f
Show file tree
Hide file tree
Showing 62 changed files with 501 additions and 3 deletions.
53 changes: 53 additions & 0 deletions markdown_callouts/github_callouts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from __future__ import annotations

import re
import xml.etree.ElementTree as etree

from markdown import Markdown, util
from markdown.blockprocessors import BlockQuoteProcessor
from markdown.extensions import Extension


# Based on https://github.com/Python-Markdown/markdown/blob/4acb949256adc535d6e6cd84c4fb47db8dda2f46/markdown/blockprocessors.py#L277
class _GitHubCalloutsBlockProcessor(BlockQuoteProcessor):
REGEX = re.compile(
r"((?:^|\n) *(?:[^>].*)?(?:^|\n)) {0,3}> *\[!([A-Za-z]{3,})\] *\n(?: *> *\n)*() *(?:> *[^\s\n]|[^\s\n>])"
)

def test(self, parent, block):
return (
bool(self.REGEX.search(block))
and not self.parser.state.isstate("blockquote")
and not util.nearing_recursion_limit()
)

def run(self, parent: etree.Element, blocks: list[str]) -> None:
block = blocks.pop(0)
m = self.REGEX.search(block)
assert m

before = block[: m.end(1)]
block = "\n".join(self.clean(line) for line in block[m.end(3) :].split("\n"))
self.parser.parseBlocks(parent, [before])
kind = m[2]

admon = etree.SubElement(parent, "div", {"class": "admonition " + kind.lower()})
title = etree.SubElement(admon, "p", {"class": "admonition-title"})
title.text = kind.title()

self.parser.state.set("blockquote")
self.parser.parseChunk(admon, block)
self.parser.state.reset()


class GitHubCalloutsExtension(Extension):
def extendMarkdown(self, md: Markdown) -> None:
parser = md.parser # type: ignore
parser.blockprocessors.register(
_GitHubCalloutsBlockProcessor(parser),
"github-callouts",
21.1, # Right before blockquote
)


makeExtension = GitHubCalloutsExtension
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ History = "https://github.com/oprypin/markdown-callouts/releases"

[project.entry-points."markdown.extensions"]
callouts = "markdown_callouts.callouts:CalloutsExtension"
github-callouts = "markdown_callouts.github_callouts:GitHubCalloutsExtension"

[tool.hatch.version]
path = "markdown_callouts/__init__.py"
Expand Down
22 changes: 22 additions & 0 deletions tests/all/adjacent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
input: |
> [!Note]
> foo
NOTE: bar
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
foo
</p>
</div>
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
bar
</p>
</div>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions tests/github/adjacent-nope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
input: |
> [!Note]
> foo
> [!Note]
> bar
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
foo
[!Note]
bar
</p>
</div>
18 changes: 18 additions & 0 deletions tests/github/adjacent-text-block.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
input: |
aaa
> [!Note]
> bbb
ccc
output: |-
<p>
aaa
</p>
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
bbb
ccc
</p>
</div>
14 changes: 14 additions & 0 deletions tests/github/adjacent-text-nope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
input: |
> aaa
> [!Note]
> bbb
> ccc
output: |-
<blockquote>
<p>
aaa
[!Note]
bbb
ccc
</p>
</blockquote>
23 changes: 23 additions & 0 deletions tests/github/adjacent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
input: |
> [!Note]
> foo
> [!Note]
> bar
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
foo
</p>
</div>
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
bar
</p>
</div>
25 changes: 25 additions & 0 deletions tests/github/barely-callout.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
input: |
> [!Warning]
a
<!-- -->
> [!Warning]
>>
output: |-
<div class="admonition warning">
<p class="admonition-title">
Warning
</p>
<p>
a
</p>
</div>
<!-- -->
<div class="admonition warning">
<p class="admonition-title">
Warning
</p>
<blockquote>
</blockquote>
</div>
12 changes: 12 additions & 0 deletions tests/github/basic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
input: |
> [!Note]
> Hello, world!
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
Hello, world!
</p>
</div>
32 changes: 32 additions & 0 deletions tests/github/blockquotes-many-joined.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
input: |
> [!Note]
> Foo.
> a
> Bar.
> [!Warning]
> Baz.
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
Foo.
</p>
</div>
<blockquote>
<p>
a
Bar.
</p>
</blockquote>
<div class="admonition warning">
<p class="admonition-title">
Warning
</p>
<p>
Baz.
</p>
</div>
34 changes: 34 additions & 0 deletions tests/github/blockquotes-split-by-comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
input: |
> [!Note]
> Foo.
<!-- -->
> a
> Bar.
<!-- -->
> [!Warning]
> Baz.
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
Foo.
</p>
</div>
<!-- -->
<blockquote>
<p>
a
Bar.
</p>
</blockquote>
<!-- -->
<div class="admonition warning">
<p class="admonition-title">
Warning
</p>
<p>
Baz.
</p>
</div>
26 changes: 26 additions & 0 deletions tests/github/blockquotes-together.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
input: |
> [!Note]
> Foo.
>
> a
> Bar.
>
> [!Warning]
> Baz.
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
Foo.
</p>
<p>
a
Bar.
</p>
<p>
[!Warning]
Baz.
</p>
</div>
10 changes: 10 additions & 0 deletions tests/github/empty-nope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
input: |
> [!Warning]
>
>
output: |-
<blockquote>
<p>
[!Warning]
</p>
</blockquote>
24 changes: 24 additions & 0 deletions tests/github/extra-space.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
input: |
> [!Note]
> foo
<!-- -->
> [!Note]
> foo
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
foo
</p>
</div>
<!-- -->
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
foo
</p>
</div>
28 changes: 28 additions & 0 deletions tests/github/just-callout-nope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
input: |
> [!Warning]
hi
> [!Warning]
>
<!-- -->
>[!Warning]
output: |-
<blockquote>
<p>
[!Warning]
</p>
</blockquote>
<p>
hi
</p>
<blockquote>
<p>
[!Warning]
</p>
</blockquote>
<!-- -->
<blockquote>
<p>
[!Warning]
</p>
</blockquote>
16 changes: 16 additions & 0 deletions tests/github/linebreak-after.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
input: |
> [!Note]
>
> Body.
> More.
output: |-
<div class="admonition note">
<p class="admonition-title">
Note
</p>
<p>
Body.
<br/>
More.
</p>
</div>
11 changes: 11 additions & 0 deletions tests/github/lowercase-nope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
input: |
> **note**: foo
output: |-
<blockquote>
<p>
<strong>
note
</strong>
: foo
</p>
</blockquote>
Loading

0 comments on commit a679a9f

Please sign in to comment.