Skip to content

Instantly share code, notes, and snippets.

@sergixnet
Created February 14, 2017 16:07
Show Gist options
  • Save sergixnet/0e4c3ce6417dfb06904d3f9a923a66f6 to your computer and use it in GitHub Desktop.
Save sergixnet/0e4c3ce6417dfb06904d3f9a923a66f6 to your computer and use it in GitHub Desktop.
Creating a markdown file from a dictionary, for use in Grav cms
# -*- coding: utf-8 -*-
import yaml
import html2text
import os
ROOT_DIR = os.getcwd()
dic = {
'title': 'My awesome title',
'date': '2016-20-02 00:00',
'taxonomy': {
'category': ['News','Pictures', 'Photography', 'Graphic design'],
'tag': ['CSS', 'HTML', 'Javascript', 'Python']
},
'slug': 'my-awesome-title',
'content': '<h1>This is a heading 1</h1><p class="css">My <strong>awesome</strong> content with a <a href="http://example.com">link</a></p><p><img src="http://www.example.com/image.jpg" class="my-image" alt="an alt" title="a title" /></p>'
}
content_html = dic.pop('content')
frontyaml = yaml.dump(dic)
frontmatter = '---\n' + frontyaml + '---\n'
content_md = html2text.html2text(content_html)
file_content = frontmatter + content_md
folder_name = 'blog'
post_folder_name = dic['slug']
post_folder_dir = os.path.join(ROOT_DIR, folder_name, post_folder_name)
if os.path.exists(post_folder_dir):
pass
else:
os.makedirs(post_folder_dir)
file_path = os.path.join(post_folder_dir, 'item.md')
with open(file_path, 'w') as f:
f.write(file_content)
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: linux-64
html2text=2015.4.14=py27_0
openssl=1.0.2k=0
pip=9.0.1=py27_1
python=2.7.13=0
pyyaml=3.12=py27_0
readline=6.2=2
setuptools=27.2.0=py27_0
sqlite=3.13.0=0
tk=8.5.18=0
wheel=0.24.0=py27_0
yaml=0.1.6=0
zlib=1.2.8=3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment