MCPcopy Create free account
hub / github.com/executablebooks/markdown-it-py / as_dict

Method as_dict

markdown_it/token.py:125–170  ·  view source on GitHub ↗

Return the token as a dictionary. :param children: Also convert children to dicts :param as_upstream: Ensure the output dictionary is equal to that created by markdown-it For example, attrs are converted to null or lists :param meta_serializer: hook for serializi

(
        self,
        *,
        children: bool = True,
        as_upstream: bool = True,
        meta_serializer: Callable[[dict[Any, Any]], Any] | None = None,
        filter: Callable[[str, Any], bool] | None = None,
        dict_factory: Callable[..., MutableMapping[str, Any]] = dict,
    )

Source from the content-addressed store, hash-verified

123 return dc.replace(self, **changes)
124
125 def as_dict(
126 self,
127 *,
128 children: bool = True,
129 as_upstream: bool = True,
130 meta_serializer: Callable[[dict[Any, Any]], Any] | None = None,
131 filter: Callable[[str, Any], bool] | None = None,
132 dict_factory: Callable[..., MutableMapping[str, Any]] = dict,
133 ) -> MutableMapping[str, Any]:
134 """Return the token as a dictionary.
135
136 :param children: Also convert children to dicts
137 :param as_upstream: Ensure the output dictionary is equal to that created by markdown-it
138 For example, attrs are converted to null or lists
139 :param meta_serializer: hook for serializing ``Token.meta``
140 :param filter: A callable whose return code determines whether an
141 attribute or element is included (``True``) or dropped (``False``).
142 Is called with the (key, value) pair.
143 :param dict_factory: A callable to produce dictionaries from.
144 For example, to produce ordered dictionaries instead of normal Python
145 dictionaries, pass in ``collections.OrderedDict``.
146
147 """
148 mapping = dict_factory((f.name, getattr(self, f.name)) for f in dc.fields(self))
149 if filter:
150 mapping = dict_factory((k, v) for k, v in mapping.items() if filter(k, v))
151 if as_upstream and "attrs" in mapping:
152 mapping["attrs"] = (
153 None
154 if not mapping["attrs"]
155 else [[k, v] for k, v in mapping["attrs"].items()]
156 )
157 if meta_serializer and "meta" in mapping:
158 mapping["meta"] = meta_serializer(mapping["meta"])
159 if children and mapping.get("children", None):
160 mapping["children"] = [
161 child.as_dict(
162 children=children,
163 filter=filter,
164 dict_factory=dict_factory,
165 as_upstream=as_upstream,
166 meta_serializer=meta_serializer,
167 )
168 for child in mapping["children"]
169 ]
170 return mapping
171
172 @classmethod
173 def from_dict(cls, dct: MutableMapping[str, Any]) -> Token:

Callers 6

test_tokenFunction · 0.95
test_serializationFunction · 0.95
test_use_existing_envFunction · 0.80
test_store_labelsFunction · 0.80
test_inline_definitionsFunction · 0.80
test_table_tokensFunction · 0.80

Calls

no outgoing calls

Tested by 6

test_tokenFunction · 0.76
test_serializationFunction · 0.76
test_use_existing_envFunction · 0.64
test_store_labelsFunction · 0.64
test_inline_definitionsFunction · 0.64
test_table_tokensFunction · 0.64