|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) [2024] [Diego Carrasco G.] |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +import os |
| 24 | +import json |
| 25 | +from nikola.plugin_categories import LateTask |
| 26 | +from nikola import utils |
| 27 | + |
| 28 | + |
| 29 | +class FlexSearchPlugin(LateTask): |
| 30 | + '''iterea sobre todos los post |
| 31 | + saca el titulo y el contenido del tituo y el url |
| 32 | + ''' |
| 33 | + name = "flexsearch_plugin" |
| 34 | + |
| 35 | + def set_site(self, site): |
| 36 | + super(FlexSearchPlugin, self).set_site(site) |
| 37 | + self.site = site |
| 38 | + site.register_path_handler('search_index', self.search_index_path) |
| 39 | + |
| 40 | + def gen_tasks(self): |
| 41 | + """Generate the search index after all posts are processed.""" |
| 42 | + self.site.scan_posts() |
| 43 | + yield self.group_task() |
| 44 | + |
| 45 | + output_path = self.site.config['OUTPUT_FOLDER'] |
| 46 | + index_file_path = os.path.join(output_path, 'assets', 'search_index.json') |
| 47 | + |
| 48 | + def build_index(): |
| 49 | + """Build the entire search index from scratch.""" |
| 50 | + index = {} |
| 51 | + for post in self.site.timeline: |
| 52 | + # Sasha: Modifico esta linea para que considere todas las entradas bajo /pages |
| 53 | + if not post.is_draft: |
| 54 | + index[post.meta('slug')] = { |
| 55 | + 'title': post.title(), |
| 56 | + 'content': post.text(strip_html=True), |
| 57 | + 'url': post.permalink(absolute=False) |
| 58 | + } |
| 59 | + with open(index_file_path, 'w', encoding='utf-8') as f: |
| 60 | + json.dump(index, f, ensure_ascii=False) |
| 61 | + |
| 62 | + task = { |
| 63 | + 'basename': self.name, |
| 64 | + 'name': 'all_posts', |
| 65 | + 'actions': [build_index], |
| 66 | + 'targets': [index_file_path], |
| 67 | + 'uptodate': [utils.config_changed({1: self.site.GLOBAL_CONTEXT})], |
| 68 | + 'clean': True, |
| 69 | + } |
| 70 | + yield task |
| 71 | + |
| 72 | + def search_index_path(self, name, lang): |
| 73 | + return [os.path.join(self.site.config['BASE_URL'], 'search_index.json'), None] |
0 commit comments