html: in-dev of sidebar fonction

This commit is contained in:
Cyrille L 2022-11-28 17:48:18 +01:00
parent 2ce6e0d4a5
commit 51389e2e3c
6 changed files with 137 additions and 24 deletions

View File

@ -34,7 +34,7 @@ Directories that must be writeable by Tyto
# Import needed libs and tyto's libs # Import needed libs and tyto's libs
import sys import sys
sys.path.insert(0, '/var/lib/tyto/program') sys.path.insert(0, '/var/lib/tyto/program')
import check, wip, domain, log import check, wip, domain, log, sidebar
#=======# #=======#
# Tools # # Tools #
@ -53,7 +53,8 @@ def tyto_args(ta):
Actions = { 'check' : check.manage_check, Actions = { 'check' : check.manage_check,
'wip' : wip.manage_wip, 'wip' : wip.manage_wip,
'domain' : domain.manage_domain, 'domain' : domain.manage_domain,
'log' : log.manage_log 'log' : log.manage_log,
'sidebar' : sidebar.manage_sidebar
} }
# Dict for Options # Dict for Options
@ -86,7 +87,7 @@ def tyto_args(ta):
try: try:
Actions[sys.argv[1]](Target, Opt) Actions[sys.argv[1]](Target, Opt)
except KeyError: except KeyError:
print(':( Invalid action "%s"'%sys.argv[1]) print(':< Invalid action "%s"'%sys.argv[1])

View File

@ -94,8 +94,8 @@ def append_f(f, line_val):
#--------------------------------------# #--------------------------------------#
def exiting(process, out, msg): def exiting(process, out, msg):
msgs = [ msgs = [
'\n:D All done !', ':D All done !',
'\n:/ Maybe next time...', ':/ Maybe next time...',
'%s'%msg '%s'%msg
] ]
@ -120,7 +120,7 @@ def manage_domain(Domain, Opt):
add_domain(set_domain) add_domain(set_domain)
elif Opt == 'Edit' or '-E': elif Opt == 'Edit':
if domain_conf: if domain_conf:
print(':> Editing',domain_conf) print(':> Editing',domain_conf)
old_conf_ID = check.get_filesum(domain_conf, True) old_conf_ID = check.get_filesum(domain_conf, True)
@ -191,10 +191,8 @@ def add_domain(set_domain):
# Ask to confirm to write activation domain # Ask to confirm to write activation domain
print(' ├─────────────────────────────') print(' ├─────────────────────────────')
confirm = input(' └ Activate domain configuration ? ') confirm = input(' └ Activate domain configuration ? ')
if confirm in ['y', 'Y']: if confirm in ['y', 'Y']: create_domain()
create_domain() else : exiting('form', 1, '')
else:
exiting('form', 1, '')
#==============# #==============#
# Get protocol # # Get protocol #
@ -347,10 +345,8 @@ def domain_form():
# Separator Pages Titles (default '-') # Separator Pages Titles (default '-')
# ------------------------------------ # ------------------------------------
global sep_titles global sep_titles
try: try: sep_titles
sep_titles except: sep_titles = '-'
except:
sep_titles = '-'
set_sep = input(' ├ Website pages separator (%s) ? '%sep_titles) set_sep = input(' ├ Website pages separator (%s) ? '%sep_titles)
if set_sep: if set_sep:

View File

@ -16,6 +16,7 @@
#---------------------------------------------------------------------- #----------------------------------------------------------------------
#********************************************************************** #**********************************************************************
import re, os import re, os
import check, log, domain import check, log, domain

View File

@ -0,0 +1,115 @@
#!/usr/bin/env python3
# Name: Tyto - Littérateur
# Type: Global functions for HTML page
# Description: Create final HTML Page
# file: sidebar.py
# Folder: /var/lib/tyto/programs/
# By echolib (XMPP: im@echolib.re)
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#------------
# funny stats
#------------
# lines:
# functions:
# comments:
#----------------------------------------------------------------------
#**********************************************************************
import re, os, sys, subprocess
import log, domain, check
#========================#
# Manage sidebar command #
#------------------------#
def manage_sidebar(article, Opt):
create = False
# Create tyto.sidebar
if not os.path.exists(domain.domain_sdb_load):
create_sdb_load()
create = True
# Wan to edit (in nano)
# Then, ask to make HTML sidebar (if file changed)
if Opt == "Edit":
old_sdb_ID = check.get_filesum(domain.domain_sdb_load, True)
edit_sdb = subprocess.run(['/usr/bin/nano',
'--linenumbers',
domain.domain_sdb_load])
new_sdb_ID = check.get_filesum(domain.domain_sdb_load, True)
if old_sdb_ID == new_sdb_ID:
sys.exit(0)
else:
ask_make_sdb = input('! Create new HTML sidebar ? ')
if not ask_make_sdb in ['y', 'Y']: domain.exiting('root', 1, '')
loop_sidebar(create)
#========================#
# Read tyto.sidebar file #
#------------------------#
def loop_sidebar(create):
Post_Err = False
sdb_posts = []
max_items = int(domain.domain_sdb_items) + 1
counter = 0 # TO not count more than set in config
sdb_loaded = (open(domain.domain_sdb_load)).read()
for line in sdb_loaded.rsplit('\n'):
if create : print(line)
elif line.startswith('#'): continue
elif len(line) == 0 : continue
else:
counter += 1
if counter == max_items: break
post_uri = '%s%s'%(domain.domain_articles, line)
if not os.path.exists(post_uri):
print(':< Unused file: %s'%post_uri)
Post_Err = True
else:
hash_post = check.get_filesum(post_uri, False)
sdb_posts = sdb_posts + [hash_post]
print(sdb_posts)
#=====================#
# Create sidebar file #
#---------------------#
def create_sdb_load():
sdb_file_fr = '# Nom: Tyto - Littérateur\n' + \
'# Type: fichier texte\n' + \
'# Description: Fichier appelé par : tyto sidebar\n' + \
'# fichier: tyto.sidebar\n' + \
'# Dossier: articles/sidebar/\n' + \
'# (depuis le dossier du domaine)\n' + \
'# Comment: 1 URI de l\'article par ligne\n' + \
'# L\'ordre définit la position\n' + \
'# exemples :\n' + \
'# index.tyto\n' + \
'# dir1/index.tyto\n'
sdb_file_en = '# Name: Tyto - Littérateur\n' + \
'# Type: Text file\n' + \
'# Description: file called with: tyto sidebar\n' + \
'# file: tyto.sidebar\n' + \
'# Folder: articles/sidebar/\n' + \
'# (from the domain folder)\n' + \
'# Comment: 1 article URI per line\n' + \
'# Order in sidebar position\n' + \
'# examples :\n' + \
'# index.tyto\n' + \
'# dir1/index.tyto\n'
if re.match('fr', domain.domain_lang, re.IGNORECASE):
sdb_file = sdb_file_fr
else:
sdb_file = sdb_file_en
file = open(domain.domain_sdb_load, "w")
file.write(sdb_file)
file.close()
msg_log = 'Sidebar > Create new sidebar file in %s'%(
domain.domain_sdb_load
)
log.append_f(domain.tyto_logs, msg_log, 0)