# Path: gitcms/blog/models.py
# class BlogPost(models.Model):
#     title = models.CharField(u'title', max_length=255)
#     slug = models.CharField(u'slug', max_length=255)
#     timestamp = models.DateTimeField(u'timestamp')
#     tags = models.ManyToManyField(Tag)
#     content = models.TextField(u'content')
#     status = models.CharField(u'status', max_length=255)
#     author = models.CharField(u'author', max_length=255)
#     keywords = models.CharField(u'keywords', max_length=255)
#     description = models.CharField(u'description', max_length=255)
# 
#     def __unicode__(self):
#         return '%s (%s)' % (self.slug, self.title)
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('blog-post', [self.timestamp.year, self.timestamp.month, self.slug])
# 
# Path: gitcms/parsedate/parsedate.py
# def parsedatetime(datetimerep):
#     '''
#     time = parsedatetime(daterep)
# 
#     Parses a datetime written out in English.
#     '''
#     return _tryformats(datetimerep,
#             ['%d %b %Y %H:%M', '%d %B %Y %H:%M', '%b %d %Y %H:%M', '%B %d %Y %H:%M'],
#             "Cannot parse '%s' as a datetime" % datetimerep)
# 
# Path: gitcms/pages/load.py
# def loaddir(directory, clear=False):
#         A = Article(title=header['title'], url=url, meta=header.get('meta', ''), author=header.get('author', ''), content=content)
# 
# Path: gitcms/tagging/models.py
# def tag_for(slug):
#     '''
#     tag = tag_for(slug)
# 
#     Returns the Tag object corresponding to slug
# 
#     Raises an exception if not found.
#     '''
#     tags = Tag.objects.filter(slug=slug).all()
#     if len(tags) == 1:
#         return tags[0]
#     if not tags:
#         raise ValueError("gitcms.tagging.tag_for: No tag for slug '%s'" % slug)
#     raise ValueError("gitcms.tagging.tag_for: Multiple tags for slug '%s' (%s)" % (slug, [t.name for t in tags]))


from .models import BlogPost
from os import path, listdir
from gitcms.parsedate import parsedatetime
from gitcms.pages.load import preprocess_rst_content
from gitcms.tagging.models import tag_for
import yaml
import time
    queue = listdir(directory)
    while queue:
        next = queue.pop()
        if next[0] == '.': continue
        if next in ('template.rst', 'template'): continue
        next = path.join(directory, next)
        if path.isdir(next):
            queue.extend([
                path.join(next,f) for f in listdir(next)
                ])
            continue

        filecontent = open(next).read()
        parts = filecontent.split('\n---\n', 1)
        if len(parts) != 2:
            raise IOError('gitcms.blog.load: expected "---" separator in file %s' % next)
        fields, content = parts
        fields = yaml.load(fields)
        fields['content'] = preprocess_rst_content(content)
        fields['timestamp'] = parsedatetime(fields['timestamp'])
        fields['timestamp'] = time.strftime('%Y-%m-%d %H:%M', fields['timestamp'])
        categories = fields.get('categories', '')
        if 'categories' in fields: del fields['categories']
        ptags = []
        if categories:
            for c in categories.split():
                ptags.append(tag_for(c))
        # if we arrived here and no errors, then it is safe
        # to add our post.
        #

# Path: gitcms/blog/models.py
# class BlogPost(models.Model):
#     title = models.CharField(u'title', max_length=255)
#     slug = models.CharField(u'slug', max_length=255)
#     timestamp = models.DateTimeField(u'timestamp')
#     tags = models.ManyToManyField(Tag)
#     content = models.TextField(u'content')
#     status = models.CharField(u'status', max_length=255)
#     author = models.CharField(u'author', max_length=255)
#     keywords = models.CharField(u'keywords', max_length=255)
#     description = models.CharField(u'description', max_length=255)
# 
#     def __unicode__(self):
#         return '%s (%s)' % (self.slug, self.title)
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('blog-post', [self.timestamp.year, self.timestamp.month, self.slug])
# 
# Path: gitcms/parsedate/parsedate.py
# def parsedatetime(datetimerep):
#     '''
#     time = parsedatetime(daterep)
# 
#     Parses a datetime written out in English.
#     '''
#     return _tryformats(datetimerep,
#             ['%d %b %Y %H:%M', '%d %B %Y %H:%M', '%b %d %Y %H:%M', '%B %d %Y %H:%M'],
#             "Cannot parse '%s' as a datetime" % datetimerep)
# 
# Path: gitcms/pages/load.py
# def loaddir(directory, clear=False):
#         A = Article(title=header['title'], url=url, meta=header.get('meta', ''), author=header.get('author', ''), content=content)
# 
# Path: gitcms/tagging/models.py
# def tag_for(slug):
#     '''
#     tag = tag_for(slug)
# 
#     Returns the Tag object corresponding to slug
# 
#     Raises an exception if not found.
#     '''
#     tags = Tag.objects.filter(slug=slug).all()
#     if len(tags) == 1:
#         return tags[0]
#     if not tags:
#         raise ValueError("gitcms.tagging.tag_for: No tag for slug '%s'" % slug)
#     raise ValueError("gitcms.tagging.tag_for: Multiple tags for slug '%s' (%s)" % (slug, [t.name for t in tags]))


# Path: gitcms/blog/load.py
from .models import BlogPost
from os import path, listdir
from gitcms.parsedate import parsedatetime
from gitcms.pages.load import preprocess_rst_content
from gitcms.tagging.models import tag_for
import yaml
import time

    queue = listdir(directory)
    while queue:
        next = queue.pop()
        if next[0] == '.': continue
        if next in ('template.rst', 'template'): continue
        next = path.join(directory, next)
        if path.isdir(next):
            queue.extend([
                path.join(next,f) for f in listdir(next)
                ])
            continue

        filecontent = open(next).read()
        parts = filecontent.split('\n---\n', 1)
        if len(parts) != 2:
            raise IOError('gitcms.blog.load: expected "---" separator in file %s' % next)
        fields, content = parts
        fields = yaml.load(fields)
        fields['content'] = preprocess_rst_content(content)
        fields['timestamp'] = parsedatetime(fields['timestamp'])
        fields['timestamp'] = time.strftime('%Y-%m-%d %H:%M', fields['timestamp'])
        categories = fields.get('categories', '')
        if 'categories' in fields: del fields['categories']
        ptags = []
        if categories:
            for c in categories.split():
                ptags.append(tag_for(c))
        # if we arrived here and no errors, then it is safe
        # to add our post.
        #

        P = BlogPost(**fields)