Package Gnumed :: Package timelinelib :: Package help :: Module helppagerepository |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.help.helppagerepository

      1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  """
     20  A wiki-like help system.
     21
     22  Used by HelpBrowserFrame in GUI.
     23
     24  Usage:
     25
     26      help_system = HelpPageRepository(...)
     27      help_system.install_page(...)
     28      help_system.install_page(...)
     29      help_system.install_page(...)
     30      page = help_system.get_page(id)
     31      html = page.render_to_html()
     32  """
     33
     34
     35  import re
     36  from markdown import markdown
     37
     38
     39 -class HelpPageRepository(object):
     40
     41 -    def __init__(self, home_page, help_resources_root_dir, page_prefix):
     42          self.help_pages = {}
     43          self.home_page = home_page
     44          self.help_resources_root_dir = help_resources_root_dir
     45          self.page_prefix = page_prefix
     46
     47 -    def install_page(self, page_id, header, body, related_pages=[]):
     48          self.help_pages[page_id] = HelpPage(self, page_id, header, body, related_pages)
     49
     50 -    def get_page(self, page_id):
     51          return self.help_pages.get(page_id, None)
     52
     53 -    def get_search_results_page(self, target_words):
     54          found_pages = self._find_pages(target_words)
     55          return self._render_search_result_page(target_words, found_pages)
     56
     57 -    def _render_search_result_page(self, target_words, found_pages):
     58          tex = ""
     59          tex += "<ul>"
     60          for page in found_pages:
     61              tex += "<li>"
     62              tex += "<a href=\"%s%s\">%s</a>" % (self.page_prefix, page.page_id, page.header)
     63              tex += "</li>"
     64          tex += "</ul>"
     65          search_page_html = "<h1>%s</h1>%s" % (
     66              _("Search results for '%s'") % target_words,
     67              tex)
     68          return search_page_html
     69
     70 -    def _find_pages(self, target_words):
     71          search_words = [r"\b%s\b" % x for x in target_words.split()]
     72          pages = []
     73          for help_page in self.help_pages.values():
     74              match = True
     75              for search_word in search_words:
     76                  if (not re.search(search_word, help_page.header, re.IGNORECASE) and
     77                     not re.search(search_word, help_page.body, re.IGNORECASE)):
     78                      match = False
     79                      break
     80              if match:
     81                  pages.append(help_page)
     82          return pages
     83
     84
     85 -class HelpPage(object):
     86
     87 -    def __init__(self, help_page_repository, page_id, header, body, related_pages):
     88          self.help_page_repository = help_page_repository
     89          self.page_id = page_id
     90          self.header = header
     91          self.body = body
     92          self.related_pages = related_pages
     93
     94 -    def render_to_html(self):
     95          html = u"<h1>%s</h1>%s" % (self.header, markdown(self.body))
     96          # Change headers
     97          html = self._replace_help_placeholder_with_proper_link(html)
     98          html = self._replace_helpfigure_placeholder_with_proper_image(html)
     99          html = self._render_related_pages(html)
    100          return html
    101
    102 -    def _replace_help_placeholder_with_proper_link(self, html):
    103          # Our link markup: Replace Help(foo) with proper link
    104          while True:
    105              match = re.search(r"Help\(([^)]+)\)", html)
    106              if match:
    107                  page = self.help_page_repository.get_page(match.group(1))
    108                  replacement = "??"
    109                  if page:
    110                      replacement = "<a href=\"%s%s\">%s</a>" % (
    111                          self.help_page_repository.page_prefix,
    112                          match.group(1), page.header)
    113                  html = html[0:match.start(0)] + replacement + html[match.end(0):]
    114              else:
    115                  break
    116          return html
    117
    118 -    def _replace_helpfigure_placeholder_with_proper_image(self, html):
    119          # Our link markup: Replace HelpFigure(foo) with proper image
    120          while True:
    121              match = re.search(r"HelpFigure\(([^)]+)\)", html)
    122              if match:
    123                  replacement = "<img src=\"%s%s.png\" border=\"0\">" % (
    124                      self.help_page_repository.resources_prefix, match.group(1))
    125                  html = html[0:match.start(0)] + replacement + html[match.end(0):]
    126              else:
    127                  break
    128          return html
    129
    130 -    def _render_related_pages(self, html):
    131          if self.related_pages:
    132              related_pages_html = "<h2>%s</h2>" % _("Related pages")
    133              related_pages_html += "<ul>"
    134              for page_id in self.related_pages:
    135                  page = self.help_page_repository.get_page(page_id)
    136                  if page:
    137                      related_pages_html += "<li>"
    138                      related_pages_html += "<a href=\"%s%s\">%s</a>" % (
    139                          self.help_page_repository.page_prefix, page.page_id, page.header)
    140                      related_pages_html += "</li>"
    141              related_pages_html += "</ul>"
    142              html = html + related_pages_html
    143          return html
    144