diff --git a/codeinclude/plugin.py b/codeinclude/plugin.py index f1cb423..eb88751 100644 --- a/codeinclude/plugin.py +++ b/codeinclude/plugin.py @@ -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: @@ -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} ``` diff --git a/tests/codeinclude/test_plugin.py b/tests/codeinclude/test_plugin.py index 9e7e01e..b1748bf 100644 --- a/tests/codeinclude/test_plugin.py +++ b/tests/codeinclude/test_plugin.py @@ -31,6 +31,17 @@ """ +EMPTY_TITLE_MARKDOWN_EXAMPLE = """ +# hello world + +some text before + +[](Foo.java) + +and some text after + +""" + c = Config(schema=DEFAULT_SCHEMA) c["site_url"] = "http://example.org/" @@ -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())