summaryrefslogtreecommitdiff
path: root/panfry/document.py
diff options
context:
space:
mode:
Diffstat (limited to 'panfry/document.py')
-rwxr-xr-xpanfry/document.py76
1 files changed, 53 insertions, 23 deletions
diff --git a/panfry/document.py b/panfry/document.py
index e03aca1..5fdd05c 100755
--- a/panfry/document.py
+++ b/panfry/document.py
@@ -10,38 +10,54 @@ from panfry.page import TextFile, Page
from panfry.toc import TOCItem
-class Document:
+class Document(object):
+ 'A panfry documment'
+
+ valid_toc_types = ('simple',
+ 'full',
+ 'json',
+ )
+
+ standalone_toc_types = ('full')
def __init__(self, path, subdoc=''):
self.src_path = os.path.join(path, 'src', subdoc)
self.meta = self.get_meta(self.src_path)
self.pages = self.get_pages(self.src_path)
- self.full_toc = True
+ self._toc_type = 'full'
self.standalone = True
- self.json_toc = False
+ self.template_path = os.path.join(path, 'templates')
self.css_file = 'css/style.css'
- self.assets_dir = 'assets'
+ self.assets_dir = os.path.join('assets', subdoc)
self.pandoc_options = []
- @property
- def set_simple_toc(self):
- self.full_toc = False
@property
- def set_json_toc(self):
- self.json_toc = True
- self.standalone = False
+ def no_pdf(self):
+ if 'NOPDF' in os.listdir(self.src_path):
+ return True
+ return False
- def set_pandoc_options(self, options):
- self.pandoc_options = options
+ @property
+ def no_epub(self):
+ if 'NOEPUB' in os.listdir(self.src_path):
+ return True
+ return False
@property
- def set_full_toc(self):
- self.full_toc = True
+ def toc_type(self):
+ return self._toc_type
+
+ @toc_type.setter
+ def toc_type(self, toc_type):
+ if toc_type in self.valid_toc_types:
+ self._toc_type = toc_type
+ self.standalone = toc_type in self.standalone_toc_types
+ else:
+ return ''
- def set_templater(self, templater):
- self.templater = templater
+ return toc_type
def expand_int_links(self, source):
'''
@@ -53,9 +69,9 @@ class Document:
returns: markdown source with expanded links
'''
- re_link1 = re.compile(r'(\[(?P<s>[^\]^]+)\])\((?P<l>#.*)\)', re.S)
- re_link2 = re.compile(r'(\[(?P<s>[^\]^]+)\][^(])', re.S)
- re_link3 = re.compile(r'(\[(?P<s>[^\]^]+)\])\[(?P<h>[^\]].*)\]', re.S)
+ re_link1 = re.compile(r'(\[(?P<s>[^\]^]+)\])\((?P<l>#[^\)]*)\)', re.S)
+ re_link2 = re.compile(r'(\[(?P<s>[^\]^]+)\])\[(?P<h>[^\]]*)\]', 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)
@@ -76,7 +92,7 @@ class Document:
tocitem.header_id,
)
- return '[%s]' % ref_txt
+ return '[%s]' % heading
@property
def title(self):
@@ -146,7 +162,7 @@ class Document:
'''
links = []
for page in self.pages:
- if self.full_toc:
+ if self.toc_type == 'full':
toc = page.toc
else:
toc = [page.toc[0]]
@@ -212,6 +228,8 @@ class Document:
return self.filename_base + '.epub'
def publish_pdf(self, pub_path):
+ if self.no_pdf:
+ return ''
pdf_path = os.path.join(pub_path, self.pdf_filename)
src = self.title_block.source
for page in self.pages:
@@ -221,7 +239,11 @@ class Document:
doc.markdown = src
pandoc.set_cwd(os.path.abspath(self.src_path))
- doc.add_argument('latex-engine=xelatex')
+ template = os.path.abspath(os.path.join(self.template_path, 'tex.template'))
+ if os.path.isfile(template):
+ print(template)
+ doc.add_argument('template=%s' % template)
+ doc.add_argument('latex-engine=lualatex')
for option in self.pandoc_options:
doc.add_argument(option)
doc.to_file(pdf_path)
@@ -230,6 +252,8 @@ class Document:
return self.pdf_filename
def publish_epub(self, pub_path):
+ if self.no_epub:
+ return ''
epub_path = os.path.join(pub_path, self.epub_filename)
src = self.title_block.source
for page in self.pages:
@@ -238,6 +262,10 @@ class Document:
doc = pandoc.Document()
doc.markdown = src
+ css = os.path.abspath(os.path.join(self.template_path, 'epub.css'))
+ if os.path.isfile(css):
+ print(css)
+ doc.add_argument('epub-stylesheet=%s' % css)
print("epub_path: %s" % epub_path)
pandoc.set_cwd(os.path.abspath(self.src_path))
for option in self.pandoc_options:
@@ -255,6 +283,8 @@ class Document:
dst = os.path.join(pub_path, asset)
if os.path.isdir(src):
copy(src, dst)
+ else:
+ copy(src, dst)
def publish_html(self, pub_path):
pandoc.set_cwd(None)
@@ -299,7 +329,7 @@ class Document:
os.remove(ref)
os.symlink(src, ref)
- if self.json_toc:
+ if self.toc_type == 'json':
write_file(os.path.join(pub_path, 'toc.json'),
unicode(self.toc_json, 'utf-8'))