Remove bogus `.md` in PHP code examples
[GitHub/WoltLab/woltlab.github.io.git] / main.py
diff --git a/main.py b/main.py
index 7867e5ac13ceb518cccf9ff87cff22869456d04a..8e816f7deb209b6ad3c7c51f8e5ae7faf67fe1a2 100644 (file)
--- a/main.py
+++ b/main.py
@@ -1,18 +1,52 @@
+import materialx.emoji
+from markdown import markdown
+
 def define_env(env):
     @env.macro
-    def codebox(language, filepath, title = ""):
-        if title is not "":
-            return f"""
+    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"""
 <div class="titledCodeBox">
-    <div class="codeBoxTitle"><code>{title}</code></div>
+    <div class="codeBoxTitle">
+        <code>{title}</code>
+        <a class="codeBoxTitleGitHubLink" href="{editLink}" title="View on GitHub">{icon}</a>
+    </div>
     ```{language}
     --8<-- "{filepath}"
     ```
 </div>
+"""
+            else:
+                return f"""
+<div class="titledCodeBox">
+    <div class="codeBoxTitle"><code>{title}</code></div>
+```{language}
+{contents}
+```
+</div>
 """
         else:
-            return f"""
+            if filepath is not None:
+                return f"""
 ```{language}
 --8<-- "{filepath}"
 ```
+"""
+            else:
+                return f"""
+```{language}
+{contents}
+```
 """