Added the necessary to handle custom search and replace

This commit is contained in:
Phyks 2013-07-23 22:03:35 +02:00
parent cb0acf7ea6
commit c4e79eb4cd
4 changed files with 38 additions and 10 deletions

View File

@ -1,7 +1,7 @@
Blogit Blogit
====== ======
A git based blogging software. Just as Jekyll and so, it takes your articles as html files and computes them to generate static page and RSS feed to serve behind a webserver. It uses git as a backend file manager (as git provide some useful features like history and hooks) and Python for scripting the conversion process. You can customize the python scripts to handle special tags (ie, not standard HTML tags) just as <code> for example. A git based blogging software. Just as Jekyll and so, it takes your articles as html files and computes them to generate static page and RSS feed to serve behind a webserver. It uses git as a backend file manager (as git provide some useful features like history and hooks) and Python for scripting the conversion process. You can customize the python scripts to handle special tags (ie, not standard HTML tags) just as <code> for example. See params file in raw dir to modify this.
This project is still a WIP. This project is still a WIP.

3
TODO
View File

@ -1,4 +1,3 @@
What happen when a file is moved with git ? What happen when a file is moved with git ?
Flake8 the whole thing ! :) Flake8 the whole thing ! :) + Clean the code
basepath ?
tags / smileys / ... tags / smileys / ...

View File

