cubicweb/web/views/wdoc.py
branch3.26
changeset 12330 6e620df7d4c3
parent 12329 d0bf06146782
child 12331 3cddf6a5473a
equal deleted inserted replaced
12329:d0bf06146782 12330:6e620df7d4c3
    21 """
    21 """
    22 
    22 
    23 
    23 
    24 from itertools import chain
    24 from itertools import chain
    25 from os.path import join
    25 from os.path import join
    26 from bisect import bisect_right
       
    27 from datetime import date
       
    28 
    26 
    29 from six import text_type
    27 from six import text_type
    30 
    28 
    31 from logilab.common.changelog import ChangeLog
       
    32 from logilab.common.date import strptime, todate
       
    33 from logilab.common.registry import yes
    29 from logilab.common.registry import yes
    34 from logilab.mtconverter import CHARSET_DECL_RGX
       
    35 
    30 
    36 from cubicweb.predicates import match_form_params
    31 from cubicweb.predicates import match_form_params
    37 from cubicweb.view import StartupView
    32 from cubicweb.view import StartupView
    38 from cubicweb.uilib import rest_publish
    33 from cubicweb.uilib import rest_publish
    39 from cubicweb.web import NotFound, action
    34 from cubicweb.web import NotFound, action
    44 try:
    39 try:
    45     from xml.etree.ElementTree import parse
    40     from xml.etree.ElementTree import parse
    46 except ImportError:
    41 except ImportError:
    47     from elementtree.ElementTree import parse
    42     from elementtree.ElementTree import parse
    48 
    43 
       
    44 
    49 def build_toc_index(node, index):
    45 def build_toc_index(node, index):
    50     try:
    46     try:
    51         nodeidx = node.attrib['resource']
    47         nodeidx = node.attrib['resource']
    52         assert not nodeidx in index, nodeidx
    48         assert nodeidx not in index, nodeidx
    53         index[nodeidx] = node
    49         index[nodeidx] = node
    54     except KeyError:
    50     except KeyError:
    55         pass
    51         pass
    56     for child in node:
    52     for child in node:
    57         build_toc_index(child, index)
    53         build_toc_index(child, index)
    58         child.parent = node
    54         child.parent = node
       
    55 
    59 
    56 
    60 def get_insertion_point(section, index):
    57 def get_insertion_point(section, index):
    61     if section.attrib.get('insertafter'):
    58     if section.attrib.get('insertafter'):
    62         snode = index[section.attrib['insertafter']]
    59         snode = index[section.attrib['insertafter']]
    63         node = snode.parent
    60         node = snode.parent
    70         node = index[section.attrib['appendto']]
    67         node = index[section.attrib['appendto']]
    71         idx = None
    68         idx = None
    72     else:
    69     else:
    73         node, idx = None, None
    70         node, idx = None, None
    74     return node, idx
    71     return node, idx
       
    72 
    75 
    73 
    76 def build_toc(config):
    74 def build_toc(config):
    77     alltocfiles = reversed(tuple(config.locate_all_files('toc.xml')))
    75     alltocfiles = reversed(tuple(config.locate_all_files('toc.xml')))
    78     maintoc = parse(next(alltocfiles)).getroot()
    76     maintoc = parse(next(alltocfiles)).getroot()
    79     maintoc.parent = None
    77     maintoc.parent = None
    93                 node.insert(idx, section)
    91                 node.insert(idx, section)
    94             section.parent = node
    92             section.parent = node
    95             build_toc_index(section, index)
    93             build_toc_index(section, index)
    96     return index
    94     return index
    97 
    95 
       
    96 
    98 def title_for_lang(node, lang):
    97 def title_for_lang(node, lang):
    99     fallback_title = None
    98     fallback_title = None
   100     for title in node.findall('title'):
    99     for title in node.findall('title'):
   101         title_lang = title.attrib['{http://www.w3.org/XML/1998/namespace}lang']
   100         title_lang = title.attrib['{http://www.w3.org/XML/1998/namespace}lang']
   102         if title_lang == lang:
   101         if title_lang == lang:
   103             return text_type(title.text)
   102             return text_type(title.text)
   104         if title_lang == 'en':
   103         if title_lang == 'en':
   105             fallback_title = text_type(title.text)
   104             fallback_title = text_type(title.text)
   106     return fallback_title
   105     return fallback_title
   107 
   106 
       
   107 
   108 def subsections(node):
   108 def subsections(node):
   109     return [child for child in node if child.tag == 'section']
   109     return [child for child in node if child.tag == 'section']
   110 
   110 
   111 # help views ##################################################################
   111 # help views ##################################################################
       
   112 
   112 
   113 
   113 class InlineHelpView(StartupView):
   114 class InlineHelpView(StartupView):
   114     __select__ = match_form_params('fid')
   115     __select__ = match_form_params('fid')
   115     __regid__ = 'wdoc'
   116     __regid__ = 'wdoc'
   116     title = _('site documentation')
   117     title = _('site documentation')
   141             self.subsections_links(node)
   142             self.subsections_links(node)
   142             self.w(u'<div class="hr"></div>')
   143             self.w(u'<div class="hr"></div>')
   143             self.navigation_links(node)
   144             self.navigation_links(node)
   144 
   145 
   145     def navigation_links(self, node):
   146     def navigation_links(self, node):
   146         req = self._cw
       
   147         parent = node.parent
   147         parent = node.parent
   148         if parent is None:
   148         if parent is None:
   149             return
   149             return
   150         brothers = subsections(parent)
   150         brothers = subsections(parent)
   151         self.w(u'<div class="docnav">\n')
   151         self.w(u'<div class="docnav">\n')
   165     def navsection(self, node, navtype):
   165     def navsection(self, node, navtype):
   166         htmlclass, imgpath, msgid = self.navinfo[navtype]
   166         htmlclass, imgpath, msgid = self.navinfo[navtype]
   167         self.w(u'<span class="%s">' % htmlclass)
   167         self.w(u'<span class="%s">' % htmlclass)
   168         self.w(u'%s : ' % self._cw._(msgid))
   168         self.w(u'%s : ' % self._cw._(msgid))
   169         self.w(u'<a href="%s">%s</a>' % (
   169         self.w(u'<a href="%s">%s</a>' % (
   170             self._cw.build_url('doc/'+node.attrib['resource']),
   170             self._cw.build_url('doc/' + node.attrib['resource']),
   171             title_for_lang(node, self._cw.lang)))
   171             title_for_lang(node, self._cw.lang)))
   172         self.w(u'</span>\n')
   172         self.w(u'</span>\n')
   173 
   173 
   174     def subsections_links(self, node, first=True):
   174     def subsections_links(self, node, first=True):
   175         sub = subsections(node)
   175         sub = subsections(node)
   178         if first:
   178         if first:
   179             self.w(u'<div class="hr"></div>')
   179             self.w(u'<div class="hr"></div>')
   180         self.w(u'<ul class="docsum">')
   180         self.w(u'<ul class="docsum">')
   181         for child in sub:
   181         for child in sub:
   182             self.w(u'<li><a href="%s">%s</a>' % (
   182             self.w(u'<li><a href="%s">%s</a>' % (
   183                 self._cw.build_url('doc/'+child.attrib['resource']),
   183                 self._cw.build_url('doc/' + child.attrib['resource']),
   184                 title_for_lang(child, self._cw.lang)))
   184                 title_for_lang(child, self._cw.lang)))
   185             self.subsections_links(child, False)
   185             self.subsections_links(child, False)
   186             self.w(u'</li>')
   186             self.w(u'</li>')
   187         self.w(u'</ul>\n')
   187         self.w(u'</ul>\n')
   188 
       
   189 
   188 
   190 
   189 
   191 class InlineHelpImageView(StartupView):
   190 class InlineHelpImageView(StartupView):
   192     __regid__ = 'wdocimages'
   191     __regid__ = 'wdocimages'
   193     __select__ = match_form_params('fid')
   192     __select__ = match_form_params('fid')
   206         else:
   205         else:
   207             raise NotFound
   206             raise NotFound
   208         self.w(open(join(resourcedir, rid)).read())
   207         self.w(open(join(resourcedir, rid)).read())
   209 
   208 
   210 
   209 
   211 
       
   212 class HelpAction(action.Action):
   210 class HelpAction(action.Action):
   213     __regid__ = 'help'
   211     __regid__ = 'help'
   214     __select__ = yes()
   212     __select__ = yes()
   215 
   213 
   216     category = 'footer'
   214     category = 'footer'