From 876ea29c09c2240ca9009b535d7ac3f583814289 Mon Sep 17 00:00:00 2001 From: Phyks Date: Sun, 21 Jul 2013 22:50:12 +0200 Subject: [PATCH] Initial commit, almost working but clearly needs improvements ! --- README.md | 18 ++ TODO | 7 + blog/2013/06/test.html | 46 +++++ blog/index.html | 46 +++++ blog/tags/test.tmp | 46 +++++ gen.py~ | 136 +++++++++++++++ gen/2013/06/test.gen | 12 ++ gen/footer.gen | 4 + gen/header.gen | 30 ++++ gen/tags/test.tmp | 1 + pre-commit.py | 373 +++++++++++++++++++++++++++++++++++++++++ pre-commit.py~ | 248 +++++++++++++++++++++++++++ raw/2013/06/test.html | 11 ++ raw/footer.html | 4 + raw/header.html | 30 ++++ raw/params | 2 + 16 files changed, 1014 insertions(+) create mode 100644 TODO create mode 100644 blog/2013/06/test.html create mode 100644 blog/index.html create mode 100644 blog/tags/test.tmp create mode 100755 gen.py~ create mode 100644 gen/2013/06/test.gen create mode 100644 gen/footer.gen create mode 100644 gen/header.gen create mode 100644 gen/tags/test.tmp create mode 100755 pre-commit.py create mode 100755 pre-commit.py~ create mode 100644 raw/2013/06/test.html create mode 100644 raw/footer.html create mode 100644 raw/header.html create mode 100644 raw/params diff --git a/README.md b/README.md index 4fd488b..f66a4c0 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,21 @@ blogit ====== A git based blogging software + +This is a WIP and it's not usable as is. Will be available soon ! :) + +Some infos that need to be explain better +========================================= + +raw contains raw files from which the conversion will be made +website contains the working copy of the blog to serve via Apache for example + + +example of syntax for an article : + +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+

Le 06/06/2013 à 00:45

+ + + + diff --git a/blog/index.html b/blog/index.html new file mode 100644 index 0000000..528a6ed --- /dev/null +++ b/blog/index.html @@ -0,0 +1,46 @@ + + + + + @titre + + + +
+
+

Phyks' Blog

+

+

Catégories

+
+ +
+
+

Derniers articles

+ +
+

Liens

+ +
+
+

Un exemple d'article

+

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+

Le 06/06/2013 à 00:45

+
+
+ + diff --git a/blog/tags/test.tmp b/blog/tags/test.tmp new file mode 100644 index 0000000..528a6ed --- /dev/null +++ b/blog/tags/test.tmp @@ -0,0 +1,46 @@ + + + + + @titre + + + +
+
+

Phyks' Blog

+

+

Catégories

+
+ +
+
+

Derniers articles

+ +
+

Liens

+ +
+
+

Un exemple d'article

+

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+

Le 06/06/2013 à 00:45

+
+
+ + diff --git a/gen.py~ b/gen.py~ new file mode 100755 index 0000000..ddb5757 --- /dev/null +++ b/gen.py~ @@ -0,0 +1,136 @@ +#!/usr/bin/python + +import sys +import shutil +import os +import datetime + +def listdirectory(path): + fichier=[] + for root, dirs, files in os.walk(path): + for i in files: + fichier.append(os.path.join(root, i)) + return fichier + +if len(sys.argv) == 1: +#If no argument specified, nothing to do + sys.exit("Script expects at least one parameter") + +for filename in sys.argv[1:]: +#Create new empty files in tags dir if new categories appeared + with open(filename, 'r') as fh: + line = fh.readline() + while "@tags=" not in line: + line = fh.readline() + line = line.strip() #Delete \n at the end of the line + tag_pos = line.find("@tags=") + tags = line[tag_pos+6:].split(",") + for tag in tags: + try: + with open("gen/tags/"+tag+".tmp", 'a+') as tag_file: + tag_file.seek(0) + if filename[4:] not in tag_file.read(): + tag_file.write(filename[4:]+"\n") #Assuming filename is relative to the repertory with .git + print("[TAGS] Found tag "+tag+" for article "+filename[4:]) + pass + except IOError: + print("[TAG ERROR] New tag found but error occured : "+tag) + + + #Generate HTML for the updated articles + fh.seek(0) + line = fh.readline() + while "@title=" not in line: + line = fh.readline() + line = line.strip() + title_pos = line.find("@title=") + title = line[title_pos+7:] + + fh.seek(0) + line = fh.readline() + while "@date=" not in line: + line = fh.readline() + line = line.strip() + date_pos = line.find("@date=") + date = line[date_pos+6:] + + fh.seek(0) + article = fh.read() + + try: + with open("gen/"+filename[4:-5]+".gen", 'w') as article_file: + article_file.write("

