new navbar manager from file tyto.navbar. Form: question to use sidebar

This commit is contained in:
Cyrille L 2023-01-10 18:25:27 +01:00
parent aef44c86ef
commit 2db9fa9af8
6 changed files with 179 additions and 83 deletions

View File

@ -26,7 +26,7 @@
import sys
sys.path.insert(0, '/var/lib/tyto/program')
import check, domain, wip, sidebar
import check, domain, wip, navbars
#====================#
# MAIN #
@ -42,11 +42,12 @@ option = ''
# Command start argument
actions = {
'check' : check.manage_check,
'wip' : wip.manage_wip,
'domain' : domain.manage_domain,
'sidebar' : sidebar.manage_sidebar
}
'check' : check.manage_check,
'wip' : wip.manage_wip,
'domain' : domain.manage_domain,
'sidebar' : navbars.manage_navbars,
'navbar' : navbars.manage_navbars
}
# Dict for Options
options = {

View File

@ -22,6 +22,7 @@ import sys, os, re, datetime
from datetime import datetime
from time import gmtime, strftime
import time
import tyto
# Load domain configuration if exists

View File

@ -18,6 +18,7 @@
#**********************************************************************
import os, locale
import tyto
#==========================#
@ -106,12 +107,16 @@ def create_domain(target, option):
db_dir = '%s/.local/tyto/%s/'%(tyto.home_dir, domain_short)
navbars_conf= '%sarticles/navbars/'%tyto.conf_dir
conf_domain = 'domain_dir = "%s"\n'%tyto.conf_dir + \
'domain_conf = "%s"\n'%tyto.domain_conf + \
'domain_articles = "%sarticles/"\n'%tyto.conf_dir + \
'domain_files = "%sarticles/files/"\n'%tyto.conf_dir + \
'domain_images = "%sarticles/images/"\n'%tyto.conf_dir + \
'domain_db = "%sarticles/"\n'%(db_dir) + \
'navbars_dir = "%s"\n'%navbars_conf + \
'navbar_load = "%styto.navbar"\n'%navbars_conf + \
'sidebar_load = "%styto.sidebar"\n'%navbars_conf + \
'\ndomain_short = "%s"\n'%domain_short + \
'domain_url = "%s"\n'%domain_url + \
'domain_wipurl = "%s"\n'%domain_wipurl
@ -284,53 +289,72 @@ def create_domain(target, option):
'domain_relme = "%s"'%domain_relme)
# Activate menu bar from root articles folders ?
#-----------------------------------------------
# Activate menu bar from tyto.navbar ?
#-------------------------------------
try: domain_menubar = tyto.domain_menubar
except: domain_menubar = 'False'
ask = ''
ask = input(' ├ Use sub-folders as menu bar ? (%s) '%domain_menubar)
ask = input(' ├ Create menu bar from tyto.navbar file ? (%s) '%(
domain_menubar
)
)
if ask in ['y', 'Y']: domain_menubar = "True"
else: domain_menubar = 'False'
if ask in ['y', 'Y']: domain_menubar = 'True'
else: domain_menubar = domain_menubar
tyto.set_file(tyto.domain_conf, False,
'domain_menubar = %s'%(domain_menubar))
# Activate side bar from tyto.sidebar ?
#--------------------------------------
try: domain_sidebar = tyto.domain_sidebar
except: domain_sidebar = 'False'
ask = ''
ask = input(' ├ Create article\'s list from tyto.sidebar file ? (%s) '%(
domain_sidebar
)
)
if ask in ['y', 'Y']: domain_sidebar = 'True'
else: domain_sidebar = domain_sidebar
tyto.set_file(tyto.domain_conf, False,
'domain_sidebar = %s'%(domain_sidebar))
# Sidebar Title
#--------------
try: sidebar_title = tyto.sidebar_title
except: sidebar_title = tyto.trans[0][tyto.n]
ask = ''
ask = input(' ├ Sidebar title ? ("%s") '%sidebar_title)
if ask: sidebar_title = ask
if '"' in sidebar_title:
sidebar_title = sidebar_title.replace('"', '')
sidebar_conf = '%sarticles/sidebar/'%tyto.conf_dir
sidebar_datas = '\nsidebar_dir = "%s"\n'%sidebar_conf + \
'sidebar_load = "%styto.sidebar"\n'%sidebar_conf + \
'sidebar_title = "%s"'%sidebar_title
tyto.set_file(tyto.domain_conf, False, sidebar_datas)
if domain_sidebar:
ask = ''
ask = input(' ├ Sidebar title ? ("%s") '%sidebar_title)
if ask: sidebar_title = ask
if '"' in sidebar_title:
sidebar_title = sidebar_title.replace('"', '')
tyto.set_file(tyto.domain_conf, False,
'sidebar_title = "%s"'%(sidebar_title))
# Sidebar Items
#--------------
try: sidebar_items = tyto.sidebar_items
except: sidebar_items = "6"
ask = ''
ask = input(' ├ [max=16] Sidebar Items ? ("%s") '%sidebar_items)
if ask:
if not ask.isdigit(): tyto.exiting("3", ask, True)
elif int(ask) in range(1,17): sidebar_items = int(ask)
if domain_sidebar:
ask = ''
ask = input(' ├ [max=16] Sidebar Items ? ("%s") '%sidebar_items)
if ask:
if not ask.isdigit(): tyto.exiting("3", ask, True)
elif int(ask) in range(1,17): sidebar_items = int(ask)
tyto.set_file(tyto.domain_conf, False,
'sidebar_items = "%s"'%int(sidebar_items))
'sidebar_items = %s'%(sidebar_items))
# Get srv root
@ -391,7 +415,7 @@ def create_domain(target, option):
folders = (
srv_wip_tpl, srv_wip_images, srv_wip_files,
srv_www_tpl, srv_www_images, srv_www_files,
domain_files, domain_images, sidebar_dir,
domain_files, domain_images, navbars_dir,
domain_db,
)
@ -404,7 +428,8 @@ def create_domain(target, option):
# Create tyto.sidebar and metas.html
print('')
create_sidebar(option, sidebar_dir)
create_sidebar(option, navbars_dir)
create_navbar(option, navbars_dir)
create_metas_file(srv_wip_tpl)
print('')
@ -463,16 +488,18 @@ def create_metas_file(srv_wip_tpl):
#==============================#
# sidebar load file translated #
#------------------------------#
def create_sidebar(opt, sidebar_dir):
def create_sidebar(opt, navbars_dir):
try: sidebar_load
except: sidebar_load = "%styto.sidebar"%sidebar_dir
except: sidebar_load = "%styto.sidebar"%navbars_dir
sdb_load_fr = '# Pour : Tyto - Littérateur\n' + \
'# Type : fichier texte\n' + \
'# Description : Fichier appelé par : tyto sidebar\n' + \
'# Description : Fichier appelé par "tyto sidebar"\n' + \
'# (Liste d\'articles)\n' + \
'# Fichier : tyto.sidebar\n' + \
'# Dossier : %s\n'%sidebar_dir + \
'# Dossier : %s\n'%navbars_dir + \
'# Comment : 1 URI de l\'article par ligne\n' + \
'# (depuis articles/)\n' + \
'# Ne commence pas par "/"\n' + \
'# L\'ordre définit la position\n' + \
'\n# %s\n'%(15 * "-") +\
@ -483,10 +510,12 @@ def create_sidebar(opt, sidebar_dir):
sdb_load_en = '# For: Tyto - Littérateur\n' + \
'# Type: Text file\n' + \
'# Description: file called with: tyto sidebar\n' + \
'# Description: file called with "tyto sidebar"\n' + \
'# (articles\'s list)\n' + \
'# File: tyto.sidebar\n' + \
'# Directory: %s\n'%sidebar_dir + \
'# Directory: %s\n'%navbars_dir + \
'# Comment: 1 article URI per line\n' + \
'# (from articles/)\n' + \
'# not begining with "/"\n' + \
'# Order in sidebar position\n' + \
'\n# %s\n'%(15 * "-") +\
@ -506,3 +535,63 @@ def create_sidebar(opt, sidebar_dir):
tyto.set_file(sidebar_load, 'new', sdb_load)
print(' ├ Create file: %s'%sidebar_load)
#=============================#
# navbar load file translated #
#-----------------------------#
def create_navbar(opt, navbars_dir):
try: navbar_load
except: navbar_load = "%styto.navbar"%navbars_dir
nav_load_fr = '# Pour : Tyto - Littérateur\n' + \
'# Type : fichier texte\n' + \
'# Description : Fichier utilisé par "tyto wip"\n' + \
'# (Liste des catégories)\n' + \
'# Fichier : tyto.navbar\n' + \
'# Dossier : %s\n'%navbars_dir + \
'# Comment : 1 nom de dossier par ligne\n' + \
'# (depuis articles/)\n' + \
'# Ne commence pas par "/"\n' + \
'# L\'ordre définit la position\n' + \
'# Note: Pour éviter l\'erreur 404 :\n' + \
'# - ajouter un article index.{ext}\n' + \
'# dans le dossier mentionné\n' + \
'# - utiliser check et wip dessus' + \
'\n# %s\n'%(15 * "-") +\
'# Exemples :\n' + \
'# documentation\n' + \
'# a-propos\n' + \
'# %s\n\n'%(15 * "-")
nav_load_en = '# For: Tyto - Littérateur\n' + \
'# Type: Text file\n' + \
'# Description: file used with "tyto wip"\n' + \
'# (categories\'s list)\n' + \
'# File: tyto.navbar\n' + \
'# Directory: %s\n'%navbars_dir + \
'# Comment: 1 folder name per line\n' + \
'# (from articles/)\n' + \
'# not begining with "/"\n' + \
'# Order in sidebar position\n' + \
'# Remember: To avoid 404 error, you must:\n' + \
'# - add index.{ext} file article\n' + \
'# in set folder\n' + \
'# - check and wip it' + \
'\n# %s\n'%(15 * "-") +\
'# Examples :\n' + \
'# documentation\n' + \
'# about\n' + \
'# %s\n\n'%(15 * "-")
if tyto.n == 0: nav_load = nav_load_fr
elif tyto.n == 1: nav_load = nav_load_en
if not opt == 'Remove' and os.path.exists(navbar_load):
ask = ''
ask = input(' ├ Initialize new navbar ? ')
if not ask in ['y', 'Y']:
return
tyto.set_file(navbar_load, 'new', nav_load)
print(' ├ Create file: %s'%navbar_load)

