Skip to content

Commit

Permalink
Merge pull request #224 from erbsenmann/master
Browse files Browse the repository at this point in the history
Added fix for unstable element index (issue #223)
  • Loading branch information
sergiocorreia authored Nov 5, 2022
2 parents b1fcddf + 7122c76 commit ff94a0c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
14 changes: 2 additions & 12 deletions panflute/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class Element(object):
"""
Base class of all Pandoc elements
"""
__slots__ = ['parent', 'location']
__slots__ = ['parent', 'location', 'index']
_children = []

def __new__(cls, *args, **kwargs):
# This is just to initialize self.parent to None
element = object.__new__(cls)
element.parent = None
element.location = None
element.index = None
return element

@property
Expand Down Expand Up @@ -134,17 +135,6 @@ def _set_content(self, value, oktypes):
# Navigation
# ---------------------------

@property
def index(self):
"""
Return position of element inside the parent.
:rtype: ``int`` | ``None``
"""
container = self.container
if container is not None:
return container.index(self)

@property
def container(self):
"""
Expand Down
8 changes: 5 additions & 3 deletions panflute/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
# These are list and dict containers that
# (a) track the identity of their parents, and
# (b) track the parent's property where they are stored
# They attach these two to the elements requested through __getattr__
# (c) track the index in the parent in case of list
# They attach these three to the elements requested through __getattr__

class ListContainer(MutableSequence):
"""
Expand Down Expand Up @@ -55,7 +56,7 @@ def __len__(self):

def __getitem__(self, i):
if isinstance(i, int):
return attach(self.list[i], self.parent, self.location)
return attach(self.list[i], self.parent, self.location, i)
else:
newlist = self.list.__getitem__(i)
obj = ListContainer(*newlist,
Expand Down Expand Up @@ -168,10 +169,11 @@ def to_json(self):
# Functions
# ---------------------------

def attach(element, parent, location):
def attach(element, parent, location, index=None):
if not isinstance(element, (int, str, bool)):
element.parent = parent
element.location = location
element.index = index
else:
debug(f'Warning: element "{type(element)}" has no parent')
return element
Expand Down
15 changes: 15 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,20 @@ def test_quotes_129():
assert output == '"Some quoted text"'


def test_index_223():
"""Index values on duplicated elements are determined using list.index()
but this returns the index first found element.
This test checks whether the index on the element corresponds with the
actual index in the parent collection.
"""
# pf https://github.com/sergiocorreia/panflute/issues/223
doc = pf.Doc(pf.Para(pf.Str("a")), pf.Para(pf.Str("b")),
pf.Para(pf.Str("a")), pf.Para(pf.Str("c")))

for (index, element) in enumerate(doc.content):
assert element.index == index


if __name__ == "__main__":
test_quotes_129()
test_index_223()

0 comments on commit ff94a0c

Please sign in to comment.