diff options
Diffstat (limited to 'plugins/localsearch/localsearch/__init__.py')
-rw-r--r-- | plugins/localsearch/localsearch/__init__.py | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/plugins/localsearch/localsearch/__init__.py b/plugins/localsearch/localsearch/__init__.py new file mode 100644 index 00000000..fdc8874d --- /dev/null +++ b/plugins/localsearch/localsearch/__init__.py | |||
@@ -0,0 +1,106 @@ | |||
1 | # -*- coding: utf-8 -*- | ||
2 | |||
3 | # Copyright © 2012-2014 Roberto Alsina and others. | ||
4 | |||
5 | # Permission is hereby granted, free of charge, to any | ||
6 | # person obtaining a copy of this software and associated | ||
7 | # documentation files (the "Software"), to deal in the | ||
8 | # Software without restriction, including without limitation | ||
9 | # the rights to use, copy, modify, merge, publish, | ||
10 | # distribute, sublicense, and/or sell copies of the | ||
11 | # Software, and to permit persons to whom the Software is | ||
12 | # furnished to do so, subject to the following conditions: | ||
13 | # | ||
14 | # The above copyright notice and this permission notice | ||
15 | # shall be included in all copies or substantial portions of | ||
16 | # the Software. | ||
17 | # | ||
18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY | ||
19 | # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
20 | # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
21 | # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS | ||
22 | # OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
23 | # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
24 | # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
25 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
26 | |||
27 | from __future__ import unicode_literals | ||
28 | import codecs | ||
29 | import json | ||
30 | import os | ||
31 | |||
32 | from nikola.plugin_categories import LateTask | ||
33 | from nikola.utils import apply_filters, config_changed, copy_tree, makedirs | ||
34 | |||
35 | # This is what we need to produce: | ||
36 | # var tipuesearch = {"pages": [ | ||
37 | # {"title": "Tipue Search, a jQuery site search engine", "text": "Tipue | ||
38 | # Search is a site search engine jQuery plugin. It's free for both commercial and | ||
39 | # non-commercial use and released under the MIT License. Tipue Search includes | ||
40 | # features such as word stemming and word replacement.", "tags": "JavaScript", | ||
41 | # "loc": "http://www.tipue.com/search"}, | ||
42 | # {"title": "Tipue Search demo", "text": "Tipue Search demo. Tipue Search is | ||
43 | # a site search engine jQuery plugin.", "tags": "JavaScript", "loc": | ||
44 | # "http://www.tipue.com/search/demo"}, | ||
45 | # {"title": "About Tipue", "text": "Tipue is a small web development/design | ||
46 | # studio based in North London. We've been around for over a decade.", "tags": "", | ||
47 | # "loc": "http://www.tipue.com/about"} | ||
48 | # ]}; | ||
49 | |||
50 | |||
51 | class Tipue(LateTask): | ||
52 | """Render the blog posts as JSON data.""" | ||
53 | |||
54 | name = "localsearch" | ||
55 | |||
56 | def gen_tasks(self): | ||
57 | self.site.scan_posts() | ||
58 | |||
59 | kw = { | ||
60 | "translations": self.site.config['TRANSLATIONS'], | ||
61 | "output_folder": self.site.config['OUTPUT_FOLDER'], | ||
62 | "filters": self.site.config['FILTERS'], | ||
63 | "timeline": self.site.timeline, | ||
64 | } | ||
65 | |||
66 | posts = self.site.timeline[:] | ||
67 | dst_path = os.path.join(kw["output_folder"], "assets", "js", | ||
68 | "tipuesearch_content.js") | ||
69 | |||
70 | def save_data(): | ||
71 | pages = [] | ||
72 | for lang in kw["translations"]: | ||
73 | for post in posts: | ||
74 | # Don't index drafts (Issue #387) | ||
75 | if post.is_draft or post.is_private or post.publish_later: | ||
76 | continue | ||
77 | text = post.text(lang, strip_html=True) | ||
78 | text = text.replace('^', '') | ||
79 | |||
80 | data = {} | ||
81 | data["title"] = post.title(lang) | ||
82 | data["text"] = text | ||
83 | data["tags"] = ",".join(post.tags) | ||
84 | data["url"] = post.permalink(lang, absolute=True) | ||
85 | pages.append(data) | ||
86 | output = json.dumps({"pages": pages}, indent=2) | ||
87 | output = 'var tipuesearch = ' + output + ';' | ||
88 | makedirs(os.path.dirname(dst_path)) | ||
89 | with codecs.open(dst_path, "wb+", "utf8") as fd: | ||
90 | fd.write(output) | ||
91 | |||
92 | task = { | ||
93 | "basename": str(self.name), | ||
94 | "name": dst_path, | ||
95 | "targets": [dst_path], | ||
96 | "actions": [(save_data, [])], | ||
97 | 'uptodate': [config_changed(kw)], | ||
98 | 'calc_dep': ['_scan_locs:sitemap'] | ||
99 | } | ||
100 | yield apply_filters(task, kw['filters']) | ||
101 | |||
102 | # Copy all the assets to the right places | ||
103 | asset_folder = os.path.join(os.path.dirname(__file__), "files") | ||
104 | for task in copy_tree(asset_folder, kw["output_folder"]): | ||
105 | task["basename"] = str(self.name) | ||
106 | yield apply_filters(task, kw['filters']) | ||