Exemple #1
0
def resolve_notebook(string, pwd=None):
	'''Takes either a notebook name or a file or dir path. For a name
	it resolves the path by looking for a notebook of that name in the
	notebook list.
	Note that the L{NotebookInfo} for an file path is not using any
	actual info from the notebook, it just passes on the uri. Use
	L{build_notebook()} to split the URI in a notebook location and
	an optional page path.
	@returns: a L{NotebookInfo} or C{None}
	'''
	assert isinstance(string, str)
	from zim.fs import isabs

	if '/' in string or os.path.sep in string:
		# FIXME do we need a isfilepath() function in fs.py ?
		if is_url_re.match(string):
			uri = string
		elif pwd and not isabs(string):
			uri = pwd + os.sep + string
		else:
			uri = string
		return NotebookInfo(uri)
	else:
		nblist = get_notebook_list()
		return nblist.get_by_name(string)
Exemple #2
0
def pack_urilist(links):
	text = ''
	for link in links:
		if is_url_re.match(link):
			link = url_encode(link, mode=URL_ENCODE_READABLE) # just to be sure
		text += '%s\r\n' % link
	return text.encode()
Exemple #3
0
	def __init__(self, uri, name=None, icon=None, mtime=None, interwiki=None, **a):
		'''Constructor

		Known values for C{name}, C{icon} etc. can be specified.
		Alternatively L{update()} can be called to read there from the
		notebook configuration (if any). If C{mtime} is given the
		object acts as a cache and L{update()} will only read the config
		if it is newer than C{mtime}

		@param uri: location uri or file path for the notebook (esp. C{user_path})
		@param name: notebook name
		@param icon: the notebook icon path
		@param mtime: the mtime when config was last read
		@param interwiki: the interwiki keyword for this notebook
		@param a: any additional arguments will be discarded
		'''
		# **a is added to be future proof of unknown values in the cache
		if isinstance(uri, str) \
		and is_url_re.match(uri) and not uri.startswith('file://'):
			self.uri = uri
			self.user_path = None
			self.name = name
		else:
			f = File(uri)
			self.uri = f.uri
			self.user_path = f.user_path # set to None when uri is not a file uri
			self.name = name or f.basename
		self.icon_path = icon
		self.icon = File(icon).uri
		self.mtime = mtime
		self.interwiki = interwiki
		self.active = None
Exemple #4
0
def interwiki_link(link):
	'''Convert an interwiki link into an url'''
	assert isinstance(link, basestring) and '?' in link
	key, page = link.split('?', 1)
	url = None
	for line in config_file('urls.list'):
		if line.startswith(key+' ') or line.startswith(key+'\t'):
			url = line[len(key):].strip()
			break
	else:
		list = get_notebook_list()
		for name, path in list.get_names():
			if name.lower() == key.lower():
				url = path + '?{NAME}'
				break

	if url and is_url_re.match(url):
		if not ('{NAME}' in url or '{URL}' in url):
			url += '{URL}'

		url = url.replace('{NAME}', page)
		url = url.replace('{URL}', url_encode(page))

		return url
	else:
		return None
Exemple #5
0
def pack_urilist(links):
	text = ''
	for link in links:
		link = link.encode('utf-8')
		if is_url_re.match(link):
			link = url_encode(link, mode=URL_ENCODE_READABLE) # just to be sure
		text += '%s\r\n' % link
	return text
Exemple #6
0
 def decode_urls(self):
     """Calls decode_url() on all links that contain urls"""
     for link in self.getiterator("link"):
         href = link.attrib["href"]
         if is_url_re.match(href):
             link.attrib["href"] = url_decode(href)
             if link.text == href:
                 link.text = link.attrib["href"]
Exemple #7
0
	def decode_urls(self, mode=URL_ENCODE_READABLE):
		'''Calls decode_url() on all links that contain urls.
		See zim.parsing for details. Modifies the parse tree.
		'''
		for link in self._etree.getiterator('link'):
			href = link.attrib['href']
			if href and is_url_re.match(href):
				link.attrib['href'] = url_decode(href, mode=mode)
				if link.text == href:
					link.text = link.attrib['href']
Exemple #8
0
	def decode_urls(self, mode=URL_ENCODE_READABLE):
		'''Calls decode_url() on all links that contain urls.
		See zim.parsing for details. Modifies the parse tree.
		'''
		for link in self._etree.getiterator('link'):
			href = link.attrib['href']
			if is_url_re.match(href):
				link.attrib['href'] = url_decode(href, mode=mode)
				if link.text == href:
					link.text = link.attrib['href']
Exemple #9
0
def resolve_notebook(string):
	'''Takes either a notebook name or a file or dir path. For a name
	it resolves the path by looking for a notebook of that name in the
	notebook list. For a path it checks if this path points to a
	notebook or to a file in a notebook.

	It returns two values, a path to the notebook directory and an
	optional page path for a file inside a notebook. If the notebook
	was not found both values are None.
	'''
	assert isinstance(string, basestring)

	page = None
	if is_url_re.match(string):
		assert string.startswith('file://')
		if '?' in string:
			filepath, page = string.split('?', 1)
		else:
			filepath = string
	elif os.path.sep in string:
		filepath = string
	else:
		nblist = get_notebook_list()
		filepath = nblist.get_by_name(string)
		if filepath is None:
			return None, None # not found

	file = File(filepath) # Fixme need generic FS Path object here
	if filepath.endswith('notebook.zim'):
		return File(filepath).dir, page
	elif file.exists(): # file exists and really is a file
		parents = list(file)
		parents.reverse()
		for parent in parents:
			if File((parent, 'notebook.zim')).exists():
				page = file.relpath(parent)
				if '.' in page:
					page, _ = page.rsplit('.', 1) # remove extension
				page = Path(page.replace('/', ':'))
				return Dir(parent), page
		else:
			return None, None
	else:
		return Dir(file.path), page

	return notebook, path