diff options
| author | Scott Bahling <sbahling@mudgum.net> | 2013-03-19 20:13:01 +0100 |
|---|---|---|
| committer | Scott Bahling <sbahling@mudgum.net> | 2013-03-19 20:13:01 +0100 |
| commit | 78c7a9252b70459bc3ed0a0f54d67630e7c18ae1 (patch) | |
| tree | ff1fd65eb7a6f300d7c799b6d9572c392d3b6a1c /panfry/document.py | |
| parent | ae99cfbd9a0372b577a23636f0677f266a559c5f (diff) | |
| download | panfry-78c7a9252b70459bc3ed0a0f54d67630e7c18ae1.tar.gz panfry-78c7a9252b70459bc3ed0a0f54d67630e7c18ae1.tar.xz panfry-78c7a9252b70459bc3ed0a0f54d67630e7c18ae1.zip | |
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
Diffstat (limited to 'panfry/document.py')
| -rwxr-xr-x | panfry/document.py | 36 |
1 files changed, 35 insertions, 1 deletions
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<s>[^\]^]+)\])\((?P<l>#.*)\)', re.S) + re_link2 = re.compile(r'(\[(?P<s>[^\]^]+)\]): (?P<l>#[a-z0-9-]+)', + re.S) + re_link3 = re.compile(r'(\[(?P<s>[^\]^]+)\][^(])', 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')) |
