Skip to content

Instantly share code, notes, and snippets.

@elssar
Last active December 12, 2015 10:38
Show Gist options
  • Save elssar/4760090 to your computer and use it in GitHub Desktop.
Save elssar/4760090 to your computer and use it in GitHub Desktop.
Gist to test out conundrum, a webframework agnostic simple blogging plugin in Python --> https://github.com/elssar/conundrum

Test post #1 for testing conundrum, a webframework agnostic blogging plugin.

Lets test syntax highliting

#!/usr/bin/python

print 'Code with shebang, and numbered lines

from markdown import markdown
from yaml import load, dump
from sys import path, argv
from datetime import datetime
from requests import get, post
from json import loads, dumps
import os

base= os.path.dirname(os.path.realpath(__file__))+'/posts/'

def fetch(id, name, user):
    gist= get('https://api.github.com/gists/'+id, headers={'user-agent': 'conundrum-blog-framework'})
    content= loads(gist.content)
    if user!= content['user']['login'] or gist.status_code!=200:
        return False
    data= {'date': datetime.now().strftime('%B %d, %Y')}
    data['description']= content['description']
    for f in content['files']:
        if content['files'][f]['language']=='Markdown':
            data['post']= content['files'][f]['content']
            data['url']= content['files'][f]['raw_url']
            data['title']= content['files'][f]['filename']
            data['author']= content['user']['login']
    with open(base+name+'.yaml', 'w') as post:
        dump(data, post)
    with open(base+'first', 'w') as f:
        f.write(name+'.yaml')
    with open(base+'archive.md', 'a') as archive:
        line= ' - [{0}]({1})   ({2})\n'.format(data['title'], name, data['date'])
        archive.write(line)
    return True

def update(name):
    try:
        with open(base+name+'.yaml', 'r') as f:
            data= load(f)
    except IOError:
        return False
    post= get(data['url'], headers={'user-agent': 'conundrum-blog-engine'})
    if post.status_code!=200:
        return False
    data['post']= post.text
    with open(base+name+'.yaml', 'w') as f:
        dump(data, f)
    return True

Switching things up a bit

#!python

print 'No shebang, but numbered lines.'

def blog(name):
    try:
        if name is None:
            with open(base+'first', 'w') as f:
                name= f.read()
        with open(base+name+'.yaml', 'r') as f:
            data= load(f)
    except IOError:
        return False
    post= markdown(data['post'], ['codehilite'])
    return post, data['date']
    

def archive():
    try:
        with open(base+'archive.md', 'r') as f:
            data= f.readlines()
        arch= ''.join(data[::-1])
        return markdown(arch)
    except IOError:
        return False

And finally

:::python

print 'No shebang, no line numbers'
    
def operate(*args):
    opts= ['-p' , '-u']
    keys= ['title', 'id', 'auth']
    headers= {'User-Agent': 'conundrum-operator', 'Content-Type': 'application/json'}
    if args[0] not in opts:
        print 'Invalid options'
        return False
    payload= {}
    for key, value in zip(keys, args[2:]):
        payload[key]= value
    body= dumps(payload)
    req= post(args[1], data= body, headers= headers)
    print req.status_code

if __name__=='__main__':
    operate(*argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment