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