Skip to content

Commit

Permalink
Merge pull request #233 from Dabsunter/master
Browse files Browse the repository at this point in the history
[Metadata] Make MetaMap/List completely pythonic
  • Loading branch information
sergiocorreia authored Mar 20, 2024
2 parents dd8b03a + 6d657ad commit 582353b
Showing 1 changed file with 19 additions and 9 deletions.
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

0 comments on commit 582353b

Please sign in to comment.