Some progress with the templating of articles. Not working.

This commit is contained in:
maste9 2015-02-15 23:27:40 +01:00 committed by Homer S
parent c72d83cf39
commit bdff627aee
2 changed files with 69 additions and 15 deletions

View File

@ -5,6 +5,10 @@ APP_NAME = picoblogger
APP_VERSION = 0.1
MAIN_TITLE = Artikel
TEMPLATE_ARTICLE_AUTHOR = Autor
TEMPLATE_ARTICLE_CREATED = Erstellt am
TEMPLATE_ARTICLE_MODIFIED = Zuletzt geändert
NAV_TITLE = Navigation
HOME_BUTTON = Aktuell
ARCHIVE_BUTTON = Archiv

View File

@ -30,11 +30,11 @@ parser_draft = subparsers.add_parser('draft', help='create, list, edit, publish
subparsers_draft = parser_draft.add_subparsers(help="draft subcommands", dest='action')
draft_parser_add = subparsers_draft.add_parser('add', help='add a new draft')
draft_parser_add.add_argument('name', help='define an alphanumeric name')
draft_parser_list = subparsers_draft.add_parser('list', help='list drafts')
draft_parser_list = subparsers_draft.add_parser('list', help='list drafts` names')
draft_parser_edit = subparsers_draft.add_parser('edit', help='edit certain draft')
draft_parser_edit.add_argument('id', help='id like specified by [list]')
draft_parser_edit.add_argument('name', help='name like specified by [list]')
draft_parser_remove = subparsers_draft.add_parser('remove', help='remove certain draft')
draft_parser_remove.add_argument('id', help='id like specified by [list]')
draft_parser_remove.add_argument('name', help='name like specified by [list]')
draft_parser_publish = subparsers_draft.add_parser('publish', help='publish draft as article')
draft_parser_publish.add_argument('name', help='name of draft to publish')
@ -76,6 +76,9 @@ except:
# path to blog's templates
tplDir = os.path.join(blog_dir,"templates")
# path to drafts
draftDir = os.path.join(blog_dir,"drafts")
# paths to configs
pbconfpath = os.path.join("/","etc","picoblogger","pb.conf") # global config
blogconfpath = os.path.join(blog_dir,"pb.conf") # local config
@ -90,11 +93,14 @@ blogconfpath = os.path.join(blog_dir,"pb.conf") # local config
def parse_conf(path):
aVars = {}
f = open(path, "r")
if os.path.exists(path):
f = open(path, "r")
else:
f = path
for line in f:
if "#" in line:
line = line[0:line.find("#")]
pattern = '^(\w+)\s*?=\s*(.+)[^#]*$'
pattern = '^(\w+)\s*?[=|:]\s*(.+)[^#]*$'
match = re.search(pattern, line)
if match:
aVars[match.group(1)] = match.group(2)
@ -135,6 +141,47 @@ except:
# Templating functions #
########################
def parse_article(path):
article = open(path,"r").read()
header = article.lstrip("HEADER_BEGIN")
print(header)
header = header.rstrip("HEADER_END")
print(header)
body = markdown.markdown(article.lstrip("HEADER_END")[1])
print(body)
article_dict = parse_conf(header)
article_dict['BODY'] = body
return article_dict
def article_to_html(article_dict):
sARTICLE = open(os.path.join(tplDir,"article.htm")).read()
sARTICLE = Template(sARTICLE).safe_substitute(l10nconf)
# Read out article txt file
tplSub = {
"ARTICLE_TITLE":article_dict['TITLE'],
"ARTICLE_AUTHOR":article_dict['AUTHOR'],
"ARTICLE_CREATED":article_dict['CREATED'],
"ARTICLE_MODIFIED":article_dict['MODIFIED'],
"ARTICLE_BODY":article_dict['BODY'],
"ARTICLE_TAGS":article_dict['TAGS']
}
sARTICLE = Template(sARTICLE).safe_substitute(tplSub)
return sARTICLE
def join_articles():
artDir = os.path.join(blog_dir,"articles")
joined_html = ""
for name in [os.path.splitext(item)[0] for item in os.listdir(draftDir) if not "~" in item]:
joined_html += article_to_html(parse_article(os.path.join(artDir, name + ".txt")))
return joined_html
def update_blog(subsection):
# Joins config defined variables into templates
@ -152,6 +199,7 @@ def update_blog(subsection):
sNAV = open(os.path.join(tplDir,"nav.htm")).read()
sNAV = Template(sNAV).safe_substitute(l10nconf)
sMAIN = open(os.path.join(tplDir,"main.htm")).read()
sMAIN = Template(sMAIN).safe_substitute(join_articles())
sASIDE = open(os.path.join(tplDir,"aside.htm")).read()
sFOOTER = open(os.path.join(tplDir,"footer.htm")).read()
@ -198,7 +246,6 @@ if vars(args)['target'] == 'blog':
# Joining all templates
update_blog(vars(args)['subsection'])
print(article_to_html("test"))
print("Updated your blog.")
@ -228,34 +275,37 @@ if vars(args)['target'] == 'draft':
with open(draft,"w", encoding=l10nconf['BLOG_CHARSET']) as f:
print(sTpl, file=f)
subprocess.call(["emacs", "nw", draft])
subprocess.call(["emacs", "-nw", draft])
## list drafts
if vars(args)['action'] == 'list':
pass
[print(item) for item in [os.path.splitext(item)[0] for item in os.listdir(draftDir) if not "~" in item]]
## edit draft
if vars(args)['action'] == 'edit':
draft = os.path.join(blog_dir,"drafts",vars(args)['id'] + ".txt")
draft = os.path.join(blog_dir,"drafts",vars(args)['name'] + ".txt")
subprocess.call(["emacs", " -nw", draft])
## remove draft
if vars(args)['action'] == 'remove':
pass
draft = os.path.join(blog_dir,"drafts",vars(args)['name'] + ".txt")
try:
os.remove(draft)
except:
print("Can't find %s."%draft)
## publish draft as article
if vars(args)['action'] == 'publish':
pass
draft = os.path.join(blog_dir,"drafts",vars(args)['name'] + ".txt")
article = os.path.join(blog_dir,"articles",vars(args)['name'] + ".txt")
os.replace(draft, article)
# article functions