Skip to content
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.
@@ -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:
("title", "")
"blog": [
("tags", []),
("categories", [])

def setup_yaml():
import yaml
""" """
def OrderedDictRepresenter(dumper, data):
return dumper.represent_mapping(',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" \

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:
def create_post(post_path, post_type):
post_vars = {
"id": str(uuid4()),
"type": post_type
with open(os.path.join(SCRIPT_DIR, "templates", "")) as f:
data =
with open(post_path, "w") as fpost:

def main(argv):
if len(argv) == 1:
print("Error: path needs to be provided")
def main(args):
post_name ="\\", "/")
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))

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

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

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

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

<!-- Post Description -->


<!-- Post Content -->

0 comments on commit 3939aa0

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