#!/usr/bin/env  python2
##
# Title:        Consortium News
##
# License:      GNU General Public License v3 -
# http://www.gnu.org/copyleft/gpl.html

# Feb 2012: Initial release
__license__ = 'GNU General Public License v3 - http://www.gnu.org/copyleft/gpl.html'
'''
consortiumnews.com
'''
import re
from calibre.web.feeds.news import BasicNewsRecipe


class ConsortiumNews(BasicNewsRecipe):

    title = u'Consortium News'
    publisher = 'Copyright © 2012 Consortiumnews. All Rights Reserved.'
    language = 'en'
    __author__ = 'kiavash'

    oldest_article = 7
    max_articles_per_feed = 100

    no_stylesheets = True
    remove_javascript = True

    # Flattens all the tables to make it compatible with Nook
    conversion_options = {'linearize_tables': True}

    remove_attributes = ['border', 'cellspacing', 'align', 'cellpadding', 'colspan',
                         'valign', 'vspace', 'hspace', 'alt', 'width', 'height']

    # Specify extra CSS - overrides ALL other CSS (IE. Added last).
    extra_css = 'body { font-family: verdana, helvetica, sans-serif; } \
                 .introduction, .first { font-weight: bold; } \
                 .cross-head { font-weight: bold; font-size: 125%; } \
                 .cap, .caption { display: block; font-size: 80%; font-style: italic; } \
                 .cap, .caption, .caption img, .caption span { display: block; margin: 5px auto; } \
                 .byl, .byd, .byline img, .byline-name, .byline-title, .author-name, .author-position, \
                    .correspondent-portrait img, .byline-lead-in, .name, .bbc-role { display: block; \
                    font-size: 80%; font-style: italic; margin: 1px auto; } \
                 .story-date, .published { font-size: 80%; } \
                 table { width: 100%; } \
                 td img { display: block; margin: 5px auto; } \
                 ul { padding-top: 10px; } \
                 ol { padding-top: 10px; } \
                 li { padding-top: 5px; padding-bottom: 5px; } \
                 h1 { font-size: 175%; font-weight: bold; } \
                 h2 { font-size: 150%; font-weight: bold; } \
                 h3 { font-size: 125%; font-weight: bold; } \
                 h4, h5, h6 { font-size: 100%; font-weight: bold; }'

    # Remove the line breaks and float left/right and picture width/height.
    preprocess_regexps = [(re.compile(r'<br[ ]*/>', re.IGNORECASE), lambda m: ''),
                          (re.compile(r'<br[ ]*clear.*/>',
                                      re.IGNORECASE), lambda m: ''),
                          (re.compile(r'float:.*?'), lambda m: ''),
                          (re.compile(r'width:.*?px'), lambda m: ''),
                          (re.compile(r'height:.*?px'), lambda m: ''),
                          (re.compile(r'<a.*?>'), lambda h1: ''),
                          (re.compile(r'</a>'), lambda h2: ''),
                          ]

    # Main article is inside this tag
    keep_only_tags = [
        dict(name='div', attrs={'id': lambda x: x and 'post-' in x})]

    remove_tags = [
        # remove 'Share this Article'
        dict(name='div', attrs={'class': 'sociable'}),
        dict(name='p', attrs={'class': 'tags'}),    # remove 'Tags: ... '
    ]

    feeds = [(u'Consortium News', u'http://feeds.feedburner.com/Consortiumnewscom')]
