def get_categories(): all_categories = [] yaml = YAML() for dir in POSTS_DIR: path = get_path(dir) for file in glob.glob(os.path.join(path, '*.md')): meta = yaml.load(get_yaml(file)[0]) if 'category' in meta: if type(meta['category']) == list: err_msg = ("[Error] File {} 'category' type" " can not be LIST!").format(file) raise Exception(err_msg) else: if meta['category'] not in all_categories: all_categories.append(meta['category']) else: if 'categories' in meta: if type(meta['categories']) == str: error_msg = ("[Error] File {} 'categories' type" " can not be STR!").format(file) raise Exception(error_msg) for ctg in meta['categories']: if ctg not in all_categories: all_categories.append(ctg) else: err_msg = ("[Error] File:{} at least " "have one category.").format(file) print(err_msg) return all_categories
def get_all_tags(): all_tags = [] yaml = YAML() for dir in POSTS_DIR: path = get_path(dir) for file in glob.glob(os.path.join(path, '*.md')): meta = yaml.load(get_yaml(file)[0]) if 'tags' in meta: for tag in meta['tags']: if tag not in all_tags: all_tags.append(tag) else: raise Exception("Didn't find 'tags' in \ post '{}' !".format(file)) return all_tags
def get_all_tags(): all_tags = [] yaml = YAML() for dir in POSTS_DIR: path = get_path(dir) posts = get_makrdown_files(path) for file in posts: meta = yaml.load(get_yaml(file)[0]) if 'tags' in meta: for tag in meta['tags']: if tag not in all_tags: all_tags.append(tag) else: raise Exception("Didn't find 'tags' in \ post '{}' !".format(file)) return all_tags
def update_lastmod(path, verbose, date): count = 0 yaml = YAML() for post in glob.glob(path): lastmod = '' if date == Date.GIT: git_log_count = subprocess.getoutput( "git log --pretty=%ad \"{}\" | wc -l".format(post)) if git_log_count == "1": continue git_lastmod = subprocess.getoutput( "git log -1 --pretty=%ad --date=iso \"{}\"".format(post)) if not git_lastmod: continue lates_commit = subprocess.check_output( ['git', 'log', '-1', '--pretty=%B', post]).decode('utf-8') if "[Automation]" in lates_commit and "Lastmod" in lates_commit: continue lastmod = git_lastmod elif date == Date.FS: t = os.path.getmtime(post) dt = datetime.datetime.fromtimestamp(t) lastmod = dt.strftime('%F %T') + time.strftime(' %z') frontmatter, line_num = get_yaml(post) meta = yaml.load(frontmatter) if 'seo' in meta: if ('date_modified' in meta['seo'] and meta['seo']['date_modified'] == lastmod): continue else: meta['seo']['date_modified'] = lastmod else: meta.insert(line_num, 'seo', dict(date_modified=lastmod)) output = 'new.md' if os.path.isfile(output): os.remove(output) with open(output, 'w', encoding='utf-8') as new, \ open(post, 'r', encoding='utf-8') as old: new.write("---\n") yaml.dump(meta, new) new.write("---\n") line_num += 2 lines = old.readlines() for line in lines: if line_num > 0: line_num -= 1 continue else: new.write(line) shutil.move(output, post) count += 1 if verbose: print("[INFO] update 'lastmod' for:" + post) if count > 0: print("[INFO] Success to update lastmod for {} post(s).".format(count))
def update_lastmod(verbose): count = 0 yaml = YAML() for post in glob.glob(os.path.join(POSTS_PATH, "*.md")): ps = subprocess.Popen(("git", "log", "--pretty=%ad", post), stdout=subprocess.PIPE) git_log_count = subprocess.check_output(('wc', '-l'), stdin=ps.stdout) ps.wait() if git_log_count.strip() == "1": continue git_lastmod = subprocess.getoutput( "git log -1 --pretty=%ad --date=iso " + post) if not git_lastmod: continue lates_commit = subprocess.getoutput("git log -1 --pretty=%B " + post) if "[Automation]" in lates_commit and "Lastmod" in lates_commit: continue frontmatter, line_num = get_yaml(post) meta = yaml.load(frontmatter) if 'seo' in meta: if ('date_modified' in meta['seo'] and meta['seo']['date_modified'] == git_lastmod): continue else: meta['seo']['date_modified'] = git_lastmod else: meta.insert(line_num, 'seo', dict(date_modified=git_lastmod)) output = 'new.md' if os.path.isfile(output): os.remove(output) with open(output, 'w') as new, open(post, 'r') as old: new.write("---\n") yaml.dump(meta, new) new.write("---\n") line_num += 2 lines = old.readlines() for line in lines: if line_num > 0: line_num -= 1 continue else: new.write(line) shutil.move(output, post) count += 1 if verbose: print("[INFO] update 'lastmod' for:" + post) if count > 0: print("[INFO] Success to update lastmod for {} post(s).".format(count))