1.1 --- a/moinformat/output/directory.py Tue Oct 26 01:32:32 2021 +0200
1.2 +++ b/moinformat/output/directory.py Fri Jun 03 18:42:46 2022 +0200
1.3 @@ -21,7 +21,7 @@
1.4
1.5 from moinformat.output.common import Output
1.6 from moinformat.utils.directory import Directory
1.7 -from os.path import extsep, join
1.8 +from os.path import extsep, join, sep
1.9
1.10 class DirectoryOutput(Output):
1.11
1.12 @@ -40,15 +40,32 @@
1.13 self.dir = Directory(metadata.get("output_filename"))
1.14 self.dir.ensure()
1.15
1.16 + # Support an encoding of the level separator for the filesystem.
1.17 + # Where it is the same as the directory separator, documents are stored
1.18 + # using nested directories, not as a flat list.
1.19 +
1.20 + self.level_sep = metadata.get("output_separator", sep)
1.21 +
1.22 # Use any document index setting as the default for the index filename.
1.23
1.24 document_index = metadata.get("document_index", "index.html")
1.25 -
1.26 self.index_name = metadata.get("index_name", document_index)
1.27 - self.page_suffix = metadata.get("page_suffix", "%shtml" % extsep)
1.28 self.root_pagename = metadata.get("root_pagename")
1.29 self.attachments_dir = metadata.get("attachments")
1.30
1.31 + # Support a common attachments directory.
1.32 +
1.33 + self.common_attachments = metadata.get("common_attachments")
1.34 +
1.35 + def _get_attachments_dir(self, pagename):
1.36 +
1.37 + "Return the attachments directory for 'pagename'."
1.38 +
1.39 + if self.common_attachments:
1.40 + return join(self.dir.filename, self.attachments_dir)
1.41 + else:
1.42 + return join(self.dir.filename, self.attachments_dir, pagename)
1.43 +
1.44 # Convenience methods.
1.45
1.46 def ensure(self, pagename):
1.47 @@ -67,7 +84,7 @@
1.48 if not pagename:
1.49 return None
1.50
1.51 - self.dir.ensure(join(self.to_filename(pagename), self.attachments_dir))
1.52 + self.dir.ensure(self._get_attachments_dir(pagename))
1.53
1.54 def get_attachment_filename(self, pagename, filename):
1.55
1.56 @@ -79,8 +96,7 @@
1.57 if not pagename:
1.58 return None
1.59
1.60 - return self.dir.get_filename(join(self.to_filename(pagename),
1.61 - self.attachments_dir, filename))
1.62 + return self.dir.get_filename(join(self._get_attachments_dir(pagename), filename))
1.63
1.64 def get_filename(self, filename):
1.65
1.66 @@ -97,18 +113,35 @@
1.67
1.68 "Return the filename corresponding to 'pagename'."
1.69
1.70 - # For the root page, use the top-level directory.
1.71 + # Encode hierarchical filenames.
1.72 +
1.73 + if self.level_sep == sep:
1.74 +
1.75 + # For the root page, use the top-level directory.
1.76
1.77 - if pagename == self.root_pagename:
1.78 - return ""
1.79 + if pagename == self.root_pagename:
1.80 + return ""
1.81 + else:
1.82 + return pagename
1.83 +
1.84 + # Encode single-directory filenames.
1.85 +
1.86 else:
1.87 - return pagename
1.88 + return self.level_sep.join(pagename.split("/"))
1.89
1.90 def to_pagename(self, filename):
1.91
1.92 "Return the pagename corresponding to 'filename'."
1.93
1.94 - return self.within(filename)
1.95 + # Encode pagenames from hierarchical filenames.
1.96 +
1.97 + if self.level_sep == sep:
1.98 + return self.within(filename)
1.99 +
1.100 + # Encode pagenames from single-directory filenames.
1.101 +
1.102 + else:
1.103 + return "/".join(filename.split(self.level_sep))
1.104
1.105 # Serialisation methods.
1.106
1.107 @@ -137,14 +170,20 @@
1.108
1.109 filename = self.to_filename(pagename)
1.110
1.111 - # Make a directory for the page.
1.112 + # For hierarchical storage, store the page inside a directory bearing
1.113 + # its name.
1.114 +
1.115 + if self.level_sep == sep:
1.116 +
1.117 + # Make a directory for the page.
1.118
1.119 - if not self.dir.exists(filename):
1.120 - self.dir.makedirs(filename)
1.121 + if not self.dir.exists(filename):
1.122 + self.dir.makedirs(filename)
1.123
1.124 - # Write to an index filename within any existing directory.
1.125 + # Write to an index filename within any existing directory.
1.126
1.127 - filename = join(filename, self.index_name)
1.128 + filename = join(filename, self.index_name)
1.129 +
1.130 self.writefile(text, filename, encoding)
1.131
1.132 output = DirectoryOutput