# vite - a simple and minimal static site generator, that JustWorks™ # Copyright (c) 2020 Anirudh Oppiliappan # Licensed under the MIT license import sys import pathlib import os import jinja2 import time import http.server import socketserver import shutil import datetime import re from myrkdown import markdown_path from huepy import * from livereload import Server from subprocess import call # constants 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") 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?")) 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")) create_config(path) create_template(path) print(good("Created project directory at %s." % (abs_path))) except FileExistsError: 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") url = os.path.splitext(os.path.basename(path))[0] 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.")) else: with open(os.path.join(PAGES_PATH, head, tail), "w") as f: to_write = ( """--- template: url: {u} title: subtitle: date: {t} ---\n""" ).format(t=today, u=url) f.write(to_write) 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 title = '' author = '' header = '' footer = '' pre_build = [] post_build = [] 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( """
{{ header }} {{ title }}
{{ body }}