def take_action(self, parsed_args):
        site = load_site()

        site_info = {
            "site_url": site.base_url,
            "site_title": site.config.get("site", "site_title"),
            "site_description": site.config.get("site", "site_description"),
            "author_name": site.config.get("author", "name"),
            "author_email": site.config.get("author", "email"),
        }

        feed_maker = FeedMaker(site_info)

        for page in site.pages.get_all_pages():
            path = site.filename_to_url(page.filename)

            if path == "/":
                continue  # Skip the index page

            url = urljoin(site.base_url, path)

            feed_maker.add_page(url, page)

        with open("sitemap.xml", "wb") as f:
            feed_maker.generate_sitemap(f)

        with open("feeds/all.rss", "wb") as f:
            feed_maker.generate_rss(f)

        with open("feeds/all.atom", "wb") as f:
            feed_maker.generate_atom(f)
Exemple #2
0
    def take_action(self, args):
        if not os.path.exists(args.template):
            raise RuntimeError("Template file %s does not exist" % args.template)

        if not args.all_posts and not args.files:
            raise RuntimeError(
                "Either list one or more files to update or use --all-posts"
            )

        site = load_site()
        blog_posts = site.pages.get_blog_posts()

        if not args.all_posts:
            files = args.files
        else:
            files = [i.filename for i in blog_posts]

        for f in files:
            if args.verbose:
                logging.info("Applying %s to %s", args.template, f)

            apply_template(
                args.template,
                f,
                site,
                blog_posts=blog_posts,
                tidy_html=args.tidy,
                update_timestamps=args.update_timestamps,
            )
    def take_action(self, args):
        if not os.path.exists(args.template):
            raise RuntimeError("Template file %s does not exist" % args.template)

        if not args.all_posts and not args.files:
            raise RuntimeError('Either list one or more files to update or use --all-posts')

        site = load_site()
        blog_posts = site.pages.get_blog_posts()

        if not args.all_posts:
            files = args.files
        else:
            files = [i.filename for i in blog_posts]

        for f in files:
            if args.verbose:
                logging.info('Applying %s to %s', args.template, f)

            apply_template(args.template, f, site,
                           blog_posts=blog_posts, tidy_html=args.tidy,
                           update_timestamps=args.update_timestamps)
    def take_action(self, parsed_args):
        site = load_site()

        site_info = {
            'site_url': site.base_url,
            'site_title': site.config.get('site', 'site_title'),
            'site_description': site.config.get('site', 'site_description'),
            'author_name': site.config.get('author', 'name'),
            'author_email': site.config.get('author', 'email'),
        }

        sitemap = Sitemap(site_info)
        rss = RSS(site_info)
        atom = Atom(site_info)

        # TODO: Split sitemap / feed generation or impement lazy loading to avoid having to parse pages rather than read from the DB
        for page in site.pages.get_all_pages():
            path = site.filename_to_url(page.filename)

            if path == '/':
                continue  # Skip the index page

            url = urljoin(site.base_url, path)

            sitemap.add_page(url, page)
            if page.is_blog_post:
                rss.add_page(url, page)
                atom.add_page(url, page)

        with open('sitemap.xml', 'wb') as f:
            f.write(sitemap.serialize())

        with open('feeds/all.rss', 'wb') as f:
            f.write(rss.serialize())

        with open('feeds/all.atom', 'wb') as f:
            f.write(atom.serialize())
