Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metadata] Make MetaMap/List completely pythonic #233

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions panflute/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .utils import check_type, check_group, encode_dict, decode_ica, debug, check_type_or_value
from .utils import load_pandoc_version, load_pandoc_reader_options
from .containers import ListContainer, DictContainer
from .containers import ListContainer, DictContainer, MutableSequence, MutableMapping
from .base import Element, Block, Inline, MetaValue
from .table_elements import (
Table, TableHead, TableFoot, TableBody, TableRow, TableCell, Caption,
Expand Down Expand Up @@ -1060,7 +1060,7 @@ def _slots_to_json(self):
# Classes - Metadata
# ---------------------------

class MetaList(MetaValue):
class MetaList(MetaValue, MutableSequence):
"""Metadata list container

:param args: contents of a metadata list
Expand All @@ -1083,11 +1083,17 @@ def __getitem__(self, i):
def __setitem__(self, i, v):
self.content[i] = builtin2meta(v)

def append(self, i):
self.content.append(i)
def __delitem__(self, i):
del self.content[i]

def __len__(self):
return len(self.content)

class MetaMap(MetaValue):
def insert(self, i, v):
self.content.insert(i, builtin2meta(v))


class MetaMap(MetaValue, MutableMapping):
"""Metadata container for ordered dicts

:param args: (key, value) tuples
Expand Down Expand Up @@ -1122,16 +1128,20 @@ def content(self, value):
value = value.dict.items()
self._content = DictContainer(*value, oktypes=MetaValue, parent=self)

# These two are convenience functions, not sure if really needed...
# (they save typing the .content and converting to metavalues)
def __getitem__(self, i):
return self.content[i]

def __setitem__(self, i, v):
self.content[i] = builtin2meta(v)

def __contains__(self, i):
return i in self.content
def __delitem__(self, i):
del self.content[i]

def __iter__(self):
return iter(self.content)

def __len__(self):
return len(self.content)


class MetaInlines(MetaValue):
Expand Down