MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / build_extension

Method build_extension

markdown/core.py:197–248  ·  view source on GitHub ↗

Build extension from a string name, then return an instance using the given `configs`. Arguments: ext_name: Name of extension as a string. configs: Configuration settings for extension. Returns: An instance of the extension with the give

(self, ext_name: str, configs: Mapping[str, Any])

Source from the content-addressed store, hash-verified

195 return self
196
197 def build_extension(self, ext_name: str, configs: Mapping[str, Any]) -> Extension:
198 """
199 Build extension from a string name, then return an instance using the given `configs`.
200
201 Arguments:
202 ext_name: Name of extension as a string.
203 configs: Configuration settings for extension.
204
205 Returns:
206 An instance of the extension with the given configuration settings.
207
208 First attempt to load an entry point. The string name must be registered as an entry point in the
209 `markdown.extensions` group which points to a subclass of the [`markdown.extensions.Extension`][] class.
210 If multiple distributions have registered the same name, the first one found is returned.
211
212 If no entry point is found, assume dot notation (`path.to.module:ClassName`). Load the specified class and
213 return an instance. If no class is specified, import the module and call a `makeExtension` function and return
214 the [`markdown.extensions.Extension`][] instance returned by that function.
215 """
216 configs = dict(configs)
217
218 entry_points = [ep for ep in util.get_installed_extensions() if ep.name == ext_name]
219 if entry_points:
220 ext = entry_points[0].load()
221 return ext(**configs)
222
223 # Get class name (if provided): `path.to.module:ClassName`
224 ext_name, class_name = ext_name.split(':', 1) if ':' in ext_name else (ext_name, '')
225
226 try:
227 module = importlib.import_module(ext_name)
228 logger.debug(
229 'Successfully imported extension module "%s".' % ext_name
230 )
231 except ImportError as e:
232 message = 'Failed loading extension "%s".' % ext_name
233 e.args = (message,) + e.args[1:]
234 raise
235
236 if class_name:
237 # Load given class name from module.
238 return getattr(module, class_name)(**configs)
239 else:
240 # Expect `makeExtension()` function to return a class.
241 try:
242 return module.makeExtension(**configs)
243 except AttributeError as e:
244 message = e.args[0]
245 message = "Failed to initiate extension " \
246 "'%s': %s" % (ext_name, message)
247 e.args = (message,) + e.args[1:]
248 raise
249
250 def registerExtension(self, extension: Extension) -> Markdown:
251 """

Callers 1

registerExtensionsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected