summaryrefslogtreecommitdiff
path: root/plugins/localsearch/localsearch/__init__.py
blob: fdc8874d27f4d2c483a4bcec546640aa5fe08fec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-

# Copyright © 2012-2014 Roberto Alsina and others.

# 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
import codecs
import json
import os

from nikola.plugin_categories import LateTask
from nikola.utils import apply_filters, config_changed, copy_tree, makedirs

# This is what we need to produce:
# var tipuesearch = {"pages": [
#     {"title": "Tipue Search, a jQuery site search engine", "text": "Tipue
#         Search is a site search engine jQuery plugin. It's free for both commercial and
#         non-commercial use and released under the MIT License. Tipue Search includes
#         features such as word stemming and word replacement.", "tags": "JavaScript",
#         "loc": "http://www.tipue.com/search"},
#     {"title": "Tipue Search demo", "text": "Tipue Search demo. Tipue Search is
#         a site search engine jQuery plugin.", "tags": "JavaScript", "loc":
#         "http://www.tipue.com/search/demo"},
#     {"title": "About Tipue", "text": "Tipue is a small web development/design
#         studio based in North London. We've been around for over a decade.", "tags": "",
#         "loc": "http://www.tipue.com/about"}
# ]};


class Tipue(LateTask):
    """Render the blog posts as JSON data."""

    name = "localsearch"

    def gen_tasks(self):
        self.site.scan_posts()

        kw = {
            "translations": self.site.config['TRANSLATIONS'],
            "output_folder": self.site.config['OUTPUT_FOLDER'],
            "filters": self.site.config['FILTERS'],
            "timeline": self.site.timeline,
        }

        posts = self.site.timeline[:]
        dst_path = os.path.join(kw["output_folder"], "assets", "js",
                                "tipuesearch_content.js")

        def save_data():
            pages = []
            for lang in kw["translations"]:
                for post in posts:
                    # Don't index drafts (Issue #387)
                    if post.is_draft or post.is_private or post.publish_later:
                        continue
                    text = post.text(lang, strip_html=True)
                    text = text.replace('^', '')

                    data = {}
                    data["title"] = post.title(lang)
                    data["text"] = text
                    data["tags"] = ",".join(post.tags)
                    data["url"] = post.permalink(lang, absolute=True)
                    pages.append(data)
            output = json.dumps({"pages": pages}, indent=2)
            output = 'var tipuesearch = ' + output + ';'
            makedirs(os.path.dirname(dst_path))
            with codecs.open(dst_path, "wb+", "utf8") as fd:
                fd.write(output)

        task = {
            "basename": str(self.name),
            "name": dst_path,
            "targets": [dst_path],
            "actions": [(save_data, [])],
            'uptodate': [config_changed(kw)],
            'calc_dep': ['_scan_locs:sitemap']
        }
        yield apply_filters(task, kw['filters'])

        # Copy all the assets to the right places
        asset_folder = os.path.join(os.path.dirname(__file__), "files")
        for task in copy_tree(asset_folder, kw["output_folder"]):
            task["basename"] = str(self.name)
            yield apply_filters(task, kw['filters'])