def update_links(self, source, target, links_re = compile(r'(\.\. .*?: )(\S*)')): old_data = self.get_text() new_data = [] not_uri = 0 base = self.parent.abspath for segment in links_re.split(old_data): not_uri = (not_uri + 1) % 3 if not not_uri: reference = get_reference(segment) # Skip external link if is_external(reference): new_data.append(segment) continue # Strip the view path = reference.path if path and path[-1] == ';download': path = path[:-1] view = '/;download' else: view = '' # Resolve the path path = base.resolve(path) # Match ? if path == source: segment = str(base.get_pathto(target)) + view new_data.append(segment) new_data = ''.join(new_data) self.get_value('data').load_state_from_string(new_data) get_context().database.change_resource(self)
def get_links(self): base = self.abspath try: doctree = self.get_doctree() except SystemMessage: # The doctree is in a incoherent state return set() # Links links = set() for node in doctree.traverse(condition=nodes.reference): refname = node.get('wiki_name') if refname is False: # Wiki link not found title = node['name'] path = checkid(title) or title path = base.resolve(path) elif refname: # Wiki link found, "refname" is the path path = base.resolve2(refname) else: # Regular link, include internal ones refuri = node.get('refuri') if refuri is None: continue reference = get_reference(refuri.encode('utf_8')) # Skip external if is_external(reference): continue path = base.resolve2(reference.path) path = str(path) links.add(path) # Images for node in doctree.traverse(condition=nodes.image): reference = get_reference(node['uri'].encode('utf_8')) # Skip external image if is_external(reference): continue # Resolve the path path = base.resolve(reference.path) path = str(path) links.add(path) return links
def get_doctree(self): parent = self.parent # Override dandling links handling class WikiReader(StandaloneReader): supported = ('wiki',) def wiki_reference_resolver(target): title = target['name'] resource = self.resolve_link(title) if resource is None: # Not Found target['wiki_name'] = False else: # Found target['wiki_name'] = str(resource.abspath) return True wiki_reference_resolver.priority = 851 unknown_reference_resolvers = [wiki_reference_resolver] # Publish! reader = WikiReader(parser_name='restructuredtext') doctree = publish_doctree(self.get_text(), reader=reader, settings_overrides=self.overrides) # Assume internal paths are relative to the container for node in doctree.traverse(condition=nodes.reference): refuri = node.get('refuri') # Skip wiki or fragment link if node.get('wiki_name') or not refuri: continue reference = get_reference(refuri.encode('utf_8')) # Skip external if is_external(reference): continue # Resolve absolute path resource = self.get_resource(reference.path, soft=True) if resource is None: resource = parent.get_resource(reference.path, soft=True) if resource is None: continue refuri = str(resource.abspath) # Restore fragment if reference.fragment: refuri = "%s#%s" % (refuri, reference.fragment) node['refuri'] = refuri # Assume image paths are relative to the container for node in doctree.traverse(condition=nodes.image): reference = get_reference(node['uri'].encode('utf_8')) # Skip external if is_external(reference): continue # Strip the view path = reference.path if path[-1][0] == ';': path = path[:-1] # Resolve absolute path resource = parent.get_resource(path, soft=True) if resource is not None: node['uri'] = str(resource.abspath) return doctree