Skip to content

Commit

Permalink
Allow empty title:
Browse files Browse the repository at this point in the history
Allow empty title for code snippets. It may
be useful for short code fragments.
  • Loading branch information
dmitry-timofeev committed Feb 17, 2020
1 parent bb93f54 commit 5363117
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
11 changes: 9 additions & 2 deletions codeinclude/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@


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

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

dedented = textwrap.dedent(selected_content)
lang_code = get_lang_class(filename)

return f'''
```{lang_code} tab="{title}"
```{header}
{dedented}
```
Expand Down
31 changes: 31 additions & 0 deletions tests/codeinclude/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
"""

EMPTY_TITLE_MARKDOWN_EXAMPLE = """
# hello world
some text before
<!--codeinclude-->
[](Foo.java)
<!--/codeinclude-->
and some text after
"""

c = Config(schema=DEFAULT_SCHEMA)
c["site_url"] = "http://example.org/"

Expand Down Expand Up @@ -85,3 +96,23 @@ def test_multi_tab_case(self):
and some text after
""").strip(),
result.strip())

def test_empty_title_case(self):
plugin = CodeIncludePlugin()
result = plugin.on_page_markdown(EMPTY_TITLE_MARKDOWN_EXAMPLE, PAGE_EXAMPLE, dict())

print(result)
self.assertEqual(textwrap.dedent("""
# hello world
some text before
```java
public class Foo {
}
```
and some text after
""").strip(),
result.strip())

0 comments on commit 5363117

Please sign in to comment.