tipue_search.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(' ', ' '))
  31. page_title = soup_title.get_text(' ', strip=True).replace('“', '"').replace('”', '"').replace('’', "'").replace('^', '^')
  32. soup_text = BeautifulSoup(page.content)
  33. page_text = soup_text.get_text(' ', strip=True).replace('“', '"').replace('”', '"').replace('’', "'").replace('¶', ' ').replace('^', '^')
  34. page_text = ' '.join(page_text.split())
  35. if getattr(page, 'category', 'None') == 'None':
  36. page_category = ''
  37. else:
  38. page_category = page.category.name
  39. page_url = self.siteurl + '/' + page.url
  40. node = {'title': page_title,
  41. 'text': page_text,
  42. 'tags': page_category,
  43. 'url': page_url}
  44. self.json_nodes.append(node)
  45. def create_tpage_node(self, srclink):
  46. srcfile = open(os.path.join(self.output_path, self.tpages[srclink]))
  47. soup = BeautifulSoup(srcfile, 'html.parser')
  48. page_text = soup.get_text()
  49. # What happens if there is not a title.
  50. if soup.title is not None:
  51. page_title = soup.title.string
  52. else:
  53. page_title = ''
  54. # Should set default category?
  55. page_category = ''
  56. page_url = urljoin(self.siteurl, self.tpages[srclink])
  57. node = {'title': page_title,
  58. 'text': page_text,
  59. 'tags': page_category,
  60. 'loc': page_url}
  61. self.json_nodes.append(node)
  62. def generate_output(self, writer):
  63. path = os.path.join(self.output_path, 'tipuesearch_content.json')
  64. pages = self.context['pages'] + self.context['articles']
  65. for article in self.context['articles']:
  66. pages += article.translations
  67. for srclink in self.tpages:
  68. self.create_tpage_node(srclink)
  69. for page in pages:
  70. self.create_json_node(page)
  71. root_node = {'pages': self.json_nodes}
  72. with open(path, 'w', encoding='utf-8') as fd:
  73. json.dump(root_node, fd, separators=(',', ':'), ensure_ascii=False)
  74. def get_generators(generators):
  75. return Tipue_Search_JSON_Generator
  76. def register():
  77. signals.get_generators.connect(get_generators)