View File

@ -17,6 +17,7 @@
#**********************************************************************
import os
import tyto
# Load domain configuration DB
@ -143,46 +144,39 @@ def create_main_page(target, article_bottom):
#----------------------------
# True in config
if domain_menubar:
# Filter these directories
nodirs = ('files', 'images', 'sidebar')
# 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
# Open HTML tags
menu_html = '%s<nav id="site_menu">\n'%(6 * ' ') + \
'%s<ul id="site_menu_items">'%(8 * ' ')
articles_dirs = os.listdir(tyto.domain_articles)
# Create folder links (d) in navbar
for d in articles_dirs:
d_uri = os.path.join(domain_articles, d)
# Check if d is a directory
if not os.path.isdir(d_uri): continue
# Filter unwanted
if d.startswith(nodirs): continue
# Must have index.* in d directory
items_indir = os.listdir(d_uri)
print(">>>", items_indir)
index = True
for item in items_indir:
if item.startswith('index.'):
break
else: index = False
if not index: 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>'%(
d, d
) + \
'</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 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 * ' ')
#-----------------------#
# Create main HTML Page #

View File

@ -17,6 +17,7 @@
#**********************************************************************
import sys
import tyto
# Load domain configuration if exists
@ -25,16 +26,25 @@ if tyto.domain_exists: exec(open(tyto.domain_conf).read())
#==============================#
# Manage arguments for sidebar #
#------------------------------#
def manage_sidebar(target, option):
# Initialize new sidebar
def manage_navbars(target, option):
import domain
actions = {
'sidebar' : domain.create_sidebar,
'navbar' : domain.create_navbar
}
if sys.argv[1] == "sidebar": file = sidebar_load
elif sys.argv[1] == 'navbar': file = navbar_load
# Initialize new file
if not target:
if option == "Remove":
import domain
domain.create_sidebar(opt, sidebar_dir)
if option == 'Edit':
print(":D Edit sidebar configuration file:", sidebar_load)
tyto.edit_file(sidebar_load)
actions[sys.argv[1]](opt, navbars_dir)
elif option == 'Edit':
print(":D Edit %s configuration file:"%sys.argv[1], file)
tyto.edit_file(file)
else:
db_exists = tyto.get_db_post(target) # Article exists + has DB ?
if not db_exists: tyto.exiting("4", '') # Needs database

View File

@ -17,6 +17,7 @@
#**********************************************************************
import os, re
import tyto, html
def manage_wip(target, option):