docs_prebuild 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. # lint: pylint
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. """Script that implements some prebuild tasks needed by target docs.prebuild
  5. """
  6. import sys
  7. import os.path
  8. import time
  9. from contextlib import contextmanager
  10. from searx import settings, get_setting, locales
  11. from searx.infopage import InfoPageSet, InfoPage
  12. _doc_user = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'docs', 'user'))
  13. def main():
  14. locales.locales_initialize()
  15. base_url = get_setting('server.base_url', None)
  16. if base_url:
  17. infopageset_ctx = _instance_infosetset_ctx(base_url)
  18. else:
  19. infopageset_ctx = _offline_infosetset_ctx()
  20. with infopageset_ctx as infopageset:
  21. for _, _, page in infopageset.iter_pages('en'):
  22. fname = os.path.join(_doc_user, os.path.basename(page.fname))
  23. with open(fname, 'w') as f:
  24. f.write(page.content)
  25. class OfflinePage(InfoPage):
  26. def get_ctx(self): # pylint: disable=no-self-use
  27. """Jinja context to render :py:obj:`DocPage.content` for offline purpose (no
  28. links to SearXNG instance)"""
  29. ctx = super().get_ctx()
  30. ctx['link'] = lambda name, url: '`%s`' % name
  31. ctx['search'] = lambda query: '`%s`' % query
  32. return ctx
  33. @contextmanager
  34. def _offline_infosetset_ctx():
  35. yield InfoPageSet(OfflinePage)
  36. @contextmanager
  37. def _instance_infosetset_ctx(base_url):
  38. # The url_for functions in the jinja templates need all routes to be
  39. # registered in the Flask app.
  40. settings['server']['secret_key'] = ''
  41. from searx.webapp import app
  42. # Specify base_url so that url_for() works for base_urls. If base_url is
  43. # specified, then these values from are given preference over any Flask's
  44. # generics (see flaskfix.py).
  45. with app.test_request_context(base_url=base_url):
  46. yield InfoPageSet()
  47. # The searx.webapp import from above fires some HTTP requests, that's
  48. # why we get a RuntimeError::
  49. #
  50. # RuntimeError: The connection pool was closed while 1 HTTP \
  51. # requests/responses were still in-flight.
  52. #
  53. # Closing network won't help ..
  54. # from searx.network import network
  55. # network.done()
  56. # waiting some seconds before ending the comand line was the only solution I
  57. # found ..
  58. time.sleep(3)
  59. if __name__ == '__main__':
  60. sys.exit(main())