From bdff627aee1d0ec35cdfc50e5f5f6e7176d03f41 Mon Sep 17 00:00:00 2001 From: maste9 Date: Sun, 15 Feb 2015 23:27:40 +0100 Subject: [PATCH] Some progress with the templating of articles. Not working. --- picoblogger/blog/l10n/de/static.conf | 4 ++ picoblogger/main.py | 80 ++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/picoblogger/blog/l10n/de/static.conf b/picoblogger/blog/l10n/de/static.conf index f707041..6441641 100644 --- a/picoblogger/blog/l10n/de/static.conf +++ b/picoblogger/blog/l10n/de/static.conf @@ -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 diff --git a/picoblogger/main.py b/picoblogger/main.py index 42833d4..f5e3b70 100755 --- a/picoblogger/main.py +++ b/picoblogger/main.py @@ -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) @@ -134,6 +140,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