MCPcopy Index your code
hub / github.com/Python-Markdown/markdown / build_extension

Method build_extension

markdown/core.py:195–246  ·  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

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

Callers 1

registerExtensionsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected