__init__.py 5.8 KB

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