# HG changeset patch # User Paul Boddie # Date 1555194580 -7200 # Node ID 15d155fac3450ea649a0fa02daf9d8d339e232b2 # Parent c80376c24b80ea2b14c891631a4da27ddd61b254# Parent bd56fc44652e0d772a0c5f908dcf289bb62590f0 Merged changes from the default branch. diff -r c80376c24b80 -r 15d155fac345 moinconvert --- a/moinconvert Sat Apr 13 19:30:33 2019 +0200 +++ b/moinconvert Sun Apr 14 00:29:40 2019 +0200 @@ -333,7 +333,7 @@ output.writepage(outtext, pagename) print >>sys.stderr, pagename - copy_attachments(p, input, output) + copy_attachments(p, input, output, all=True) # Install any theme resources. diff -r c80376c24b80 -r 15d155fac345 moinformat/input/common.py --- a/moinformat/input/common.py Sat Apr 13 19:30:33 2019 +0200 +++ b/moinformat/input/common.py Sun Apr 14 00:29:40 2019 +0200 @@ -3,7 +3,7 @@ """ Input context common functionality. -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 @@ -44,6 +44,18 @@ return [] + def all_attachments(self): + + "Return all attachment filenames in the context." + + return [] + + def get_attachments(self, pagename): + + "Return all attachment filenames for the given 'pagename'." + + return [] + # Page characteristics. def parent(self, pagename): diff -r c80376c24b80 -r 15d155fac345 moinformat/input/directory.py --- a/moinformat/input/directory.py Sat Apr 13 19:30:33 2019 +0200 +++ b/moinformat/input/directory.py Sun Apr 14 00:29:40 2019 +0200 @@ -45,7 +45,7 @@ self.level_sep = metadata.get("input_separator", sep) - # Search recursively in nested directories for pages and files. + # Search recursively in nested directories for pages. self.nested = self.level_sep == sep @@ -65,7 +65,20 @@ "Return all attachment filenames in the context." - return self.dir.select_files("%s/*" % self.attachments_dir, self.nested) + return self.dir.select_files(join(self.attachments_dir, "*"), True) + + def get_attachments(self, pagename): + + """ + Return all attachment filenames for the given 'pagename'. Each filename + is relative to the appropriate attachment directory. + """ + + attachments_dir = Directory(join(self.dir.filename, + self.attachments_dir, + pagename)) + + return attachments_dir.select_files("*") # Page characteristics. @@ -73,7 +86,7 @@ "Return the subpage filenames of 'pagename'." - pattern = self.to_filename("%s/*" % pagename) + pattern = self.to_filename("%s%s*" % (pagename, self.level_sep)) return self.dir.select_files(pattern, self.nested) def subpages(self, pagename): @@ -105,11 +118,8 @@ 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)) + return self.dir.get_filename(join(self.attachments_dir, + pagename, filename)) # NOTE: Translation methods should encode filenames appropriately. diff -r c80376c24b80 -r 15d155fac345 moinformat/macros/attachlist.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moinformat/macros/attachlist.py Sun Apr 14 00:29:40 2019 +0200 @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +""" +AttachList macro for Moin compatibility. + +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 moinformat.macros.common import Macro +from moinformat.tree.moin import Link, LinkLabel, List, ListItem, Text +from moinformat.utils.links import LinkTarget + +class AttachListMacro(Macro): + + "An attachment list macro." + + name = "AttachList" + + def evaluate(self): + + "Evaluate the macro, producing a list of attachments." + + # Obtain the parameters. + + args = self.node.args + + pagename = args and args[0] or self.metadata.get("pagename") + mimetype = len(args) > 1 and args[1] or None + + # Access the input context to get the attachment details. + + input = self.metadata.get_input() + filenames = pagename and input.get_attachments(pagename) or [] + + # Select attachments by type. + # NOTE: To do. + + # Prepare a list of links. + + items = [] + indent = 0 + marker = "*" + space = " " + num = None + + for filename in filenames: + text = [Text(filename)] + nodes = [Link([LinkLabel(text)], LinkTarget("attachment", filename))] + items.append(ListItem(nodes, indent, marker, space, num)) + + # Replace the macro node with the list. + + macro = self.node + macro.parent.replace(macro, List(items)) + +macro = AttachListMacro + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r c80376c24b80 -r 15d155fac345 moinformat/macros/toc.py --- a/moinformat/macros/toc.py Sat Apr 13 19:30:33 2019 +0200 +++ b/moinformat/macros/toc.py Sun Apr 14 00:29:40 2019 +0200 @@ -127,7 +127,7 @@ else: new_items = [] - new_list = List(new_items, indent, marker, num) + new_list = List(new_items) # Add the list to the current item, if any. diff -r c80376c24b80 -r 15d155fac345 moinformat/parsers/moin.py --- a/moinformat/parsers/moin.py Sat Apr 13 19:30:33 2019 +0200 +++ b/moinformat/parsers/moin.py Sun Apr 14 00:29:40 2019 +0200 @@ -327,7 +327,7 @@ "Create a list, starting with 'item'." - list = List([item], item.indent, item.marker, item.num) + list = List([item]) self.parse_region_details(list, self.list_pattern_names, True) return list diff -r c80376c24b80 -r 15d155fac345 moinformat/tree/moin.py --- a/moinformat/tree/moin.py Sat Apr 13 19:30:33 2019 +0200 +++ b/moinformat/tree/moin.py Sun Apr 14 00:29:40 2019 +0200 @@ -3,7 +3,7 @@ """ Moin wiki format document tree nodes. -Copyright (C) 2017, 2018 Paul Boddie +Copyright (C) 2017, 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 @@ -404,20 +404,26 @@ "A list." - def __init__(self, nodes, indent, marker, num): + def __init__(self, nodes): Container.__init__(self, nodes) - self.indent = indent - self.marker = marker - self.num = num + self.init() + + def init(self): + self.first = first = self.nodes and self.nodes[0] or None + self.indent = first and first.indent + self.marker = first and first.marker + self.num = first and first.num def __repr__(self): - return "List(%r, %r, %r, %r)" % (self.nodes, self.indent, self.marker, self.num) + return "List(%r)" % self.nodes def prettyprint(self, indent=""): l = ["%sList: indent=%d marker=%r num=%r" % (indent, self.indent, self.marker, self.num)] return self._prettyprint(l, indent) def to_string(self, out): + if not self.first: + self.init() out.start_list(self.indent, self.marker, self.num) self._to_string(out) out.end_list(self.indent, self.marker, self.num) diff -r c80376c24b80 -r 15d155fac345 moinformat/utils/copying.py --- a/moinformat/utils/copying.py Sat Apr 13 19:30:33 2019 +0200 +++ b/moinformat/utils/copying.py Sun Apr 14 00:29:40 2019 +0200 @@ -21,32 +21,43 @@ from shutil import copy -def copy_attachments(parser, input, output): +def copy_attachments(parser, input, output, all=False): - "Copy attachments referenced by 'parser' from 'input' to 'output'." + """ + For attachments referenced by the page processed by 'parser', copy them from + 'input' to 'output'. If 'all' is set to a true value, all stored attachments + are copied; otherwise, only those explicitly linked in the page are copied. + """ pagename = parser.metadata.get("pagename") if not pagename: return - for link_target in parser.link_targets: - - # Obtain attachments. + # Obtain attachments. - if link_target.get_type() == "attachment": + if not all: + filenames = [] + for link_target in parser.link_targets: + if link_target.get_type() == "attachment": + filenames.append(link_target.get_identifier()) + else: + input = parser.metadata.get_input() + filenames = input.get_attachments(pagename) - # Obtain the attachment filename, the source location and the - # destination. + # Copy attachments. - filename = link_target.get_identifier() - input_filename = input.get_attachment_filename(pagename, filename) - output_filename = output.get_attachment_filename(pagename, filename) + for filename in filenames: + + # Obtain the source location and the destination. - # Copy the file if possible. + input_filename = input.get_attachment_filename(pagename, filename) + output_filename = output.get_attachment_filename(pagename, filename) - if input_filename and output_filename: - output.ensure_attachments(pagename) - copy(input_filename, output_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