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

STY: Simplify code #1101

Merged
merged 2 commits into from
Jul 12, 2022
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
47 changes: 18 additions & 29 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,14 +1059,11 @@ def add_bookmark_destination(
dest: Union[PageObject, TreeObject],
parent: Union[None, TreeObject, IndirectObject] = None,
) -> IndirectObject:
dest_ref = self._add_object(dest)

outline_ref = self.get_outline_root()

if parent is None:
parent = outline_ref
parent = self.get_outline_root()

parent = cast(TreeObject, parent.get_object())
dest_ref = self._add_object(dest)
parent.add_child(dest_ref, self)

return dest_ref
Expand Down Expand Up @@ -1137,24 +1134,21 @@ def add_bookmark(
:meth:`addLink()<addLink>` for details.
"""
page_ref = NumberObject(pagenum)
action = DictionaryObject()
zoom_args: ZoomArgsType = []
for a in args:
if a is not None:
zoom_args.append(NumberObject(a))
else:
zoom_args.append(NullObject())
zoom_args: ZoomArgsType = [
NullObject() if a is None else NumberObject(a) for a in args
]
dest = Destination(
NameObject("/" + title + " bookmark"), page_ref, NameObject(fit), *zoom_args
)
dest_array = dest.dest_array
action.update(
{
NameObject(GoToActionArguments.D): dest_array,
NameObject(GoToActionArguments.S): NameObject("/GoTo"),
}

action_ref = self._add_object(
DictionaryObject(
{
NameObject(GoToActionArguments.D): dest.dest_array,
NameObject(GoToActionArguments.S): NameObject("/GoTo"),
}
)
)
action_ref = self._add_object(action)
bookmark = _create_bookmark(action_ref, title, color, italic, bold)

if parent is None:
Expand Down Expand Up @@ -1537,26 +1531,21 @@ def add_link(
else:
rect = RectangleObject(rect)

zoom_args: ZoomArgsType = []
for a in args:
if a is not None:
zoom_args.append(NumberObject(a))
else:
zoom_args.append(NullObject())
zoom_args: ZoomArgsType = [
NullObject() if a is None else NumberObject(a) for a in args
]
dest = Destination(
NameObject("/LinkName"), page_dest, NameObject(fit), *zoom_args
) # TODO: create a better name for the link
dest_array = dest.dest_array

lnk = DictionaryObject()
lnk.update(
lnk = DictionaryObject(
{
NameObject("/Type"): NameObject(PG.ANNOTS),
NameObject("/Subtype"): NameObject("/Link"),
NameObject("/P"): page_link,
NameObject("/Rect"): rect,
NameObject("/Border"): ArrayObject(border_arr),
NameObject("/Dest"): dest_array,
NameObject("/Dest"): dest.dest_array,
}
)
lnk_ref = self._add_object(lnk)
Expand Down