# Copyright (c) 2012 Roberto Alsina y otros.

# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import unicode_literals, print_function

import os

from pygments import highlight
from pygments.lexers import get_lexer_for_filename, TextLexer
from pygments.formatters import HtmlFormatter

from nikola.plugin_categories import Task
from nikola import utils


class Listings(Task):
    """Render pretty listings."""

    name = "render_listings"

    def gen_tasks(self):
        """Render pretty code listings."""
        kw = {
            "default_lang": self.site.config["DEFAULT_LANG"],
            "listings_folder": self.site.config["LISTINGS_FOLDER"],
            "output_folder": self.site.config["OUTPUT_FOLDER"],
            "index_file": self.site.config["INDEX_FILE"],
        }

        # Things to ignore in listings
        ignored_extensions = (".pyc",)

        def render_listing(in_name, out_name, folders=[], files=[]):
            if in_name:
                with open(in_name, 'r') as fd:
                    try:
                        lexer = get_lexer_for_filename(in_name)
                    except:
                        lexer = TextLexer()
                    code = highlight(fd.read(), lexer,
                                     HtmlFormatter(cssclass='code',
                                                   linenos="table", nowrap=False,
                                                   lineanchors=utils.slugify(f),
                                                   anchorlinenos=True))
                title = os.path.basename(in_name)
            else:
                code = ''
                title = ''
            crumbs = utils.get_crumbs(os.path.relpath(out_name,
                                                      kw['output_folder']),
                                      is_file=True)
            context = {
                'code': code,
                'title': title,
                'crumbs': crumbs,
                'lang': kw['default_lang'],
                'folders': folders,
                'files': files,
                'description': title,
            }
            self.site.render_template('listing.tmpl', out_name,
                                      context)
        flag = True
        template_deps = self.site.template_system.template_deps('listing.tmpl')
        for root, dirs, files in os.walk(kw['listings_folder']):
            flag = False
            # Render all files
            out_name = os.path.join(
                kw['output_folder'],
                root, kw['index_file']
            )
            yield {
                'basename': self.name,
                'name': out_name,
                'file_dep': template_deps,
                'targets': [out_name],
                'actions': [(render_listing, [None, out_name, dirs, files])],
                # This is necessary to reflect changes in blog title,
                # sidebar links, etc.
                'uptodate': [utils.config_changed(
                    self.site.config['GLOBAL_CONTEXT'])],
                'clean': True,
            }
            for f in files:
                ext = os.path.splitext(f)[-1]
                if ext in ignored_extensions:
                    continue
                in_name = os.path.join(root, f)
                out_name = os.path.join(
                    kw['output_folder'],
                    root,
                    f) + '.html'
                yield {
                    'basename': self.name,
                    'name': out_name,
                    'file_dep': template_deps + [in_name],
                    'targets': [out_name],
                    'actions': [(render_listing, [in_name, out_name])],
                    # This is necessary to reflect changes in blog title,
                    # sidebar links, etc.
                    'uptodate': [utils.config_changed(
                        self.site.config['GLOBAL_CONTEXT'])],
                    'clean': True,
                }
        if flag:
            yield {
                'basename': self.name,
                'actions': [],
            }

[((3399, 3429), 'os.walk', 'os.walk', (["kw['listings_folder']"], {}), "(kw['listings_folder'])\n", (3406, 3429), False, 'import os\n'), ((3510, 3567), 'os.path.join', 'os.path.join', (["kw['output_folder']", 'root', "kw['index_file']"], {}), "(kw['output_folder'], root, kw['index_file'])\n", (3522, 3567), False, 'import os\n'), ((2582, 2607), 'os.path.basename', 'os.path.basename', (['in_name'], {}), '(in_name)\n', (2598, 2607), False, 'import os\n'), ((2717, 2763), 'os.path.relpath', 'os.path.relpath', (['out_name', "kw['output_folder']"], {}), "(out_name, kw['output_folder'])\n", (2732, 2763), False, 'import os\n'), ((4304, 4325), 'os.path.join', 'os.path.join', (['root', 'f'], {}), '(root, f)\n', (4316, 4325), False, 'import os\n'), ((4179, 4198), 'os.path.splitext', 'os.path.splitext', (['f'], {}), '(f)\n', (4195, 4198), False, 'import os\n'), ((4353, 4395), 'os.path.join', 'os.path.join', (["kw['output_folder']", 'root', 'f'], {}), "(kw['output_folder'], root, f)\n", (4365, 4395), False, 'import os\n'), ((2096, 2127), 'pygments.lexers.get_lexer_for_filename', 'get_lexer_for_filename', (['in_name'], {}), '(in_name)\n', (2118, 2127), False, 'from pygments.lexers import get_lexer_for_filename, TextLexer\n'), ((4004, 4060), 'nikola.utils.config_changed', 'utils.config_changed', (["self.site.config['GLOBAL_CONTEXT']"], {}), "(self.site.config['GLOBAL_CONTEXT'])\n", (4024, 4060), False, 'from nikola import utils\n'), ((2188, 2199), 'pygments.lexers.TextLexer', 'TextLexer', ([], {}), '()\n', (2197, 2199), False, 'from pygments.lexers import get_lexer_for_filename, TextLexer\n'), ((4895, 4951), 'nikola.utils.config_changed', 'utils.config_changed', (["self.site.config['GLOBAL_CONTEXT']"], {}), "(self.site.config['GLOBAL_CONTEXT'])\n", (4915, 4951), False, 'from nikola import utils\n'), ((2468, 2484), 'nikola.utils.slugify', 'utils.slugify', (['f'], {}), '(f)\n', (2481, 2484), False, 'from nikola import utils\n')]