Clean up codebox macro implementation
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 16 Dec 2021 14:35:53 +0000 (15:35 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 16 Dec 2021 14:38:55 +0000 (15:38 +0100)
main.py

diff --git a/main.py b/main.py
index 0bc8a53ae608a2fb39905be7940f3afeae78ebb9..de0d857461b710499541b0db5a3cd61d50c191fb 100644 (file)
--- a/main.py
+++ b/main.py
@@ -3,41 +3,20 @@ from markdown import markdown
 
 def define_env(env):
     @env.macro
-    def codebox(title = None, language = "", filepath = None, contents = ""):
-        if title is not None:
-            if filepath is not None:
-                editLink = f"""{env.variables['config']['repo_url']}tree/{env.variables['config']['edit_uri'].split("/")[1]}/snippets/{filepath}"""
-                icon = markdown(':material-link:',
-                    extensions=['pymdownx.emoji'],
-                    extension_configs={
-                        'pymdownx.emoji': {
-                            'emoji_index': materialx.emoji.twemoji,
-                            'emoji_generator': materialx.emoji.to_svg
-                        }
-                    }
-                ).replace('<p>', '').replace('</p>', '')
-                
-                return f"""
+    def codebox(*, title, language, filepath):
+        editLink = f"""{env.variables['config']['repo_url']}tree/{env.variables['config']['edit_uri'].split("/")[1]}/snippets/{filepath}"""
+        icon = markdown(':material-link:',
+            extensions=['pymdownx.emoji'],
+            extension_configs={
+                'pymdownx.emoji': {
+                    'emoji_index': materialx.emoji.twemoji,
+                    'emoji_generator': materialx.emoji.to_svg
+                }
+            }
+        ).replace('<p>', '').replace('</p>', '')
+
+        return f"""
 ```{language} title='{title} <a class="codeBoxTitleGitHubLink" href="{editLink}" title="View on GitHub">{icon}</a>'
 --8<-- "{filepath}"
 ```
-"""
-            else:
-                return f"""
-```{language} title="{title}"
-{contents}
-```
-"""
-        else:
-            if filepath is not None:
-                return f"""
-```{language}
---8<-- "{filepath}"
-```
-"""
-            else:
-                return f"""
-```{language}
-{contents}
-```
 """