Skip to content

Commit 40d266f

Browse files
committed
Add simple plugin test case
1 parent ca174ca commit 40d266f

File tree

8 files changed

+121
-94
lines changed

8 files changed

+121
-94
lines changed
File renamed without changes.

codeinclude/plugin.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import re
2+
import os
3+
import shlex
4+
import textwrap
5+
6+
from mkdocs.plugins import BasePlugin
7+
from codeinclude.resolver import select
8+
9+
RE_SNIPPET = r"""(?x)
10+
^
11+
(?P<leading_space>\s*)
12+
(?P<marker><!--codeinclude-->)
13+
(?P<ignored_space>\s*)
14+
\[(?P<title>[^\]]*)\]\((?P<filename>[^)]+)\)
15+
([\t ]+(?P<params>.*))?
16+
$
17+
"""
18+
19+
20+
def get_substitute(page, title, filename, lines, block, inside_block):
21+
22+
page_parent_dir = os.path.dirname(page.file.abs_src_path)
23+
import_path = os.path.join(page_parent_dir, filename)
24+
with open(import_path) as f:
25+
content = f.read()
26+
27+
selected_content = select(
28+
content, lines=lines, block=block, inside_block=inside_block
29+
)
30+
31+
dedented = textwrap.dedent(selected_content)
32+
33+
return '\n```java tab="' + title + '"\n' + dedented + "\n```\n\n"
34+
35+
36+
class CodeIncludePlugin(BasePlugin):
37+
def on_page_markdown(self, markdown, page, config, site_navigation=None, **kwargs):
38+
"Provide a hook for defining functions from an external module"
39+
40+
results = ""
41+
for line in markdown.splitlines():
42+
43+
# handle each line
44+
snippet_match = re.match(RE_SNIPPET, line)
45+
if snippet_match:
46+
title = snippet_match.group("title")
47+
filename = snippet_match.group("filename")
48+
indent = snippet_match.group("leading_space")
49+
raw_params = snippet_match.group("params")
50+
51+
if raw_params:
52+
params = dict(token.split(":") for token in shlex.split(raw_params))
53+
lines = params.get("lines", "")
54+
block = params.get("block", "")
55+
inside_block = params.get("inside_block", "")
56+
else:
57+
lines = ""
58+
block = ""
59+
inside_block = ""
60+
61+
code_block = get_substitute(
62+
page, title, filename, lines, block, inside_block
63+
)
64+
65+
# re-indent
66+
code_block = re.sub("^", indent, code_block, flags=re.MULTILINE)
67+
results += code_block
68+
else:
69+
results += line + "\n"
70+
71+
return results
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@ def select(
8383
result += source_lines[i - 1] + "\n"
8484
last_selected = i
8585

86+
if result == "":
87+
return text
88+
8689
return result

src/codeinclude/plugin.py

Lines changed: 0 additions & 92 deletions
This file was deleted.

tests/codeinclude/test_plugin.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import textwrap
3+
import unittest
4+
5+
from mkdocs.config import Config, DEFAULT_SCHEMA
6+
from mkdocs.structure.files import File
7+
from mkdocs.structure.pages import Page
8+
9+
from codeinclude.plugin import CodeIncludePlugin
10+
11+
MARKDOWN_EXAMPLE = """
12+
# hello world
13+
14+
some text before
15+
<!--codeinclude--> [foo](Foo.java)
16+
and some text after
17+
18+
"""
19+
20+
c = Config(schema=DEFAULT_SCHEMA)
21+
c["site_url"] = "http://example.org/"
22+
23+
PAGE_EXAMPLE = Page("", File(os.path.abspath("./fixture/text.md"), "/src", "/dest", False), c)
24+
25+
class PluginTextCase(unittest.TestCase):
26+
27+
def test_simple_case(self):
28+
plugin = CodeIncludePlugin()
29+
result = plugin.on_page_markdown(MARKDOWN_EXAMPLE, PAGE_EXAMPLE, dict())
30+
31+
print(result)
32+
self.assertEqual(result.strip(), textwrap.dedent("""
33+
# hello world
34+
35+
some text before
36+
37+
```java tab=\"foo\"
38+
public class Foo {
39+
40+
}
41+
```
42+
43+
and some text after
44+
""").strip())

tests/codeinclude/test_resolver.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import unittest
2-
31
from codeinclude.resolver import select
42

53
CODE_BLOCK_EXAMPLE = """

tests/fixture/Foo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class Foo {
2+
3+
}
File renamed without changes.

0 commit comments

Comments
 (0)