You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.8 KiB
Python
83 lines
1.8 KiB
Python
from jinja2 import Environment, PackageLoader, select_autoescape
|
|
import frontmatter
|
|
from math import floor
|
|
import sys, getopt
|
|
|
|
import http.server
|
|
import socketserver
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
# parse argument from commandline
|
|
|
|
folder = ''
|
|
try:
|
|
opts, args = getopt.getopt(argv,"hf:",["folder="])
|
|
except getopt.GetoptError:
|
|
print('generate.py -f <folder>')
|
|
sys.exit(2)
|
|
for opt, arg in opts:
|
|
if opt == '-h':
|
|
print('generate.py -f <folder>')
|
|
sys.exit()
|
|
elif opt in ("-f", "--folder"):
|
|
folder = arg
|
|
|
|
|
|
|
|
# read the contents
|
|
|
|
with open(f"{folder}/contents.md", "r") as f:
|
|
metadata, body = frontmatter.parse(f.read())
|
|
contents = metadata['contents']
|
|
|
|
page = {
|
|
'folder': folder,
|
|
'post_width': 50,
|
|
'post_height': 50,
|
|
'page_width': 210,
|
|
'page_height': 297,
|
|
'background': 'white',
|
|
'margin': 5,
|
|
'unit': 'mm'
|
|
}
|
|
|
|
|
|
page.update(metadata)
|
|
|
|
page['rows']= floor((page['page_height'] - 2 * page['margin']) / page['post_height'])
|
|
page['cols']= floor((page['page_width']- 2 * page['margin']) / page['post_width'])
|
|
|
|
|
|
|
|
|
|
# generate the template
|
|
|
|
env = Environment(
|
|
loader=PackageLoader("postit"),
|
|
autoescape=select_autoescape()
|
|
)
|
|
|
|
|
|
|
|
|
|
template = env.get_template('table.html')
|
|
render = template.render(contents=contents, page=page)
|
|
|
|
with open(f'{folder}/index.html', 'w') as output:
|
|
output.write(render)
|
|
|
|
|
|
|
|
PORT = 8000
|
|
handler = http.server.SimpleHTTPRequestHandler
|
|
|
|
with socketserver.TCPServer(("", PORT), handler) as httpd:
|
|
print(f"Server started at http://localhost:{str(PORT)}/{folder}")
|
|
httpd.serve_forever()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:]) |