all repos — py-vite @ 96c06efec07117f0e478ff7d06cc420b05d93972

the original vite, written in python

Formatting with black, fix default templating

Signed-off-by: Anirudh Oppiliappan <x@icyphox.sh>
Anirudh Oppiliappan x@icyphox.sh
Fri, 04 Oct 2019 12:16:35 +0530
commit

96c06efec07117f0e478ff7d06cc420b05d93972

parent

6f65595dc14e64a61655412a6536db162b93cba6

3 files changed, 116 insertions(+), 87 deletions(-)

jump to
M vite/__init__.pyvite/__init__.py

@@ -1,1 +1,1 @@

-__version__ = '1.5.2' +__version__ = "1.5.2"
M vite/cli.pyvite/cli.py

@@ -6,38 +6,45 @@ from vite import __version__

def main(): - desc = green('A simple and minimal static site generator.') - usage = lightblue('vite') + ' [options]' + desc = green("A simple and minimal static site generator.") + usage = lightblue("vite") + " [options]" parser = argparse.ArgumentParser(description=desc, usage=usage) - parser.add_argument('-v', '--version', action='version', version='{version}'.format(version=__version__)) - sp = parser.add_subparsers(dest='cmd', help='Options to help create, build and serve your project.') - #for cmd in ['init', 'new']: - sp_init = sp.add_parser('init') - sp_new = sp.add_parser('new') - for cmd in ['build', 'serve']: + parser.add_argument( + "-v", + "--version", + action="version", + version="{version}".format(version=__version__), + ) + sp = parser.add_subparsers( + dest="cmd", help="Options to help create, build and serve your project." + ) + # for cmd in ['init', 'new']: + sp_init = sp.add_parser("init") + sp_new = sp.add_parser("new") + for cmd in ["build", "serve"]: sp.add_parser(cmd) - sp_init.add_argument('path') - sp_new.add_argument('path') + sp_init.add_argument("path") + sp_new.add_argument("path") args = parser.parse_args() if len(sys.argv) == 1: parser.print_help() sys.exit(1) - elif args.cmd == 'init': + elif args.cmd == "init": if args.path: vite.create_project(args.path) else: parser.print_help() - elif args.cmd == 'new': + elif args.cmd == "new": if args.path: vite.import_config() vite.create_path(args.path) else: parser.print_help() - elif args.cmd == 'build': + elif args.cmd == "build": vite.import_config() vite.builder() - elif args.cmd == 'serve': + elif args.cmd == "serve": vite.import_config() vite.server() else:
M vite/vite.pyvite/vite.py

@@ -21,80 +21,83 @@ from subprocess import call

