diff --git a/pre-commit.py b/pre-commit.py index 0176323..1400846 100755 --- a/pre-commit.py +++ b/pre-commit.py @@ -85,8 +85,8 @@ def auto_dir(path): if not os.path.exists(directory): os.makedirs(directory) except IOError: - sys.exit("[ERROR] An error occured while creating "+path+" file \ - and parent dirs.") + sys.exit("[ERROR] An error occured while creating "+path+" file " + "and parent dirs.") # Replace some user specific syntax tags (to repplace smileys for example) @@ -100,17 +100,17 @@ def replace_tags(article, search_list, replace_list): try: opts, args = getopt.gnu_getopt(sys.argv, "hf", ["help", "force-regen"]) except getopt.GetoptError: - sys.exit("Error while parsing command line arguments. \ - See pre-commit -h for more infos on how to use.") + sys.exit("Error while parsing command line arguments. " + "See pre-commit -h for more infos on how to use.") force_regen = False for opt, arg in opts: if opt in ("-h", "--help"): print("Usage :") - print("This should be called automatically as a pre-commit git hook. \ - You can also launch it manually right before commiting.\n") - print("This script generates static pages ready to be served behind \ - your webserver.\n") + print("This should be called automatically as a pre-commit git hook. " + "You can also launch it manually right before commiting.\n") + print("This script generates static pages ready to be served behind " + "your webserver.\n") print("Usage :") print("-h \t --help \t displays this help message.") print("-f \t --force-regen \t force complete rebuild of all pages.") @@ -136,9 +136,9 @@ try: else: params[option.strip()] = value.strip() except IOError: - sys.exit("[ERROR] Unable to load raw/params file which defines important \ - parameters. Does such a file exist ? \ - See doc for more info on this file.") + sys.exit("[ERROR] Unable to load raw/params file which defines important " + "parameters. Does such a file exist ? See doc for more info " + "on this file.") # Fill lists for modified, deleted and added files @@ -151,13 +151,13 @@ if not force_regen: try: changes = subprocess.check_output(["git", "diff", "--cached", "--name-status"], universal_newlines=True) except: - sys.exit("[ERROR] An error occured when fetching file changes \ - from git.") + sys.exit("[ERROR] An error occured when fetching file changes " + "from git.") changes = changes.strip().split("\n") if changes == [""]: - sys.exit("[ERROR] Nothing to do... Did you had new files with \ - \"git add\" before ?") + sys.exit("[ERROR] Nothing to do... Did you had new files with " + "\"git add\" before ?") for changed_file in changes: if changed_file[0].startswith("A"): @@ -174,15 +174,14 @@ else: added_files = list_directory("raw") if not added_files and not modified_files and not deleted_files: - sys.exit("[ERROR] Nothing to do... Did you had new files with \ - \"git add\" before ?") + sys.exit("[ERROR] Nothing to do... Did you had new files with " + "\"git add\" before ?") # Only keep modified raw articles files for filename in list(added_files): direct_copy = False if not filename.startswith("raw/"): - # TODO : Delete files starting by gen / blog ? + same thing for modified / deleted added_files.remove(filename) continue @@ -193,8 +192,9 @@ for filename in list(added_files): if ((not filename.endswith(".html") and not filename.endswith(".ignore")) or direct_copy): - print("[INFO] (Not HTML file) Copying directly not html file \ - "+filename[4:]+" to blog dir.") + # Note : this deal with CSS, images or footer file + print("[INFO] (Not HTML file) Copying directly not html file " + + filename[4:]+" to blog dir.") auto_dir("blog/"+filename[4:]) shutil.copy(filename, "blog/"+filename[4:]) @@ -202,8 +202,8 @@ for filename in list(added_files): continue if filename.endswith(".ignore"): - print("[INFO] (Not published) Found not published article \ - "+filename[4:-7]+".") + print("[INFO] (Not published) Found not published article " + + filename[4:-7]+".") added_files.remove(filename) continue @@ -221,16 +221,16 @@ for filename in list(modified_files): if ((not filename.endswith("html") and not filename.endswith("ignore")) or direct_copy): - print("[INFO] (Not HTML file) Updating directly not html file \ - "+filename[4:]+" to blog dir.") + print("[INFO] (Not HTML file) Updating directly not html file " + + filename[4:]+" to blog dir.") auto_dir("blog/"+filename[4:]) shutil.copy(filename, "blog/"+filename[4:]) modified_files.remove(filename) continue if filename.endswith("ignore"): - print("[INFO] (Not published) Found not published article \ - "+filename[4:-7]+".") + print("[INFO] (Not published) Found not published article " + + filename[4:-7]+".") added_files.remove(filename) continue @@ -248,16 +248,16 @@ for filename in list(deleted_files): if ((not filename.endswith("html") and not filename.endswith("ignore")) or direct_copy): - print("[INFO] (Not HTML file) Copying directly not html file \ - "+filename[4:]+" to blog dir.") + print("[INFO] (Not HTML file) Copying directly not html file " + + filename[4:]+" to blog dir.") auto_dir("blog/"+filename[4:]) shutil.copy(filename, "blog/"+filename[4:]) deleted_files.remove(filename) continue if filename.endswith("ignore"): - print("[INFO] (Not published) Found not published article \ - "+filename[4:-7]+".") + print("[INFO] (Not published) Found not published article " + + filename[4:-7]+".") added_files.remove(filename) continue @@ -275,8 +275,8 @@ for filename in added_files: sys.exit("[ERROR] Unable to open file "+filename+".") if not tags: - sys.exit("[ERROR] (TAGS) In added article "+filename[4:]+" : \ - No tags found !") + sys.exit("[ERROR] (TAGS) In added article "+filename[4:]+" : " + "No tags found !") for tag in tags: try: auto_dir("gen/tags/"+tag+".tmp") @@ -284,11 +284,11 @@ for filename in added_files: tag_file.seek(0) if filename[4:] not in tag_file.read(): tag_file.write(filename[4:]+"\n") - print("[INFO] (TAGS) Found tag "+tag+" in article \ - "+filename[4:]) + print("[INFO] (TAGS) Found tag "+tag+" in article " + + filename[4:]) except IOError: - sys.exit("[ERROR] (TAGS) New tag found but an error \ - occured in article "+filename[4:]+": "+tag+".") + sys.exit("[ERROR] (TAGS) New tag found but an error " + "occured in article "+filename[4:]+": "+tag+".") for filename in modified_files: try: @@ -298,8 +298,8 @@ for filename in modified_files: sys.exit("[ERROR] Unable to open file "+filename+".") if not tags: - sys.exit("[ERROR] (TAGS) In modified article "+filename[4:]+" : \ - No tags found !") + sys.exit("[ERROR] (TAGS) In modified article "+filename[4:]+" : " + " No tags found !") for tag in list_directory("gen/tags/"): try: @@ -318,19 +318,19 @@ for filename in modified_files: print("[INFO] (TAGS) Deleted tag "+tag[:tag.index(".tmp")]+" in modified article "+filename[4:]) tags.remove(tag_file[9:]) except IOError: - sys.exit("[ERROR] (TAGS) An error occured when parsing tags \ - of article "+filename[4:]+".") + sys.exit("[ERROR] (TAGS) An error occured when parsing tags " + " of article "+filename[4:]+".") for tag in tags: # New tags created try: auto_dir("gen/tags/"+tag+".tmp") with open("gen/tags/"+tag+".tmp", "a+") as tag_file: # Delete tag file here if empty after deletion tag_file.write(filename[4:]+"\n") - print("[INFO] (TAGS) Found new tag "+tag+" for \ - modified article "+filename[4:]) + print("[INFO] (TAGS) Found new tag "+tag+" for " + "modified article "+filename[4:]) except IOError: - sys.exit("[ERROR] (TAGS) An error occured when parsing tags \ - of article "+filename[4:]+".") + sys.exit("[ERROR] (TAGS) An error occured when parsing tags " + "of article "+filename[4:]+".") # Delete tags for deleted files and delete all generated files for filename in deleted_files: @@ -341,8 +341,8 @@ for filename in deleted_files: sys.exit("[ERROR] Unable to open file "+filename+".") if not tags: - sys.exit("[ERROR] (TAGS) In deleted article "+filename[4:]+" : \ - No tags found !") + sys.exit("[ERROR] (TAGS) In deleted article "+filename[4:]+" : " + "No tags found !") for tag in tags: try: @@ -350,38 +350,37 @@ for filename in deleted_files: tag_file_old_content = tag_file.read() tag_file.truncate() # Delete file in tag - tag_file.write(tag_file_old_content.replace(filename[4:]+"\n", "")) + tag_file_write = tag_file_old_content.replace(filename[4:]+"\n", "") + if tag_file_write: + tag_file.write(tag_file_write) except IOError: - sys.exit("[ERROR] An error occured while deleting article \ - "+filename[4:]+" from tags files.") + sys.exit("[ERROR] An error occured while deleting article" + + filename[4:]+" from tags files.") + + if not tag_file_write: + try: + os.unlink(tag) + print("[INFO] (TAGS) No more article with tag "+tag[8:-4]+", deleting it.") + except FileNotFoundError: + print("[INFO] "+tag+" was found to be empty but there was an error during deletion. You should check manually.") # Delete generated files try: os.unlink("gen/"+filename[4:-5]+".gen") os.unlink("blog/"+filename[4:]) except FileNotFoundError: - print("[INFO] Article "+filename[4:]+" seems to not have already been \ - generated. You should check manually.") + print("[INFO] Article "+filename[4:]+" seems to not have already been " + "generated. You should check manually.") - print("[INFO] Deleted article "+filename[4:]+" in both gen and blog \ - directories") + print("[INFO] Deleted article "+filename[4:]+" in both gen and blog " + "directories") -# TODO : Delete following code -# Delete empty tags files -#for tag in list_directory("gen/tags"): -# try: -# with open(tag, 'r') as tag_file: -# content = tag_file.read().strip() -# if content == '': -# try: -# os.unlink(tag) -# os.unlink(tag.replace("gen", "blog")) -# except FileNotFoundError: -# print("[INFO] "+tag+" was found to be empty but there was an error during deletion. You should check manually.") -# print("[INFO] (TAGS) No more article with tag "+tag[8:-4]+", deleting it.") -# except IOError: -# sys.exit("[ERROR] Unable to open "+tag+".") +# Common lists that are used multiple times +last_articles = latest_articles("raw/", int(params["NB_ARTICLES_INDEX"])) +tags_full_list = list_directory("gen/tags") + +# Generate html for each article for filename in added_files+modified_files: try: with open(filename, 'r') as fh: @@ -409,7 +408,7 @@ for filename in added_files+modified_files: date_readable = "Le "+date[0:2]+"/"+date[2:4]+"/"+date[4:8]+" à "+date[9:11]+":"+date[11:13] # Write generated HTML for this article in gen / - #TODO : Replace tags + article = replace_tags(article, search_list, replace_list) try: auto_dir("gen/"+filename[4:-5]+".gen") with open("gen/"+filename[4:-5]+".gen", 'w') as article_file: @@ -418,117 +417,97 @@ for filename in added_files+modified_files: except IOError: sys.exit("[ERROR] An error occured when writing generated HTML for article "+filename[4:]+".") -#===================================== -#Generate headers file (except title) +# Starting to generate header file (except title) +tags_header = "