"+title+"

"+article+"

"+date+"

\n") + print("[GEN ARTICLES] Article "+filename[4:]+" generated") + except: + print("[GEN ARTICLES ERROR] An error occurred while generating article "+filename[4:]) + +#Generate headers file (except title) +with open("raw/header.html") as header_fh: + #Tags + tags = os.listdir("gen/tags") + header = header_fh.read() + tags_header = "" + header = header.replace("@categories", tags_header, 1) + + #Articles + latest_articles = listdirectory("gen") + latest_articles = [i for i in latest_articles if i[4:8].isdigit()] + latest_articles.sort() + articles_header = "" + header = header.replace("@articles", articles_header, 1) + + with open("gen/header.gen", "w") as header_gen_fh: + header_gen_fh.write(header) + print("[GEN HEADER] Header has been generated successfully") + +#Generate footer file +shutil.copy("raw/footer.html", "gen/footer.gen") +print("[GEN FOOTER] Footer has been generated successfully") + +#Regenerate index file +#* First, get last 25 articles +count = 0 +this_year = datetime.now().year +this_month = datetime.now().month +#* Try this year, first +for article in listdir("gen/"+this_year+"/"+this_month): + if not is_dir("gen/"+this_year+"/"+this_month+"/"+article): + count += 1 + date = article + last_25[date] = article +#* Then, get back progressively +#TODO + +#* Then generate effectively the index file + +#Regenerate tags pages +for tag in listdir("gen/tags"): + if not isdir(tag): + with open(tag) as tag_fh: + with open("gen/header.gen") as header_handler: + tag_content = header_handler.read() + tag_content.replace("@titre</title", "<title>"+tag+"") + while line = tag_fh.readline(): + line = line.replace(".html", ".gen") + with open("gen/"+line) as article_handler: + tag_content += "
"+article_handler.read()+"
\n" + with open("gen/footer.gen") as footer_handler: + tag_content += footer_handler.read() + +#Finish articles pages generation + +#Generate RSS diff --git a/gen/2013/06/test.gen b/gen/2013/06/test.gen new file mode 100644 index 0000000..44b5fbf --- /dev/null +++ b/gen/2013/06/test.gen @@ -0,0 +1,12 @@ +

Un exemple d'article

+

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+

Le 06/06/2013 à 00:45

diff --git a/gen/footer.gen b/gen/footer.gen new file mode 100644 index 0000000..7681d48 --- /dev/null +++ b/gen/footer.gen @@ -0,0 +1,4 @@ + + + + diff --git a/gen/header.gen b/gen/header.gen new file mode 100644 index 0000000..9bfa727 --- /dev/null +++ b/gen/header.gen @@ -0,0 +1,30 @@ + + + + + @titre + + + +
+
+

Phyks' Blog

+

+

Catégories

+
+ +
+
+

Derniers articles

+ +
+

Liens

