From 78c7a9252b70459bc3ed0a0f54d67630e7c18ae1 Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Tue, 19 Mar 2013 20:13:01 +0100 Subject: Expand internal links for split html documents Internal links need to be converted to page.html#section_id links so they work accross split html documents --- panfry/document.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'panfry/document.py') diff --git a/panfry/document.py b/panfry/document.py index aa29561..ce3a0ca 100755 --- a/panfry/document.py +++ b/panfry/document.py @@ -29,6 +29,39 @@ class Document: def set_templater(self, templater): self.templater = templater + def expand_int_links(self, source): + ''' + Converts all internal links to full url links + so that links reference accross the split up html pages + + Arguments: + source = original markdown source + + returns: markdown source with expanded links + ''' + re_link1 = re.compile(r'(\[(?P[^\]^]+)\])\((?P#.*)\)', re.S) + re_link2 = re.compile(r'(\[(?P[^\]^]+)\]): (?P#[a-z0-9-]+)', + re.S) + re_link3 = re.compile(r'(\[(?P[^\]^]+)\][^(])', re.S) + source = re_link1.sub(self._get_cross_link, source) + source = re_link2.sub(self._get_cross_link, source) + source = re_link3.sub(self._get_cross_link, source) + + return source + + def _get_cross_link(self, m): + print('get_link: %s' % m.groupdict()) + ref_txt = m.group('s') + if 'l' in m.groupdict(): + header = m.group('l')[1:] + else: + header = ref_txt.lower().replace(' ', '-') + + for page in self.pages: + for tocitem in page.toc: + if tocitem.section_id == header: + return '[%s](%s#%s)' % (ref_txt, page.htmlfile, header) + @property def title(self): if self.header.firstline.startswith('%'): @@ -165,7 +198,8 @@ class Document: doc.add_argument('toc') doc.add_argument('template=%s' % template_file.name) doc.add_argument('css=%s' % self.css_file) - doc.markdown = '%s\n%s' % (self.header.source, page.source) + doc.markdown = '%s\n%s' % (self.header.source, + self.expand_int_links(page.source)) content = doc.html write_file(os.path.join(pub_path, page.htmlfile), unicode(content, 'utf-8')) -- cgit v1.2.3