__init__.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Render SearXNG instance documentation.
  3. Usage in a Flask app route:
  4. .. code:: python
  5. from searx import infopage
  6. _INFO_PAGES = infopage.InfoPageSet(infopage.MistletoePage)
  7. @app.route('/info/<pagename>', methods=['GET'])
  8. def info(pagename):
  9. locale = request.preferences.get_value('locale')
  10. page = _INFO_PAGES.get_page(pagename, locale)
  11. """
  12. __all__ = ['InfoPage', 'InfoPageSet']
  13. import os
  14. import os.path
  15. import logging
  16. import typing
  17. import urllib.parse
  18. from functools import cached_property
  19. import jinja2
  20. from flask.helpers import url_for
  21. from markdown_it import MarkdownIt
  22. from .. import get_setting
  23. from ..version import GIT_URL
  24. from ..locales import LOCALE_NAMES
  25. logger = logging.getLogger('searx.infopage')
  26. _INFO_FOLDER = os.path.abspath(os.path.dirname(__file__))
  27. class InfoPage:
  28. """A page of the :py:obj:`online documentation <InfoPageSet>`."""
  29. def __init__(self, fname):
  30. self.fname = fname
  31. @cached_property
  32. def raw_content(self):
  33. """Raw content of the page (without any jinja rendering)"""
  34. with open(self.fname, 'r', encoding='utf-8') as f:
  35. return f.read()
  36. @cached_property
  37. def content(self):
  38. """Content of the page (rendered in a Jinja context)"""
  39. ctx = self.get_ctx()
  40. template = jinja2.Environment().from_string(self.raw_content)
  41. return template.render(**ctx)
  42. @cached_property
  43. def title(self):
  44. """Title of the content (without any markup)"""
  45. t = ""
  46. for l in self.raw_content.split('\n'):
  47. if l.startswith('# '):
  48. t = l.strip('# ')
  49. return t
  50. @cached_property
  51. def html(self):
  52. """Render Markdown (CommonMark_) to HTML by using markdown-it-py_.
  53. .. _CommonMark: https://commonmark.org/
  54. .. _markdown-it-py: https://github.com/executablebooks/markdown-it-py
  55. """
  56. return (
  57. MarkdownIt("commonmark", {"typographer": True}).enable(["replacements", "smartquotes"]).render(self.content)
  58. )
  59. def get_ctx(self):
  60. """Jinja context to render :py:obj:`InfoPage.content`"""
  61. def _md_link(name, url):
  62. url = url_for(url, _external=True)
  63. return "[%s](%s)" % (name, url)
  64. def _md_search(query):
  65. url = '%s?q=%s' % (url_for('search', _external=True), urllib.parse.quote(query))
  66. return '[%s](%s)' % (query, url)
  67. ctx = {}
  68. ctx['GIT_URL'] = GIT_URL
  69. ctx['get_setting'] = get_setting
  70. ctx['link'] = _md_link
  71. ctx['search'] = _md_search
  72. return ctx
  73. def __repr__(self):
  74. return f'<{self.__class__.__name__} fname={self.fname!r}>'
  75. class InfoPageSet: # pylint: disable=too-few-public-methods
  76. """Cached rendering of the online documentation a SearXNG instance has.
  77. :param page_class: render online documentation by :py:obj:`InfoPage` parser.
  78. :type page_class: :py:obj:`InfoPage`
  79. :param info_folder: information directory
  80. :type info_folder: str
  81. """
  82. def __init__(
  83. self, page_class: typing.Optional[typing.Type[InfoPage]] = None, info_folder: typing.Optional[str] = None
  84. ):
  85. self.page_class = page_class or InfoPage
  86. self.folder: str = info_folder or _INFO_FOLDER
  87. """location of the Markdown files"""
  88. self.CACHE: typing.Dict[tuple, typing.Optional[InfoPage]] = {}
  89. self.locale_default: str = 'en'
  90. """default language"""
  91. self.locales: typing.List[str] = [
  92. locale.replace('_', '-') for locale in os.listdir(_INFO_FOLDER) if locale.replace('_', '-') in LOCALE_NAMES
  93. ]
  94. """list of supported languages (aka locales)"""
  95. self.toc: typing.List[str] = [
  96. 'search-syntax',
  97. 'about',
  98. 'donate',
  99. ]
  100. """list of articles in the online documentation"""
  101. def get_page(self, pagename: str, locale: typing.Optional[str] = None):
  102. """Return ``pagename`` instance of :py:obj:`InfoPage`
  103. :param pagename: name of the page, a value from :py:obj:`InfoPageSet.toc`
  104. :type pagename: str
  105. :param locale: language of the page, e.g. ``en``, ``zh_Hans_CN``
  106. (default: :py:obj:`InfoPageSet.i18n_origin`)
  107. :type locale: str
  108. """
  109. locale = locale or self.locale_default
  110. if pagename not in self.toc:
  111. return None
  112. if locale not in self.locales:
  113. return None
  114. cache_key = (pagename, locale)
  115. if cache_key in self.CACHE:
  116. return self.CACHE[cache_key]
  117. # not yet instantiated
  118. fname = os.path.join(self.folder, locale.replace('-', '_'), pagename) + '.md'
  119. if not os.path.exists(fname):
  120. logger.info('file %s does not exists', fname)
  121. self.CACHE[cache_key] = None
  122. return None
  123. page = self.page_class(fname)
  124. self.CACHE[cache_key] = page
  125. return page
  126. def iter_pages(self, locale: typing.Optional[str] = None, fallback_to_default=False):
  127. """Iterate over all pages of the TOC"""
  128. locale = locale or self.locale_default
  129. for page_name in self.toc:
  130. page_locale = locale
  131. page = self.get_page(page_name, locale)
  132. if fallback_to_default and page is None:
  133. page_locale = self.locale_default
  134. page = self.get_page(page_name, self.locale_default)
  135. if page is not None:
  136. # page is None if the page was deleted by the administrator
  137. yield page_name, page_locale, page