# HG changeset patch # User Paul Boddie # Date 1654274566 -7200 # Node ID ad04b52492dfd5a1a726fa928ddfbb637d26fdc5 # Parent 4b86f7f894b80acf091ffc427470946588d143a6 Added support for a common attachments directory. diff -r 4b86f7f894b8 -r ad04b52492df moinconvert --- a/moinconvert Tue Oct 26 01:32:32 2021 +0200 +++ b/moinconvert Fri Jun 03 18:42:46 2022 +0200 @@ -3,7 +3,7 @@ """ Moin wiki format converter. -Copyright (C) 2018, 2019 Paul Boddie +Copyright (C) 2018, 2019, 2021 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 @@ -96,6 +96,7 @@ mappings = [] output_dirs = [] output_encodings = [] + output_page_seps = [] pagenames = [] root_pagenames = [] theme_names = [] @@ -215,6 +216,12 @@ l = output_encodings continue + # Switch to collecting output page hierarchy separators. + + elif arg == "--output-page-sep": + l = output_page_seps + continue + # Switch to collecting page names. elif arg == "--pagename": @@ -271,6 +278,7 @@ "output_encoding" : getvalue(output_encodings), "output_format" : format, "output_filename" : output_dir, + "output_separator" : getvalue(output_page_seps), "root_pagename" : getvalue(root_pagenames, "FrontPage"), "theme_name" : not fragment and \ "%s.%s" % (getvalue(theme_names, "default"), format) or None, @@ -407,6 +415,8 @@ files --output-encoding Indicate the character encoding used in serialised document files +--output-page-sep Indicate the separator used in filenames to encode + hierarchical relationships (subpages and descendant pages) --theme Indicate a theme for serialised documents, typically requiring an output directory to be useful --tree Produce a document tree representation on standard output diff -r 4b86f7f894b8 -r ad04b52492df moinformat/output/directory.py --- a/moinformat/output/directory.py Tue Oct 26 01:32:32 2021 +0200 +++ b/moinformat/output/directory.py Fri Jun 03 18:42:46 2022 +0200 @@ -21,7 +21,7 @@ from moinformat.output.common import Output from moinformat.utils.directory import Directory -from os.path import extsep, join +from os.path import extsep, join, sep class DirectoryOutput(Output): @@ -40,15 +40,32 @@ self.dir = Directory(metadata.get("output_filename")) self.dir.ensure() + # Support an encoding of the level separator for the filesystem. + # Where it is the same as the directory separator, documents are stored + # using nested directories, not as a flat list. + + self.level_sep = metadata.get("output_separator", sep) + # Use any document index setting as the default for the index filename. document_index = metadata.get("document_index", "index.html") - self.index_name = metadata.get("index_name", document_index) - self.page_suffix = metadata.get("page_suffix", "%shtml" % extsep) self.root_pagename = metadata.get("root_pagename") self.attachments_dir = metadata.get("attachments") + # Support a common attachments directory. + + self.common_attachments = metadata.get("common_attachments") + + def _get_attachments_dir(self, pagename): + + "Return the attachments directory for 'pagename'." + + if self.common_attachments: + return join(self.dir.filename, self.attachments_dir) + else: + return join(self.dir.filename, self.attachments_dir, pagename) + # Convenience methods. def ensure(self, pagename): @@ -67,7 +84,7 @@ if not pagename: return None - self.dir.ensure(join(self.to_filename(pagename), self.attachments_dir)) + self.dir.ensure(self._get_attachments_dir(pagename)) def get_attachment_filename(self, pagename, filename): @@ -79,8 +96,7 @@ if not pagename: return None - return self.dir.get_filename(join(self.to_filename(pagename), - self.attachments_dir, filename)) + return self.dir.get_filename(join(self._get_attachments_dir(pagename), filename)) def get_filename(self, filename): @@ -97,18 +113,35 @@ "Return the filename corresponding to 'pagename'." - # For the root page, use the top-level directory. + # Encode hierarchical filenames. + + if self.level_sep == sep: + + # For the root page, use the top-level directory. - if pagename == self.root_pagename: - return "" + if pagename == self.root_pagename: + return "" + else: + return pagename + + # Encode single-directory filenames. + else: - return pagename + return self.level_sep.join(pagename.split("/")) def to_pagename(self, filename): "Return the pagename corresponding to 'filename'." - return self.within(filename) + # Encode pagenames from hierarchical filenames. + + if self.level_sep == sep: + return self.within(filename) + + # Encode pagenames from single-directory filenames. + + else: + return "/".join(filename.split(self.level_sep)) # Serialisation methods. @@ -137,14 +170,20 @@ filename = self.to_filename(pagename) - # Make a directory for the page. + # For hierarchical storage, store the page inside a directory bearing + # its name. + + if self.level_sep == sep: + + # Make a directory for the page. - if not self.dir.exists(filename): - self.dir.makedirs(filename) + if not self.dir.exists(filename): + self.dir.makedirs(filename) - # Write to an index filename within any existing directory. + # Write to an index filename within any existing directory. - filename = join(filename, self.index_name) + filename = join(filename, self.index_name) + self.writefile(text, filename, encoding) output = DirectoryOutput