Commit 38ec7b5d authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers 🍪

add support for dynamic paths, fixes #5

parent c67cfb81
......@@ -15,9 +15,9 @@ def find_all(dirpath):
files.extend( find_all(path) )
return files
def generate(args, datadir='_data', outdir='site/'):
out_path = pathlib.Path(outdir)
data_path = pathlib.Path(datadir)
def generate(args):
out_path = pathlib.Path(args.out_dir)
data_path = pathlib.Path(args.data_dir)
template_path = pathlib.Path(args.template_dir)
generator = build.get_builder("html", args.template_dir)
......
......@@ -17,67 +17,69 @@ from j2static.tools.merge import load_data
from http.server import HTTPServer, BaseHTTPRequestHandler
PORT = 8080
TEMPLATE_DIR = "_templates/"
DATA_DIR = pathlib.Path("./_data/")
class TemplateHTTPServer(BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
super().__init__(request, client_address, server)
def do_GET(self):
file_path = self.path
if file_path[-1] == "/":
file_path += "index.html"
builder = get_builder("html", TEMPLATE_DIR)
if builder.filter(file_path):
try:
context = []
template_path = pathlib.Path(file_path[1:])
data_file = DATA_DIR / template_path.parent / (template_path.stem + ".json")
if data_file.exists():
context = load_data(data_file)
data = builder.render(file_path, context=context)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
except jinja2.TemplateNotFound as e:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
data = "{e} does not exist as a template".format(e=e)
except jinja2.TemplateSyntaxError as e:
self.send_response(500)
self.send_header('Content-type', 'text/html')
self.end_headers()
data = "<h1>template error</h1> <p>{e.name}, line {e.lineno}: <samp>{e.message}</samp></p>".format(e=e)
self.wfile.write(data.encode())
else:
fpath = pathlib.Path(TEMPLATE_DIR) / pathlib.Path(file_path[1:])
print(fpath.resolve())
if not fpath.exists():
self.send_response(404)
data = "not valid page"
else:
mime, enc = mimetypes.guess_type(str(fpath))
self.send_response(200)
self.send_header('content-type', mime)
self.end_headers()
with open(fpath, 'rb') as content:
shutil.copyfileobj(content, self.wfile)
def makeRequestHandler(args):
class TemplateHTTPServer(BaseHTTPRequestHandler, object):
def __init__(self, request, client_address, server):
self.template_dir = args.template_dir
self.data_dir = args.data_dir
super().__init__(request, client_address, server)
def do_GET(self):
file_path = self.path
if file_path[-1] == "/":
file_path += "index.html"
builder = get_builder("html", self.template_dir)
if builder.filter(file_path):
try:
context = []
template_path = pathlib.Path(file_path[1:])
data_file = self.data_dir / template_path.parent / (template_path.stem + ".json")
if data_file.exists():
context = load_data(data_file)
data = builder.render(file_path, context=context)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
except jinja2.TemplateNotFound as e:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
data = "{e} does not exist as a template".format(e=e)
except jinja2.TemplateSyntaxError as e:
self.send_response(500)
self.send_header('Content-type', 'text/html')
self.end_headers()
data = "<h1>template error</h1> <p>{e.name}, line {e.lineno}: <samp>{e.message}</samp></p>".format(e=e)
self.wfile.write(data.encode())
else:
fpath = pathlib.Path(self.template_dir) / pathlib.Path(file_path[1:])
print(fpath.resolve())
if not fpath.exists():
self.send_response(404)
data = "not valid page"
else:
mime, enc = mimetypes.guess_type(str(fpath))
self.send_response(200)
self.send_header('content-type', mime)
self.end_headers()
with open(fpath, 'rb') as content:
shutil.copyfileobj(content, self.wfile)
return TemplateHTTPServer
def serve(args):
Handler = TemplateHTTPServer
Handler = makeRequestHandler(args)
httpd = HTTPServer(('', PORT), Handler)
print("webpage will be served on http://localhost:8080")
httpd.serve_forever()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment