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

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

      1  # Copyright (C) 2009, 2010, 2011  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 HelpBrowser in GUI.
     23
     24  Usage:
     25
     26      help_system = HelpSystem(...)
     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 HelpSystem(object):
     40
     41 -    def __init__(self, home_page, resources_prefix, page_prefix):
     42          self.pages = {}
     43          self.home_page = home_page
     44          self.resources_prefix = resources_prefix
     45          self.page_prefix = page_prefix
     46
     47 -    def install_page(self, id, header="", body="", related_pages=[]):
     48          self.pages[id] = HelpPage(self, id, header, body, related_pages)
     49
     50 -    def get_page(self, id):
     51          return self.pages.get(id, None)
     52
     53 -    def get_search_results_page(self, search):
     54          matches = self._get_pages_matching_search(search)
     55          # search
     56          tex = ""
     57          tex += "<ul>"
     58          for page in matches:
     59              tex += "<li>"
     60              tex += "<a href=\"%s%s\">%s</a>" % (self.page_prefix, page.id, page.header)
     61              tex += "</li>"
     62          tex += "</ul>"
     63          search_page_html = "<h1>%s</h1>%s" % (
     64              _("Search results for '%s'") % search,
     65              tex)
     66          return search_page_html
     67
     68 -    def _get_pages_matching_search(self, search):
     69          search_words = search.split(" ")
     70          content_res = [r"\b%s\b" % x for x in search_words]
     71          matches = []
     72          for page in self.pages.values():
     73              match = True
     74              for content_re in content_res:
     75                  if (not re.search(content_re, page.header, re.IGNORECASE) and
     76                      not re.search(content_re, page.body, re.IGNORECASE)):
     77                      match = False
     78                      break
     79              if match:
     80                  matches.append(page)
     81          return matches
     82
     83
     84 -class HelpPage(object):
     85
     86 -    def __init__(self, help_system, id, header, body, related_pages):
     87          self.help_system = help_system
     88          self.id = id
     89          self.header = header
     90          self.body = body
     91          self.related_pages = related_pages
     92
     93 -    def render_to_html(self):
     94          html = u"<h1>%s</h1>%s" % (self.header, markdown(self.body))
     95          # Change headers
     96          # Our link markup: Replace Help(foo) with proper link
     97          while True:
     98              match = re.search(r"Help\(([^)]+)\)", html)
     99              if match:
    100                  page = self.help_system.get_page(match.group(1))
    101                  replacement = "??"
    102                  if page:
    103                      replacement = "<a href=\"%s%s\">%s</a>" % (
    104                          self.help_system.page_prefix,
    105                          match.group(1), page.header)
    106                  html = html[0:match.start(0)] + replacement + \
    107                         html[match.end(0):]
    108              else:
    109                  break
    110          # Our link markup: Replace HelpFigure(foo) with proper image
    111          while True:
    112              match = re.search(r"HelpFigure\(([^)]+)\)", html)
    113              if match:
    114                  replacement = "<img src=\"%s%s.png\" border=\"0\">" % (
    115                      self.help_system.resources_prefix, match.group(1))
    116                  html = html[0:match.start(0)] + replacement + \
    117                         html[match.end(0):]
    118              else:
    119                  break
    120          # Related pages
    121          if self.related_pages:
    122              related_pages_html = "<h2>%s</h2>" % _("Related pages")
    123              related_pages_html += "<ul>"
    124              for page_id in self.related_pages:
    125                  page = self.help_system.get_page(page_id)
    126                  if page:
    127                      related_pages_html += "<li>"
    128                      related_pages_html += "<a href=\"%s%s\">%s</a>" % (
    129                          self.help_system.page_prefix, page.id, page.header)
    130                      related_pages_html += "</li>"
    131              related_pages_html += "</ul>"
    132              html = html + related_pages_html
    133          return html
    134