1.1 --- a/moinconvert Sat Apr 13 00:07:45 2019 +0200
1.2 +++ b/moinconvert Sat Apr 13 00:08:31 2019 +0200
1.3 @@ -19,8 +19,8 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from moinformat import errors, make_parser, make_serialiser, Metadata, parse, \
1.8 - serialise
1.9 +from moinformat import copy_attachments, errors, make_parser, make_serialiser, \
1.10 + Metadata, parse, serialise
1.11 from os.path import split
1.12 import sys
1.13
1.14 @@ -333,6 +333,8 @@
1.15 output.writepage(outtext, pagename)
1.16 print >>sys.stderr, pagename
1.17
1.18 + copy_attachments(p, input, output)
1.19 +
1.20 # Install any theme resources.
1.21
1.22 if theme:
2.1 --- a/moinformat/__init__.py Sat Apr 13 00:07:45 2019 +0200
2.2 +++ b/moinformat/__init__.py Sat Apr 13 00:08:31 2019 +0200
2.3 @@ -26,6 +26,7 @@
2.4 from moinformat.parsers import get_parser, make_parser, parse
2.5 from moinformat.serialisers import get_serialiser, make_serialiser, serialise
2.6 from moinformat.themes import make_theme
2.7 +from moinformat.utils.copying import copy_attachments
2.8 import moinformat.errors as errors
2.9
2.10 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/moinformat/input/directory.py Sat Apr 13 00:07:45 2019 +0200
3.2 +++ b/moinformat/input/directory.py Sat Apr 13 00:08:31 2019 +0200
3.3 @@ -21,7 +21,7 @@
3.4
3.5 from moinformat.input.common import Input
3.6 from moinformat.utils.directory import Directory
3.7 -from os.path import sep
3.8 +from os.path import join, sep
3.9
3.10 class DirectoryInput(Input):
3.11
3.12 @@ -93,6 +93,24 @@
3.13
3.14 return self.readpath(self.dir.get_filename(filename), encoding)
3.15
3.16 + # Convenience methods.
3.17 +
3.18 + def get_attachment_filename(self, pagename, filename):
3.19 +
3.20 + """
3.21 + Return the full path of an attachment file for the given 'pagename'
3.22 + having the given 'filename'.
3.23 + """
3.24 +
3.25 + if not pagename:
3.26 + return None
3.27 +
3.28 + if self.nested:
3.29 + return self.dir.get_filename(join(self.to_filename(pagename),
3.30 + self.attachments_dir, filename))
3.31 + else:
3.32 + return self.dir.get_filename(join(self.attachments_dir, filename))
3.33 +
3.34 # NOTE: Translation methods should encode filenames appropriately.
3.35
3.36 def to_filename(self, pagename):
4.1 --- a/moinformat/input/standalone.py Sat Apr 13 00:07:45 2019 +0200
4.2 +++ b/moinformat/input/standalone.py Sat Apr 13 00:08:31 2019 +0200
4.3 @@ -3,7 +3,7 @@
4.4 """
4.5 Stand-alone input context.
4.6
4.7 -Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk>
4.8 +Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk>
4.9
4.10 This program is free software; you can redistribute it and/or modify it under
4.11 the terms of the GNU General Public License as published by the Free Software
4.12 @@ -40,6 +40,17 @@
4.13 f = codecs.getreader(self.encoding)(stream)
4.14 return f.read()
4.15
4.16 + # Convenience methods.
4.17 +
4.18 + def get_attachment_filename(self, pagename, filename):
4.19 +
4.20 + """
4.21 + Prevent independent output by returning a filename of None corresponding
4.22 + to the given 'pagename' and any specified 'filename'.
4.23 + """
4.24 +
4.25 + return None
4.26 +
4.27 input = StandaloneInput
4.28
4.29 # vim: tabstop=4 expandtab shiftwidth=4
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/moinformat/utils/copying.py Sat Apr 13 00:08:31 2019 +0200
5.3 @@ -0,0 +1,52 @@
5.4 +#!/usr/bin/env python
5.5 +
5.6 +"""
5.7 +Copying utilities.
5.8 +
5.9 +Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk>
5.10 +
5.11 +This program is free software; you can redistribute it and/or modify it under
5.12 +the terms of the GNU General Public License as published by the Free Software
5.13 +Foundation; either version 3 of the License, or (at your option) any later
5.14 +version.
5.15 +
5.16 +This program is distributed in the hope that it will be useful, but WITHOUT
5.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
5.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
5.19 +details.
5.20 +
5.21 +You should have received a copy of the GNU General Public License along with
5.22 +this program. If not, see <http://www.gnu.org/licenses/>.
5.23 +"""
5.24 +
5.25 +from shutil import copy
5.26 +
5.27 +def copy_attachments(parser, input, output):
5.28 +
5.29 + "Copy attachments referenced by 'parser' from 'input' to 'output'."
5.30 +
5.31 + pagename = parser.metadata.get("pagename")
5.32 +
5.33 + if not pagename:
5.34 + return
5.35 +
5.36 + for link_target in parser.link_targets:
5.37 +
5.38 + # Obtain attachments.
5.39 +
5.40 + if link_target.get_type() == "attachment":
5.41 +
5.42 + # Obtain the attachment filename, the source location and the
5.43 + # destination.
5.44 +
5.45 + filename = link_target.get_identifier()
5.46 + input_filename = input.get_attachment_filename(pagename, filename)
5.47 + output_filename = output.get_attachment_filename(pagename, filename)
5.48 +
5.49 + # Copy the file if possible.
5.50 +
5.51 + if input_filename and output_filename:
5.52 + output.ensure_attachments(pagename)
5.53 + copy(input_filename, output_filename)
5.54 +
5.55 +# vim: tabstop=4 expandtab shiftwidth=4