Skip to content

Commit

Permalink
Sort recipes by main (first) category into sections
Browse files Browse the repository at this point in the history
This allows to sensibly group multiple recipes in a book, and to
enable a structured table-of-content.

The first category, in the absence of a better mechanism, is treated
specially to primarily group recipes.  It is the first sorting
criteria and primary ToC entries are generated per first category.
The recipe name remains the secondary sorting criterion.

Fixes thinkle#57.

Signed-off-by: Martin Pohlack <[email protected]>
  • Loading branch information
Martin Pohlack committed Jun 21, 2020
1 parent 543cee2 commit 6551168
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions gourmet/plugins/import_export/epub_plugin/epub_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def __init__(self, outFileName):

# This adds the field also known as keywords in some programs.
self.ebook.add_metadata('DC', 'subject', "cooking")
self.prev_category = None
self.current_section = None

def addRecipeCssFromFile(self, filename):
""" Adds the CSS file from filename to the book. The style will be added
Expand Down Expand Up @@ -94,7 +96,13 @@ def getFileForRecipeID(self, id, ext=".xhtml"):
"""
return "recipe_%i%s" % (id,ext)

def addRecipeText(self, uniqueId, title, text):
def flush_toc_section(self):
if self.current_section:
self.toc.append( (epub.Section(self.prev_category),
self.current_section) )
self.current_section = None

def addRecipeText(self, uniqueId, title, text, first_cat=None):
""" Adds the recipe text as a chapter.
"""
uniqueName = self.getFileForRecipeID(uniqueId, ext="")
Expand All @@ -108,13 +116,21 @@ def addRecipeText(self, uniqueId, title, text):
# add chapter
self.ebook.add_item(c1)
self.spine.append(c1)

# define Table Of Contents
self.toc.append( epub.Link(fileName, title, uniqueName) )
l = epub.Link(fileName, title, uniqueName)

# Define Table Of Contents. Collect entries per section and
# flush on section change.
if self.prev_category is None or (first_cat != self.prev_category and first_cat is not None):
self.flush_toc_section()
self.prev_category = first_cat
self.current_section = [l]
else:
self.current_section.append(l)

def finish(self):
""" Finish the book and writes it to the disk.
"""
self.flush_toc_section()
self.ebook.toc = self.toc

# add default NCX and Nav file
Expand Down Expand Up @@ -264,7 +280,12 @@ def write_foot (self):
self.preparedDocument.append(RECIPE_FOOT)

self._grab_attr_(self.r,'id')
self.doc.addRecipeText(self._grab_attr_(self.r,'id'), self.get_title(), "".join(self.preparedDocument) )
cats = self.rd.get_cats(self.r)
first_cat = None
if cats:
first_cat = cats[0]
self.doc.addRecipeText(self._grab_attr_(self.r,'id'), self.get_title(), "".join(self.preparedDocument),
first_cat=first_cat)

class website_exporter (ExporterMultirec):
def __init__ (self, rd, recipe_table, out, conv=None, ext='epub', copy_css=True,
Expand All @@ -285,13 +306,25 @@ def __init__ (self, rd, recipe_table, out, conv=None, ext='epub', copy_css=True,

if conv:
self.exportargs['conv']=conv

# Sort by first category first so we can build chapters in the
# book, secondary by name.
recipe_table.sort(key = lambda x: (self._get_first_category(x, rd), x.title))
ExporterMultirec.__init__(self, rd, recipe_table, out,
one_file=True,
create_file=False,
ext=self.ext,
exporter=epub_exporter,
exporter_kwargs=self.exportargs)

@staticmethod
def _get_first_category(r, rd):
cats = rd.get_cats(r)
if cats:
return cats[0]
else:
return None

def recipe_hook (self, rec, filename, exporter):
"""Add index entry"""
# TODO: Do some cool things here.
Expand Down

0 comments on commit 6551168

Please sign in to comment.