# -*- coding: utf-8 -*-
# Copyright (c) 2012 Michael Babich
# See LICENSE.txt or http://opensource.org/licenses/MIT

'''Acts as an interface between what Flask serves and what goes on in
the rest of the application.
'''
from avenue import app, api
from flask import render_template, make_response
from copy import copy
import yaml
from os import path

def forum_generator(site_name, forum_name):
    navbar_file = open(path.join(path.dirname(__file__), 'data', 'navbar.yml'))
    navbar = yaml.load(navbar_file)
    navbar_file.close()

    tag_file = open(path.join(path.dirname(__file__), 'data', 'tags.yml'))
    tags = yaml.load(tag_file)
    tag_file.close()

    def render_forum(thread_title='', main_title='', html_title='', posts=[], threaded=False, content=''):
        return render_template('forum.html',
                               style='night',
                               sidebar=navbar,
                               thread_title=thread_title,
                               main_title=main_title,
                               html_title=html_title,
                               posts=posts,
                               threaded=threaded,
                               content=content)

    def forum_page(filename, content):
        data = open(path.join(path.dirname(__file__), 'data', filename))
        thread = yaml.load(data)
        data.close()

        html_title = '%s :: %s :: %s' % (thread['title'], forum_name, site_name)
        main_title = '%s -- %s' % (site_name, forum_name)

        for post in thread['posts']:
            if 'tags' in post:
                for i in range(len(post['tags'])):
                    post['tags'][i] = tags[post['tags'][i]]

        return render_forum(main_title=main_title,
                            thread_title=thread['title'],
                            html_title=html_title,
                            posts=thread['posts'],
                            threaded=thread['threaded'],
                            content=content)

    return forum_page

make_page = forum_generator('Zombie Raptor', 'Main Forum')

@app.route('/')
def index():
    return make_page('front_page.yml', 'blog')

@app.route('/f/')
def f():
    return ''

@app.route('/f/main/')
def main_forum():
    return make_page('main_forum.yml', 'index')

@app.route('/f/main/post/')
def post():
    return ''

@app.route('/f/main/post/1')
def sample_post():
    return make_page('sample.yml', 'post')


@app.route('/night.css')
def night():
    conf = open(path.join(path.dirname(__file__), 'data', 'style.yml'))
    style = yaml.load(conf)
    conf.close()

    response = make_response(render_template('main.css',
                                             text=style['text'],
                                             background=style['background'],
                                             post=style['post']))
    response.mimetype = 'text/css'
    return response

@@ -353,58 +353,73 @@
 def 
-forum_generator(site_name, forum_
+read_data(filename):%0A    filename = '%25s.yml' %25 file
 name
-):
+%0A
 %0A    
-navbar
+data
 _fil
@@ -473,33 +473,27 @@
 a', 
-'navbar.yml'
+filename
 ))%0A    
-navbar
+data
  = y
@@ -501,22 +501,20 @@
 ml.load(
-navbar
+data
 _file)%0A 
@@ -516,22 +516,20 @@
 le)%0A    
-navbar
+data
 _file.cl
@@ -543,129 +543,125 @@
     
-tag_file = open(path.join(path.dirname(__file__), 'data', 'tags.yml'))%0A    tags = yaml.load(tag_file)%0A    tag_file.close(
+return data%0A%0Adef forum_generator(site_name, forum_name):%0A    navbar = read_data('navbar')%0A    tags = read_data('tags'
 )%0A%0A 
@@ -1264,62 +1264,27 @@
     
-data = open(path.join(path.dirname(__file__), '
+thread = read_
 data
-', 
+(
 file
@@ -1292,63 +1292,8 @@
 ame)
-)%0A        thread = yaml.load(data)%0A        data.close()
 %0A%0A  
@@ -2068,20 +2068,16 @@
 ont_page
-.yml
 ', 'blog
@@ -2195,20 +2195,16 @@
 in_forum
-.yml
 ', 'inde
@@ -2339,20 +2339,16 @@
 ('sample
-.yml
 ', 'post
@@ -2398,119 +2398,33 @@
     
-conf = open(path.join(path.dirname(__file__), '
+style = read_
 data
-', 
+(
 'style
-.yml'))%0A    style = yaml.load(conf)%0A    conf.close(
+'
 )%0A%0A 

Move data file reading to its own function.