def list_files_in_dir(dir, root_only=None): pages = [] files_to_yield = [] if root_only: files = glob.glob("%s/*.md" % dir) files.extend(glob.glob("%s/*.txt" % dir)) files.extend(glob.glob("%s/*.pdf" % dir)) else: files = glob.glob("%s/**/*.md" % dir, recursive=True) files.extend(glob.glob("%s/**/*.txt" % dir, recursive=True)) files.extend(glob.glob("%s/**/*.epub" % dir, recursive=True)) files.extend(glob.glob("%s/**/*.pdf" % dir, recursive=True)) for file in files: files_to_yield.append(file.replace('%s/' % dir, '')) for page in files_to_yield: path, filename = os.path.split(page) name, extension = filename.split('.')[-2:] if extension == 'md': if path: p = flatpages.get("%s/%s" % (path, name)) elif name != "index": # Don't include or it'll override actual p = flatpages.get("%s" % (name)) else: p = None if p and is_published(p): if ('POST_LINK_STYLE' in app.config and app.config['POST_LINK_STYLE'] == "date" and path.startswith(app.config['POST_DIRECTORY'])): path = path.replace("%s/" % app.config['POST_DIRECTORY'], '') pages.append({'name': name, 'dir': path, 'ext': 'html'}) pages.append({'name': name, 'dir': path, 'ext': 'md'}) else: pages.append({'name': name, 'dir': path, 'ext': extension}) return pages
def index(): if app.config['SITE_STYLE'] == "static": p = flatpages.get("index") return render_template('index_static.html', post=p, site=app.config) elif app.config['SITE_STYLE'] == "hybrid": page = flatpages.get("index") fp = helpers.get_featured_posts() ap = helpers.get_posts() featured_posts = fp[:int(app.config['FEATURED_POSTS_COUNT'])] all_posts = ap[:int(app.config['RECENT_POSTS_COUNT'])] updates = helpers.get_updates(True) if updates: latest_update = updates[-1] if updates else None latest_update['html'] = ( latest_update['html'] if 'html' in latest_update else helpers.pandoc_markdown(latest_update['text'])) else: latest_update = "" return render_template('index_hybrid.html', post=page, directory=app.config['POST_DIRECTORY'], featured_posts=featured_posts, all_posts=all_posts, latest_update=latest_update, site=app.config) elif app.config['SITE_STYLE'] == "blog": p = helpers.get_featured_posts() ap = helpers.get_posts() featured_posts = p[:int(app.config['FEATURED_POSTS_COUNT'])] all_posts = ap[:int(app.config['RECENT_POSTS_COUNT'])] return render_template('index_list.html', directory=app.config['POST_DIRECTORY'], featured_posts=featured_posts, all_posts=all_posts, site=app.config) else: abort(404)
def nested_content(name, ext, dir=None, year=None, month=None): if dir: path = '{}/{}'.format(dir, name) else: dir = app.config['POST_DIRECTORY'] if year and month: month = "{:02d}".format(month) path = '%s/%s/%s/%s' % (dir, year, month, name) if ext == "html": if os.path.isfile('%s/%s.%s' % (app.config['CONTENT_PATH'], path, ext)): return send_from_directory( '%s/%s' % (app.config['CONTENT_PATH'], dir), '%s.%s' % (name, ext)) else: page = flatpages.get(path) if helpers.is_published_or_draft(page): if dir == app.config['POST_DIRECTORY']: return render_template('post.html', post=page, directory=dir, ext=ext, site=app.config) else: if ('templateType' in page.meta and page.meta['templateType'] == "post"): template_type = "post.html" else: template_type = "page.html" return render_template(template_type, post=page, directory=dir, ext=ext, site=app.config) else: abort(404) elif ext in ["md", "pdf", "epub"]: path = "{}/{}".format(app.config['CONTENT_PATH'], dir) filename = "{}.{}".format(name, ext) file = '{}/{}'.format(path, filename) mime = mimetypes.guess_type(file)[0] if mime in ["application/pdf", "application/epub+zip"]: return send_from_directory(path, filename, as_attachment=True) else: return helpers.get_flatfile_or_404(file) else: abort(404)
def feed(): tz = app.config['TIMEZONE'] posts = helpers.get_posts() feed = FeedGenerator() feed.title('%s' % app.config['TITLE']) feed.link(href=app.config['BASE_URL'] + url_for('feed'), rel='self') feed.subtitle(app.config.get('DESCRIPTION', "")) feed.author(name=app.config.get('AUTHOR', "")) feed.link(href=app.config['BASE_URL'], rel='alternate') for p in posts[:10]: post = flatpages.get(p.path) if ('POST_LINK_STYLE' in app.config and app.config['POST_LINK_STYLE'] == "date"): url = "%s/%s" % (app.config['BASE_URL'], p.slug) else: url = "{}{}".format( app.config['BASE_URL'], url_for('nested_content', name=p.slug, dir=app.config['POST_DIRECTORY'], ext='html')) entry = feed.add_entry() entry.title(p.meta['title']) entry.guid(guid=url, permalink=True) entry.author(name=p.meta.get('author', app.config.get('AUTHOR', ""))) entry.link(href=url) entry.updated( timezone(tz).localize(p.meta.get('updated', p.meta['date']))) entry.published(timezone(tz).localize(p.meta['date'])) entry.description(post.meta.get('description', '')) # It takes a while to render all of the HTML here but # then at least it is in memory and the rest of the # build process goes quickly. The rendering has to # happen anyway so there isn't any performance increase # by not including the full HTML here in content. entry.content(post.html) return Response(feed.rss_str(pretty=True), mimetype="application/rss+xml")
def page(name, ext): if ext == "html": p = flatpages.get(name) if helpers.is_published(p): if 'footer' in p.meta: setattr(p, 'footer', helpers.pandoc_markdown(p.meta['footer'])) return render_template('page.html', post=p, site=app.config) else: abort(404) elif name == "resume" and ext == "pdf": p = flatpages.get(name) import pypandoc from weasyprint import HTML, CSS from weasyprint.text.fonts import FontConfiguration resume_html = pypandoc.convert_file("%s/resume.md" % app.config['CONTENT_PATH'], 'html', format='md') font_config = FontConfiguration() header = f""" <h1>{app.config['AUTHOR']}</h1> <div class="header"> <a href="https://{p.meta['website']}">{p.meta['website']}</a> <span class="align-right">{p.meta['location']}</span> </div> <div class="header"> <a href="mailto:{p.meta['email']}">{p.meta['email']}</a> <span class="align-right">{p.meta['phone']}</span> </div> """ html = HTML(string=f"{header}{resume_html}") css = CSS(string=""" body { font-size: 69%; font-family: "Open Sans"; line-height: 1.4; text-align: justify; text-justify: inter-word; } h1 { margin-bottom: 0; } ul { padding-left: 25px; } ul ul { list-style-type: disc; } .header { color: grey; } .header a { color: grey; text-decoration: none; } p a { color: black; text-decoration: none; } .align-right { float: right; } @page { size: letter; margin: 1cm 1.5cm 1cm 1cm; } """) html.write_pdf(f"{app.config['CONTENT_PATH']}/{name}.pdf", stylesheets=[css], font_config=font_config) return send_from_directory(app.config['CONTENT_PATH'], '%s.%s' % (name, "pdf")) elif ext in ['txt', 'md']: file = '{}/{}.{}'.format(app.config['CONTENT_PATH'], name, ext) return helpers.get_flatfile_or_404(file) elif ext == "pdf": return send_from_directory(app.config['CONTENT_PATH'], '%s.%s' % (name, "pdf")) else: abort(404)
def feed_all(): tz = app.config['TIMEZONE'] feed = FeedGenerator() feed.title('All content from %s' % app.config['TITLE']) feed.link(href=app.config['BASE_URL'] + url_for('feed_all'), rel='self') feed.subtitle(app.config.get('DESCRIPTION', "")) feed.author(name=app.config.get('AUTHOR', "")) feed.link(href=app.config['BASE_URL'], rel='alternate') updates = helpers.get_updates() posts = helpers.get_posts() all_items = [] for u in updates: all_items.append({ 'title': u['date'].strftime("%Y-%m-%d %H:%M:%S %z"), 'author': app.config.get('AUTHOR', ""), 'link': "{}/updates.html#{}".format(app.config['BASE_URL'], u['date'].strftime('%Y-%m-%d_%H%M%S')), 'updated': timezone(tz).localize(u['date']), 'published': timezone(tz).localize(u['date']), 'content': "%s" % (u['html'] if 'html' in u else helpers.pandoc_markdown(u['text'])) }) for p in posts: post = flatpages.get(p.path) if ('POST_LINK_STYLE' in app.config and app.config['POST_LINK_STYLE'] == "date"): url = "%s/%s" % (app.config['BASE_URL'], p.slug) else: url = "{}{}".format( app.config['BASE_URL'], url_for('nested_content', name=p.slug, dir=app.config['POST_DIRECTORY'], ext='html')) html = ("<b>{}</b> • <span>{}</span> " "<a href=\"{}\">{}</a>".format( p.meta['title'], post.meta.get('description', ""), url, app.config['BASE_NAME'])) all_items.append({ 'title': p.meta['date'].strftime("%Y-%m-%d %H:%M:%S %z"), 'author': p.meta.get('author', app.config.get('AUTHOR', "")), 'link': url, 'updated': timezone(tz).localize(p.meta.get('updated', p.meta['date'])), 'published': timezone(tz).localize(p.meta['date']), 'content': html }) for i in sorted(all_items, key=lambda k: k['title']): entry = feed.add_entry() entry.title(i['title']) entry.guid(guid=i['link'], permalink=True) entry.author(name=i['author']) entry.link(href=i['link']) entry.updated(i['updated']) entry.published(i['published']) entry.description(i['content']) return Response(feed.rss_str(pretty=True), mimetype="application/rss+xml")