Link to source code file on GitHub in code boxes with filenames (#166)
[GitHub/WoltLab/woltlab.github.io.git] / main.py
CommitLineData
bcc4cb57
MS
1import materialx.emoji
2from markdown import markdown
3
3c732517
MS
4def define_env(env):
5 @env.macro
f778fce2
MS
6 def codebox(title = None, language = "", filepath = None, contents = ""):
7 if title is not None:
8 if filepath is not None:
bcc4cb57
MS
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
f778fce2 20 return f"""
3c732517 21<div class="titledCodeBox">
bcc4cb57
MS
22 <div class="codeBoxTitle">
23 <code>{title}</code>
24 <a class="codeBoxTitleGitHubLink" href="{editLink}" title="View on GitHub">{icon}</a>
25 </div>
3c732517
MS
26 ```{language}
27 --8<-- "{filepath}"
28 ```
29</div>
f778fce2
MS
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>
3c732517
MS
39"""
40 else:
f778fce2
MS
41 if filepath is not None:
42 return f"""
3c732517
MS
43```{language}
44--8<-- "{filepath}"
45```
f778fce2
MS
46"""
47 else:
48 return f"""
49```{language}
50{contents}
51```
3c732517 52"""