Ejemplo n.º 1
0
def new_post(filename):
    try:
        load_config()
        load_base()
        load_path()
        initial_content = 'title: Your post title\ndate: %s\n\nStart writing here...' % datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        write(g.path.posts, filename + '.md', initial_content)
        puts(colored.green('New post is written successfully.'))
    except:
        puts(colored.red('Post cannot be written.'))
Ejemplo n.º 2
0
def handle_post(filename):
    puts('Reading %s now...' % filename)
    with codecs.open(get_path(g.path.posts, filename), mode='r', encoding='utf-8') as f:
        lines = f.readlines()
        if lines[0].startswith('---'):
            lines.pop(0)
        for l, line in enumerate(lines):
            if line.startswith('---') or line.startswith('\n'):
                """
                post.title: title of the post
                post.slug: url of the post, same as the filename
                post.date: standard datetime
                post.content: html content of the post
                """
                post = ObjectDict(dict((a.lower(), b) for a,b in yaml.load(''.join(lines[:l])).iteritems()))
                post.slug = re.split('\.+', filename)[0]

                if type(post.date) is datetime:
                    pass
                elif type(post.date) is date:
                    post.date = datetime.combine(post.date, time(0, 0))
                else:
                    puts(colored.yellow('Post %s has wrong date format.' % filename))

                post.content = markdown.markdown(''.join(lines[l + 1:]))
                g.archive.append(post)
                with indent(2, quote='>'):
                    puts('read successfully.')
                break
    try:
        html = render_template('post.html', post=post)
        with indent(2, quote='>'):
            puts('rendered successfully.')
        try:
            write(g.path.site_post, re.split('\.+', filename)[0] + '.html', html)
            with indent(2, quote='>'):
                puts('written successfully.')
        except:
            puts(colored.red('%s could not be written.' % filename))
    except:
        puts(colored.red('%s could not be rendered.' % filename))
Ejemplo n.º 3
0
def handle_page(filename):
    if not filename == 'post.html':
        html = render_template(filename, posts=g.archive)
        write(g.path.site, filename, html)