1.1 --- a/moinformat/utils/copying.py Sun Apr 14 00:01:23 2019 +0200
1.2 +++ b/moinformat/utils/copying.py Sun Apr 14 00:07:33 2019 +0200
1.3 @@ -21,32 +21,43 @@
1.4
1.5 from shutil import copy
1.6
1.7 -def copy_attachments(parser, input, output):
1.8 +def copy_attachments(parser, input, output, all=False):
1.9
1.10 - "Copy attachments referenced by 'parser' from 'input' to 'output'."
1.11 + """
1.12 + For attachments referenced by the page processed by 'parser', copy them from
1.13 + 'input' to 'output'. If 'all' is set to a true value, all stored attachments
1.14 + are copied; otherwise, only those explicitly linked in the page are copied.
1.15 + """
1.16
1.17 pagename = parser.metadata.get("pagename")
1.18
1.19 if not pagename:
1.20 return
1.21
1.22 - for link_target in parser.link_targets:
1.23 -
1.24 - # Obtain attachments.
1.25 + # Obtain attachments.
1.26
1.27 - if link_target.get_type() == "attachment":
1.28 + if not all:
1.29 + filenames = []
1.30 + for link_target in parser.link_targets:
1.31 + if link_target.get_type() == "attachment":
1.32 + filenames.append(link_target.get_identifier())
1.33 + else:
1.34 + input = parser.metadata.get_input()
1.35 + filenames = input.get_attachments(pagename)
1.36
1.37 - # Obtain the attachment filename, the source location and the
1.38 - # destination.
1.39 + # Copy attachments.
1.40
1.41 - filename = link_target.get_identifier()
1.42 - input_filename = input.get_attachment_filename(pagename, filename)
1.43 - output_filename = output.get_attachment_filename(pagename, filename)
1.44 + for filename in filenames:
1.45 +
1.46 + # Obtain the source location and the destination.
1.47
1.48 - # Copy the file if possible.
1.49 + input_filename = input.get_attachment_filename(pagename, filename)
1.50 + output_filename = output.get_attachment_filename(pagename, filename)
1.51
1.52 - if input_filename and output_filename:
1.53 - output.ensure_attachments(pagename)
1.54 - copy(input_filename, output_filename)
1.55 + # Copy the file if possible.
1.56 +
1.57 + if input_filename and output_filename:
1.58 + output.ensure_attachments(pagename)
1.59 + copy(input_filename, output_filename)
1.60
1.61 # vim: tabstop=4 expandtab shiftwidth=4