# constants -PAGES_PATH = 'pages/' -BUILD_PATH = 'build/' -TEMPL_PATH = 'templates/' -TEMPL_FILE = '' +PAGES_PATH = "pages/" +BUILD_PATH = "build/" +TEMPL_PATH = "templates/" +TEMPL_FILE = "" PORT = 1911 def import_config(): try: sys.path.append(os.getcwd()) - globals()['config'] = __import__('config') + globals()["config"] = __import__("config") global TEMPL_FILE TEMPL_FILE = os.path.join(TEMPL_PATH, config.template) except ImportError: - print(bad('Error: config.py not found.')) - print(que('Are you sure you\'re in a project directory?')) + print(bad("Error: config.py not found.")) + print(que("Are you sure you're in a project directory?")) sys.exit(1) def create_project(path): try: abs_path = pathlib.Path(path).resolve() - cur_path = pathlib.Path('.').resolve() - os.makedirs(os.path.join(path, 'build')) - os.mkdir(os.path.join(path, 'pages')) - os.mkdir(os.path.join(path, 'templates')) - os.mkdir(os.path.join(path, 'static')) + cur_path = pathlib.Path(".").resolve() + os.makedirs(os.path.join(path, "build")) + os.mkdir(os.path.join(path, "pages")) + os.mkdir(os.path.join(path, "templates")) + os.mkdir(os.path.join(path, "static")) create_config(path) create_template(path) - print(good('Created project directory at %s.' % (abs_path))) + print(good("Created project directory at %s." % (abs_path))) except FileExistsError: - print(bad('Error: specified path exists.')) + print(bad("Error: specified path exists.")) def create_path(path): head, tail = os.path.split(path) now = datetime.datetime.now() - today = now.strftime('%Y-%m-%d') + today = now.strftime("%Y-%m-%d") try: os.makedirs(os.path.join(PAGES_PATH, head)) except FileExistsError: pass if os.path.exists(os.path.join(PAGES_PATH, head, tail)): - print(bad('Error: specified path exists.')) + print(bad("Error: specified path exists.")) else: - with open(os.path.join(PAGES_PATH, head, tail), 'w') as f: + with open(os.path.join(PAGES_PATH, head, tail), "w") as f: to_write = ( -"""--- + """--- template: title: subtitle: date: {t} ---\n""" - ).format(t=today) + ).format(t=today) f.write(to_write) - print(good('Created %s.') % (os.path.join(PAGES_PATH, head, tail))) + print(good("Created %s.") % (os.path.join(PAGES_PATH, head, tail))) def create_config(path): - with open(os.path.join(path, 'config.py'), 'w') as f: - f.write("""# config.py - Vite's configuration script + with open(os.path.join(path, "config.py"), "w") as f: + f.write( + """# config.py - Vite's configuration script title = '' author = '' header = '' footer = '' post_build = [] -template = 'index.html' # default is index.html\n""") +template = 'index.html' # default is index.html\n""" + ) def create_template(path): - with open(os.path.join(path, 'templates', 'index.html'), 'w') as f: - f.write("""<!DOCTYPE html> + with open(os.path.join(path, "templates", "index.html"), "w") as f: + f.write( + """<!DOCTYPE html> <html> <header> {{ header }}

@@ -112,99 +115,117 @@ {{ footer }}

