1.1 --- a/moinformat/output/directory.py Mon Aug 06 15:45:11 2018 +0200
1.2 +++ b/moinformat/output/directory.py Mon Aug 06 15:50:16 2018 +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, split, splitext
1.8 +from os.path import extsep, join
1.9
1.10 class DirectoryOutput(Output):
1.11
1.12 @@ -42,9 +42,40 @@
1.13
1.14 self.index_name = self.parameters.get("index_name") or "index.html"
1.15 self.page_suffix = self.parameters.get("page_suffix") or "%shtml" % extsep
1.16 + self.root_pagename = self.parameters.get("root_pagename") or "FrontPage"
1.17
1.18 # Convenience methods.
1.19
1.20 + def ensure(self, pagename):
1.21 +
1.22 + "Ensure that the given 'pagename' exists."
1.23 +
1.24 + if not pagename:
1.25 + return None
1.26 +
1.27 + self.dir.ensure(self.to_filename(pagename))
1.28 +
1.29 + def ensure_attachments(self, pagename):
1.30 +
1.31 + "Ensure that attachment storage for the given 'pagename' exists."
1.32 +
1.33 + if not pagename:
1.34 + return None
1.35 +
1.36 + self.dir.ensure(join(self.to_filename(pagename), "attachments"))
1.37 +
1.38 + def get_attachment_filename(self, pagename, filename):
1.39 +
1.40 + """
1.41 + Return the full path of an attachment file for the given 'pagename'
1.42 + having the given 'filename'.
1.43 + """
1.44 +
1.45 + if not pagename:
1.46 + return None
1.47 +
1.48 + return self.dir.get_filename(join(self.to_filename(pagename), "attachments", filename))
1.49 +
1.50 def get_filename(self, filename):
1.51
1.52 """
1.53 @@ -60,22 +91,18 @@
1.54
1.55 "Return the filename corresponding to 'pagename'."
1.56
1.57 - return "%s%s" % (pagename, self.page_suffix)
1.58 + # For the root page, use the top-level directory.
1.59 +
1.60 + if pagename == self.root_pagename:
1.61 + return ""
1.62 + else:
1.63 + return pagename
1.64
1.65 def to_pagename(self, filename):
1.66
1.67 "Return the pagename corresponding to 'filename'."
1.68
1.69 - # Take the leafname as the pagename from an arbitrary filename, removing
1.70 - # any file extension.
1.71 -
1.72 - return splitext(split(filename)[-1])[0]
1.73 -
1.74 - def to_parent_filename(self, pagename):
1.75 -
1.76 - "Return the parent page filename corresponding to 'pagename'."
1.77 -
1.78 - return pagename
1.79 + return self.within(filename)
1.80
1.81 # Serialisation methods.
1.82
1.83 @@ -102,39 +129,16 @@
1.84 the page is a parent of other pages.
1.85 """
1.86
1.87 - dir = self.dir
1.88 - parent = self.parent(pagename)
1.89 -
1.90 - # The page may have a parent.
1.91 -
1.92 - if parent:
1.93 - parentfile = self.to_filename(parent)
1.94 - parentdir = self.to_parent_filename(parent)
1.95 -
1.96 - # Relocate any file for the parent to an index file within a page
1.97 - # directory.
1.98 + filename = self.to_filename(pagename)
1.99
1.100 - if dir.isfile(parentfile):
1.101 - parent_tmp = "%s.tmp" % parentfile
1.102 - dir.rename(parentfile, parent_tmp)
1.103 - if not dir.exists(parentdir):
1.104 - dir.makedirs(parentdir)
1.105 - dir.rename(parent_tmp, join(parentdir, self.index_name))
1.106 + # Make a directory for the page.
1.107
1.108 - # Or make a directory for the parent.
1.109 -
1.110 - elif not dir.exists(parentdir):
1.111 - dir.makedirs(parentdir)
1.112 + if not self.dir.exists(filename):
1.113 + self.dir.makedirs(filename)
1.114
1.115 # Write to an index filename within any existing directory.
1.116
1.117 - dirname = self.to_parent_filename(pagename)
1.118 -
1.119 - if dir.isdir(dirname):
1.120 - filename = join(dirname, self.index_name)
1.121 - else:
1.122 - filename = self.to_filename(pagename)
1.123 -
1.124 + filename = join(filename, self.index_name)
1.125 self.writefile(text, filename, encoding)
1.126
1.127 output = DirectoryOutput