Last active
December 23, 2015 01:49
-
-
Save yen3/6562566 to your computer and use it in GitHub Desktop.
Generate a new post with default contents in nikola.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
DEFAULT_CONTENTS="""<!-- | |
.. link: | |
.. description: | |
.. tags: all | |
.. date: $time | |
.. title: $title | |
.. slug: $slug | |
--> | |
""" | |
from sys import argv | |
from datetime import datetime | |
from string import Template | |
from re import compile as regex_compile | |
TITLE_REGEX = regex_compile('\w+') | |
def main(): | |
if len(argv) > 1: | |
article_title = argv[1] | |
file_name_title = "-".join(TITLE_REGEX.findall(article_title.lower())) | |
today = datetime.today() | |
file_timestamp = "%04d%02d%2d" % (today.year, today.month, today.day) | |
article_timestamp = "%04d/%02d/%02d %02d:%02d:%02d" % (today.year, today.month, today.day, today.hour, today.minute, today.second) | |
article_slug = "%s_%s" % (file_timestamp, file_name_title) | |
filename = article_slug + ".md" | |
f = open("./posts/" + filename, "w") | |
f.write(Template(DEFAULT_CONTENTS).substitute(title=article_title, time=article_timestamp, slug=article_slug)) | |
f.close() | |
print "Generate file in " + "./posts/" + filename | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment