html: better navbar management. Use virtual nginx line from navbar.html

This commit is contained in:
Cyrille L 2023-01-11 10:00:42 +01:00
parent 2db9fa9af8
commit 4e21403e3d
3 changed files with 75 additions and 45 deletions

View File

@ -21,12 +21,13 @@ import os
import tyto
# Load domain configuration DB
exec(open(tyto.domain_conf).read(),globals())
exec(open(tyto.domain_conf).read())
Tyto = 'Tyto - Littérateur'
tytogit = 'https://git.a-lec.org/echolib/tyto-litterateur'
tytoweb = 'https://tyto.echolib.re'
navbar_file = '%snavbar.html'%srv_wip_tpl
#==========================#
# Load article DB #
@ -135,48 +136,19 @@ def create_main_page(target, article_bottom):
'%sid="site_logo_image">\n'%(15 * " ") + \
'%s</a>'%(8 * " ")
#------------------------------------#
# Create HTML menu from root folders #
#------------------------------------#
menu_html = ''
# Conditions to create navbar
#----------------------------
# True in config
# Check for menu bar
# Delete file if deactivated in conf
if domain_menubar:
# Create folder links (from tyto.navbar) in navbar
try:
nav_file = open(navbar_load, 'r').read()
nav_bar = True
except:
tyto.exiting("1", '(navbar) %s'%navbar_load, False)
nav_bar = False
if nav_bar:
# Open HTML tags
menu_html = '%s<nav id="site_menu">\n'%(6 * ' ') + \
'%s<ul id="site_menu_items">'%(8 * ' ')
nolines = ('#', '/')
for line in nav_file.rsplit('\n'):
if not line or line.startswith(nolines): continue
dir_uri = os.path.join(domain_articles, line)
if not os.path.isdir(dir_uri):
tyto.exiting("1", dir_uri, False)
continue
# Add link to HTML structure
menu_item = '\n%s<li class="site_menu_item">'%(10 * ' ') + \
'<a class="site_menu_link" href="/%s/">%s</a>'%(
line, line
) + \
'</li>'
menu_html = '%s%s'%(menu_html, menu_item)
# Close HTML tags
menu_html = '\n%s\n%s</ul>\n%s</nav>\n'%(menu_html, 8 * ' ', 6 * ' ')
if os.path.exists(navbar_file):
tyto.exiting("25", 'menu from: %s'%navbar_file, False)
else:
tyto.exiting("24", '(menu file): %s'%navbar_file, False)
else:
if os.path.exists(navbar_file) and os.stat(navbar_file).st_size > 1:
tyto.set_file(navbar_file, 'new', '')
tyto.exiting("26", 'domain_menubar', False)
tyto.exiting('27', 'contents (menu file): %s'%navbar_file, False)
#-----------------------#
# Create main HTML Page #
@ -200,7 +172,7 @@ def create_main_page(target, article_bottom):
' </a>\n' + \
' <p id="site_about">%s</p>\n'%domain_about + \
' </div>\n' + \
'%s'%menu_html + \
'<!--# include virtual="/template/navbar.html"-->' + \
' </header>\n' + \
'\n' + \
' <article id="article_main">\n' + \
@ -209,3 +181,55 @@ def create_main_page(target, article_bottom):
'\n' + \
' </body>\n' + \
'</html>'
#====================================#
# Create HTML menu from root folders #
#------------------------------------#
def create_navbar():
# Conditions to create navbar
if not domain_menubar:
tyto.set_file(navbar_file, 'new', '')
tyto.exiting("26", 'domain_menubar', False)
if os.path.exists(navbar_file) and os.stat(navbar_file).st_size > 1:
tyto.exiting('27', 'contents (menu file): %s'%navbar_file, False)
return
# True in config
# Create folder links (from navbars/tyto.navbar)
try: nav_file = open(navbar_load, 'r').read()
except: tyto.exiting("1", '(navbar) %s'%navbar_load, True)
# Open HTML tags
menu_html = '%s<nav id="site_menu">\n'%(6 * ' ') + \
'%s<ul id="site_menu_items">'%(8 * ' ')
nolines = ('#', '/')
for line in nav_file.rsplit('\n'):
if not line or line.startswith(nolines): continue
if '=' in line:
direc = line.rsplit('=')[0].rstrip()
title = '%stitle="%s"\n'%(15 * ' ', line.rsplit('=')[1].lstrip())
else:
direc = line
title = ''
dir_uri = os.path.join(domain_articles, direc)
if not os.path.isdir(dir_uri):
tyto.exiting("28", dir_uri, False)
continue
# Add link to HTML structure
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)
# Close HTML tags
menu_html = '\n%s\n%s</ul>\n%s</nav>\n'%(menu_html, 8 * ' ', 6 * ' ')
tyto.set_file('%snavbar.html'%srv_wip_tpl, 'new', menu_html)

View File

@ -18,7 +18,7 @@
#**********************************************************************
import sys
import tyto
import tyto, html
# Load domain configuration if exists
if tyto.domain_exists: exec(open(tyto.domain_conf).read())
@ -43,6 +43,9 @@ def manage_navbars(target, option):
elif option == 'Edit':
print(":D Edit %s configuration file:"%sys.argv[1], file)
tyto.edit_file(file)
elif option == 'New':
html.create_navbar()
else:
db_exists = tyto.get_db_post(target) # Article exists + has DB ?

View File

@ -507,6 +507,9 @@ def exiting(nbr, value, out):
'23' : ':? %sCorrupted database%s: %s'%(CY, CS, value),
'24' : ':? %sfile missing%s %s'%(CY, CS, value),
'25' : ':D Add contents %s'%value,
'26' : ':? %sDeactivated%s "%s" in domain conf'%(CY, CS, value),
'27' : ':? %sDeleted%s %s'%(CY, CS, value),
'28' : ':? %sUnused directory%s: %s'%(CY, CS, value),
'255' : ':| Maybe later...'
}