paul@100 | 1 | #!/usr/bin/env python |
paul@100 | 2 | |
paul@100 | 3 | """ |
paul@100 | 4 | Directory output context. |
paul@100 | 5 | |
paul@100 | 6 | Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@100 | 7 | |
paul@100 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@100 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@100 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@100 | 11 | version. |
paul@100 | 12 | |
paul@100 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@100 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@100 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@100 | 16 | details. |
paul@100 | 17 | |
paul@100 | 18 | You should have received a copy of the GNU General Public License along with |
paul@100 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@100 | 20 | """ |
paul@100 | 21 | |
paul@100 | 22 | from moinformat.output.common import Output |
paul@104 | 23 | from moinformat.utils.directory import Directory |
paul@140 | 24 | from os.path import extsep, join, split, splitext |
paul@100 | 25 | |
paul@137 | 26 | class DirectoryOutput(Output): |
paul@100 | 27 | |
paul@100 | 28 | "A directory output context." |
paul@100 | 29 | |
paul@104 | 30 | name = "directory" |
paul@100 | 31 | |
paul@104 | 32 | def __init__(self, parameters=None): |
paul@100 | 33 | |
paul@104 | 34 | "Initialise the context with the given 'parameters'." |
paul@100 | 35 | |
paul@104 | 36 | if not parameters or not parameters.has_key("filename"): |
paul@104 | 37 | raise ValueError, parameters |
paul@100 | 38 | |
paul@104 | 39 | Output.__init__(self, parameters) |
paul@137 | 40 | self.dir = Directory(parameters["filename"]) |
paul@137 | 41 | self.dir.ensure() |
paul@140 | 42 | |
paul@140 | 43 | self.index_name = self.parameters.get("index_name") or "index.html" |
paul@140 | 44 | self.page_suffix = self.parameters.get("page_suffix") or "%shtml" % extsep |
paul@137 | 45 | |
paul@137 | 46 | # Convenience methods. |
paul@100 | 47 | |
paul@137 | 48 | def get_filename(self, filename): |
paul@131 | 49 | |
paul@137 | 50 | """ |
paul@137 | 51 | Return the full path of a file with the given 'filename' found within |
paul@137 | 52 | the directory. The full path is an absolute path. |
paul@137 | 53 | """ |
paul@137 | 54 | |
paul@137 | 55 | return self.dir.get_filename(filename) |
paul@131 | 56 | |
paul@131 | 57 | # Name translation methods. |
paul@131 | 58 | |
paul@131 | 59 | def to_filename(self, pagename): |
paul@131 | 60 | |
paul@131 | 61 | "Return the filename corresponding to 'pagename'." |
paul@131 | 62 | |
paul@140 | 63 | return "%s%s" % (pagename, self.page_suffix) |
paul@131 | 64 | |
paul@131 | 65 | def to_pagename(self, filename): |
paul@131 | 66 | |
paul@131 | 67 | "Return the pagename corresponding to 'filename'." |
paul@131 | 68 | |
paul@140 | 69 | # Take the leafname as the pagename from an arbitrary filename, removing |
paul@140 | 70 | # any file extension. |
paul@140 | 71 | |
paul@140 | 72 | return splitext(split(filename)[-1])[0] |
paul@131 | 73 | |
paul@140 | 74 | def to_parent_filename(self, pagename): |
paul@140 | 75 | |
paul@140 | 76 | "Return the parent page filename corresponding to 'pagename'." |
paul@140 | 77 | |
paul@140 | 78 | return pagename |
paul@131 | 79 | |
paul@131 | 80 | # Serialisation methods. |
paul@131 | 81 | |
paul@131 | 82 | def can_write(self): |
paul@131 | 83 | |
paul@131 | 84 | "Return whether this context supports page writing." |
paul@131 | 85 | |
paul@131 | 86 | return True |
paul@131 | 87 | |
paul@131 | 88 | def writefile(self, text, filename, encoding=None): |
paul@131 | 89 | |
paul@131 | 90 | """ |
paul@131 | 91 | Write 'text' to the file having the given 'filename'. If the |
paul@131 | 92 | optional 'encoding' is specified, override the general encoding. |
paul@131 | 93 | """ |
paul@131 | 94 | |
paul@137 | 95 | return self.writepath(text, self.dir.get_filename(filename), encoding) |
paul@131 | 96 | |
paul@131 | 97 | def writepage(self, text, pagename, encoding=None): |
paul@131 | 98 | |
paul@131 | 99 | """ |
paul@131 | 100 | Write 'text' to the file having the given 'pagename' and optional |
paul@131 | 101 | 'encoding'. If 'parent' is specified and a true value, it indicates that |
paul@131 | 102 | the page is a parent of other pages. |
paul@131 | 103 | """ |
paul@131 | 104 | |
paul@137 | 105 | dir = self.dir |
paul@140 | 106 | parent = self.parent(pagename) |
paul@131 | 107 | |
paul@131 | 108 | # The page may have a parent. |
paul@131 | 109 | |
paul@140 | 110 | if parent: |
paul@140 | 111 | parentfile = self.to_filename(parent) |
paul@140 | 112 | parentdir = self.to_parent_filename(parent) |
paul@131 | 113 | |
paul@131 | 114 | # Relocate any file for the parent to an index file within a page |
paul@131 | 115 | # directory. |
paul@131 | 116 | |
paul@140 | 117 | if dir.isfile(parentfile): |
paul@140 | 118 | parent_tmp = "%s.tmp" % parentfile |
paul@140 | 119 | dir.rename(parentfile, parent_tmp) |
paul@140 | 120 | if not dir.exists(parentdir): |
paul@140 | 121 | dir.makedirs(parentdir) |
paul@140 | 122 | dir.rename(parent_tmp, join(parentdir, self.index_name)) |
paul@131 | 123 | |
paul@131 | 124 | # Or make a directory for the parent. |
paul@131 | 125 | |
paul@140 | 126 | elif not dir.exists(parentdir): |
paul@140 | 127 | dir.makedirs(parentdir) |
paul@131 | 128 | |
paul@131 | 129 | # Write to an index filename within any existing directory. |
paul@131 | 130 | |
paul@140 | 131 | dirname = self.to_parent_filename(pagename) |
paul@140 | 132 | |
paul@140 | 133 | if dir.isdir(dirname): |
paul@140 | 134 | filename = join(dirname, self.index_name) |
paul@140 | 135 | else: |
paul@140 | 136 | filename = self.to_filename(pagename) |
paul@131 | 137 | |
paul@137 | 138 | self.writefile(text, filename, encoding) |
paul@131 | 139 | |
paul@104 | 140 | output = DirectoryOutput |
paul@100 | 141 | |
paul@100 | 142 | # vim: tabstop=4 expandtab shiftwidth=4 |