Exemple #1
0
 def parse_new_posts(self, filename_list):
     for filename in filename_list:
         post_content = self.database.get_item('posts', filename)['content']
         if post_content:
             post_tmp = Post(filename=filename)
             post_tmp.parse_from_db(post_content)
             dir_name = os.path.join(settings.PUBLISH_DIR,
                                     post_tmp.url.strip("/\\"))
             file_path = os.path.join(dir_name, 'index.html')
             if os.path.exists(file_path):
                 os.remove(file_path)
             else:
                 logger.warning("Field to remove file: '{0}".format(
                     post_tmp.title))
         post_tmp = Post(filename=filename)
         file_content = open(os.path.join(filename),'r')\
             .read().decode('utf8')
         if not post_tmp.check_illegal(file_content, filename=filename):
             # If the post_content is not illegal, pass it.
             logger.warning(
                 "This post doesn't have a correct format: '{0}".format(
                     filename))
             continue
         else:
             post_tmp.parse()
         self.posts.append(post_tmp)
         self.new_posts.append(post_tmp)
         post_dict = post_tmp.__dict__.copy()
         post_dict['pub_time'] = time.mktime(
             post_dict['pub_time'].timetuple())
         post_dict.pop('config', None)
         last_mod_time = os.path.getmtime(os.path.join(filename))
         post_dict_in_db = {
             'last_mod_time': last_mod_time,
             'content': post_dict
         }
         self.database.set_item('posts', filename, post_dict_in_db)
Exemple #2
0
 def parse_removed_posts(self, filename_list):
     for filename in filename_list:
         post_content = self.database.get_item('posts', filename)['content']
         post_tmp = Post(filename=filename)
         post_tmp.parse_from_db(post_content)
         self.database.remove_item('posts', filename)
         self.removed_posts.append(post_tmp)
         dname = os.path.join(settings.PUBLISH_DIR,
                              post_tmp.url.strip("/\\"))
         filepath = os.path.join(dname, 'index.html')
         try:
             os.remove(filepath)
         except Exception, e:
             logger.warning("Field to remove file: '{0}".format(
                 post_tmp.title))
Exemple #3
0
 def parse_old_posts(self, filename_list):
     for filename in filename_list:
         post_content = self.database.get_item('posts', filename)['content']
         post_tmp = Post(filename=filename)
         post_tmp.parse_from_db(post_content)
         self.posts.append(post_tmp)
         post_dict = post_tmp.__dict__.copy()
         post_dict['pub_time'] = time.mktime(
             post_dict['pub_time'].timetuple())
         post_dict.pop('config', None)
         last_mod_time = os.path.getmtime(os.path.join(filename))
         post_dict_in_db = {
             'last_mod_time': last_mod_time,
             'content': post_dict
         }
         self.database.set_item('posts', filename, post_dict_in_db)
Exemple #4
0
def create_post(config):
    flag = 0
    if len(sys.argv) == 1:
        usage()
    elif len(sys.argv) != 1:
        post = Post(config)
        pinyin = PinYin()
        pinyin.load_word()
        string = sys.argv[1]  # Assume that argv[1] is the title user inputed.
        post.title = string
        post.slug = pinyin.hanzi2pinyin_split(string=string, split="-")
        dt = datetime.now()
        post.pub_time = dt.strftime("%Y-%m-%d %H:%M")
        new_post = SAMPLE % (post.title, post.pub_time, post.slug)
        file_title = dt.strftime("%Y-%m-%d") + '-' + post.slug + '.markdown'
        target = os.path.normpath(os.path.join('source/posts', file_title))
        open(target, 'w+').write(new_post)
    else:
        usage()
Exemple #5
0
    def get_posts(self):
        '''
        Get posts from markdown files
        '''
        filenames = [
        ]  # filenames is a list of the markdown file's name from the posts' directory
        for filename in os.listdir(self.posts_dir):
            if not filename.startswith('.'):
                filenames.append(filename)

        # db_filenames is a list of the filenames read from db.json
        db_filenames = []
        for db_filename in self.db['posts']:
            db_filenames.append(db_filename.encode('utf8'))
        new_filenames, old_filenames = self.get_new_filenames(
            filenames, db_filenames, 'posts')
        categories_tmp = {}
        post_content = {}
        posts_dict = {}
        for filename in old_filenames:
            post_content = dict(self.db['posts'][filename]['content'])
            post_tmp = Post(self.config)
            post_tmp.get_from_db(post_content)
            self.posts.append(post_tmp)
            for category in post_tmp.categories:
                if category in self.categories:
                    self.categories[category].add_post(post_tmp)
                else:
                    self.categories[category] = Category(self.config, category)
                    self.categories[category].add_post(post_tmp)
            post_dict = post_tmp.__dict__.copy()
            post_dict['pub_time'] = time.mktime(
                post_dict['pub_time'].timetuple())
            post_dict.pop('config', None)
            last_mod_time = os.path.getmtime(
                os.path.join(
                    self.posts_dir,
                    filename))
            posts_dict[filename] = {
                'last_mod_time': last_mod_time,
                'content': post_dict}
        for filename in new_filenames:
            post_tmp = Post(self.config)
            post_tmp.save(
                open(
                    os.path.join(
                        self.posts_dir,
                        filename),
                    'r').read().decode('utf8'))
            self.posts.append(post_tmp)
            for category in post_tmp.categories:
                if category in self.categories:
                    self.categories[category].add_post(post_tmp)
                else:
                    self.categories[category] = Category(self.config, category)
                    self.categories[category].add_post(post_tmp)
            post_dict = post_tmp.__dict__.copy()
            post_dict['pub_time'] = time.mktime(
                post_dict['pub_time'].timetuple())
            post_dict.pop('config', None)
            last_mod_time = os.path.getmtime(
                os.path.join(
                    self.posts_dir,
                    filename))
            posts_dict[filename] = {
                'last_mod_time': last_mod_time,
                'content': post_dict}
        self.db['posts'] = posts_dict
        self.posts_sort()
        self.page_number = len(self.posts) / 5