python 从着名的github页面同步(需要python3)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从着名的github页面同步(需要python3)相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python3

"""
syncs from notable to github pages
run it with

curl -sLf https://gist.github.com/roccowang/3e7fe8550cc5780736e71d3a7cc6260c/raw/notable2blog.py |NOTABLE_SRC="{NOTABLE_HOME}/notes" NOTABLE_COMMIT=TRUE python3
"""

import argparse
import frontmatter
import os
import subprocess
import time
import re
from io import BytesIO

NotebookPrefix = 'Notebooks'
PublishTag = 'Notebooks/0.Publish'
DefaultPath = os.path.expanduser('~/.blog')
DefaultPosts = os.path.expanduser('~/.blog/source/_posts')

def parse():
    p = argparse.ArgumentParser()
    p.add_argument("-s", "--src", help="md source dir")
    p.add_argument("--commit", help="whether commit and push", action="store_true")
    return p.parse_args()

class Converter:
    def __init__(self, args):
        self.args = args
        try:
            self.validate()
        except:
            print(F"validate failed. src:{self.args.src}")
            exit(-1)

    def validate(self):
        return os.path.isdir(self.args.src)

    def begin(self):
        self.prepare()
        for (dirpath, dirs, files) in os.walk(self.args.src):
            for filename in files:
                #print(os.path.join(dirpath, filename))
                self.convert(os.path.join(dirpath, filename))
        if self.args.commit:
            self.doCommit()
        return

    """
    prepare git
    ~/.blog/roccowang.github.io
    """
    def prepare(self):
        p = DefaultPath
        if not os.path.exists(p):
            subprocess.run(['git', 'clone', '--single-branch', '--branch', 'raw',
                'https://github.com/roccowang/roccowang.github.io.git', p], 
                check=True)
        else:
            os.chdir(p)
            subprocess.run(['git', 'pull', 'origin', 'raw'], check=True)
        os.chdir(p)
        subprocess.run(['git', 'checkout', 'raw'], check=True)
        os.chdir(F'{p}/source/_posts')
        return

    def doCommit(self):
        ts = time.strftime('{%Y-%m-%d %H:%M:%S}')
        os.chdir(DefaultPosts)
        subprocess.run(['git', 'add', '-A', '.'], check=True)
        try:
            subprocess.run(['git', 'commit', '-m', F"synced from notable. {ts}"], check=True)
        except subprocess.CalledProcessError:
            return
        subprocess.run(['git', 'push', 'origin', 'raw'], check=True)
        return

    def convert(self, filename):
        n, ext = os.path.splitext(filename)
        if ext not in ['.md', '.txt']:
            #print(F"{n}, {ext} not ending with md or txt")
            return
        with open(filename) as f:
            post = frontmatter.load(f)

        if not 'tags' in post.metadata:
            return
        for tag in post.metadata['tags']:
            if tag.startswith(PublishTag):
                #print(F"found {PublishTag} in {post.metadata['tags']}")
                post.content = self.edit(post.content)
                self.doConvert(post, tag)
                return
        return

    """
    * <img> -> ![](), 
    * delete first # heading,
    * s/^#//g
    """
    def edit(self, content):
        newcontent = content
        newcontent = re.sub('<img src="?([^" ]+).+?\/>', r'![](\1)', newcontent)
        newcontent = re.sub('^# .+', r'', newcontent)
        newcontent = re.sub('#(#.+)', r'\1', newcontent)
        return newcontent

    def doConvert(self, post, categorytag):
        category = categorytag[len(PublishTag):]
        if len(category)==0:
            category='Digest'
        else:
            category=category[1:]
        post.metadata['category'] = category

        newtags = set()
        for tag in post.metadata['tags']:
            if tag.startswith(NotebookPrefix):
                continue
            #print(F"tag:{tag}")
            for split in tag.split('/'):
                newtags.add(split)

        post.metadata['tags'] = list(newtags)

        #f = BytesIO()
        #frontmatter.dump(post, f)
        #print(f.getvalue().decode('utf-8')) # doctest: +NORMALIZE_WHITESPACE

        with open(F"{post.metadata['title']}.md", "wb") as f:
            frontmatter.dump(post, f)
        return

def run():
    args = parse()
    if os.getenv("NOTABLE_COMMIT", "NO") in ["YES","Y","TRUE"]:
        args.commit = True
    args.src=os.getenv('NOTABLE_SRC', args.src)
    c = Converter(args)
    c.begin()

if __name__ == "__main__":
    run()

以上是关于python 从着名的github页面同步(需要python3)的主要内容,如果未能解决你的问题,请参考以下文章

CSS 着名的梅耶重置

维乐SensoTT让你成为赛场焦点!

用着名的Square / Rectangle示例打破Liskov原理给出了逻辑错误

创建Python GUI

打字稿,需要模块作为类

eclipse简介及下载