+ +
+
diff --git a/gen/tags/test.tmp b/gen/tags/test.tmp new file mode 100644 index 0000000..ebffaf3 --- /dev/null +++ b/gen/tags/test.tmp @@ -0,0 +1 @@ +2013/06/test.html diff --git a/pre-commit.py b/pre-commit.py new file mode 100755 index 0000000..c4da937 --- /dev/null +++ b/pre-commit.py @@ -0,0 +1,373 @@ +#!/usr/bin/python + +#TODO : gitignore + +import sys +import shutil +import os +import datetime +import subprocess + +def isset(variable): + return variable in locals() or variable in globals() + +def list_directory(path): + fichier=[] + for root, dirs, files in os.walk(path): + for i in files: + fichier.append(os.path.join(root, i)) + return fichier + +def get_tags(fh): + line = fh.readline() + while "@tags=" not in line: + line = fh.readline() + if "@tags" not in line: + return [] + + line = line.strip() #Delete \n at the end of the line + tag_pos = line.find("@tags=") + tags = line[tag_pos+6:].split(",") + return tags + +def latest_articles(directory, number): + now = datetime.datetime.now() + counter = 0 + latest_articles = [] + + for i in range(int(now.strftime('%Y')), 0, -1): + if counter>=25: + break + + if os.path.isdir(directory+"/"+str(i)): + for j in range(12, 0, -1): + if j<10: + j = "0"+str(j) + + if os.path.isdir(directory+"/"+str(i)+"/"+str(j)): + articles_list = list_directory(directory+str(i)+"/"+str(j)) + articles_list.sort(key=lambda x: os.stat(x).st_mtime) #Sort by date the articles + + latest_articles += articles_list[:number-counter] + if len(latest_articles) < number-counter: + counter+=len(articles_list) + else: + counter=25 + + #Delete directory in file names + return latest_articles + +def auto_dir(path): + directory = os.path.dirname(path) + try: + if not os.path.exists(directory): + os.makedirs(directory) + except IOError: + sys.exit("[ERROR] An error occured while creating "+path+" file and parent dirs.") + +#Find the changes to be committed +try: + #TODO : Check this command + changes = subprocess.check_output(["git", "diff", "--cached", "--name-status"], universal_newlines=True) +except: + sys.exit("[ERROR] An error occured when running git diff.") + +#Fill lists for modified, deleted and added files +modified_files = [] +deleted_files = [] +added_files = [] + +changes = changes.strip().split("\n") +if changes == [""]: + sys.exit("[ERROR] Nothing to do...") + +for changed_file in changes: + if changed_file[0] == "A": + added_files.append(changed_file[changed_file.index("\t")+1:]) + elif changed_file[0] == "M": + modified_files.append(changed_file[changed_file.index("\t")+1:]) + elif changed_file[0] == "D": + deleted_files.append(changed_file[changed_file.index("\t")+1:]) + else: + sys.exit("[ERROR] An error occured when running git diff.") + +#Only keep modified raw articles files +for filename in list(added_files): + if filename[:4] != "raw/": + added_files.remove(filename) + + try: + int(filename[4:8]) + except ValueError: + added_files.remove(filename) + +for filename in list(modified_files): + if filename[:4] != "raw/": + modified_files.remove(filename) + + try: + int(filename[4:6]) + except ValueError: + modified_files.remove(filename) + +for filename in list(deleted_files): + if filename[:4] != "raw/": + deleted_files.remove(filename) + + try: + int(filename[4:6]) + except ValueError: + deleted_files.remove(filename) + + + +print("[INFO] Added files : "+", ".join(added_files)) +print("[INFO] Modified files : "+", ".join(modified_files)) +print("[INFO] Deleted filed : "+", ".join(deleted_files)) + +print("[INFO] Updating tags for added and modified files") +for filename in added_files: + try: + with open(filename, 'r') as fh: + tags = get_tags(fh) + if len(tags) > 0: + for tag in tags: + try: + auto_dir("gen/tags/"+tag+".tmp") + with open("gen/tags/"+tag+".tmp", 'a+') as tag_file: + tag_file.seek(0) + if filename[4:] not in tag_file.read(): + tag_file.write(filename[4:]+"\n") + print("[INFO] (TAGS) Found tag "+tag+" for article "+filename[4:]) + except IOError: + sys.exit("[ERROR] (TAGS) New tag found but error occured in article "+filename[4:]+": "+tag+".") + else: + sys.exit("[ERROR] (TAGS) In added article "+filename[4:]+" : No tags found !") + except IOError: + sys.exit("[ERROR] Unable to open file "+filename+".") + +for filename in modified_files: + try: + with open(filename, 'r') as fh: + tags = get_tags(fh) + if(len(tags)) > 0: + for tag in list_directory("gen/tags/"): + try: + with open(tag, 'r+') as tag_file: + if tag[tag.index("tags/") + 5:tag.index(".tmp")] in tags and filename[4:] not in tag_file.read(): + tag_file.seek(0, 2) #Append to end of file + tag_file.write(filename[4:]+"\n") + print("[INFO] (TAGS) Found new tag "+tag[:tag.index(".tmp")]+" for modified article "+filename[4:]) + tags.remove(tag_file[9:]) + if tag[tag.index("tags/") + 5:tag_index(".tmp")] not in tags and filename[4:] in tag_file.read(): + old_tag_file_content = tag_file.read() + tag_file.truncate() + tag_file.write(old_tag_file_content.replace(filename[4:]+"\n", "")) + 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:]+".") + + for tag in tags: #New tags added + try: + auto_dir("gen/tags/"+tag+".tmp") + with open("gen/tags/"+tag+".tmp", "a+") as tag_file: + tag_file.write(filename[4:]+"\n") + 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:]+".") + else: + sys.exit("[ERROR] (TAGS) In modified article "+filename[4:]+" : No tags found !") + except IOError: + sys.exit("[ERROR] Unable to open file "+filename+".") + +#Delete tags for deleted files and delete all generated files +for filename in deleted_files: + try: + with open(filename, 'r') as fh: + tags = get_tags(fh) + if len(tags) > 0: + for tag in tags: + try: + with open("gen/tags/"+tag+".tmp", 'r+') as tag_file: + old_tag_file_content = tag_file.read() + tag_file.truncate() + #Delete file in tag + tag_file.write(old_tag_file_content.replace(filename[4:]+"\n", "")) + except IOError: + sys.exit("[ERROR] An error occured while deleting article "+filename[4:]+" from tags files.") + else: + sys.exit("[ERROR] (TAGS) In deleted article "+filename[4:]+" : No tags found !") + except IOError: + sys.exit("[ERROR] Unable to open file "+filename+".") + + #Delete generated files + try: + os.unlink("gen/"+filename[4:-5]+".gen") + os.unlink("blog/"+filename[4:]) + except FileNotFoundError: + print("[INFO] Article "+filename[4:]+" was not already generated. You should check manually.") + + print("[INFO] Deleted article "+filename[4:]+" in both gen and blog directories") + +#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+".") + +#(Re)Generate HTML files +for filename in added_files+modified_files: + try: + with open(filename, 'r') as fh: + #Generate HTML for the updated articles + for line in fh.readlines(): + if "@title=" in line: + line = line.strip() + title_pos = line.find("@title=") + title = line[title_pos+7:] + continue + + if "@date=" in line: + line = line.strip() + date_pos = line.find("@date=") + date = line[date_pos+6:] + continue + + if isset("date") and isset("title"): + break + + fh.seek(0) + article = fh.read() + date = "Le "+date[0:2]+"/"+date[2:4]+"/"+date[4:8]+" à "+date[9:11]+":"+date[11:13] + + try: + auto_dir("gen/"+filename[4:-5]+".gen") + with open("gen/"+filename[4:-5]+".gen", 'w') as article_file: + article_file.write("

