docs_prebuild 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. """Script that implements some prebuild tasks needed by target docs.prebuild
  4. """
  5. import sys
  6. import os.path
  7. import time
  8. from contextlib import contextmanager
  9. from searx import settings, get_setting, locales
  10. from searx.infopage import InfoPageSet, InfoPage
  11. _doc_user = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'docs', 'user'))
  12. def main():
  13. locales.locales_initialize()
  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', encoding='utf-8') as f:
  23. f.write(page.content)
  24. class OfflinePage(InfoPage): # pylint: disable=missing-class-docstring
  25. def get_ctx(self):
  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 # pylint: disable=import-outside-toplevel
  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, that's
  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. if __name__ == '__main__':
  59. sys.exit(main())