Skip to content

Commit

Permalink
MAINT: Use add_bookmark_destination in add_bookmark (#1100)
Browse files Browse the repository at this point in the history
Reduce code duplication

See #1098
  • Loading branch information
MartinThoma authored Jul 12, 2022
1 parent d7b64dc commit c420beb
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,9 @@ def getNamedDestRoot(self) -> ArrayObject: # pragma: no cover
return self.get_named_dest_root()

def add_bookmark_destination(
self, dest: Union[PageObject, TreeObject], parent: Optional[TreeObject] = None
self,
dest: Union[PageObject, TreeObject],
parent: Union[None, TreeObject, IndirectObject] = None,
) -> IndirectObject:
dest_ref = self._add_object(dest)

Expand Down Expand Up @@ -1153,21 +1155,11 @@ def add_bookmark(
}
)
action_ref = self._add_object(action)

outline_ref = self.get_outline_root()

if parent is None:
parent = outline_ref

bookmark = _create_bookmark(action_ref, title, color, italic, bold)

bookmark_ref = self._add_object(bookmark)

assert parent is not None, "hint for mypy"
parent_obj = cast(TreeObject, parent.get_object())
parent_obj.add_child(bookmark_ref, self)

return bookmark_ref
if parent is None:
parent = self.get_outline_root()
return self.add_bookmark_destination(bookmark, parent)

def addBookmark(
self,
Expand Down

0 comments on commit c420beb

Please sign in to comment.