1.1 --- a/moinformat/input/directory.py Sun Aug 05 18:13:36 2018 +0200
1.2 +++ b/moinformat/input/directory.py Sun Aug 05 18:15:54 2018 +0200
1.3 @@ -21,6 +21,7 @@
1.4
1.5 from moinformat.input.common import Input
1.6 from moinformat.utils.directory import Directory
1.7 +from os.path import sep
1.8
1.9 class DirectoryInput(Input, Directory):
1.10
1.11 @@ -38,6 +39,35 @@
1.12 Input.__init__(self, parameters)
1.13 Directory.__init__(self, parameters["filename"])
1.14
1.15 + # Support an encoding of the level separator for the filesystem.
1.16 + # Where it is the same as the directory separator, documents are stored
1.17 + # using nested directories, not as a flat list.
1.18 +
1.19 + self.level_sep = parameters and parameters.get("separator") or sep
1.20 +
1.21 + def all(self):
1.22 +
1.23 + "Return all pages in the context."
1.24 +
1.25 + return map(self.to_pagename, self.select_files("*"))
1.26 +
1.27 + # Page characteristics.
1.28 +
1.29 + def subpage_filenames(self, pagename):
1.30 +
1.31 + "Return the subpage filenames of 'pagename'."
1.32 +
1.33 + pattern = self.to_filename("%s/*" % pagename)
1.34 + return self.select_files(pattern)
1.35 +
1.36 + def subpages(self, pagename):
1.37 +
1.38 + "Return the subpages of 'pagename'."
1.39 +
1.40 + return map(self.to_pagename, self.subpage_filenames(pagename))
1.41 +
1.42 + # Page access methods.
1.43 +
1.44 def readfile(self, filename, encoding=None):
1.45
1.46 """
1.47 @@ -45,7 +75,27 @@
1.48 'encoding'.
1.49 """
1.50
1.51 - return Input.readfile(self, self.get_filename(filename), encoding)
1.52 + return self.readpath(self.get_filename(filename), encoding)
1.53 +
1.54 + # NOTE: Translation methods should encode filenames appropriately.
1.55 +
1.56 + def to_filename(self, pagename):
1.57 +
1.58 + "Return the filename corresponding to 'pagename'."
1.59 +
1.60 + if sep == self.level_sep:
1.61 + return pagename
1.62 + else:
1.63 + return self.level_sep.join(pagename.split("/"))
1.64 +
1.65 + def to_pagename(self, filename):
1.66 +
1.67 + "Return the pagename corresponding to 'filename'."
1.68 +
1.69 + if sep == self.level_sep:
1.70 + return filename
1.71 + else:
1.72 + return "/".join(filename.split(self.level_sep))
1.73
1.74 input = DirectoryInput
1.75