Inital commit
This commit is contained in:
178
render.py
Normal file
178
render.py
Normal file
@@ -0,0 +1,178 @@
|
||||
import os
|
||||
import yaml
|
||||
import shutil
|
||||
from jinja2 import Template
|
||||
|
||||
# from header import headerGen
|
||||
from renderMD import renderMD
|
||||
from renderSCSS import renderSCSS
|
||||
|
||||
with open('template.html', 'r') as file:
|
||||
template = Template(file.read())
|
||||
|
||||
srcDir = '../notes'
|
||||
destDir = '../public'
|
||||
|
||||
firstItem = True
|
||||
|
||||
def headerGen(item, root):
|
||||
global firstItem
|
||||
if(root and not firstItem):
|
||||
separator = '<span class="menuSeparator">|</span>'
|
||||
else:
|
||||
separator = ''
|
||||
firstItem = False
|
||||
|
||||
lable = item['lable']
|
||||
|
||||
if(not ('klickable' in item and item['klickable'] is False)):
|
||||
lable = '<a href="' + item["url"] + '">' + item["lable"] + '</a>'
|
||||
|
||||
if('sub' not in item):
|
||||
return '<li>' + separator + '<span class="menuLable">' + lable + '</span></li>'
|
||||
|
||||
data = ''
|
||||
subicon = ''
|
||||
if(root):
|
||||
subicon = ' <svg class="menuDropIcon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><path d="M1,3.7L5,8L9,3.7" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" fill="none" stroke="#eee"></path></svg>'
|
||||
data += ' data-hassub="true"'
|
||||
|
||||
html = '<li' + data + '>' + separator + '<span class="menuLable sub">' + lable + subicon + '</span><ul class="submenu">'
|
||||
html += listPages(item['sub'], item['url'], headerGen)
|
||||
|
||||
return html + '</ul></li>'
|
||||
|
||||
def sidebarGen(item, root):
|
||||
if(not ('klickable' in item and item['klickable'] is False)):
|
||||
item['lable'] = '<a href="' + item["url"] + '">' + item["lable"] + '</a>'
|
||||
|
||||
if('sub' not in item):
|
||||
return '<li><span class="menuLable">' + item["lable"] + '</span></li>'
|
||||
|
||||
html = '<li><span class="menuLable sub">' + item["lable"] + '</span><ul class="submenu">'
|
||||
html += listPages(item['sub'], item['url'], sidebarGen)
|
||||
|
||||
return html + '</ul></li>'
|
||||
|
||||
def listPages(items, parentUrl, cb):
|
||||
root = False
|
||||
if(firstItem):
|
||||
root = True
|
||||
html = ""
|
||||
for item in items:
|
||||
|
||||
if(type(item) is dict):
|
||||
if('url' not in item):
|
||||
if('lable' not in item):
|
||||
print('url or lable reqired')
|
||||
continue
|
||||
if('klickable' in item and item['klickable'] is False):
|
||||
item["url"] = parentUrl
|
||||
else:
|
||||
item["url"] = parentUrl + '/' + item["lable"]
|
||||
|
||||
if(item['url'] == ''):
|
||||
item['url'] = '/'
|
||||
|
||||
if(item["url"][0] != '/'):
|
||||
item["url"] = parentUrl + '/' + item["url"]
|
||||
|
||||
#WARNING: XSS vanrable!
|
||||
if('lable' not in item):
|
||||
item["lable"] = item["url"].split('/')[-1]
|
||||
|
||||
# if(not ('klickable' in item and item['klickable'] is False)):
|
||||
html += cb(item, root)
|
||||
continue
|
||||
|
||||
if(type(item) is str):
|
||||
html += cb({"lable": item, "url": parentUrl + '/' + item}, root)
|
||||
continue
|
||||
|
||||
return html
|
||||
|
||||
sidebar = ''
|
||||
def renderPage(item, root):
|
||||
global srcDir, destDir, header, template, sidebar
|
||||
html = ''
|
||||
|
||||
srcFile = item['url']
|
||||
if(os.path.isdir(srcDir + item['url'])):
|
||||
if(item['url'][-1] == '/'):
|
||||
srcFile = item['url'] + 'index.md'
|
||||
else:
|
||||
srcFile = item['url'] + '/index.md'
|
||||
elif(len(item['url'].split('.')) > 1 and len(item['url'].split('.')[-1]) < 5):
|
||||
srcFile = '.'.join(item['url'].split('.')[0:-1]) + '.md'
|
||||
|
||||
if(not os.path.isfile(srcDir + srcFile)):
|
||||
print('faild to find path to page for ' + item['url'])
|
||||
else:
|
||||
print('md: ' + srcFile)
|
||||
|
||||
if(not os.path.isdir(destDir + '/'.join(srcFile.split('/')[0:-1]))):
|
||||
os.mkdir(destDir + '/'.join(srcFile.split('/')[0:-1]))
|
||||
|
||||
if('sidebar' in item):
|
||||
sidebar = '<aside><nav><ul id="sidemenu">'
|
||||
sidebar += listPages(item['sidebar'], item['url'], sidebarGen)
|
||||
sidebar += '</ul></nav></aside>'
|
||||
|
||||
target = open(destDir + '.'.join(srcFile.split('.')[0:-1]) + '.html', "w")
|
||||
target.write(template.render(
|
||||
header = header,
|
||||
sidebar = sidebar,
|
||||
body = renderMD(srcDir + srcFile)
|
||||
))
|
||||
target.close()
|
||||
|
||||
if('sidebar' in item):
|
||||
listPages(item['sidebar'], item['url'], renderPage)
|
||||
sidebar = ''
|
||||
if('sub' in item):
|
||||
listPages(item['sub'], item['url'], renderPage)
|
||||
|
||||
return ''
|
||||
|
||||
def copyAssets(d):
|
||||
global srcDir, destDir
|
||||
for f in os.listdir(srcDir + d):
|
||||
if(os.path.isdir(srcDir + d + f)):
|
||||
if(not os.path.isdir(destDir + d + f)):
|
||||
os.mkdir(destDir + d + f)
|
||||
copyAssets(d + f + '/')
|
||||
continue
|
||||
ext = f.split('.')[-1]
|
||||
|
||||
|
||||
if(ext == 'md'):
|
||||
# print('md: ' + d + f)
|
||||
# target = open(destDir + d + '.'.join(f.split('.')[0:-1]) + '.html', "w")
|
||||
# target.write(template.render(
|
||||
# header = header,
|
||||
# body = renderMD(srcDir + d + f)
|
||||
# ))
|
||||
# target.close()
|
||||
continue
|
||||
|
||||
|
||||
if(ext == 'scss'):
|
||||
print('scss: ' + d + f)
|
||||
target = open(destDir + d + '.'.join(f.split('.')[0:-1]) + '.css', "w")
|
||||
target.write(renderSCSS(srcDir + d + f))
|
||||
target.close()
|
||||
continue
|
||||
|
||||
print("copy: " + d + f)
|
||||
shutil.copyfile(srcDir + d + f, destDir + d + f)
|
||||
|
||||
if(__name__ == '__main__'):
|
||||
with open('index.yml', 'r') as file:
|
||||
index = yaml.safe_load(file)
|
||||
|
||||
header = '<header><nav><ul id="menu">'
|
||||
header += listPages(index, '', headerGen)
|
||||
header += '</ul></nav></header>'
|
||||
|
||||
copyAssets('/')
|
||||
listPages(index, '', renderPage)
|
||||
Reference in New Issue
Block a user