From ceec24105c0b105d01a6a4b19123e00beb397c31 Mon Sep 17 00:00:00 2001 From: Cyrille L Date: Mon, 21 Nov 2022 15:58:47 +0100 Subject: [PATCH] check: image in post + titles ; wip: fix empty lines in bCodes converter --- src/var/lib/tyto/program/check.py | 97 ++++++++++++++++++------------- src/var/lib/tyto/program/wip.py | 6 +- 2 files changed, 59 insertions(+), 44 deletions(-) diff --git a/src/var/lib/tyto/program/check.py b/src/var/lib/tyto/program/check.py index 4c126c9..cd49666 100644 --- a/src/var/lib/tyto/program/check.py +++ b/src/var/lib/tyto/program/check.py @@ -153,11 +153,10 @@ def create_domain_dirs(path_type): def get_filesum(path,src): from hashlib import blake2b file_sum = blake2b(digest_size=4) - if src: - file_sum.update(open(path, 'rb').read()) - else: - file_sum.update(path.encode()) - return file_sum.hexdigest() + + if src: file_sum.update(open(path, 'rb').read()) + else : file_sum.update(path.encode()) + return file_sum.hexdigest() #=========================# @@ -169,13 +168,13 @@ def manage_check(file_post, Force): post_IDs(file_post) # Start checking Post, then exit if errors found in - check_article(post_uri, Force) + process_article(post_uri, Force) #==========================# # Pre-processing post file # #--------------------------# -def check_article(post_uri, Force): +def process_article(post_uri, Force): global article # Check needed directories. Create if not exist create_domain_dirs('db') @@ -201,10 +200,11 @@ def check_article(post_uri, Force): # backup hash_wip/www values hash_wip = post_wip[0];time_wip = post_wip[1] hash_www = post_www[0];time_www = post_www[1] + # Compare chk Hashes. # Pass if Force, and not same if hash_chk == post_chk[0] and not Force: - print(':) Check was already done, on',post_chk[1]) + print(':) Check was already done, on', post_chk[1]) sys.exit(0) # Processing @@ -218,6 +218,8 @@ def check_article(post_uri, Force): # Check markers in headers check_post_header(headers.rsplit('\n')) + if Post_Err: sys.exit(1) + # Protect bCodes, keep markers for stats, before checking other markers wip.convert_bcodes(article.rsplit('\n'), @@ -249,9 +251,7 @@ def check_article(post_uri, Force): # Error in article #--------------------- - if Post_Err: - print(':( Invalid article. Needs corrections') - sys.exit(1) + if Post_Err: sys.exit(1) # No Domain registred yet #------------------------- @@ -297,8 +297,8 @@ def post_to_strings(post_uri): # Split post in 2 strings for Headers and Article global headers, article, post_lines, headers_ln - post_lines = file_string.rsplit('\n') headers = article = '' + post_lines = file_string.rsplit('\n') post = False for line in post_lines: @@ -320,20 +320,23 @@ def post_to_strings(post_uri): # Loop into headers to check markers # #------------------------------------# def check_post_header(headers): - global Post_Err - global stats_links_uniq, stats_files_uniq, stats_images_uniq, stats_abbrs_uniq - global stats_links_p, stats_files_p, stats_images_p, stats_abbrs_p - global stats_links # Mandatory markers global title, about, author, tags, date - # Set Stats - stats_links_uniq = stats_files_uniq = stats_images_uniq = stats_abbrs_uniq = 0 - stats_links = stats_links_p = stats_files_p = stats_images_p = stats_abbrs_p = 0 - # Set Mandatory marker. 0:True/False 1:ln 2: Content title = about = author = tags = date = ('','','') + # Set Stats + global stats_links_uniq, stats_links, stats_links_p + global stats_files_uniq, stats_files_p + global stats_images_uniq, stats_images_p + global stats_abbrs_uniq, stats_abbrs_p + + stats_links_uniq = stats_links = stats_links_p = 0 + stats_files_uniq = stats_files_p = 0 + stats_images_uniq = stats_images_p = 0 + stats_abbrs_uniq = stats_abbrs_p = 0 + # Set Optional markers. 0:ln 1:Name 2:URL 3:Alt link = file = image = ('','','','') @@ -799,8 +802,8 @@ def check_article_markers(article): #------------------------ for ln, line in enumerate(article.rsplit('\n'), 1): - # Do not check if in precode: [[ and ]] - if re.match(markers_reg[2][0], line): precode = True + # Do not check line if in precode: [[ and ]] + if re.match(markers_reg[2][0], line): precode = True elif re.match(markers_reg[2][1], line): precode = False if precode: continue @@ -833,15 +836,15 @@ def check_article_markers(article): elif re.match(markers_reg[1][1], line): stats_qe += 1 # Check if referenced in header for markers - for marker_p in '_image:', '_code:', '_brut:': - if re.match(r'^%s'%marker_p, line): - m_name = line.split(':')[1][0] - marker_h = marker_p[1:len(marker_p)] - if not re.findall(r'%s\s+%s'%(marker_h, m_name), headers): + for marker in '_image:', '_code:', '_brut:': + if re.match(r'^%s'%marker, line): + m_name = line.split(':')[1].split(' ')[0] + marker_h = marker[1:len(marker)] + if not re.findall(r'\b%s\s+%s\b'%(marker_h, m_name), headers): msg_log = 'Line %s. Unused marker "%s %s" in header'%( ln+headers_ln, marker_h, m_name ) - log.append_f(post_logs,msg_log,1) + log.append_f(post_logs, msg_log, 1) Post_Err = True #------------------------------ @@ -962,7 +965,7 @@ def check_article_titles(article): #------------------------ for ln, line in enumerate(article, 1): - # Do not check if in precode: [[ and ]] + # Do not check line if in precode: [[ and ]] if re.match(markers_reg[2][0], line): precode = True stats_bcodes += 1 @@ -977,22 +980,33 @@ def check_article_titles(article): title = line.split(' ', 1) ht = line[1] + # Check title marker #[1-6] if ht == ' ' or ht == '#': stats_comments += 1 continue - if ht.isnumeric() and not int(ht) in range(1,7): - msg_log = 'Line %s. Mismatch title number "%s" (1-6)'%( - ln + headers_ln, ht) - log.append_f(post_logs,msg_log,1) - Post_Err = True - continue + # Title number not in range + elif ht.isdigit(): + if not int(ht) in range(1,7): + msg_log = 'Line %s. Mismatch title number "%s" (1-6)'%( + ln + headers_ln, ht) + log.append_f(post_logs,msg_log,1) + Post_Err = True + continue + + # Not a digit in marker title + else: + msg_log = 'Line %s. Mismatch title number "%s" (1-6)'%( + ln + headers_ln, ht) + log.append_f(post_logs,msg_log,1) + Post_Err = True + continue + + # Check description's title try: title[1] - if title[1] == ' ' or title[1] == '': - Title_Err = True - else: - stats_titles += 1 + if not title[1] or title[1] == ' ': Title_Err = True + else : stats_titles += 1 except: Title_Err = True @@ -1010,8 +1024,7 @@ def check_article_titles(article): #---------------------------# def check_links_anchors(article): global Post_Err - - #for ln, line in enumerate(article.rsplit('\n'), 1): + anchors_link = re.findall(r'\>_(.*?)_\<', article) for anchor in anchors_link: anchor_id = anchor.rsplit(':',1)[0] @@ -1042,7 +1055,7 @@ def check_lists_contents(article): if inlist and not re.match(r'^\+|^\=|\s', line): msg_log = 'Content list not "+" or "=": %s'%line - log.append_f(post_logs,msg_log,1) + log.append_f(post_logs, msg_log, 1) Post_Err = True #====================================# diff --git a/src/var/lib/tyto/program/wip.py b/src/var/lib/tyto/program/wip.py index 729864f..c7dd5d7 100644 --- a/src/var/lib/tyto/program/wip.py +++ b/src/var/lib/tyto/program/wip.py @@ -573,7 +573,8 @@ def convert_bcodes(article, fm, lm, css): for line in article: if line.startswith(fm): bCode = True - article_temp = '%s\n%s'%(article_temp, line) + if article_temp: article_temp = '%s\n%s'%(article_temp, line) + else : article_temp = line continue if line.startswith(lm): @@ -594,7 +595,8 @@ def convert_bcodes(article, fm, lm, css): if bCode_lines: bCode_lines = '%s\n%s'%(bCode_lines, line) else : bCode_lines = line else: - article_temp = '%s\n%s'%(article_temp, line) + if article_temp: article_temp = '%s\n%s'%(article_temp, line) + else : article_temp = line #====================================# # Protect iCodes contents to base64 #