Skip to content

Commit 5363117

Browse files
Allow empty title:
Allow empty title for code snippets. It may be useful for short code fragments.
1 parent bb93f54 commit 5363117

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

codeinclude/plugin.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@
3434

3535

3636
def get_substitute(page, title, filename, lines, block, inside_block):
37+
# Compute the fence header
38+
lang_code = get_lang_class(filename)
39+
header = lang_code
40+
title = title.strip()
41+
if len(title) > 0:
42+
header += f' tab="{title}"'
3743

44+
# Select the code content
3845
page_parent_dir = os.path.dirname(page.file.abs_src_path)
3946
import_path = os.path.join(page_parent_dir, filename)
4047
with open(import_path) as f:
@@ -45,9 +52,9 @@ def get_substitute(page, title, filename, lines, block, inside_block):
4552
)
4653

4754
dedented = textwrap.dedent(selected_content)
48-
lang_code = get_lang_class(filename)
55+
4956
return f'''
50-
```{lang_code} tab="{title}"
57+
```{header}
5158
{dedented}
5259
```
5360

tests/codeinclude/test_plugin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
3232
"""
3333

34+
EMPTY_TITLE_MARKDOWN_EXAMPLE = """
35+
# hello world
36+
37+
some text before
38+
<!--codeinclude-->
39+
[](Foo.java)
40+
<!--/codeinclude-->
41+
and some text after
42+
43+
"""
44+
3445
c = Config(schema=DEFAULT_SCHEMA)
3546
c["site_url"] = "http://example.org/"
3647

@@ -85,3 +96,23 @@ def test_multi_tab_case(self):
8596
and some text after
8697
""").strip(),
8798
result.strip())
99+
100+
def test_empty_title_case(self):
101+
plugin = CodeIncludePlugin()
102+
result = plugin.on_page_markdown(EMPTY_TITLE_MARKDOWN_EXAMPLE, PAGE_EXAMPLE, dict())
103+
104+
print(result)
105+
self.assertEqual(textwrap.dedent("""
106+
# hello world
107+
108+
some text before
109+
110+
```java
111+
public class Foo {
112+
113+
}
114+
```
115+
116+
and some text after
117+
""").strip(),
118+
result.strip())

0 commit comments

Comments
 (0)