Skip to content
Permalink
Browse files

Modified python script to create a new post.

- Added template for new posts
  • Loading branch information
edoren committed Nov 6, 2019
1 parent bad4181 commit 3939aa028ff000a36edf9feb02d8e71d04c66e52
@@ -1,10 +1,10 @@
---
id: 72b0b949-23fe-4bba-b86d-3a0815083034
id: '72b0b949-23fe-4bba-b86d-3a0815083034'
date: '2017-04-08T12:23:33-05:00'
title: Understanding Dynamic Linking in C++
title: 'Understanding Dynamic Linking in C++'
tags: [C++, Linux]
categories: [tutorials]
type: blog
type: 'blog'
---

If you've ever used C or C ++ for sure you've had to use methods that make your job easier, no one likes to re-invent the wheel (unless you're a caveman). And is that many times we find cases where we have to do the calculation of a square root with `sqrt`, or we just want to use ` printf` to print a message on the screen.
@@ -1,10 +1,10 @@
---
id: 72b0b949-23fe-4bba-b86d-3a0815083034
id: '72b0b949-23fe-4bba-b86d-3a0815083034'
date: '2017-04-08T12:23:33-05:00'
title: Entendiendo el Linkeo Dynámico en C++
title: 'Entendiendo el Linkeo Dynámico en C++'
tags: [C++, Linux]
categories: [tutorials]
type: blog
type: 'blog'
---

Si has llegado a usar C o C++ de seguro alguna vez ha tenido la necesidad de hacer uso de métodos que le faciliten tu trabajo, a nadie le gusta re inventar la rueda (a menos que seas un cavernícola). Y es que muchas veces nos encontramos con casos en los que tenemos que hacer el cálculo de una raiz cuadrada con `sqrt`, o simplemente queremos usar `printf` para imprimir un mensaje en la pantalla.
95 new.py
@@ -1,85 +1,56 @@
#!/usr/bin/env python3

import argparse
import os
import os.path
from collections import OrderedDict
from uuid import uuid4
from datetime import datetime, timezone
from uuid import uuid4

post_types = {
"_default": [
("id", lambda: str(uuid4())),
("date", lambda: datetime.now(timezone.utc).astimezone()
.replace(microsecond=0).isoformat()),
("title", "")
],
"blog": [
("tags", []),
("categories", [])
]
}


def setup_yaml():
import yaml
""" https://stackoverflow.com/a/8661021 """
def OrderedDictRepresenter(dumper, data):
return dumper.represent_mapping('tag:yaml.org,2002:map', data.items())
yaml.add_representer(OrderedDict, OrderedDictRepresenter)
return yaml


def create_post(post_type, post_path):
page_template = "---\n{}---\n\n<!--description-->\n" \
"\n<!--more-->\n\n<!--content-->\n"

contents = post_types["_default"]
if post_type is not None:
if post_type in post_types:
contents += post_types[post_type]

for i in range(len(contents)):
key, value = contents[i]
if callable(value):
contents[i] = (key, value())

contents.append(("type", post_type))
contents = OrderedDict(contents)

yaml = setup_yaml()
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))

content_dump = yaml.dump(contents, default_flow_style=False, indent=4)

with open(post_path, "w") as f:
f.write(page_template.format(content_dump))
def create_post(post_path, post_type):
post_vars = {
"id": str(uuid4()),
"date": datetime.now(timezone.utc).astimezone().isoformat(),
"type": post_type
}
with open(os.path.join(SCRIPT_DIR, "templates", "new_post.md")) as f:
data = f.read()
with open(post_path, "w") as fpost:
fpost.write(data.format(**post_vars))


def main(argv):
if len(argv) == 1:
print("Error: path needs to be provided")
exit(255)
def main(args):
post_name = args.name.replace("\\", "/")
post_type = args.type
content_folder = os.path.join(SCRIPT_DIR, "content")

name = argv[1].replace("\\", "/")
content_folder = "content"
post_path = os.path.join(content_folder, post_name)
if not os.path.splitext(post_path)[1]:
post_path += ".md"

post_path = os.path.abspath(os.path.join(content_folder, name))
if os.path.exists(post_path):
print("Error: {} already exists".format(post_path))
exit(255)
exit(-1)

post_folder = os.path.dirname(post_path)
if not os.path.exists(post_folder):
os.makedirs(post_folder)

tmp = name.split("/")
if (len(tmp) == 1):
post_type = None
else:
post_type = tmp[0]

create_post(post_type, post_path)
create_post(post_path, post_type)


if __name__ == "__main__":
from sys import argv
main(argv)
parser = argparse.ArgumentParser()
parser.add_argument("name",
help="New post file name, this can be a path.\n"
"Eg. blog/tutorials/my_post.md",
type=str)
parser.add_argument("-t, --type",
dest="type",
help="The type of the new post. Defaults to 'blog'",
default="blog",
type=str)
main(parser.parse_args())
@@ -0,0 +1,15 @@
---
id: '{id}'
date: '{date}'
title: ''
tags: []
categories: []
type: '{type}'
draft: true
---

<!-- Post Description -->

<!--more-->

<!-- Post Content -->

0 comments on commit 3939aa0

Please sign in to comment.
You can’t perform that action at this time.