From 47ab118686d3216598b57188a67c81f9cf169c50 Mon Sep 17 00:00:00 2001 From: luxagraf Date: Sun, 1 Nov 2015 21:16:42 -0500 Subject: added epub script basics and styled figments archives and details --- app/figments/build.py | 15 ++++++- app/figments/ebook.py | 79 +++++++++++++++++++++++++++++++++ design/sass/_figments.scss | 32 ++++++++++++- design/templates/archives/figments.html | 27 ++++++----- design/templates/details/figments.html | 5 ++- 5 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 app/figments/ebook.py diff --git a/app/figments/build.py b/app/figments/build.py index b0890fd..58835e5 100644 --- a/app/figments/build.py +++ b/app/figments/build.py @@ -2,11 +2,12 @@ from builder.base import * from .models import Figment, Series -class BuildSrc(Build): +class BuildFigments(Build): def build(self): self.build_archive() self.build_topic_archive() self.build_detail_pages() + self.build_detail_epub() self.build_feed() def build_detail_pages(self): @@ -21,6 +22,18 @@ class BuildSrc(Build): s = render_to_string('details/note.txt', c).encode('utf-8') self.write_file(path, s, 'txt', entry.slug) + def build_detail_epub(self): + ''' + Grab all the notes, render them to a template string and write that out to the filesystem + ''' + for entry in Figment.objects.filter(status__exact=1): + c = Context({'object': entry, 'MEDIA_URL': settings.BAKED_MEDIA_URL, 'IMAGES_URL': settings.BAKED_IMAGES_URL, 'SITE_URL':settings.SITE_URL}) + t = render_to_string('details/fignments.html', c).encode('utf-8') + path = 'figments/' + self.write_file(path, t, 'html', entry.slug) + s = render_to_string('details/note.txt', c).encode('utf-8') + self.write_file(path, s, 'txt', entry.slug) + def build_archive(self): path = 'figments/' c = Context({ diff --git a/app/figments/ebook.py b/app/figments/ebook.py new file mode 100644 index 0000000..23e3fc5 --- /dev/null +++ b/app/figments/ebook.py @@ -0,0 +1,79 @@ +import os +from ebooklib import epub + +from django.conf import settings +from typogrify.filters import typogrify + +def generate_epub_file(f): + book = epub.EpubBook() + + # add metadata + book.set_identifier('lux23') + book.set_title(f.title) + book.set_language('en') + + book.add_author('Scott Gilbertson') + + # intro chapter + c1 = epub.EpubHtml(title='Introduction', file_name='intro.xhtml', lang='en') + c1.content=u'

Forward

Thank you for downloading my story %s. I hope you enjoy it and please feel free to email me if you have any questions or comments.

If you enjoy this story and would like to read more, head on over to luxagraf.net.

– Scott Gilbertson
  sng@luxagraf.net

' % f.title + + # chapter + c2 = epub.EpubHtml(title=f.title, file_name='story.xhtml') + c2.content='

%s

' % f.title + c2.content+= typogrify(f.body_html) + + # add chapters to the book + book.add_item(c1) + book.add_item(c2) + + # create table of contents + # - add section + # - add auto created links to chapters + + book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'), + epub.Link('story.xhtml', f.title, 'story'), + ) + + # add navigation files + book.add_item(epub.EpubNcx()) + book.add_item(epub.EpubNav()) + + # define css style + style = ''' +@namespace epub "http://www.idpf.org/2007/ops"; +body { + font-family: Georgia, Times, Times New Roman, serif; +} +h2 { + text-align: left; + text-transform: uppercase; + font-weight: 200; +} +ol { + list-style-type: none; +} +ol > li:first-child { + margin-top: 0.3em; +} +nav[epub|type~='toc'] > ol > li > ol { + list-style-type:square; +} +nav[epub|type~='toc'] > ol > li > ol > li { + margin-top: 0.3em; +} +''' + + # add css file + nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style) + book.add_item(nav_css) + + # create spine + book.spine = ['nav', c1, c2] + path, filename = os.path.split(f.get_absolute_url()) + fpath = '%s%s/' %(settings.FLATFILES_ROOT, path) + if not os.path.isdir(fpath): + os.makedirs(fpath) + bk = '%s%s.epub' %(fpath, f.slug) + # create epub file + epub.write_epub(bk, book, {}) diff --git a/design/sass/_figments.scss b/design/sass/_figments.scss index cc4d7f5..1204743 100644 --- a/design/sass/_figments.scss +++ b/design/sass/_figments.scss @@ -1,4 +1,34 @@ -.kindle { +.kindle, .epub { + display: inline; @include smcaps; @include fontsize(11); } +.kindle:after { + content: "ยท"; + color: #999; + padding-left: 0.75em; +} +.fig-archive { + @include constrain_narrow; + @include breakpoint(beta) { + text-align: left; + + } + h1 { + margin-top: 2.5em; + @include smcaps; + @include fontsize(16); + } + p:first-of-type { + margin-bottom: 4em; + @include breakpoint(beta) { + text-align: left; + @include fontsize(18); + font-style: italic + } + } + h2 { + @include smcaps; + @include fontsize(18); + } +} diff --git a/design/templates/archives/figments.html b/design/templates/archives/figments.html index 5c5e398..8b5c78e 100644 --- a/design/templates/archives/figments.html +++ b/design/templates/archives/figments.html @@ -1,18 +1,23 @@ {% extends 'base.html' %} {% load typogrify_tags %} -{% block primary %} -
+{% block primary %} +
+

Figments of Imagination

+

I dislike the term “fiction” because it implies that there is a non-fiction and I categorically deny that such a thing can exist. So I call these stories “less true stories mostly made up” and hope for the best, where “the best” is that you enjoy them.

+ {% for object in object_list %} -
-
-

{%if object.template_name == 1 or object.template_name == 3 %}{{object.title|smartypants|safe}}{%else%}{{object.title|smartypants|widont|safe}}{%endif%}

- -
-
- {{object.title|safe|smartypants|widont}} -
-
+ {%endfor%}
{% endblock %} diff --git a/design/templates/details/figments.html b/design/templates/details/figments.html index 3aeb690..1f8bfc7 100644 --- a/design/templates/details/figments.html +++ b/design/templates/details/figments.html @@ -7,7 +7,10 @@

{%if object.template_name == 1 or object.template_name == 3 %}{{object.title|smartypants|safe}}{%else%}{{object.title|smartypants|widont|safe}}{%endif%}

-

Send to Kindle

+
-- cgit v1.2.3-70-g09d2