Skip to content

Commit

Permalink
Merge 0cee47c into 4061c14
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed May 25, 2020
2 parents 4061c14 + 0cee47c commit 1f62844
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
11 changes: 5 additions & 6 deletions knowledge_repo/app/routes/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,12 @@ def download():
post = current_repo.post(request.args.get('post')) # Note: `post` is expected to be a post path
resource_type = request.args.get('type', 'source')

if resource_type in ('kp', 'zip'):
filename = os.path.basename(post.path)
if resource_type == 'zip':
filename = filename[:-3] + '.zip'
if resource_type == 'post':
format = request.args.get('format', 'zip')
filename = os.path.basename(post.path)[:-2] + format
return Response(
post.to_string(format=resource_type),
mimetype="application/zip",
post.to_string(format=format),
mimetype="application/octet-stream",
headers={u"Content-disposition": "attachment; filename={}".format(filename)})
elif resource_type == 'source':
path = request.args.get('path', None)
Expand Down
2 changes: 2 additions & 0 deletions knowledge_repo/app/templates/markdown-base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
{% endblock %}

{% block panel_right %}

{% if can_download %}
<span class='section'>Downloads</span>
<a href="{{url_for('posts.download')}}?type=kp&post={{post_path|urlencode}}" class="btn btn-primary btn-download" style='display: block;'>Portable Knowledge Post</a>
<a href="{{url_for('posts.download')}}?type=post&format=pdf&post={{post_path|urlencode}}" class="btn btn-primary btn-download" style='display: block;'>PDF</a>
<a href="{{url_for('posts.download')}}?type=zip&post={{post_path|urlencode}}" class="btn btn-primary btn-download" style='display: block;'>ZIP Archive</a>
{% if downloads %}
<span class='subsection'>Source Files</span>
Expand Down
30 changes: 30 additions & 0 deletions knowledge_repo/converters/pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from ..converter import KnowledgePostConverter

from .html import HTMLConverter


class PDFConverter(KnowledgePostConverter):
'''
Use this as a template for new KnowledgePostConverters.
'''
_registry_keys = ['pdf']

@property
def dependencies(self):
# Dependencies required for this converter on top of core knowledge-repo dependencies
return ['weasyprint']

def from_file(self, filename, **opts):
raise NotImplementedError

def from_string(self, filename, **opts):
raise NotImplementedError

def to_file(self, filename, **opts):
with open(filename, 'wb') as f:
f.write(self.to_string())

def to_string(self, **opts):
from weasyprint import HTML
html = HTMLConverter(self.kp).to_string()
return HTML(string=html).write_pdf()

0 comments on commit 1f62844

Please sign in to comment.