diff --git a/calibre-plugin/plugin-defaults.ini b/calibre-plugin/plugin-defaults.ini index fd48a29e..14fee2a2 100644 --- a/calibre-plugin/plugin-defaults.ini +++ b/calibre-plugin/plugin-defaults.ini @@ -400,16 +400,18 @@ chapter_title_strip_pattern:^[0-9]+[\.: -]+(?=[^0-9]|$) mark_new_chapters:false ## chapter title patterns use python template substitution. The -## ${index} is the 'chapter' number and ${title} is the chapter title, -## after applying chapter_title_strip_pattern. Those are the only -## variables available. +## ${number} is the 'chapter' number and ${title} is the chapter +## title, after applying chapter_title_strip_pattern. ${index04} is +## chapter number padded with leading zeros (mostly for internal use) +## such as 0001. ${index} == ${number} for backward compatibility. A +## few site adapters add additional chapter metadata. ## The basic pattern used when not using add_chapter_numbers or ## mark_new_chapters chapter_title_def_pattern:${title} ## Pattern used with add_chapter_numbers, but not mark_new_chapters -chapter_title_add_pattern:${index}. ${title} +chapter_title_add_pattern:${number}. ${title} ## Pattern used with mark_new_chapters, but not add_chapter_numbers ## (new) is just text and can be changed. @@ -417,7 +419,7 @@ chapter_title_new_pattern:(new) ${title} ## Pattern used with add_chapter_numbers and mark_new_chapters ## (new) is just text and can be changed. -chapter_title_addnew_pattern:${index}. (new) ${title} +chapter_title_addnew_pattern:${number}. (new) ${title} ## Uses a python template substitution. The ${title} is the default ## title of a new anthology, in the case of a series, or @@ -684,7 +686,7 @@ use_threadmark_wordcounts:true ## chapter_title_*_pattern settings. ## Example: #tocpage_entry: -# ${chapter} ${date} ${kwords}
+# ${chapter} ${date} ${kwords}
## The 'date' value for chapters mentioned above can be formated with ## datethreadmark_format. Otherwise it will default to diff --git a/fanficfare/adapters/base_adapter.py b/fanficfare/adapters/base_adapter.py index 913b4bc0..329eda94 100644 --- a/fanficfare/adapters/base_adapter.py +++ b/fanficfare/adapters/base_adapter.py @@ -75,7 +75,7 @@ class BaseSiteAdapter(Configurable): self.story = Story(configuration) self.story.setMetadata('site',self.getConfigSection()) self.story.setMetadata('dateCreated',datetime.now()) - self.chapterUrls = [] # tuples of (chapter title,chapter url) + self.chapterUrls = [] # dicts of (chapter title,chapter url) self.chapterFirst = None self.chapterLast = None self.oldchapters = None @@ -150,7 +150,7 @@ class BaseSiteAdapter(Configurable): if self.ignore_chapter_url_list == None: self.ignore_chapter_url_list = [ self.normalize_chapterurl(u) for u in self.getConfig('ignore_chapter_url_list').splitlines() ] if self.normalize_chapterurl(url) not in self.ignore_chapter_url_list: - meta = dict(othermeta) # copy + meta = defaultdict(unicode,othermeta) # copy othermeta meta.update({'title':stripHTML(title),'url':url}) # after other to make sure they are set self.chapterUrls.append(meta) self.story.setMetadata('numChapters', self.num_chapters()) @@ -166,7 +166,7 @@ class BaseSiteAdapter(Configurable): return self.chapterUrls[i].get(attr,None) def get_chapters(self): - return copy.copy(self.chapterUrls) + return copy.deepcopy(self.chapterUrls) def del_chapter(self,i): del self.chapterUrls[i] diff --git a/fanficfare/defaults.ini b/fanficfare/defaults.ini index ad2f7479..9b955794 100644 --- a/fanficfare/defaults.ini +++ b/fanficfare/defaults.ini @@ -403,16 +403,18 @@ chapter_title_strip_pattern:^[0-9]+[\.: -]+(?=[^0-9]|$) mark_new_chapters:false ## chapter title patterns use python template substitution. The -## ${index} is the 'chapter' number and ${title} is the chapter title, -## after applying chapter_title_strip_pattern. Those are the only -## variables available. +## ${number} is the 'chapter' number and ${title} is the chapter +## title, after applying chapter_title_strip_pattern. ${index04} is +## chapter number padded with leading zeros (mostly for internal use) +## such as 0001. ${index} == ${number} for backward compatibility. A +## few site adapters add additional chapter metadata. ## The basic pattern used when not using add_chapter_numbers or ## mark_new_chapters chapter_title_def_pattern:${title} ## Pattern used with add_chapter_numbers, but not mark_new_chapters -chapter_title_add_pattern:${index}. ${title} +chapter_title_add_pattern:${number}. ${title} ## Pattern used with mark_new_chapters, but not add_chapter_numbers ## (new) is just text and can be changed. @@ -420,7 +422,7 @@ chapter_title_new_pattern:(new) ${title} ## Pattern used with add_chapter_numbers and mark_new_chapters ## (new) is just text and can be changed. -chapter_title_addnew_pattern:${index}. (new) ${title} +chapter_title_addnew_pattern:${number}. (new) ${title} ## Reorder ships so b/a and c/b/a become a/b and a/b/c. '/' is no ## longer hard coded and can be changed and added to with @@ -711,7 +713,7 @@ use_threadmark_wordcounts:true ## chapter_title_*_pattern settings. ## Example: #tocpage_entry: -# ${chapter} ${date} ${kwords}
+# ${chapter} ${date} ${kwords}
## The 'date' value for chapters mentioned above can be formated with ## datethreadmark_format. Otherwise it will default to diff --git a/fanficfare/story.py b/fanficfare/story.py index 77758398..97f658ce 100644 --- a/fanficfare/story.py +++ b/fanficfare/story.py @@ -1020,8 +1020,12 @@ class Story(Configurable): 'toctitle':chapter['title'], 'new':newchap, 'number':len(self.chapters)+1, - 'index':len(self.chapters)+1, - '0index':"%04d"%(len(self.chapters)+1)}) + 'index04':"%04d"%(len(self.chapters)+1)}) + ## Due to poor planning on my part, chapter_title_*_pattern + ## expect index==1 while output settings expected index=0001. + ## index04 is to disambiguate, but index is kept for users' + ## pre-existing settings. + chapter['index']=chapter['index04'] self.chapters.append(chapter) def getChapters(self,fortoc=False): @@ -1065,22 +1069,17 @@ class Story(Configurable): else: usetempl = templ # logger.debug("chap(%s)"%chap) - # Chapter = namedtuple('Chapter', 'url title html origtitle toctitle new') chapter = defaultdict(unicode,chap) - chapter['chapter'] = chapter['title'] = usetempl.substitute(chap) - chapter['origtitle'] = templ.substitute(chap) - chapter['toctitle'] = toctempl.substitute(chap) - chapter['index'] = chap['number'] ## Due to poor planning on my part, chapter_title_*_pattern - retval.append(chapter) ## expect index==1 not index=0001 like output settings. - # Chapter(chap.url, - # # 'new' - # usetempl.substitute({'index':index+1,'title':chap.title}), - # chap.html, - # # 'orig' - # templ.substitute({'index':index+1,'title':chap.title}), - # # 'toc' - # toctempl.substitute({'index':index+1,'title':chap.title}), - # chap.new) ) + ## Due to poor planning on my part, + ## chapter_title_*_pattern expect index==1 not + ## index=0001 like output settings. index04 is now + ## used, but index is still included for backward + ## compatibility. + chapter['index'] = chapter['number'] + chapter['chapter'] = chapter['title'] = usetempl.substitute(chapter) + chapter['origtitle'] = templ.substitute(chapter) + chapter['toctitle'] = toctempl.substitute(chapter) + retval.append(chapter) else: retval = self.chapters diff --git a/fanficfare/writers/writer_epub.py b/fanficfare/writers/writer_epub.py index d51babe7..049126d0 100644 --- a/fanficfare/writers/writer_epub.py +++ b/fanficfare/writers/writer_epub.py @@ -118,7 +118,7 @@ ${value}
''') self.EPUB_TOC_ENTRY = string.Template(''' -${chapter}
+${chapter}
''') self.EPUB_TOC_PAGE_END = string.Template(''' @@ -540,12 +540,12 @@ div { margin: 0pt; padding: 0pt; } for index, chap in enumerate(self.story.getChapters(fortoc=True)): if chap['html']: i=index+1 - items.append(("file%04d"%i, - "OEBPS/file%04d.xhtml"%i, + items.append(("file%s"%chap['index04'], + "OEBPS/file%s.xhtml"%chap['index04'], "application/xhtml+xml", chap['title'])) - itemrefs.append("file%04d"%i) - chapurlmap[chap['url']]="file%04d.xhtml"%i # url -> relative epub file name. + itemrefs.append("file%s"%chap['index04']) + chapurlmap[chap['url']]="file%s.xhtml"%chap['index04'] # url -> relative epub file name. if dologpage: if self.getConfig("logpage_at_end") == "true": @@ -715,12 +715,6 @@ div { margin: 0pt; padding: 0pt; } chap['title']=removeEntities(chap['title']) chap['origchapter']=removeEntities(chap['origtitle']) chap['tocchapter']=removeEntities(chap['toctitle']) - # vals={'url':removeEntities(chap.url), - # 'chapter':removeEntities(chap.title), - # 'origchapter':removeEntities(chap.origtitle), - # 'tocchapter':removeEntities(chap.toctitle), - # 'index':"%04d"%(index+1), - # 'number':index+1} # escape double quotes in all vals. for k,v in chap.items(): if isinstance(v,basestring): chap[k]=v.replace('"','"') @@ -734,7 +728,7 @@ div { margin: 0pt; padding: 0pt; } # (200k+) fullhtml = re.sub(r'(

|
)\n*',r'\1\n',fullhtml) - outputepub.writestr("OEBPS/file%04d.xhtml"%(index+1),fullhtml.encode('utf-8')) + outputepub.writestr("OEBPS/file%s.xhtml"%chap['index04'],fullhtml.encode('utf-8')) del fullhtml if self.story.calibrebookmark: diff --git a/fanficfare/writers/writer_html.py b/fanficfare/writers/writer_html.py index 79f2c0a7..9dbd254e 100644 --- a/fanficfare/writers/writer_html.py +++ b/fanficfare/writers/writer_html.py @@ -137,7 +137,7 @@ ${output_css} ## anchor to work. Which it does by default. This ## could also be made configurable if some user ## changed it. - chapurlmap[chap['url']]="#section%04d"%(index+1) # url -> index + chapurlmap[chap['url']]="#section"%chap['index04'] # url -> index for index, chap in enumerate(self.story.getChapters()): if chap['html']: @@ -157,7 +157,6 @@ ${output_css} logging.debug('Writing chapter text for: %s' % chap['title']) - # vals={'url':chap.url, 'chapter':chap.title, 'index':"%04d"%(index+1), 'number':index+1} self._write(out,CHAPTER_START.substitute(chap)) self._write(out,chap_data) self._write(out,CHAPTER_END.substitute(chap)) diff --git a/fanficfare/writers/writer_mobi.py b/fanficfare/writers/writer_mobi.py index 9b039cc1..f37ee2fe 100644 --- a/fanficfare/writers/writer_mobi.py +++ b/fanficfare/writers/writer_mobi.py @@ -164,7 +164,6 @@ ${value}
for index, chap in enumerate(self.story.getChapters()): if chap['html']: logger.debug('Writing chapter text for: %s' % chap['title']) - # vals={'url':chap.url, 'chapter':chap.title, 'index':"%04d"%(index+1), 'number':index+1} fullhtml = CHAPTER_START.substitute(chap) + chap['html'] + CHAPTER_END.substitute(chap) # ffnet(& maybe others) gives the whole chapter text # as one line. This causes problems for nook(at diff --git a/fanficfare/writers/writer_txt.py b/fanficfare/writers/writer_txt.py index 46182cb8..b5c10647 100644 --- a/fanficfare/writers/writer_txt.py +++ b/fanficfare/writers/writer_txt.py @@ -157,7 +157,6 @@ End file. for index, chap in enumerate(self.story.getChapters()): if chap['html']: logging.debug('Writing chapter text for: %s' % chap['title']) - # vals={'url':chap.url, 'chapter':chap.title, 'index':"%04d"%(index+1), 'number':index+1} self._write(out,self.lineends(self.wraplines(removeAllEntities(CHAPTER_START.substitute(chap))))) self._write(out,self.lineends(html2text(chap['html'],bodywidth=self.wrap_width))) self._write(out,self.lineends(self.wraplines(removeAllEntities(CHAPTER_END.substitute(chap)))))