1
- ## File based on https://github.com/processone/docs.ejabberd.im/blob/master/shortcodes.py and
2
- ## https://github.com/squidfunk/mkdocs-material/tree/master/material/overrides/hooks
3
-
4
1
# Copyright (c) 2016-2024 Martin Donath <martin.donath@squidfunk.com>
2
+ # Copy from https://github.com/squidfunk/mkdocs-material/blob/master/src/overrides/hooks/shortcodes.py
5
3
6
4
# Permission is hereby granted, free of charge, to any person obtaining a copy
7
5
# of this software and associated documentation files (the "Software"), to
@@ -45,8 +43,23 @@ def replace(match: Match):
45
43
type , args = match .groups ()
46
44
args = args .strip ()
47
45
if type == "version" :
46
+ if args .startswith ("insiders-" ):
47
+ return _badge_for_version_insiders (args , page , files )
48
+ else :
48
49
return _badge_for_version (args , page , files )
50
+ elif type == "sponsors" : return _badge_for_sponsors (page , files )
49
51
elif type == "flag" : return flag (args , page , files )
52
+ elif type == "option" : return option (args )
53
+ elif type == "setting" : return setting (args )
54
+ elif type == "feature" : return _badge_for_feature (args , page , files )
55
+ elif type == "plugin" : return _badge_for_plugin (args , page , files )
56
+ elif type == "extension" : return _badge_for_extension (args , page , files )
57
+ elif type == "utility" : return _badge_for_utility (args , page , files )
58
+ elif type == "example" : return _badge_for_example (args , page , files )
59
+ elif type == "default" :
60
+ if args == "none" : return _badge_for_default_none (page , files )
61
+ elif args == "computed" : return _badge_for_default_computed (page , files )
62
+ else : return _badge_for_default (args , page , files )
50
63
51
64
# Otherwise, raise an error
52
65
raise RuntimeError (f"Unknown shortcode: { type } " )
@@ -58,13 +71,46 @@ def replace(match: Match):
58
71
)
59
72
60
73
# -----------------------------------------------------------------------------
74
+ # Helper functions
75
+ # -----------------------------------------------------------------------------
76
+
61
77
# Create a flag of a specific type
62
78
def flag (args : str , page : Page , files : Files ):
63
79
type , * _ = args .split (" " , 1 )
64
80
if type == "experimental" : return _badge_for_experimental (page , files )
65
-
81
+ elif type == "required" : return _badge_for_required (page , files )
82
+ elif type == "customization" : return _badge_for_customization (page , files )
83
+ elif type == "metadata" : return _badge_for_metadata (page , files )
84
+ elif type == "multiple" : return _badge_for_multiple (page , files )
66
85
raise RuntimeError (f"Unknown type: { type } " )
67
86
87
+ # Create a linkable option
88
+ def option (type : str ):
89
+ _ , * _ , name = re .split (r"[.:]" , type )
90
+ return f"[`{ name } `](#+{ type } ){{ #+{ type } }}\n \n "
91
+
92
+ # Create a linkable setting - @todo append them to the bottom of the page
93
+ def setting (type : str ):
94
+ _ , * _ , name = re .split (r"[.*]" , type )
95
+ return f"`{ name } ` {{ #{ type } }}\n \n [{ type } ]: #{ type } \n \n "
96
+
97
+ # -----------------------------------------------------------------------------
98
+
99
+ # Resolve path of file relative to given page - the posixpath always includes
100
+ # one additional level of `..` which we need to remove
101
+ def _resolve_path (path : str , page : Page , files : Files ):
102
+ path , anchor , * _ = f"{ path } #" .split ("#" )
103
+ path = _resolve (files .get_file_from_path (path ), page )
104
+ return "#" .join ([path , anchor ]) if anchor else path
105
+
106
+ # Resolve path of file relative to given page - the posixpath always includes
107
+ # one additional level of `..` which we need to remove
108
+ def _resolve (file : File , page : Page ):
109
+ path = posixpath .relpath (file .src_uri , page .file .src_uri )
110
+ return posixpath .sep .join (path .split (posixpath .sep )[1 :])
111
+
112
+ # -----------------------------------------------------------------------------
113
+
68
114
# Create badge
69
115
def _badge (icon : str , text : str = "" , type : str = "" ):
70
116
classes = f"mdx-badge mdx-badge--{ type } " if type else "mdx-badge"
@@ -75,6 +121,14 @@ def _badge(icon: str, text: str = "", type: str = ""):
75
121
f"</span>" ,
76
122
])
77
123
124
+ # Create sponsors badge
125
+ def _badge_for_sponsors (page : Page , files : Files ):
126
+ icon = "material-heart"
127
+ href = _resolve_path ("features.md" , page , files )
128
+ return _badge (
129
+ icon = f"[:{ icon } :]({ href } 'Sponsors only')" ,
130
+ type = "heart"
131
+ )
78
132
79
133
# Create badge for version
80
134
def _badge_for_version (text : str , page : Page , files : Files ):
@@ -83,24 +137,135 @@ def _badge_for_version(text: str, page: Page, files: Files):
83
137
84
138
# Return badge
85
139
icon = "material-tag-outline"
140
+ href = f"{ path } "
141
+ return _badge (
142
+ icon = f"[:{ icon } :]({ href } 'Minimum version')" ,
143
+ text = f"[{ text } ]({ path } )" if spec else ""
144
+ )
145
+
146
+ # Create badge for feature
147
+ def _badge_for_feature (text : str , page : Page , files : Files ):
148
+ icon = "material-toggle-switch"
149
+ href = _resolve_path ("features.md#config" , page , files )
150
+ return _badge (
151
+ icon = f"[:{ icon } :]({ href } 'Configurable feature')" ,
152
+ text = text
153
+ )
154
+
155
+ # Create badge for plugin
156
+ def _badge_for_plugin (text : str , page : Page , files : Files ):
157
+ icon = "material-floppy"
158
+ href = _resolve_path ("features.md#plugin" , page , files )
159
+ return _badge (
160
+ icon = f"[:{ icon } :]({ href } 'Plugin')" ,
161
+ text = text
162
+ )
163
+
164
+ # Create badge for extension
165
+ def _badge_for_extension (text : str , page : Page , files : Files ):
166
+ icon = "material-language-markdown"
167
+ href = _resolve_path ("features.md#extension" , page , files )
168
+ return _badge (
169
+ icon = f"[:{ icon } :]({ href } 'Markdown extension')" ,
170
+ text = text
171
+ )
172
+
173
+ # Create badge for utility
174
+ def _badge_for_utility (text : str , page : Page , files : Files ):
175
+ icon = "material-package-variant"
176
+ href = _resolve_path ("features.md#utility" , page , files )
177
+ return _badge (
178
+ icon = f"[:{ icon } :]({ href } 'Third-party utility')" ,
179
+ text = text
180
+ )
181
+
182
+ # Create badge for example
183
+ def _badge_for_example (text : str , page : Page , files : Files ):
184
+ return "\n " .join ([
185
+ _badge_for_example_download (text , page , files ),
186
+ _badge_for_example_view (text , page , files )
187
+ ])
188
+
189
+ # Create badge for example view
190
+ def _badge_for_example_view (text : str , page : Page , files : Files ):
191
+ icon = "material-folder-eye"
192
+ href = f"https://mkdocs-material.github.io/examples/{ text } /"
86
193
return _badge (
87
- icon = f"[:{ icon } :]({ path } 'Minimum version')" ,
88
- text = f"{ text } " if spec else ""
194
+ icon = f"[:{ icon } :]({ href } 'View example')" ,
195
+ type = "right"
196
+ )
197
+
198
+ # Create badge for example download
199
+ def _badge_for_example_download (text : str , page : Page , files : Files ):
200
+ icon = "material-folder-download"
201
+ href = f"https://mkdocs-material.github.io/examples/{ text } .zip"
202
+ return _badge (
203
+ icon = f"[:{ icon } :]({ href } 'Download example')" ,
204
+ text = f"[`.zip`]({ href } )" ,
205
+ type = "right"
89
206
)
90
207
91
208
# Create badge for default value
92
209
def _badge_for_default (text : str , page : Page , files : Files ):
93
210
icon = "material-water"
211
+ href = _resolve_path ("features.md#default" , page , files )
94
212
return _badge (
95
- icon = f"[:{ icon } :]('Default value')" ,
213
+ icon = f"[:{ icon } :]({ href } 'Default value')" ,
96
214
text = text
97
215
)
98
216
217
+ # Create badge for empty default value
218
+ def _badge_for_default_none (page : Page , files : Files ):
219
+ icon = "material-water-outline"
220
+ href = _resolve_path ("features.md#default" , page , files )
221
+ return _badge (
222
+ icon = f"[:{ icon } :]({ href } 'Default value is empty')"
223
+ )
224
+
225
+ # Create badge for computed default value
226
+ def _badge_for_default_computed (page : Page , files : Files ):
227
+ icon = "material-water-check"
228
+ href = _resolve_path ("features.md#default" , page , files )
229
+ return _badge (
230
+ icon = f"[:{ icon } :]({ href } 'Default value is computed')"
231
+ )
232
+
233
+ # Create badge for metadata property flag
234
+ def _badge_for_metadata (page : Page , files : Files ):
235
+ icon = "material-list-box-outline"
236
+ href = _resolve_path ("features.md#metadata" , page , files )
237
+ return _badge (
238
+ icon = f"[:{ icon } :]({ href } 'Metadata property')"
239
+ )
240
+
241
+ # Create badge for required value flag
242
+ def _badge_for_required (page : Page , files : Files ):
243
+ icon = "material-alert"
244
+ href = _resolve_path ("features.md#required" , page , files )
245
+ return _badge (
246
+ icon = f"[:{ icon } :]({ href } 'Required value')"
247
+ )
248
+
249
+ # Create badge for customization flag
250
+ def _badge_for_customization (page : Page , files : Files ):
251
+ icon = "material-brush-variant"
252
+ href = _resolve_path ("features.md#custom" , page , files )
253
+ return _badge (
254
+ icon = f"[:{ icon } :]({ href } 'Customization')"
255
+ )
256
+
257
+ # Create badge for multiple instance flag
258
+ def _badge_for_multiple (page : Page , files : Files ):
259
+ icon = "material-inbox-multiple"
260
+ href = _resolve_path ("features.md#experimental" , page , files )
261
+ return _badge (
262
+ icon = f"[:{ icon } :]({ href } 'Multiple instances')"
263
+ )
99
264
100
265
# Create badge for experimental flag
101
266
def _badge_for_experimental (page : Page , files : Files ):
102
267
icon = "material-flask-outline"
268
+ href = _resolve_path ("features.md#experimental-features" , page , files )
103
269
return _badge (
104
- icon = f"[:{ icon } :]('Experimental')" ,
105
- text = f"Experimental"
106
- )
270
+ icon = f"[:{ icon } :]({ href } 'Experimental')"
271
+ )
0 commit comments