@@ -76,6 +76,7 @@ def test_jpg(h, f):
7676json_dir = 'json'
7777media_dir = 'media'
7878archive_dir = 'archive'
79+ flat_dir = 'flat'
7980theme_dir = 'theme'
8081save_dir = '../'
8182backup_css = 'backup.css'
@@ -142,6 +143,10 @@ def mkdir(dir, recursive=False):
142143 raise
143144
144145
146+ def tag_bare_name (tag ):
147+ # Replaces tag, quotes, and unicode quotes
148+ return tag .strip ("Tag " ).replace ("'" , "" ).replace (unichr (8219 ), "" ).replace (unichr (8217 ), "" )
149+
145150def path_to (* parts ):
146151 return join (save_folder , * parts )
147152
@@ -386,13 +391,94 @@ def next_month(inc):
386391
387392 return first_file
388393
394+ class FlatIndex :
395+
396+ def __init__ (self , blog , body_class = 'index' ):
397+ self .blog = blog
398+ self .body_class = body_class
399+ self .index = defaultdict (lambda : defaultdict (list ))
400+
401+ def add_post (self , post ):
402+ self .index [post .tm .tm_year ][post .tm .tm_mon ].append (post )
403+ return self
404+
405+ def save_index (self , index_dir = '.' , title = None ):
406+ subtitle = self .blog .title if title else self .blog .subtitle
407+ title = title or self .blog .title
408+
409+ if re .findall (r'(?i)Tags' , index_dir ):
410+ index_dir = 'Tags/'
411+ title = tag_bare_name (title )
412+
413+ posts = []
414+ for year in sorted (self .index .keys (), reverse = options .reverse_index ):
415+ self .collect_flat (index_dir , year , posts )
416+
417+ self .save_posts (posts , index_dir , title )
418+
419+ def save_posts (self , posts , index_dir , title ):
420+ total_posts = len (posts )
421+ per_page = options .posts_per_page if options .posts_per_page >= 1 else total_posts
422+ FILE_FMT = '%s-p%d.html'
423+ page = 0
424+
425+ def name_for_page (file_fmt , page_num ):
426+ if page_num < 0 :
427+ return ""
428+
429+ if page_num == 0 :
430+ return title + ".html"
431+ else :
432+ return FILE_FMT % (title , page )
433+
434+ while posts != []:
435+ if options .dirs :
436+ base = save_dir + flat_dir + '/'
437+ else :
438+ base = ''
439+
440+ previous_page = ""
441+ file_name = name_for_page (FILE_FMT , page )
442+ previous_page = name_for_page (FILE_FMT , page - 1 )
443+
444+ file_page = open_text (flat_dir , index_dir , file_name )
445+
446+ current_posts = posts [0 :per_page ]
447+ for post in current_posts :
448+ posts .remove (post )
449+ current_page_content = [self .blog .header (body_class = 'flat' )]
450+
451+ current_page_content .extend (post .get_post () for post in current_posts )
452+
453+ if posts != []:
454+ np = name_for_page (FILE_FMT , page + 1 )
455+ else :
456+ np = ""
457+
458+ current_page_content .append (self .blog .footer (base , previous_page , np , "" ))
459+
460+ file_page .write ('\n ' .join (current_page_content ))
461+ page += 1
462+
463+ return
464+
465+
466+ def collect_month_posts (self , index_dir , year , month ):
467+ return sorted (self .index [year ][month ], key = lambda x : x .date , reverse = options .reverse_index )
468+
469+ def collect_flat (self , index_dir , year , posts ):
470+ for month in sorted (self .index [year ].keys (), reverse = options .reverse_index ):
471+ posts += self .collect_month_posts (index_dir , year , month )
472+
473+ return posts
389474
390475class Indices :
391476
392477 def __init__ (self , blog ):
393478 self .blog = blog
394- self .main_index = Index (blog )
395- self .tags = defaultdict (lambda : Index (blog , 'tag-archive' ))
479+ self .index_type = FlatIndex if options .flat_index else Index
480+ self .main_index = self .index_type (blog )
481+ self .tags = defaultdict (lambda : self .index_type (blog , 'tag-archive' ))
396482
397483 def build_index (self ):
398484 filter = join ('*' , dir_index ) if options .dirs else '*' + post_ext
@@ -405,12 +491,15 @@ def build_index(self):
405491
406492 def save_index (self ):
407493 self .main_index .save_index ()
494+ level = '../../' if options .flat_index else '../../../'
408495 if options .tag_index :
409- self .save_tag_index ()
496+ self .save_tag_index (level )
497+ if options .flat_index :
498+ self .fixup_media_links ()
410499
411- def save_tag_index (self ):
500+ def save_tag_index (self , level = '../../../' ):
412501 global save_dir
413- save_dir = '../../../'
502+ save_dir = level
414503 mkdir (path_to (tag_index_dir ))
415504 self .fixup_media_links ()
416505 tag_index = [self .blog .header ('Tag index' , 'tag-index' , self .blog .title , True ), '<ul>' ]
@@ -478,9 +567,9 @@ def footer(self, base, previous_page, next_page, suffix):
478567 f = '<footer><nav>'
479568 f += '<a href=%s%s rel=index>Index</a>\n ' % (save_dir , dir_index )
480569 if previous_page :
481- f += '| <a href=%s%s%s rel=prev>Previous</a>\n ' % (base , previous_page , suffix )
570+ f += '| <a href=" %s%s%s" rel=prev>Previous</a>\n ' % (base , previous_page , suffix )
482571 if next_page :
483- f += '| <a href=%s%s%s rel=next>Next</a>\n ' % (base , next_page , suffix )
572+ f += '| <a href=" %s%s%s" rel=next>Next</a>\n ' % (base , next_page , suffix )
484573 f += '</nav></footer>\n '
485574 return f
486575
@@ -1082,6 +1171,9 @@ def request_callback(option, opt, value, parser):
10821171 parser .add_option ('--tag-index' , action = 'store_true' ,
10831172 help = "also create an archive per tag"
10841173 )
1174+ parser .add_option ('--flat-index' , action = 'store_true' ,
1175+ help = "Create a flat index as opposed to archive"
1176+ )
10851177 parser .add_option ('-a' , '--auto' , type = 'int' , metavar = "HOUR" ,
10861178 help = "do a full backup at HOUR hours, otherwise do an incremental backup"
10871179 " (useful for cron jobs)"
0 commit comments