# HG changeset patch # User Paul Boddie # Date 1555106911 -7200 # Node ID f5e5c9da103efa080a0f6d58abb8924ffc36b591 # Parent 90e7ce5b6cefb8c607b78bd131bb0d108faf0c7a Support the copying of attachments into the converted content. diff -r 90e7ce5b6cef -r f5e5c9da103e moinconvert --- a/moinconvert Sat Apr 13 00:07:45 2019 +0200 +++ b/moinconvert Sat Apr 13 00:08:31 2019 +0200 @@ -19,8 +19,8 @@ this program. If not, see . """ -from moinformat import errors, make_parser, make_serialiser, Metadata, parse, \ - serialise +from moinformat import copy_attachments, errors, make_parser, make_serialiser, \ + Metadata, parse, serialise from os.path import split import sys @@ -333,6 +333,8 @@ output.writepage(outtext, pagename) print >>sys.stderr, pagename + copy_attachments(p, input, output) + # Install any theme resources. if theme: diff -r 90e7ce5b6cef -r f5e5c9da103e moinformat/__init__.py --- a/moinformat/__init__.py Sat Apr 13 00:07:45 2019 +0200 +++ b/moinformat/__init__.py Sat Apr 13 00:08:31 2019 +0200 @@ -26,6 +26,7 @@ from moinformat.parsers import get_parser, make_parser, parse from moinformat.serialisers import get_serialiser, make_serialiser, serialise from moinformat.themes import make_theme +from moinformat.utils.copying import copy_attachments import moinformat.errors as errors # vim: tabstop=4 expandtab shiftwidth=4 diff -r 90e7ce5b6cef -r f5e5c9da103e moinformat/input/directory.py --- a/moinformat/input/directory.py Sat Apr 13 00:07:45 2019 +0200 +++ b/moinformat/input/directory.py Sat Apr 13 00:08:31 2019 +0200 @@ -21,7 +21,7 @@ from moinformat.input.common import Input from moinformat.utils.directory import Directory -from os.path import sep +from os.path import join, sep class DirectoryInput(Input): @@ -93,6 +93,24 @@ return self.readpath(self.dir.get_filename(filename), encoding) + # Convenience methods. + + def get_attachment_filename(self, pagename, filename): + + """ + Return the full path of an attachment file for the given 'pagename' + having the given 'filename'. + """ + + if not pagename: + return None + + if self.nested: + return self.dir.get_filename(join(self.to_filename(pagename), + self.attachments_dir, filename)) + else: + return self.dir.get_filename(join(self.attachments_dir, filename)) + # NOTE: Translation methods should encode filenames appropriately. def to_filename(self, pagename): diff -r 90e7ce5b6cef -r f5e5c9da103e moinformat/input/standalone.py --- a/moinformat/input/standalone.py Sat Apr 13 00:07:45 2019 +0200 +++ b/moinformat/input/standalone.py Sat Apr 13 00:08:31 2019 +0200 @@ -3,7 +3,7 @@ """ Stand-alone input context. -Copyright (C) 2018 Paul Boddie +Copyright (C) 2018, 2019 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -40,6 +40,17 @@ f = codecs.getreader(self.encoding)(stream) return f.read() + # Convenience methods. + + def get_attachment_filename(self, pagename, filename): + + """ + Prevent independent output by returning a filename of None corresponding + to the given 'pagename' and any specified 'filename'. + """ + + return None + input = StandaloneInput # vim: tabstop=4 expandtab shiftwidth=4 diff -r 90e7ce5b6cef -r f5e5c9da103e moinformat/utils/copying.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moinformat/utils/copying.py Sat Apr 13 00:08:31 2019 +0200 @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +""" +Copying utilities. + +Copyright (C) 2019 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from shutil import copy + +def copy_attachments(parser, input, output): + + "Copy attachments referenced by 'parser' from 'input' to 'output'." + + pagename = parser.metadata.get("pagename") + + if not pagename: + return + + for link_target in parser.link_targets: + + # Obtain attachments. + + if link_target.get_type() == "attachment": + + # Obtain the attachment filename, the source location and the + # destination. + + filename = link_target.get_identifier() + input_filename = input.get_attachment_filename(pagename, filename) + output_filename = output.get_attachment_filename(pagename, filename) + + # Copy the file if possible. + + if input_filename and output_filename: + output.ensure_attachments(pagename) + copy(input_filename, output_filename) + +# vim: tabstop=4 expandtab shiftwidth=4