110
110
lines = [line + '\n' for line in lines]
111
111
self.file.writelines(lines)
113
def get_default(self):
114
'''Returns uri for the default notebook'''
113
122
def get_names(self):
114
123
'''Generator function that yield tuples with the notebook
115
124
name and the notebook path.
131
141
def get_by_name(self, name):
142
'''Get the uri for a notebook'''
132
143
for n, path in self.get_names():
133
144
if n.lower() == name.lower():
1173
1179
page.set_parsetree(tree)
1175
def resolve_file(self, filename, path):
1176
'''Resolves a file or directory path relative to a page. Returns a
1177
File object. However the file does not have to exist.
1181
def resolve_file(self, filename, path=None):
1182
'''Resolves a file or directory path relative to a page or
1183
Notebook. Returns a File object. However the file does not
1179
File urls and paths that start with '~/' or '~user/' are considered
1180
absolute paths and are returned unmodified.
1186
File urls and paths that start with '~/' or '~user/' are
1187
considered absolute paths and the corresponding File objects
1188
are returned. Also handles windows absolute paths.
1182
1190
In case the file path starts with '/' the the path is taken relative
1183
1191
to the document root - this can e.g. be a parent directory of the
1184
notebook. Defaults to the home dir.
1192
notebook. Defaults to the filesystem root when no document root
1186
1195
Other paths are considered attachments and are resolved relative
1187
to the namespce below the page.
1189
Because this is used to resolve file links and is supposed to be
1190
platform independent it tries to convert windows filenames to
1196
to the namespace below the page. If no path is given but the
1197
notebook has a root folder, this folder is used as base path.
1199
assert isinstance(filename, basestring)
1193
1200
filename = filename.replace('\\', '/')
1194
1201
if filename.startswith('~') or filename.startswith('file:/'):
1195
1202
return File(filename)
1202
1209
# make absolute on unix
1203
1210
return File(filename)
1205
# TODO - how to deal with '..' in the middle of the path ?
1206
filepath = [p for p in filename.split('/') if len(p) and p != '.']
1207
if not filepath: # filename is e.g. "."
1208
return self.get_attachments_dir(path)
1209
pagepath = path.name.split(':')
1210
filename = filepath.pop()
1211
while filepath and filepath[0] == '..':
1213
print 'TODO: handle paths relative to notebook but outside notebook dir'
1214
return File('/TODO')
1218
pagename = ':'+':'.join(pagepath + filepath)
1219
dir = self.get_attachments_dir(Path(pagename))
1220
return dir.file(filename)
1213
dir = self.get_attachments_dir(path)
1215
assert self.dir, 'Can not resolve relative path for notebook without root folder'
1218
return File((dir, filename))
1222
1220
def relative_filepath(self, file, path=None):
1223
'''Returns a filepath relative to either the documents dir (/xxx), the
1224
attachments dir (if a path is given) (./xxx or ../xxx) or the users
1225
home dir (~/xxx). Returns None otherwise.
1221
'''Returns a filepath relative to either the documents dir
1222
(/xxx), the attachments dir (if a path is given) or the notebook
1223
folder (./xxx or ../xxx) or the users home dir (~/xxx).
1224
Returns None otherwise.
1227
Intended as the counter part of resolve_file().
1228
Typically this function is used to present the user with readable paths
1229
or to shorten the paths inserted in the wiki code. It is advised to
1226
Intended as the counter part of resolve_file(). Typically this
1227
function is used to present the user with readable paths or to
1228
shorten the paths inserted in the wiki code. It is advised to
1230
1229
use file uris for links that can not be made relative.
1234
1233
dir = self.get_attachments_dir(path)
1235
1234
if file.ischild(dir):
1236
1235
return './'+file.relpath(dir)
1240
1239
downpath = file.relpath(parent)
1241
1240
up = 1 + uppath.count('/')
1242
1241
return '../'*up + downpath
1242
elif root and file.ischild(root):
1243
return './'+file.relpath(root)
1244
1245
dir = self.get_document_root()
1245
1246
if dir and file.ischild(dir):