@ -8,6 +8,7 @@ import shutil
import os import os
import datetime import datetime
import subprocess import subprocess
import re
from time import gmtime, strftime, mktime from time import gmtime, strftime, mktime
@ -75,6 +76,12 @@ def auto_dir(path):
sys.exit("[ERROR] An error occured while creating "+path+" file \ sys.exit("[ERROR] An error occured while creating "+path+" file \
and parent dirs.") and parent dirs.")
def replace_tags(article, search_list, replace_list):
for search, replace in zip(search_list, replace_list):
re.sub(search, replace, article)
try: try:
opts, args = getopt.gnu_getopt(sys.argv, "hf", ["help", "force-regen"]) opts, args = getopt.gnu_getopt(sys.argv, "hf", ["help", "force-regen"])
except getopt.GetoptError: except getopt.GetoptError:
@ -94,12 +101,19 @@ for opt, arg in opts:
force_regen = True force_regen = True
#Set parameters #Set parameters
search_list = []
replace_list = []
with open("raw/params", "r") as params_fh: with open("raw/params", "r") as params_fh:
params = {} params = {}
for line in params_fh.readlines(): for line in params_fh.readlines():
if line.strip() == "" or line.strip()[0] == "#": if line.strip() == "" or line.strip()[0] == "#":
continue continue
option, value = line.split("=", 1) option, value = line.split("=", 1)
if option == "SEARCH":
search_list = value.strip().split(",")
elif option == "REPLACE":
replace_list = value.strip().split(",")
else:
params[option.strip()] = value.strip() params[option.strip()] = value.strip()
#Fill lists for modified, deleted and added files #Fill lists for modified, deleted and added files
@ -336,6 +350,7 @@ for filename in added_files+modified_files:
fh.seek(0) fh.seek(0)
article = fh.read() article = fh.read()
article = replace_tags(article, search_list, replace_list)
date = "Le "+date[0:2]+"/"+date[2:4]+"/"+date[4:8]+" à "+date[9:11]+":"+date[11:13] date = "Le "+date[0:2]+"/"+date[2:4]+"/"+date[4:8]+" à "+date[9:11]+":"+date[11:13]
try: try:
@ -422,7 +437,7 @@ try:
index = header_gen_fh.read() index = header_gen_fh.read()
for article in last_articles_index: for article in last_articles_index:
with open(article, "r") as article_fh: with open(article, "r") as article_fh:
index += article_fh.read() index += replace_tags(article_fh.read(), search_list, replace_list)
with open("gen/footer.gen") as footer_gen_fh: with open("gen/footer.gen") as footer_gen_fh:
index += footer_gen_fh.read() index += footer_gen_fh.read()
@ -447,7 +462,7 @@ for tag in list_directory("gen/tags"):
for line in tag_gen_fh_lines: for line in tag_gen_fh_lines:
line = line.replace(".html", ".gen") line = line.replace(".html", ".gen")
with open("gen/"+line.strip(), "r") as article_handler: with open("gen/"+line.strip(), "r") as article_handler:
tag_content += article_handler.read() tag_content += replace_tags(article_handler.read(), search_list, replace_list)
with open("gen/footer.gen", "r") as footer_handler: with open("gen/footer.gen", "r") as footer_handler:
tag_content += footer_handler.read() tag_content += footer_handler.read()
tag_fh.write(tag_content) tag_fh.write(tag_content)
@ -457,6 +472,7 @@ for tag in list_directory("gen/tags"):
#Finish articles pages generation #Finish articles pages generation
for filename in added_files+modified_files: for filename in added_files+modified_files:
try: try:
auto_dir("blog/"+filename[4:]) auto_dir("blog/"+filename[4:])
with open("blog/"+filename[4:], "w") as article_fh: with open("blog/"+filename[4:], "w") as article_fh:
@ -472,7 +488,7 @@ for filename in added_files+modified_files:
article_gen_fh.seek(0) article_gen_fh.seek(0)
article = article.replace("@titre", params["BLOG_TITLE"]+" - "+title, 1) article = article.replace("@titre", params["BLOG_TITLE"]+" - "+title, 1)
article += article_gen_fh.read() article += replace_tags(article_gen_fh.read(), search_list, replace_list)
with open("gen/footer.gen", "r") as footer_gen_fh: with open("gen/footer.gen", "r") as footer_gen_fh:
article += footer_gen_fh.read() article += footer_gen_fh.read()
article_fh.write(article) article_fh.write(article)
@ -512,7 +528,7 @@ for i in years_list:
for article in articles_list: for article in articles_list:
try: try:
with open(article, "r") as article_fh: with open(article, "r") as article_fh:
article_content = article_fh.read() article_content = replace_tags(article_fh.read(), search_list, replace_list)
page_month += article_content page_month += article_content
page_year += article_content page_year += article_content
except IOError: except IOError:
@ -570,7 +586,7 @@ for article in last_articles_index:
<title>"+title+"</title> \ <title>"+title+"</title> \
<link>"+params["BLOG_URL"]+article[5:]+"</link> \ <link>"+params["BLOG_URL"]+article[5:]+"</link> \
<guid isPermaLink=\"false\">"+params["BLOG_URL"]+article[5:]+"</guid> \ <guid isPermaLink=\"false\">"+params["BLOG_URL"]+article[5:]+"</guid> \
<description><![CDATA["+article_fh.read()+"]]></description> \ <description><![CDATA["+replace_tags(article_fh.read(), search_list, replace_list)+"]]></description> \
<pubDate>"+date+"</pubDate> \ <pubDate>"+date+"</pubDate> \
<category>"+', '.join(tags)+"</category> \ <category>"+', '.join(tags)+"</category> \
<author>"+params["WEBMASTER"]+"</author> \ <author>"+params["WEBMASTER"]+"</author> \

View File

@ -1,2 +1,15 @@
BLOG_TITLE = "Phyks' blog" BLOG_TITLE = Phyks' blog
NB_ARTICLES_INDEX = 20 NB_ARTICLES_INDEX = 20
BLOG_URL = file:///home/lucas/Blog/git/blog/
#RSS params
WEBMASTER = webmaster@phyks.me (Phyks)
LANGUAGE = fr
DESCRIPTION =
COPYRIGHT =
#Personal replacements
#Items are passed as arguments to a python regex,
#Take care to match the format
#SEARCH = "item1", "item2"
#REPLACE = "item1_bis", "item2_bis"