"+title+"

"+article+"

"+date+"

\n") + print("[GEN ARTICLES] Article "+filename[4:]+" generated") + except IOError: + print("[GEN ARTICLES ERROR] An error occurred while generating article "+filename[4:]) + except IOError: + sys.exit("[ERROR] Unable to open file "+filename+".") + +#Generate headers file (except title) +try: + with open("raw/header.html", "r") as header_fh: + #Tags + tags = list_directory("gen/tags") + header = header_fh.read() + tags_header = "
    " + for tag in tags: + tags_header += "
  • "+tag[9:-4]+"
  • " + tags_header += "
" + header = header.replace("@categories", tags_header, 1) + + #Articles + latest_articles_list = latest_articles("gen/", 5) + articles_header = "
    " + for article in latest_articles_list: + try: + with open(article, 'r') as fh: + line = fh.readline() + while "@title" not in line: + line = fh.readline() + line = line.strip() + title_pos = line.find("@title=") + title = line[title_pos+7:] + + articles_header += "
  • "+title+"
  • " + except IOError: + sys.exit("[ERROR] Unable to open file "+article+".") + articles_header += "
" + header = header.replace("@articles", articles_header, 1) + + try: + auto_dir("gen/header.gen") + with open("gen/header.gen", "w") as header_gen_fh: + header_gen_fh.write(header) + print("[INFO] (HEADER) Header has been generated successfully.") + except FileNotFoundError: + sys.exit("[ERROR] (HEADER) An error occured while writing header file.") + except IOError: + sys.exit("[ERROR] Unable to open gen/header.gen for writing.") +except IOError: + sys.exit("[ERROR] Unable to open raw/header.html file.") + +#Generate footer file +if not os.path.isfile("raw/footer.html"): + sys.exit("[ERROR] (FOOTER) Footer file (raw/footer.html) not found.") +try: + shutil.copy("raw/footer.html", "gen/footer.gen") + print("[INFO] (FOOTER) Footer has been generated successfully") +except IOError: + sys.exit("[ERROR] Unable to copy the footer.html file.") + +#Regenerate index file +last_25_articles = latest_articles("gen/", 25) +try: + auto_dir("blog/index.html") + with open("blog/index.html", "w") as index_fh: + try: + with open("gen/header.gen", "r") as header_gen_fh: + index = header_gen_fh.read() + for article in last_25_articles: + with open(article, "r") as article_fh: + index += article_fh.read() + with open("gen/footer.gen") as footer_gen_fh: + index += footer_gen_fh.read() + index_fh.write(index) + print("[INFO] (INDEX) Index page has been generated successfully.") + except IOError: + sys.exit("[ERROR] Error while creating index.html file") +except IOError: + sys.exit("[ERROR] Unable to open index.html file for writing.") + +#Regenerate tags pages +for tag in list_directory("gen/tags"): + try: + auto_dir(tag.replace("gen/", "blog/")) + with open(tag.replace("gen/", "blog/"), "w") as tag_fh: + with open(tag, "r") as tag_gen_fh: + with open("gen/header.gen", "r") as header_fh: + tag_content = header_fh.read() + tag_content.replace("@titre</title", "<title>"+tag[4:-4]+"") + tag_gen_fh_lines = tag_gen_fh.readlines() + for line in tag_gen_fh_lines: + line = line.replace(".html", ".gen") + with open("gen/"+line.strip(), "r") as article_handler: + tag_content += article_handler.read() + with open("gen/footer.gen", "r") as footer_handler: + tag_content += footer_handler.read() + tag_fh.write(tag_content) + print("[INFO] (TAGS) Tag page for "+tag+" has been generated successfully.") + except IOError: + sys.exit("[ERROR] An error occured while generating tag page \""+tag[9:-4]+"\"") + +#Finish articles pages generation +for filename in added_files+modified_files: + try: + auto_dir("blog/"+filename[4:]) + with open("blog/"+filename[4:], "w") as article_fh: + with open("gen/header.gen", "r") as header_gen_fh: + article = header_gen_fh.read() + with open("gen/"+filename[4:-5]+".gen", "r") as article_gen_fh: + article += article_gen_fh.read() + with open("gen/footer.gen", "r") as footer_gen_fh: + article += footer_gen_fh.read() + article_fh.write(article) + print("[INFO] (ARTICLES) Article page for "+filename[4:]+" has been generated successfully.") + except IOError: + sys.exit("[ERROR] An error occured while generating article "+filename[4:]+" page.") + +#Generate RSS +#TODO diff --git a/pre-commit.py~ b/pre-commit.py~ new file mode 100755 index 0000000..77e9fa2 --- /dev/null +++ b/pre-commit.py~ @@ -0,0 +1,248 @@ +#!/usr/bin/python + +#TODO : gitignore +#unlink => try ... except -> no exception on filenotfound +#filename[4:] not good + +import sys +import shutil +import os +import datetime +import subprocess + +def isset(variable): + return variable in locals() or variable in globals() + +def list_directory(path): + fichier=[] + for root, dirs, files in os.walk(path): + for i in files: + fichier.append(os.path.join(root, i)) + return fichier + +def get_tags(fh): + line = fh.readline() + while "@tags=" not in line: + line = fh.readline() + if "@tags" not in line: + return [] + + line = line.strip() #Delete \n at the end of the line + tag_pos = line.find("@tags=") + tags = line[tag_pos+6:].split(",") + return tags + + +#Find the changes to be committed +try: + #TODO : Check this command + changes = subprocess.check_output(["git", "diff", "--cached", "--name-status"], universal_newlines=True) +except: + sys.stderr.write("[ERROR] An error occured when running git diff") + sys.exit(1) + +#Fill lists for modified, deleted and added files +modified_files = [] +deleted_files = [] +added_files = [] + +changes = changes.strip().split("\n") + +for changed_file in changes: + if changed_file[0] == "A": + added_files.append(changed_file[changed_file.index("\t")+1:]) + elif changed_file[0] == "M": + modified_files.append(changed_file[changed_file.index("\t")+1:]) + elif changed_file[0] == "D": + deleted_files.append(changed_file[changed_file.index("\t")+1:]) + else: + sys.stderr.write("[ERROR] An error occured when running git diff") + sys.exit(1) + +print("[INFO] Added files : "+", ".join(added_files)) +print("[INFO] Modified files : "+", ".join(modified_files)) +print("[INFO] Deleted filed : "+", ".join(deleted_files)) + +print("[INFO] Updating tags for added and modified files") +for filename in added_files: + with open(filename, 'r') as fh: + tags = get_tags(fh) + if len(tags) > 0: + for tag in tags: + try: + with open("gen/tags/"+tag+".tmp", 'a+') as tag_file: + tag_file.seek(0) + if filename[4:] not in tag_file.read(): + tag_file.write(filename[4:]+"\n") + print("[INFO] (TAGS) Found tag "+tag+" for article "+filename[4:]) + pass + except IOError: + sys.stderr.write("[ERROR] (TAGS) New tag found but error occured in article "+filename[4:]+": "+tag) + else: + sys.stderr.write("[ERROR] (TAGS) In added article "+filename[4:]+" : No tags found !") + +for filename in modified_files: + with open(filename, 'r') as fh: + tags = get_tags(fh) + if(len(tags)) > 0: + for tag in list_directory("gen/tags/"): + try: + with open(tag, 'r+') as tag_file: + if tag[tag.index("tags/") + 5:tag.index(".tmp")] in tags and filename[4:] not in tag_file.read(): + tag_file.seek(0, 2) #Append to end of file + tag_file.write(filename[4:]+"\n") + print("[INFO] (TAGS) Found new tag "+tag[:tag.index(".tmp")+" for modified article "+filename[4:]) + if tag[tag.index("tags/") + 5:tag_index(".tmp")] not in tags and filename[4:] in tag_file.read(): + old_tag_file_content = tag_file.read() + tag_file.truncate() + tag_file.write(old_tag_file_content.replace(filename"\n", "")) + print("[INFO] (TAGS) Deleted tag "+tag[:tag.index(".tmp")+" in modified article "+filename[4:]) + except IOError: + sys.stderr.write("[ERROR] (TAGS) An error occured when parsing tags of article "+filename[4:]) + else: + sys.stderr.write("[ERROR] (TAGS) In modified article "+filename[4:]+" : No tags found !") + + +#Delete tags for deleted files and delete all generated files +for filename in deleted_files: + with open(filename, 'r') as fh: + tags = get_tags(fh) + if len(tags) > 0: + for tag in tags: + try: + with open("gen/tags/"+tag+".tmp", 'r+') as tag_file: + old_tag_file_content = tag_file.read() + tag_file.truncate() + #Delete file in tag + tag_file.write(old_tag_file_content).replace(filename+"\n", "")) + except IOError: + sys.stderr.write("[ERROR] An error occured while deleting article "+filename[4:]+" from tags files") + else: + sys.stderr.write("[ERROR] (TAGS) In deleted article "+filename[4:]+" : No tags found !") + #Delete generated files + os.unlink("gen/"+filename[4:-5]+".gen") + os.unlink("blog/"+filename) + print("[INFO] Deleted article "+filename[4:]+" in both gen and blog directories") + +#Delete empty tags files +for tag in list_directory("gen/tags"): + with open(tag, 'r') as tag_file: + content = tag_file.read().strip() + if content == '': + os.unlink(tag) + #TODO : Delete tag in blog directory too + print("[INFO] (TAGS) No more article with tag "+tag[]+", deleting it.") + +#TODO || +# \/ + +#(Re)Generate HTML files +for filename in added_files: + with open(filename, 'r') as fh: + #Generate HTML for the updated articles + for line in fh.readlines(): + if "@title=" in line: + line = line.strip() + title_pos = line.find("@title=") + title = line[title_pos+7:] + continue + + if "@date=" in line: + line = line.strip() + date_pos = line.find("@date=") + date = line[date_pos+6:] + continue + + if isset("date") and isset("title"): + break + + fh.seek(0) + article = fh.read() + + try: + with open("gen/"+filename[4:-5]+".gen", 'w') as article_file: + article_file.write("

