summaryrefslogtreecommitdiff
path: root/panfry
diff options
context:
space:
mode:
Diffstat (limited to 'panfry')
-rwxr-xr-xpanfry/document.py76
-rwxr-xr-xpanfry/main.py30
-rwxr-xr-xpanfry/page.py11
-rwxr-xr-xpanfry/templater.py4
4 files changed, 87 insertions, 34 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'))
diff --git a/panfry/main.py b/panfry/main.py
index 1b90181..5d05d20 100755
--- a/panfry/main.py
+++ b/panfry/main.py
@@ -33,13 +33,20 @@ class StoppableHttpServer(BaseHTTPServer.HTTPServer):
self.handle_request()
+def get_INDEX(path, index):
+ INDEX = os.path.join('src', path, 'INDEX')
+ if os.path.isfile(INDEX):
+ for line in open(INDEX, 'r').readlines():
+ newpath = os.path.join(path, line.strip())
+ index = get_INDEX(newpath, index)
+ index.append(newpath)
+ return index
+
def get_env():
env = panfry.cli.init_argparser().parse_args()
env.pub_path = os.path.abspath(env.pub_path)
- env.index = ['']
if env.cmd in ['gen']:
- INDEX = os.path.join(env.doc_path, 'src', 'INDEX')
if not os.path.isdir(env.doc_path):
print("%s directory not found. Aborting..." % env.doc_path)
exit(1)
@@ -54,8 +61,8 @@ def get_env():
if env.pandoc_options:
env.pandoc_options = list(shlex.split(env.pandoc_options))
- if os.path.isfile(INDEX):
- env.index += open(INDEX, 'r').readlines()
+ env.index = ['']
+ env.index = get_INDEX('', env.index)
return env
@@ -63,15 +70,16 @@ def get_env():
def gen(env):
for item in env.index:
item = item.strip()
+ print("Processing: %s..." % item)
docpubpath = os.path.join(env.pub_path, item)
document = Document(env.doc_path, item)
- document.set_templater(Templater(env.templates_path))
- document.set_pandoc_options(env.pandoc_options)
+ document.templater = Templater(env.templates_path)
+ document.pandoc_options = env.pandoc_options
if env.simple_toc:
- document.set_simple_toc
+ document.toc_type = 'simple'
if env.json_toc:
- document.set_json_toc
+ document.toc_type = 'json'
if env.clean and os.path.exists(env.pub_path):
print("Cleanup, removing %s..." % env.pub_path)
@@ -93,11 +101,13 @@ def gen(env):
###### Create PDF
pdffile = document.publish_pdf(docpubpath)
- print("Wrote PDF: %s" % pdffile)
+ if pdffile:
+ print("Wrote PDF: %s" % pdffile)
###### Create epub
epubfile = document.publish_epub(docpubpath)
- print("Wrote epub: %s" % epubfile)
+ if epubfile:
+ print("Wrote epub: %s" % epubfile)
###### Create HTML
document.publish_html(docpubpath)
diff --git a/panfry/page.py b/panfry/page.py
index efd3561..b3bd78c 100755
--- a/panfry/page.py
+++ b/panfry/page.py
@@ -85,7 +85,18 @@ class Page(TextFile):
'''
toc = []
heading = ''
+ code = 0
for line in self.markdown.split('\n'):
+ # Ignore if we are inside fenced code block
+ if line.startswith('~~~') or line.startswith('```'):
+ if not code:
+ ### FIXME. Need better way to get length of ~~
+ ### or `` characters.
+ code = len(line.strip())
+ elif len(line.strip()) >= code:
+ code = 0
+ if code:
+ continue
line = line.strip()
if heading and re.match('[=]{2}', line):
toc.append(TOCItem(heading, 1, self.htmlfile))
diff --git a/panfry/templater.py b/panfry/templater.py
index 40fc17a..2cd9204 100755
--- a/panfry/templater.py
+++ b/panfry/templater.py
@@ -17,7 +17,9 @@ class Templater:
def __init__(self, path):
self.templates_path = path
self.j2 = Environment(loader=FileSystemLoader(self.templates_path),
- line_statement_prefix='#')
+ line_statement_prefix='#',
+ extensions=['jinja2.ext.loopcontrols'],
+ )
def page_template(self, doc, page):
'''