Example #1
0
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 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.check_output([
            "git", "log", "-1", "--pretty=%ad", "--date=iso", post]).strip()

        if not git_lastmod:
            continue

        frontmatter, line_num = get_yaml(post)
        meta = yaml.load(frontmatter)

        if 'lastmod' in meta:
            if meta['lastmod'] == git_lastmod:
                continue
            else:
                meta['lastmod'] = git_lastmod
        else:
            meta.insert(line_num, 'lastmod', 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

    print ("[INFO] Success to update lastmod for {} post(s).").format(count)
def main():
    count = 0
    yaml = YAML()

    for post in glob.glob(os.path.join(POSTS_PATH, "*.md")):
        git_lastmod = subprocess.check_output(
            ["git", "log", "-1", "--pretty=%ad", "--date=short",
             post]).strip()

        if not git_lastmod:
            continue

        frontmatter, line_num = get_yaml(post)
        meta = yaml.load(frontmatter)

        if 'sitemap' in meta:
            if 'lastmod' in meta['sitemap']:
                if meta['sitemap']['lastmod'] == git_lastmod:
                    continue

                meta['sitemap']['lastmod'] = git_lastmod

            else:
                meta['sitemap'].insert(0, 'lastmod', git_lastmod)
        else:
            meta.insert(line_num, 'sitemap', {'lastmod': 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
        print "[INFO] update 'lastmod' for: '{}'".format(post)

    print "[NOTICE] Success! Update all posts's lastmod.\n"

    if count > 0:
        subprocess.call(["git", "add", POSTS_PATH])
        subprocess.call(
            ["git", "commit", "-m", "[Automation] Update lastmod of post(s)."])
Example #4
0
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("Cannot found 'tags' in \
                  post '{}' !".format(file))

    return all_tags