tipue_search.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tipue Search
  4. ============
  5. A Pelican plugin to serialize generated HTML to JSON
  6. that can be used by jQuery plugin - Tipue Search.
  7. Copyright (c) Talha Mansoor
  8. """
  9. from __future__ import unicode_literals
  10. import os.path
  11. import json
  12. from bs4 import BeautifulSoup
  13. from codecs import open
  14. from pelican import signals
  15. class Tipue_Search_JSON_Generator(object):
  16. def __init__(self, context, settings, path, theme, output_path, *null):
  17. self.output_path = output_path
  18. self.context = context
  19. self.siteurl = settings.get('SITEURL')
  20. self.json_nodes = []
  21. def create_json_node(self, page):
  22. if getattr(page, 'status', 'published') != 'published':
  23. return
  24. page_title = page.title
  25. soup = BeautifulSoup(page.content, 'html.parser')
  26. page_text = soup.get_text()
  27. if getattr(page, 'category') == 'None':
  28. page_category = ''
  29. else:
  30. page_category = page.category.name
  31. page_url = self.siteurl + '/' + page.url
  32. node = {'title': page_title,
  33. 'text': page_text,
  34. 'tags': page_category,
  35. 'loc': page_url}
  36. self.json_nodes.append(node)
  37. def generate_output(self, writer):
  38. path = os.path.join(self.output_path, 'tipuesearch_content.json')
  39. pages = self.context['pages'] + self.context['articles']
  40. for article in self.context['articles']:
  41. pages += article.translations
  42. for page in pages:
  43. self.create_json_node(page)
  44. root_node = {'pages': self.json_nodes}
  45. with open(path, 'w', encoding='utf-8') as fd:
  46. json.dump(root_node, fd, indent=4)
  47. def get_generators(generators):
  48. return Tipue_Search_JSON_Generator
  49. def register():
  50. signals.get_generators.connect(get_generators)