Exemple #1
0
def add_notes():
    user = User.query.filter_by(username='******').first()
    notedir = os.path.abspath(os.path.dirname(__file__)) + '/notes'
    os.chdir(notedir)
    for each in os.listdir('.'):
        nowdir = notedir + '/' + each
        if os.path.isdir(nowdir):
            book = Notebook.query.filter_by(name=each).first()
            if book == None:
                book = Notebook(name=each)
                db.session.add(book)
                db.session.commit()
            os.chdir(nowdir)
            for file in os.listdir('.'):
                div = file.split('.')
                if len(div) > 1 and (div[1] == 'md'):
                    with open(nowdir + '/' + file, "r")as fin:
                        body = fin.read()
                        note = Post(title=div[0], body=body, author_id=user.id, notebook_id=book.id)
                        db.session.add(note)
                if file == 'README':
                    with open(nowdir + '/' + file, "r")as fin:
                        text = fin.read()
                        book.descripton = text
            db.session.add(book)
            db.session.commit()
Exemple #2
0
def standardize():
    user = User.query.filter_by(username='******').first()
    notedir = os.path.abspath(os.path.dirname(__file__)) + '/notes'
    appdir = os.path.abspath(os.path.dirname(__file__)) + '/app/templates/notes'
    os.chdir(notedir)
    for each in os.listdir('.'):
        nowdir = notedir + '/' + each
        if os.path.isdir(nowdir):
            os.chdir(nowdir)
            if os.path.isdir(appdir + '/' + each) == False:
                os.mkdir(appdir + '/' + each)
            book = Notebook.query.filter_by(name=each).first()
            if book == None:
                book = Notebook(name=each)
                db.session.add(book)
                db.session.commit()
            for file in os.listdir('.'):
                div = file.split('.')
                if len(div) > 1 and (div[1] == 'html'):
                    with open(nowdir + '/' + file, "r", encoding="utf-8")as fin:
                        text = fin.readlines()
                        text[0] = """{% extends "base.html" %}\n"""
                        text[1] = """{% block title %}羽儿之家 - {{ title }}{% endblock %}\n"""
                        text[2] = """{% block head %}    {{ super() }}\n"""
                        for i in range(len(text)):
                            if text[i].split('\n')[0] == "</head>":
                                text[i] = """{% endblock %}{% block content %}\n"""
                        # text[-7] = """{% endblock %}{% block content %}\n"""
                        text[-1] = """{% endblock %}\n"""
                        with open(appdir + '/' + each + '/' + file, "w", encoding="utf-8") as fout:
                            fout.writelines(text)
                        note = Post.query.filter_by(title=div[0]).first()
                        if note == None:
                            body = "<a href=\"" + str(
                                url_for('notebook.note_html', bookname=each, notename=div[0])) + "\">" + div[0] + "</a>"
                            note = Post(title=div[0], body=body, author_id=user.id, notebook_id=book.id)
                            db.session.add(note)
                if file == 'README':
                    with open(nowdir + '/' + file, "r", encoding="utf-8")as fin:
                        text = fin.read()
                        book.descripton = text
                        db.session.add(book)
        db.session.commit()