"+title+"

"+article+"

"+date+"

\n") + print("[GEN ARTICLES] Article "+filename[4:]+" generated") + except: + print("[GEN ARTICLES ERROR] An error occurred while generating article "+filename[4:]) + +#Generate headers file (except title) +with open("raw/header.html") as header_fh: + #Tags + tags = os.listdir("gen/tags") + header = header_fh.read() + tags_header = "
    " + for tag in tags: + tags_header += "
  • "+tag[:-4]+"
  • " + tags_header += "
" + header = header.replace("@categories", tags_header, 1) + + #Articles + latest_articles = list_directory("gen") + latest_articles = [i for i in latest_articles if i[4:8].isdigit()] + latest_articles.sort() + articles_header = "
    " + for article in latest_articles[0:5]: + with open(article, 'r') as fh: + line = fh.readline() + while "@title" not in line: + line = fh.readline() + line = line.strip() + title_pos = line.find("@title=") + title = line[title_pos+7:] + + articles_header += "
  • "+title+"
  • " + articles_header += "
" + header = header.replace("@articles", articles_header, 1) + + with open("gen/header.gen", "w") as header_gen_fh: + header_gen_fh.write(header) + print("[GEN HEADER] Header has been generated successfully") + +#Generate footer file +shutil.copy("raw/footer.html", "gen/footer.gen") +print("[GEN FOOTER] Footer has been generated successfully") + +#Regenerate index file +#* First, get last 25 articles +last_25_articles = latest_articles[0:25] +#* Then generate effectively the index file +with open("blog/index.html", "w") as index_fh: + with open("gen/header.gen", "r") as header_gen_fh: + index = header_gen_fh.read() + for article in last_25_articles: + with open("gen/"+article, "r") as article_fh: + index += article_fh.read() + with open("gen/footer.gen") as footer_gen_fh: + index += footer_gen_fh.read() + index_fh.write(index) + +#Regenerate tags pages +for tag in listdir("gen/tags"): + if not isdir(tag): + with open(tag) as tag_fh: + with open("gen/header.gen") as header_handler: + tag_content = header_handler.read() + tag_content.replace("@titre</title", "<title>"+tag+"") + tag_fh_lines = tag_fh.readlines() + for line in tag_fh_lines: + line = line.replace(".html", ".gen") + with open("gen/"+line) as article_handler: + tag_content += "
"+article_handler.read()+"
\n" + with open("gen/footer.gen") as footer_handler: + tag_content += footer_handler.read() + tag_fh.write(tag_content) + +#Finish articles pages generation +for filename in sys.argv[1]: + with open("blog/"+filename, "w") as article_fh: + with open("gen/header.gen", "r") as header_gen_fh: + article = header_gen_fh.read() + with open("gen/"+filename, "r") as article_gen_fh: + article += article_gen_fh.read() + with open("gen/footer.gen", "r") as footer_gen_fh: + article += footer_gen_fh.read() + article_fh.write(article) + + +#Generate RSS diff --git a/raw/2013/06/test.html b/raw/2013/06/test.html new file mode 100644 index 0000000..642b48b --- /dev/null +++ b/raw/2013/06/test.html @@ -0,0 +1,11 @@ + +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+ +

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

diff --git a/raw/footer.html b/raw/footer.html new file mode 100644 index 0000000..7681d48 --- /dev/null +++ b/raw/footer.html @@ -0,0 +1,4 @@ +
+
+ + diff --git a/raw/header.html b/raw/header.html new file mode 100644 index 0000000..459e00b --- /dev/null +++ b/raw/header.html @@ -0,0 +1,30 @@ + + + + + @titre + + + +
+
+

Phyks' Blog

+

+

Catégories

+
+ @categories +
+
+

Derniers articles

+
+ @articles +
+
+

Liens

+ +
+
diff --git a/raw/params b/raw/params new file mode 100644 index 0000000..ea40704 --- /dev/null +++ b/raw/params @@ -0,0 +1,2 @@ +BLOG_TITLE = "Phyks' blog" +NB_ARTICLES_INDEX = 20