| 1 | import materialx.emoji |
| 2 | from markdown import markdown |
| 3 | |
| 4 | def define_env(env): |
| 5 | @env.macro |
| 6 | def codebox(title = None, language = "", filepath = None, contents = ""): |
| 7 | if title is not None: |
| 8 | if filepath is not None: |
| 9 | editLink = f"""{env.variables['config']['repo_url']}tree/{env.variables['config']['edit_uri'].split("/")[1]}/snippets/{filepath}""" |
| 10 | icon = markdown(':material-link:', |
| 11 | extensions=['pymdownx.emoji'], |
| 12 | extension_configs={ |
| 13 | 'pymdownx.emoji': { |
| 14 | 'emoji_index': materialx.emoji.twemoji, |
| 15 | 'emoji_generator': materialx.emoji.to_svg |
| 16 | } |
| 17 | } |
| 18 | ).replace('<p>', '').replace('</p>', '') |
| 19 | |
| 20 | return f""" |
| 21 | <div class="titledCodeBox"> |
| 22 | <div class="codeBoxTitle"> |
| 23 | <code>{title}</code> |
| 24 | <a class="codeBoxTitleGitHubLink" href="{editLink}" title="View on GitHub">{icon}</a> |
| 25 | </div> |
| 26 | ```{language} |
| 27 | --8<-- "{filepath}" |
| 28 | ``` |
| 29 | </div> |
| 30 | """ |
| 31 | else: |
| 32 | return f""" |
| 33 | <div class="titledCodeBox"> |
| 34 | <div class="codeBoxTitle"><code>{title}</code></div> |
| 35 | ```{language} |
| 36 | {contents} |
| 37 | ``` |
| 38 | </div> |
| 39 | """ |
| 40 | else: |
| 41 | if filepath is not None: |
| 42 | return f""" |
| 43 | ```{language} |
| 44 | --8<-- "{filepath}" |
| 45 | ``` |
| 46 | """ |
| 47 | else: |
| 48 | return f""" |
| 49 | ```{language} |
| 50 | {contents} |
| 51 | ``` |
| 52 | """ |