summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xpanfry/document.py36
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'))