navbar creation. Complete rewrite

This commit is contained in:
Cyrille L 2023-03-06 15:26:12 +01:00
parent 596c06d265
commit 1d3322c75b
1 changed files with 120 additions and 45 deletions

View File

@ -340,12 +340,16 @@ def create_sidebar(option):
logs.out("33", target, False) logs.out("33", target, False)
#====================================# #==================================#
# Create HTML menu from root folders # # Create HTML navbar from config #
#------------------------------------# # If no index in src: STOP #
# For wip, create if no index.html #
# for publish all/navbar, STOP #
#----------------------------------#
def create_navbar(option): def create_navbar(option):
domain.domain_needed() domain.domain_needed()
# more confitions to pass
try: try:
db.navbar_load db.navbar_load
if not os.path.exists(db.navbar_load): if not os.path.exists(db.navbar_load):
@ -353,24 +357,32 @@ def create_navbar(option):
except: except:
logs.out("1", 'Navbar load file', True) logs.out("1", 'Navbar load file', True)
# Set target from option
pub_opts = ('www', 'pub')
if option == 'wip': target = db.wip_navbar if option == 'wip': target = db.wip_navbar
elif option == 'www': target = db.www_navbar elif option == 'www': target = db.www_navbar
elif option == 'pub': target = db.www_navbar elif option == 'pub': target = db.www_navbar
# If content in sidebar, go True # If content and error
navbar_new = False navbar_new = False
navbar_has = False navbar_has = False
e_src_index = False
e_www_index = False # If error, do not create file in www server
# Set first HTML line
menu_html = '%s<nav id="site_menu">\n'%(6 * ' ') + \ menu_html = '%s<nav id="site_menu">\n'%(6 * ' ') + \
'%s<ul id="site_menu_items">'%(8 * ' ') '%s<ul id="site_menu_items">'%(8 * ' ')
# Not a line if it starts with...
nolines = ('#', '/') nolines = ('#', '/')
navbar_lines = open(db.navbar_load, 'r').read() navbar_lines = open(db.navbar_load, 'r').read()
for line in navbar_lines.rsplit('\n'):
if not line or line.startswith(nolines):
continue
for line in navbar_lines.rsplit('\n'):
if not line or line.startswith(nolines): continue
# A line is registred
navbar_has = True navbar_has = True
# Get HTML title if defined (commented after) # Get HTML title if defined (commented after)
if '#' in line: if '#' in line:
direc = line.rsplit('#')[0].rstrip() direc = line.rsplit('#')[0].rstrip()
@ -385,39 +397,31 @@ def create_navbar(option):
logs.out("24", '%s/'%dir_uri, False) logs.out("24", '%s/'%dir_uri, False)
continue continue
# Check if an index.[ext] and index.html exist and log # Check if an index.[ext] exists (src, wip, www)
index_src = False src_index = ''
index_wip = False wip_index = '%s%s/index.html'%(db.srv_wip, direc)
index_www = False www_index = '%s%s/index.html'%(db.srv_www, direc)
e_www_index = False
for f in os.listdir(dir_uri):
# An index file must be in the directory
if f.startswith("index."):
index_src = True
if option == 'wip':
wip_index = '%s%s/index.html'%(db.srv_wip, direc)
if not os.path.exists(wip_index):
logs.out('26', 'in "%s": %s'%(direc, wip_index), False)
continue
else:
index_wip = True
elif option == 'www':
www_index = '%s%s/index.html'%(db.srv_www, direc)
if not os.path.exists(www_index):
logs.out('26', 'in "%s": %s'%(direc, www_index), False)
e_www_index = True
continue
else:
index_www = True
else:
break
if not index_src: src_index = [ \
logs.out("26", 'in "%s": %s'%(direc, dir_uri), False) filename \
continue for filename in os.listdir(dir_uri) \
if filename.startswith("index.") \
and os.path.isfile \
]
if not src_index:
src_index = '%s%s/index.[ext]'%(db.domain_articles, direc)
e_src_index = True
logs.out('26', 'in src "%s/": %s'%(direc, src_index), False)
if e_www_index: # Showing unused index.html server file
logs.out("1", '(index article)', True) if option == 'wip':
if not os.path.exists(wip_index):
logs.out('26', 'in wip "%s/": %s'%(direc, wip_index), False)
elif option in pub_opts:
if not os.path.exists(www_index):
logs.out('26', 'in www "%s/": %s'%(direc, www_index), False)
e_www_index = True
continue
# Add link to HTML structure # Add link to HTML structure
navbar_new = True navbar_new = True
@ -430,6 +434,17 @@ def create_navbar(option):
'%s</li>'%(10 * ' ') '%s</li>'%(10 * ' ')
menu_html = '%s%s'%(menu_html, menu_item) menu_html = '%s%s'%(menu_html, menu_item)
# Stop if index error in src dir
if e_src_index or e_www_index:
if not option == 'pub':
logs.out("1", '(index article)', True)
return
# Stop if no www_index (src/dir/index.html)
if option in pub_opts and e_www_index:
logs.out("1", '(index article)', False)
return
# Nothing to do # Nothing to do
if not navbar_new: if not navbar_new:
if not navbar_has: if not navbar_has:
@ -439,11 +454,12 @@ def create_navbar(option):
logs.out('28', '(navbar)', False) logs.out('28', '(navbar)', False)
return return
# Create file if sidebar has content
ask_html = ' ├ Replace %s ? '%target
res = ''
# Ask to replace (except from 'publish template')
if not option == 'pub' and os.path.exists(target): if not option == 'pub' and os.path.exists(target):
ask_html = ' ├ Replace %s ? '%target
res = ''
try: try:
res = input(ask_html) res = input(ask_html)
except KeyboardInterrupt: except KeyboardInterrupt:
@ -457,6 +473,65 @@ def create_navbar(option):
tyto.set_file(target, 'New', menu_html) tyto.set_file(target, 'New', menu_html)
print(' ├ Create file: %s'%target) print(' ├ Create file: %s'%target)
'''
# An index file must be in the src directory
for f in os.listdir(dir_uri):
print('>>> f=', f)
if f.startswith("index."):
if option == 'wip':
if not os.path.exists(wip_index):
logs.out('26', 'in wip "%s/": %s'%(direc, wip_index), False)
continue
elif option == 'www':
if not os.path.exists(www_index):
logs.out('26', 'in www "%s/": %s'%(direc, www_index), False)
e_www_index = True
continue
else:
logs.out("26", 'in src "%s/": %s'%(direc, dir_uri), False)
e_src_index = True
# Add link to HTML structure
navbar_new = True
menu_item = '\n%s<li class="site_menu_item">\n'%(10 * ' ') + \
'%s<a class="site_menu_link"\n'%(12 * ' ') + \
'%s'%title + \
'%shref="/%s/">%s</a>\n'%(
15 * ' ', direc, direc
) + \
'%s</li>'%(10 * ' ')
menu_html = '%s%s'%(menu_html, menu_item)
if e_src_index or e_www_index:
logs.out("1", '(index article)', True)
# Nothing to do
if not navbar_new:
if not navbar_has:
logs.out('29', '(navbar): %s'%db.navbar_load, False)
return
else:
logs.out('28', '(navbar)', False)
return
# Ask to replace (except from 'publish template')
if not option == 'pub' and os.path.exists(target):
ask_html = ' ├ Replace %s ? '%target
res = ''
try:
res = input(ask_html)
except KeyboardInterrupt:
print('')
logs.out("255", '', True)
if not res in ['y', 'Y']:
logs.out("255", '', True)
# Close HTML tags
menu_html = '\n%s\n%s</ul>\n%s</nav>\n'%(menu_html, 8 * ' ', 6 * ' ')
tyto.set_file(target, 'New', menu_html)
print(' ├ Create file: %s'%target)
'''
#========================================================# #========================================================#
# Create metas.html from _configs/tyto.metas.html # # Create metas.html from _configs/tyto.metas.html #
@ -529,6 +604,6 @@ def create_user_footer(option):
if user_footer: user_footer = "%s\n %s"%(user_footer, line) if user_footer: user_footer = "%s\n %s"%(user_footer, line)
else: user_footer = ' %s'%line else: user_footer = ' %s'%line
tyto.set_file(target, True, user_footer) tyto.set_file(target, 'New', user_footer)
print(log_load) print(log_load)