<p> {{ author }} </p> <footer> - """) + """ + ) + # jinja2 def jinja_render(html, tmpl): - template_loader = jinja2.FileSystemLoader('./') + template_loader = jinja2.FileSystemLoader("./") env = jinja2.Environment(loader=template_loader) try: template = env.get_template(tmpl) - meta = html.metadata - output = template.render(title=meta['title'] if 'title' in meta else config.title, - author=meta['author'] if 'author' in meta else config.author, - header=meta['header'] if 'header' in meta else config.header, - footer=meta['footer'] if 'footer' in meta else config.footer, - date=meta['date'] if 'date' in meta else '', - subtitle=meta['subtitle'] if 'subtitle' in meta else '', - body=html) - return output except jinja2.exceptions.TemplateNotFound: - print(bad('Error: specified template not found: %s' % (tmpl))) - sys.exit(1) + template = env.get_template(TEMPL_FILE) + meta = html.metadata + output = template.render( + title=meta["title"] if "title" in meta else config.title, + author=meta["author"] if "author" in meta else config.author, + header=meta["header"] if "header" in meta else config.header, + footer=meta["footer"] if "footer" in meta else config.footer, + date=meta["date"] if "date" in meta else "", + subtitle=meta["subtitle"] if "subtitle" in meta else "", + body=html, + ) + return output def fm_template(metadata): try: - page_template = os.path.join(os.path.join(TEMPL_PATH, metadata['template'])) + page_template = os.path.join(os.path.join(TEMPL_PATH, metadata["template"])) except KeyError: page_template = TEMPL_FILE return page_template def markdown_render(filename): - html = markdown_path(os.path.join(PAGES_PATH, filename), extras=['metadata', 'fenced-code-blocks', 'header-ids', 'footnotes', 'smarty-pants']) + html = markdown_path( + os.path.join(PAGES_PATH, filename), + extras=[ + "metadata", + "fenced-code-blocks", + "header-ids", + "footnotes", + "smarty-pants", + ], + ) return html def html_gen(): - def index_render(f, d=''): + def index_render(f, d=""): index_html = markdown_render(os.path.join(d, f)) output = jinja_render(index_html, fm_template(index_html.metadata)) - with open(os.path.join(BUILD_PATH, d, 'index.html'), 'w') as ff: + with open(os.path.join(BUILD_PATH, d, "index.html"), "w") as ff: ff.write(output) if d: - print(run('Rendered ' + white('%s/%s') % (d, f))) + print(run("Rendered " + white("%s/%s") % (d, f))) else: - print(run('Rendered ' + white('%s') % (f))) + print(run("Rendered " + white("%s") % (f))) - def normal_render(f, d=''): + def normal_render(f, d=""): html_text = markdown_render(os.path.join(d, f)) html_file = os.path.splitext(os.path.join(BUILD_PATH, d, f))[0] os.mkdir(html_file) output = jinja_render(html_text, fm_template(html_text.metadata)) - with open(os.path.join(html_file, 'index.html'), 'w') as ff: + with open(os.path.join(html_file, "index.html"), "w") as ff: ff.write(output) if d: - print(run('Rendered ' + white('%s/%s') % (d, f))) + print(run("Rendered " + white("%s/%s") % (d, f))) else: - print(run('Rendered ' + white('%s') % (f))) - + print(run("Rendered " + white("%s") % (f))) for root, dirs, files in os.walk(PAGES_PATH): for d in dirs: os.mkdir(os.path.join(BUILD_PATH, d)) for f in os.listdir(os.path.join(PAGES_PATH, d)): - if os.path.splitext(f)[1] != '.md': - shutil.copyfile(os.path.join(PAGES_PATH, d, f), - os.path.join(BUILD_PATH, d, f)) - print(run('Copied ' + white('%s/%s') % (d, f))) - elif f == '_index.md': + if os.path.splitext(f)[1] != ".md": + shutil.copyfile( + os.path.join(PAGES_PATH, d, f), os.path.join(BUILD_PATH, d, f) + ) + print(run("Copied " + white("%s/%s") % (d, f))) + elif f == "_index.md": index_render(f, d) else: normal_render(f, d) for f in os.listdir(PAGES_PATH): if os.path.isfile(os.path.join(PAGES_PATH, f)): - if os.path.splitext(f)[1] != '.md': - shutil.copyfile(os.path.join(PAGES_PATH, f), os.path.join(BUILD_PATH, f)) - print(run('Copied ' + white('%s') % (f))) - elif f == '_index.md': + if os.path.splitext(f)[1] != ".md": + shutil.copyfile( + os.path.join(PAGES_PATH, f), os.path.join(BUILD_PATH, f) + ) + print(run("Copied " + white("%s") % (f))) + elif f == "_index.md": index_render(f) else: normal_render(f) def server(): -# handler = http.server.SimpleHTTPRequestHandler -# os.chdir(os.path.join(os.getcwd(), BUILD_PATH)) + # handler = http.server.SimpleHTTPRequestHandler + # os.chdir(os.path.join(os.getcwd(), BUILD_PATH)) server = Server() try: - print(run(f'Serving the {italic(yellow("build"))} directory at {white(f"http://localhost:{PORT}")}')) - print(white('Ctrl+C') + ' to stop.') - server.serve(port=PORT, root='build/') + print( + run( + f'Serving the {italic(yellow("build"))} directory at {white(f"http://localhost:{PORT}")}' + ) + ) + print(white("Ctrl+C") + " to stop.") + server.serve(port=PORT, root="build/") except KeyboardInterrupt: - print(info('Stopping server.')) + print(info("Stopping server.")) sys.exit(1)

@@ -224,20 +245,21 @@ def builder():

path = os.getcwd() start = time.process_time() if not os.listdir(os.path.join(path, PAGES_PATH)): - print(info(italic('pages') + ' directory is empty. Nothing to build.')) + print(info(italic("pages") + " directory is empty. Nothing to build.")) sys.exit(1) else: clean() html_gen() - if os.path.exists(os.path.join(os.getcwd(), 'static')): - shutil.copytree(os.path.join(os.getcwd(), 'static'), os.path.join(BUILD_PATH, 'static')) - print(good('Done in %0.5fs.' % (time.process_time() - start))) + if os.path.exists(os.path.join(os.getcwd(), "static")): + shutil.copytree( + os.path.join(os.getcwd(), "static"), os.path.join(BUILD_PATH, "static") + ) + print(good("Done in %0.5fs." % (time.process_time() - start))) try: - if config.post_build != '': - print(run('Running post-build actions...')) + if config.post_build != "": + print(run("Running post-build actions...")) for s in config.post_build: - print(info(f'{s}')) + print(info(f"{s}")) call([s]) except AttributeError: pass -