tipue_search.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. try:
  15. from urlparse import urljoin
  16. except ImportError:
  17. from urllib.parse import urljoin
  18. from pelican import signals
  19. class Tipue_Search_JSON_Generator(object):
  20. def __init__(self, context, settings, path, theme, output_path, *null):
  21. self.output_path = output_path
  22. self.context = context
  23. self.siteurl = settings.get('SITEURL')
  24. self.tpages = settings.get('TEMPLATE_PAGES')
  25. self.output_path = output_path
  26. self.json_nodes = []
  27. def create_json_node(self, page):
  28. if getattr(page, 'status', 'published') != 'published':
  29. return
  30. soup_title = BeautifulSoup(page.title.replace(' ', ' '), 'html.parser')
  31. page_title = soup_title.get_text(' ', strip=True).replace('“', '"').replace('”', '"').replace('’', "'").replace('^', '^')
  32. soup_text = BeautifulSoup(page.content, 'html.parser')
  33. page_text = soup_text.get_text(' ', strip=True).replace('“', '"').replace('”', '"').replace('’', "'").replace('¶', ' ').replace('^', '^')
  34. page_text = ' '.join(page_text.split())
  35. page_category = page.category.name if getattr(page, 'category', 'None') != 'None' else ''
  36. page_url = page.url if page.url else '.'
  37. node = {'title': page_title,
  38. 'text': page_text,
  39. 'tags': page_category,
  40. 'url': page_url}
  41. self.json_nodes.append(node)
  42. def create_tpage_node(self, srclink):
  43. srcfile = open(os.path.join(self.output_path, self.tpages[srclink]), encoding='utf-8')
  44. soup = BeautifulSoup(srcfile, 'html.parser')
  45. page_title = soup.title.string if soup.title is not None else ''
  46. page_text = soup.get_text()
  47. # Should set default category?
  48. page_category = ''
  49. page_url = urljoin(self.siteurl, self.tpages[srclink])
  50. node = {'title': page_title,
  51. 'text': page_text,
  52. 'tags': page_category,
  53. 'url': page_url}
  54. self.json_nodes.append(node)
  55. def generate_output(self, writer):
  56. path = os.path.join(self.output_path, 'tipuesearch_content.json')
  57. pages = self.context['pages'] + self.context['articles']
  58. for article in self.context['articles']:
  59. pages += article.translations
  60. for srclink in self.tpages:
  61. self.create_tpage_node(srclink)
  62. for page in pages:
  63. self.create_json_node(page)
  64. root_node = {'pages': self.json_nodes}
  65. with open(path, 'w', encoding='utf-8') as fd:
  66. json.dump(root_node, fd, separators=(',', ':'), ensure_ascii=False)
  67. def get_generators(generators):
  68. return Tipue_Search_JSON_Generator
  69. def register():
  70. signals.get_generators.connect(get_generators)