让我的 vimwiki blog 按年生成首页

我的 vimwiki blog,有个很偷懒的功能:生成首页,是用 python 写的

这个功能是挂在我的 wiki 查找里的,查找功能可以看这里 利用 python 实现 vimwiki 的 wiki 词查找

查找时候,输入 *,就会把所有的 wiki 词按格式归整好,写入 index.wiki 里,再把 index 生成 html,就得到了现在的首页.

可是……太长了…

太长了不是我的错,我写的太多了.

首页太不友好了,不是所有的人都喜欢用浏览器的 ctrl+f

听取闲耘™ 的建议,按年来分吧.

要改 search_wiki.py

己搞定,不过这个模块是越来越复杂了

#!/usr/bin/env python
#encoding=utf-8
'''
vimwiki 中用来查找 wiki 词
'''
import glob
import sys
import os
import time

class SearchWiki:
    def __init__(self, wiki_name):
        self.wiki_name = wiki_name
        self.mergered_all = {}
        self.mergered_all_sorted = []
        self.files_time = {}
    def search(self):
        '''找到wiki文件,并加上时间'''
        file_names = glob.glob('*%s*.wiki' % self.wiki_name)
        for i in file_names:
            modify_time = time.localtime(os.path.getmtime(i))
            self.files_time![pic](i) = modify_time
    def getYear(self, file_time):
        '''返回年'''
        return file_time.tm_year
    def mergerByYear(self):
        '''按年份来归并'''
        for i in self.files_time:
            year = self.getYear(self.files_time![pic](i))
            mergered_files_dic = self.mergered_all.get(year) 
            if mergered_files_dic is None:
                dic = {i:self.files_time![pic](i)}
                self.mergered_all![pic](year) = dic
            else:
                mergered_files_dic![pic](i) = self.files_time![pic](i)
    def sortByTime(self):
        '''按时间排序'''
        for i in self.mergered_all:
            files_time = self.mergered_all![pic](i)
            files_time_sorted = sorted(files_time.items(), key=lambda by:by![pic](1), reverse=True)
            self.mergered_all![pic](i) = files_time_sorted
    def sortByYear(self):
       self.mergered_all_sorted = sorted(self.mergered_all.items(), key=lambda by:by![pic](0), reverse=True)
    def writeContent(self, f, year, files_time_sorted):

        print >>f, '----------------%s-----------------'%year
        for file_info in files_time_sorted:
            splited_name= file_info![pic](0).rsplit('.',1)
            name=splited_name![pic](0)
            if(name != 'index' and name != 'search'):
                #if(self.print_time):
                *    print >>f,'['+name+']('+name+'.md)'+'    '+time.strftime("%Y年%m月%d\
                *    %H:%S", file_info![pic](1))
                #else:
                print >>f, '['+name+']('+name+'.md)'

    def writeContentIndex(self, f, files_info_sorted):
        for file_info in files_info_sorted:
            splited_name= file_info![pic](0).rsplit('.',1)
            name=splited_name![pic](0)
            if(name != 'index' and name != 'search'):
                #if(self.print_time):
                *    print >>f,'# ['+name+']('+name+'.md)'+'    '+time.strftime("%Y年%m月%d\
                *    %H:%S", file_info![pic](1))
                #else:
                print >>f, '* ['+name+']('+name+'.md)'
      
    def createIndex(self, year, files_times_sorted):
        file_name = year

        now_time = time.localtime()
        if(year == now_time.tm_year):
            file_name = 'index'
        f=open('%s.wiki'%file_name,'w')
        print >>f,''
        print >>f,'# bigzhu的坑'
        print >>f,''
        print >>f,'''
        * 这里看似blog,其实又不是blog,这里是[bigzhu](bigzhu.md)%s年的知识库,极尽简洁.条目会反复修改,最新的会被顶到最上面来.

        * 分享的目的在[走向](走向.md)里写了,如果有文章帮到你了,给我留个言吧.

        * google 进来的朋友,ctrl+f找你要的內容,所有条目都在本页列了. 有条目没內容的,给我留言,让我生成html就可以
        '''%year

        if file_name != 'index':
            print >>f,''
            print >>f, '* [%s年的文章](%s.md)'%(year+1, year+1)
            self.writeContentIndex(f, files_times_sorted)
            print >>f, '* [%s年的文章](%s.md)'%(year-1, year-1)
        else:
            self.writeContentIndex(f, files_times_sorted)
            print >>f, '* [%s年的文章](%s.md)'%(year-1, year-1)
        f.close()
       
    def writeResult(self):
        if(self.wiki_name == '*'):
            for i in self.mergered_all:
                self.createIndex(i, self.mergered_all![pic](i))

        f=open('search.wiki','w')
        print >>f,'- status: draft'
        #print self.mergered_all
        for i in self.mergered_all_sorted:
            self.writeContent(f, i![pic](0), i![pic](1))
        f.close()

if __name__=='__main__':
    args=sys.argv
    if(len(args)==1):
        print '请输入要查找的 wiki 关键字'
    #elif(len(args)>2):
    *    wiki_name=args![pic](1)
    *    seartch_wiki = SearchWiki(wiki_name)
    *    files_time = seartch_wiki.search()
    *    if(args![pic](2)=='time'):
    *        seartch_wiki.write_result(files_time, True)
    *    else:
    *        seartch_wiki.write_result(files_time)
    elif(len(args)==2):
        wiki_name=args![pic](1)
        seartch_wiki = SearchWiki(wiki_name)
        seartch_wiki.search()
        seartch_wiki.mergerByYear()
        seartch_wiki.sortByTime()
        seartch_wiki.sortByYear()
        seartch_wiki.writeResult()