all repos — py-vite @ 9b7ca8070dbe98b914544b321bf27edb199d4bd0

the original vite, written in python

make.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
#!/usr/bin/env python3

import os
import sys
import jinja2
import time

from markdown2 import markdown_path
from huepy import *

# import config file
try:
    sys.path.append(os.getcwd())
    import config
except ModuleNotFoundError:
    print(bad('Error: config.py not found.'))
    print(que('Are you sure you\'re in a project directory?'))
    sys.exit(1)

# constants
PAGES_PATH = 'pages/'
BUILD_PATH = 'build/'
TEMPL_PATH = 'templates/'
TEMPL_FILE = TEMPL_PATH + config.template


# jinja2
def jinja_render(html_text, TEMPL_FILE):
    template_loader = jinja2.FileSystemLoader('./')
    env = jinja2.Environment(loader=template_loader)
    template = env.get_template(TEMPL_FILE)
    output = template.render(title=config.title,
                             author=config.author,
                             header=config.header,
                             footer=config.footer,
                             body=html_text)
    return output


def markdown_render(filename):
    html_text = markdown_path(PAGES_PATH + filename)
    return html_text


def html_gen():
    for page in os.listdir(PAGES_PATH):
        html_text = markdown_render(page)
        html_file= os.path.splitext(os.path.join(BUILD_PATH, page))[0]
        if not os.path.exists(html_file):
            os.mkdir(html_file)
        output = jinja_render(html_text, TEMPL_FILE)
        with open(os.path.join(html_file, 'index.html'), 'w') as f:
            f.write(output)
            print(run('Rendered %s.' % (page)))   


def main():
    start = time.process_time()
    TEMPL_FILE = TEMPL_PATH + config.template
    if not os.listdir(PAGES_PATH):
        print(info(italic('pages') + ' directory is empty. Nothing to build.'))
        sys.exit(1)
    else:
        try:
           html_gen()
           print(info('Done in %0.5fs.' % (time.process_time() - start)))
        except jinja2.exceptions.TemplateNotFound:
            print(bad('Error: specified template not found: %s' % (TEMPL_FILE)))


if __name__ == "__main__":
    main()