示例#1
0
文件: config.py 项目: shunfan/hazel
def install(template):
    load_base()
    force_mkdir(get_path(g.path.templates, template))
    templates = urllib2.urlopen('https://raw.github.com/shunfan/hazel/master/templates.yml').read()
    templates = yaml.load(templates)
    try:
        git_template = templates[template]
        os.chdir(g.path.templates)
        os.system('git clone %s' % git_template)
        puts(colored.green('Template is installed properly, please change the template name in the general config file manually.'))
    except:
        shutil.rmtree(get_path(g.path.templates, template))
        puts(colored.red('Template %s is not found or installed properly.' % template))
示例#2
0
文件: build.py 项目: shunfan/hazel
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))
示例#3
0
文件: config.py 项目: shunfan/hazel
def default_config():
    default_config_path = get_path(g.hazel_path, 'default', 'config.yml')
    safe_copy(default_config_path, get_path(os.getcwd(), 'config.yml'))