# -*- coding: utf8 -*-
from flask import Flask, render_template, Blueprint
from helpers import jinja_template_loader
from filters import FILTERS
from globals import GLOBALS
from demo_data import example_article, gallery_images

app = Flask(__name__, static_folder='../static', template_folder='..')
app.jinja_env.filters.update(FILTERS)
app.jinja_env.globals.update(GLOBALS)
app.jinja_loader = jinja_template_loader

article = Blueprint('article', __name__, template_folder='templates')
@article.route('/article/<slug>/')
@article.route('/article/<slug>')
def gallery_by_slug(slug):
    return slug
app.register_blueprint(article)

def _read_file(filename):
    '''Tries to read a file. If it fails, it returns an empty string'''

    try:
        return open(filename).read()
    except:
        return ''


demo_css = _read_file('demo.css')
demo_js = _read_file('demo.js')


@app.route('/')
def index():
    ctx = {
        'article': example_article['data']['article'],
        'brand': 'wired',
        'gallery_images': gallery_images,
        'css': demo_css,
        'js': demo_js
    }

    return render_template('/demo/demo.html', **ctx)

if __name__ == '__main__':
    app.run(debug=True)
