1.1 --- a/moinformat/input/common.py Sun Aug 05 18:13:36 2018 +0200
1.2 +++ b/moinformat/input/common.py Sun Aug 05 18:15:54 2018 +0200
1.3 @@ -19,6 +19,7 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +from os.path import split
1.8 import codecs
1.9
1.10 class Input:
1.11 @@ -34,9 +35,51 @@
1.12 self.parameters = parameters
1.13 self.encoding = parameters and parameters.get("encoding") or self.default_encoding
1.14
1.15 + def all(self):
1.16 +
1.17 + "Return all pages in the context."
1.18 +
1.19 + return []
1.20 +
1.21 + # Page characteristics.
1.22 +
1.23 + def parent(self, pagename):
1.24 +
1.25 + "Return the parent of 'pagename'."
1.26 +
1.27 + return pagename.rsplit("/", 1)[0]
1.28 +
1.29 + def subpages(self, pagename):
1.30 +
1.31 + "Return the subpages of 'pagename'."
1.32 +
1.33 + return []
1.34 +
1.35 + # Page access methods.
1.36 +
1.37 def readfile(self, filename, encoding=None):
1.38
1.39 """
1.40 + Return the contents of the file having the given 'filename' and optional
1.41 + 'encoding'. This implementation treats 'filename' as a path.
1.42 + """
1.43 +
1.44 + return self.readpath(filename, encoding)
1.45 +
1.46 + def readpage(self, pagename, encoding=None):
1.47 +
1.48 + """
1.49 + Return the contents of the file having the given 'pagename' and optional
1.50 + 'encoding'.
1.51 + """
1.52 +
1.53 + return self.readfile(self.to_filename(pagename), encoding)
1.54 +
1.55 + # Input methods.
1.56 +
1.57 + def readpath(self, filename, encoding=None):
1.58 +
1.59 + """
1.60 Return the contents of the file having the given 'filename'. If the
1.61 optional 'encoding' is specified, override the general encoding.
1.62 """
1.63 @@ -47,4 +90,20 @@
1.64 finally:
1.65 f.close()
1.66
1.67 + # Name translation methods.
1.68 +
1.69 + def to_filename(self, pagename):
1.70 +
1.71 + "Return the filename corresponding to 'pagename'."
1.72 +
1.73 + return pagename
1.74 +
1.75 + def to_pagename(self, filename):
1.76 +
1.77 + "Return the pagename corresponding to 'filename'."
1.78 +
1.79 + # Take the leafname as the pagename from an arbitrary filename.
1.80 +
1.81 + return split(filename)[-1]
1.82 +
1.83 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/moinformat/input/directory.py Sun Aug 05 18:13:36 2018 +0200
2.2 +++ b/moinformat/input/directory.py Sun Aug 05 18:15:54 2018 +0200
2.3 @@ -21,6 +21,7 @@
2.4
2.5 from moinformat.input.common import Input
2.6 from moinformat.utils.directory import Directory
2.7 +from os.path import sep
2.8
2.9 class DirectoryInput(Input, Directory):
2.10
2.11 @@ -38,6 +39,35 @@
2.12 Input.__init__(self, parameters)
2.13 Directory.__init__(self, parameters["filename"])
2.14
2.15 + # Support an encoding of the level separator for the filesystem.
2.16 + # Where it is the same as the directory separator, documents are stored
2.17 + # using nested directories, not as a flat list.
2.18 +
2.19 + self.level_sep = parameters and parameters.get("separator") or sep
2.20 +
2.21 + def all(self):
2.22 +
2.23 + "Return all pages in the context."
2.24 +
2.25 + return map(self.to_pagename, self.select_files("*"))
2.26 +
2.27 + # Page characteristics.
2.28 +
2.29 + def subpage_filenames(self, pagename):
2.30 +
2.31 + "Return the subpage filenames of 'pagename'."
2.32 +
2.33 + pattern = self.to_filename("%s/*" % pagename)
2.34 + return self.select_files(pattern)
2.35 +
2.36 + def subpages(self, pagename):
2.37 +
2.38 + "Return the subpages of 'pagename'."
2.39 +
2.40 + return map(self.to_pagename, self.subpage_filenames(pagename))
2.41 +
2.42 + # Page access methods.
2.43 +
2.44 def readfile(self, filename, encoding=None):
2.45
2.46 """
2.47 @@ -45,7 +75,27 @@
2.48 'encoding'.
2.49 """
2.50
2.51 - return Input.readfile(self, self.get_filename(filename), encoding)
2.52 + return self.readpath(self.get_filename(filename), encoding)
2.53 +
2.54 + # NOTE: Translation methods should encode filenames appropriately.
2.55 +
2.56 + def to_filename(self, pagename):
2.57 +
2.58 + "Return the filename corresponding to 'pagename'."
2.59 +
2.60 + if sep == self.level_sep:
2.61 + return pagename
2.62 + else:
2.63 + return self.level_sep.join(pagename.split("/"))
2.64 +
2.65 + def to_pagename(self, filename):
2.66 +
2.67 + "Return the pagename corresponding to 'filename'."
2.68 +
2.69 + if sep == self.level_sep:
2.70 + return filename
2.71 + else:
2.72 + return "/".join(filename.split(self.level_sep))
2.73
2.74 input = DirectoryInput
2.75
3.1 --- a/moinformat/input/standalone.py Sun Aug 05 18:13:36 2018 +0200
3.2 +++ b/moinformat/input/standalone.py Sun Aug 05 18:15:54 2018 +0200
3.3 @@ -40,15 +40,6 @@
3.4 f = codecs.getreader(self.encoding)(stream)
3.5 return f.read()
3.6
3.7 - def readfile(self, filename, encoding=None):
3.8 -
3.9 - """
3.10 - Return the contents of the file having the given 'filename'. If the
3.11 - optional 'encoding' is specified, override the general encoding.
3.12 - """
3.13 -
3.14 - return Input.readfile(self, filename, encoding)
3.15 -
3.16 input = StandaloneInput
3.17
3.18 # vim: tabstop=4 expandtab shiftwidth=4