From c9d14c0f7d8be6f9636542d7caf78a3b47c450a1 Mon Sep 17 00:00:00 2001 From: David Nicolazo Date: Mon, 26 Jun 2023 14:13:06 +0200 Subject: [PATCH 1/4] feat(metadata): make MetaMap/List completely pythonic --- panflute/elements.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/panflute/elements.py b/panflute/elements.py index 9585b31..26c7146 100644 --- a/panflute/elements.py +++ b/panflute/elements.py @@ -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, @@ -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 @@ -1082,12 +1082,15 @@ def __getitem__(self, i): def __setitem__(self, i, v): self.content[i] = builtin2meta(v) + + def __len__(self): + return len(self.content) - def append(self, i): - self.content.append(i) + def insert(self, i, v): + self.content.insert(i, builtin2meta(v)) -class MetaMap(MetaValue): +class MetaMap(MetaValue, MutableMapping): """Metadata container for ordered dicts :param args: (key, value) tuples @@ -1122,16 +1125,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): From d4abfb0ac86b6aad7b8c29fe201316d8fb0eb51a Mon Sep 17 00:00:00 2001 From: David Nicolazo Date: Mon, 26 Jun 2023 14:27:44 +0200 Subject: [PATCH 2/4] style: whitespace in blank line --- panflute/elements.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/panflute/elements.py b/panflute/elements.py index 26c7146..a7258a5 100644 --- a/panflute/elements.py +++ b/panflute/elements.py @@ -1082,7 +1082,7 @@ def __getitem__(self, i): def __setitem__(self, i, v): self.content[i] = builtin2meta(v) - + def __len__(self): return len(self.content) From c7f6735a52159c80dc90ef0e05ce1ce2c6759a0a Mon Sep 17 00:00:00 2001 From: David Nicolazo Date: Mon, 26 Jun 2023 14:37:25 +0200 Subject: [PATCH 3/4] style: remove (all) trailing whitespace --- panflute/elements.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/panflute/elements.py b/panflute/elements.py index a7258a5..e63104c 100644 --- a/panflute/elements.py +++ b/panflute/elements.py @@ -1130,13 +1130,13 @@ def __getitem__(self, i): def __setitem__(self, i, v): self.content[i] = builtin2meta(v) - + def __delitem__(self, i): del self.content[i] - + def __iter__(self): return iter(self.content) - + def __len__(self): return len(self.content) From 6d657adf669839eca5e119e5853a2ce06f28f1b4 Mon Sep 17 00:00:00 2001 From: David Nicolazo Date: Mon, 26 Jun 2023 21:52:13 +0200 Subject: [PATCH 4/4] fix(metadata): forgot __delitem__ on MetaList --- panflute/elements.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/panflute/elements.py b/panflute/elements.py index e63104c..a965c35 100644 --- a/panflute/elements.py +++ b/panflute/elements.py @@ -1083,6 +1083,9 @@ def __getitem__(self, i): def __setitem__(self, i, v): self.content[i] = builtin2meta(v) + def __delitem__(self, i): + del self.content[i] + def __len__(self): return len(self.content)