Last active
January 27, 2022 18:46
-
-
Save ayushgoel/70eff5e48afcc2d98b45 to your computer and use it in GitHub Desktop.
Create a post file to be used in Jekyll aware blogs.
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
# Byte-compiled / optimized / DLL files | |
__pycache__/ | |
*.py[cod] | |
*$py.class | |
# Distribution / packaging | |
.Python | |
env/ | |
build/ | |
develop-eggs/ | |
dist/ | |
downloads/ | |
eggs/ | |
.eggs/ | |
lib/ | |
lib64/ | |
parts/ | |
sdist/ | |
var/ | |
*.egg-info/ | |
.installed.cfg | |
*.egg | |
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 | |
import argparse | |
import create_dir | |
import filename | |
import front_matter | |
VERSION = "0.1.0" | |
def create_post(posts_dir, post_extension, post_title): | |
print("Will create file in DIR = {}".format(posts_dir)) | |
file_name = filename.sample_filename(post_title, post_extension) | |
file_path = posts_dir + file_name | |
create_dir.create_dir_if_not_exists_already(posts_dir) | |
with open(file_path, "w") as f: | |
f.write(front_matter.front_matter(" ", " ", [" "])) | |
print("Did create file = {}".format(file_path)) | |
def main(args): | |
create_post(args.dir, args.ext, args.title) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description = "Create sample post file to be used by Jekyll") | |
parser.add_argument("--dir", default = "_posts/", help = "Directory path to write the sample post in. Default: _posts/") | |
parser.add_argument("--ext", default = "md", help = "Extension type for the post. Default: .md") | |
parser.add_argument("--title", default = "title", help = "Title of the post. Default: title") | |
parser.add_argument("--version", action = "version", version = "%(prog)s {}".format(VERSION)) | |
args = parser.parse_args() | |
main(args) |
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 | |
import os | |
import argparse | |
def create_dir_if_not_exists_already(path): | |
if not os.path.exists(path): | |
os.makedirs(path) | |
def main(args): | |
if not args.path: | |
parser.error("Required argument path not given") | |
create_dir_if_not_exists_already(args.path) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description = "Create a directory of given name if one does not exist already.") | |
parser.add_argument("--path", help = "Directory path to create.") | |
args = parser.parse_args() | |
main(args) |
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 | |
import datetime | |
import argparse | |
def filename(date, title, ext): | |
name = date + "-" + title | |
if ext: | |
name = name + "." + ext | |
return name | |
def date_for_filename(): | |
return datetime.date.today().isoformat() | |
def sample_filename(title, ext): | |
return filename(date_for_filename(), title, ext) | |
def main(args): | |
print(sample_filename(args.title, args.ext)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description = "Give filename for a given title according to Jekyll post.") | |
parser.add_argument("--title", default = "title", help = "Title for post. Default: title") | |
parser.add_argument("--ext", help = "Optional extension for filename.") | |
args = parser.parse_args() | |
main(args) |
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 | |
import argparse | |
def wrap(demarker, content): | |
return demarker + content + demarker | |
def add_front_matter(content, label, value): | |
if value: | |
content += "{0}: {1}\n".format(label, value) | |
return content | |
def front_matter(layout, category, tags): | |
content = "" | |
content = add_front_matter(content, "layout", layout) | |
content = add_front_matter(content, "category", category) | |
if tags: | |
content_tags = " ".join(tags) | |
content = add_front_matter(content, "tags", content_tags) | |
demarker = "---\n" | |
return wrap(demarker, content) | |
def main(args): | |
print(front_matter(args.layout, args.category, args.tags)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description = "Create YAML front matter to be used by Jekyll") | |
parser.add_argument("--layout", help = "Layout to be used.") | |
parser.add_argument("--category", help = "Category for the post.") | |
parser.add_argument("--tags", nargs = "*", help = "Tags for the post.") | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment