# Path: native_tags/decorators.py
# def tag(bucket, doc):
#     def wrapped(inner, **options):


import datetime
import feedparser
from django.template import TemplateSyntaxError
from native_tags.decorators import function
    Syntax::
    
        {% include_feed [feed_url] [num_items] [template_name] %}
    
    Example::
    
        {% include_feed "http://www2.ljworld.com/rss/headlines/" 10 feed_includes/ljworld_headlines.html %}
    
    """
    feed = feedparser.parse(feed_url)
    items = []
    
    if len(args) == 2:
        # num_items, template_name
        num_items, template_name = args
    elif len(args) == 1:
        # template_name
        num_items, template_name = None, args[0]
    else:
        raise TemplateSyntaxError("'include_feed' tag takes either two or three arguments")
        
    num_items = int(num_items) or len(feed['entries'])
    for i in range(num_items):
        pub_date = feed['entries'][i].updated_parsed
        published = datetime.date(pub_date[0], pub_date[1], pub_date[2])
        items.append({ 'title': feed['entries'][i].title,
                       'summary': feed['entries'][i].summary,
                       'link': feed['entries'][i].link,
                       'date': published })
    return template_name, { 'items': items, 'feed': feed }

# Path: native_tags/decorators.py
# def tag(bucket, doc):
#     def wrapped(inner, **options):


# Path: native_tags/contrib/feeds.py
import datetime
import feedparser
from django.template import TemplateSyntaxError
from native_tags.decorators import function

    Syntax::
    
        {% include_feed [feed_url] [num_items] [template_name] %}
    
    Example::
    
        {% include_feed "http://www2.ljworld.com/rss/headlines/" 10 feed_includes/ljworld_headlines.html %}
    
    """
    feed = feedparser.parse(feed_url)
    items = []
    
    if len(args) == 2:
        # num_items, template_name
        num_items, template_name = args
    elif len(args) == 1:
        # template_name
        num_items, template_name = None, args[0]
    else:
        raise TemplateSyntaxError("'include_feed' tag takes either two or three arguments")
        
    num_items = int(num_items) or len(feed['entries'])
    for i in range(num_items):
        pub_date = feed['entries'][i].updated_parsed
        published = datetime.date(pub_date[0], pub_date[1], pub_date[2])
        items.append({ 'title': feed['entries'][i].title,
                       'summary': feed['entries'][i].summary,
                       'link': feed['entries'][i].link,
                       'date': published })
    return template_name, { 'items': items, 'feed': feed }

include_feed = function(include_feed, inclusion=True)