Exemple #5
0
    def take_action(self, args):
        site = load_site()

        logging.info("Updating indices under %s", site.base_dir)

        logging.debug("Loading recent posts")
        recent_posts = site.pages.get_recent_posts(4)

        logging.debug("Loading _templates/index.html")
        # TODO: read template name from a config file
        template = PyQuery(parse_html("_templates/index.html").getroot())

        template.find("title").text(site.config.get("site", "site_title"))

        post_list = template.find(".post-list").removeClass("placeholder")
        post_template = post_list.children().eq(0).clone().removeClass(
            "placeholder")
        post_list.empty()

        for post in recent_posts:
            p = post_template.clone()

            uri = site.filename_to_url(post.filename)

            html = post.html.getroot()
            html.make_links_absolute(uri)

            p.find("a.title").removeClass("placeholder").attr(
                "href", uri).text(post.title)

            post_date = post.get_publication_date()
            p.find(".date").removeClass("placeholder").text(
                post_date.strftime("%b %d")).attr("datetime",
                                                  post_date.isoformat())

            summary = html.cssselect(".summary")
            if summary:
                p.find(".summary").removeClass("placeholder").html(
                    lxml_inner_html(summary[0]).strip())
            else:
                p.find(".summary").remove()

            # Work around https://github.com/gawel/pyquery/issues/31 by correctly parsing Unicode
            # into an HTML Element instance rather than passing in the text directly.
            body = p.find(".body").removeClass("placeholder").empty()
            for i in PyQuery(html_from_string(post.articleBody)).contents():
                if isinstance(i, str):
                    # Work around https://github.com/gawel/pyquery/issues/32 by forcing
                    # lxml.etree._ElementUnicodeResult into str:
                    i = str(i)
                body.append(i)

            p.appendTo(post_list)

        orphans = template.find(".placeholder")
        if orphans:
            logging.error("Template contained unexpanded placeholders: %s",
                          orphans)

        logging.info("Replacing index.html")
        with open("index.html", "wb") as f:
            # We don't use template.outerHtml because that would lose the doctype
            f.write(
                tostring(template[0].getroottree(),
                         method="html",
                         encoding="utf-8"))

        if args.tidy:
            logging.info("Tidying index.html")
            tidy("index.html")
Exemple #6
0
    def take_action(self, args):
        site = load_site()

        logging.info('Updating indices under %s', site.base_dir)

        logging.debug('Loading recent posts')
        recent_posts = site.pages.get_recent_posts(4)

        logging.debug('Loading _templates/index.html')
        # TODO: read template name from a config file
        template = PyQuery(parse_html("_templates/index.html").getroot())

        template.find('title').text(site.config.get('site', 'site_title'))

        post_list = template.find('.post-list').removeClass("placeholder")
        post_template = post_list.children().eq(0).clone().removeClass("placeholder")
        post_list.empty()

        for post in recent_posts:
            p = post_template.clone()

            uri = site.filename_to_url(post.filename)

            html = post.html.getroot()
            html.make_links_absolute(uri)

            p.find('a.title').removeClass('placeholder') \
                .attr('href', uri) \
                .text(post.title)

            post_date = post.get_publication_date()
            p.find('.date').removeClass('placeholder') \
                .text(post_date.strftime('%b %d')) \
                .attr("datetime", post_date.isoformat())

            summary = html.cssselect('.summary')
            if summary:
                p.find('.summary').removeClass('placeholder').html(lxml_inner_html(summary[0]).strip())
            else:
                p.find('.summary').remove()

            # Work around https://github.com/gawel/pyquery/issues/31 by correctly parsing Unicode
            # into an HTML Element instance rather than passing in the text directly.
            body = p.find('.body').removeClass('placeholder').empty()
            for i in PyQuery(html_from_string(post.articleBody)).contents():
                if isinstance(i, str):
                    # Work around https://github.com/gawel/pyquery/issues/32 by forcing
                    # lxml.etree._ElementUnicodeResult into str:
                    i = str(i)
                body.append(i)

            p.appendTo(post_list)

        orphans = template.find(".placeholder")
        if orphans:
            logging.error('Template contained unexpanded placeholders: %s', orphans)

        logging.info('Replacing index.html')
        with open('index.html', 'wb') as f:
            # We don't use template.outerHtml because that would lose the doctype
            f.write(tostring(template[0].getroottree(), method='html', encoding='utf-8'))

        if args.tidy:
            logging.info('Tidying index.html')
            tidy("index.html")