web/views/wdoc.py
branchtls-sprint
changeset 1802 d628defebc17
parent 1364 3acc823121b6
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
    51         idx = node.getchildren().index(snode)
    51         idx = node.getchildren().index(snode)
    52     else:
    52     else:
    53         node = index[section.attrib['appendto']]
    53         node = index[section.attrib['appendto']]
    54         idx = None
    54         idx = None
    55     return node, idx
    55     return node, idx
    56                      
    56 
    57 def build_toc(config):
    57 def build_toc(config):
    58     alltocfiles = reversed(tuple(config.locate_all_files('toc.xml')))
    58     alltocfiles = reversed(tuple(config.locate_all_files('toc.xml')))
    59     maintoc = parse(alltocfiles.next()).getroot()
    59     maintoc = parse(alltocfiles.next()).getroot()
    60     maintoc.parent = None
    60     maintoc.parent = None
    61     index = {}
    61     index = {}
    62     build_toc_index(maintoc, index)
    62     build_toc_index(maintoc, index)
    63     # insert component documentation into the tree according to their toc.xml
    63     # insert component documentation into the tree according to their toc.xml
    64     # file 
    64     # file
    65     for fpath in alltocfiles:
    65     for fpath in alltocfiles:
    66         toc = parse(fpath).getroot()
    66         toc = parse(fpath).getroot()
    67         for section in toc:
    67         for section in toc:
    68             node, idx = get_insertion_point(section, index)
    68             node, idx = get_insertion_point(section, index)
    69             if idx is None:
    69             if idx is None:
    71             else:
    71             else:
    72                 node.insert(idx, section)
    72                 node.insert(idx, section)
    73             section.parent = node
    73             section.parent = node
    74             build_toc_index(section, index)
    74             build_toc_index(section, index)
    75     return index
    75     return index
    76     
    76 
    77 def title_for_lang(node, lang):
    77 def title_for_lang(node, lang):
    78     for title in node.findall('title'):
    78     for title in node.findall('title'):
    79         if title.attrib['{http://www.w3.org/XML/1998/namespace}lang'] == lang:
    79         if title.attrib['{http://www.w3.org/XML/1998/namespace}lang'] == lang:
    80             return unicode(title.text)
    80             return unicode(title.text)
    81 
    81 
    86 
    86 
    87 class InlineHelpView(StartupView):
    87 class InlineHelpView(StartupView):
    88     __select__ = match_form_params('fid')
    88     __select__ = match_form_params('fid')
    89     id = 'wdoc'
    89     id = 'wdoc'
    90     title = _('site documentation')
    90     title = _('site documentation')
    91     
    91 
    92     def call(self):
    92     def call(self):
    93         fid = self.req.form['fid']
    93         fid = self.req.form['fid']
    94         for lang in chain((self.req.lang, self.vreg.property_value('ui.language')),
    94         for lang in chain((self.req.lang, self.vreg.property_value('ui.language')),
    95                           self.config.available_languages()):
    95                           self.config.available_languages()):
    96             rid = '%s_%s.rst' % (fid, lang)
    96             rid = '%s_%s.rst' % (fid, lang)
   105         except KeyError:
   105         except KeyError:
   106             node = None
   106             node = None
   107         else:
   107         else:
   108             self.navigation_links(node)
   108             self.navigation_links(node)
   109             self.w(u'<div class="hr"></div>')
   109             self.w(u'<div class="hr"></div>')
   110             self.w(u'<h1>%s</h1>' % (title_for_lang(node, self.req.lang)))            
   110             self.w(u'<h1>%s</h1>' % (title_for_lang(node, self.req.lang)))
   111         data = open(join(resourcedir, rid)).read()
   111         data = open(join(resourcedir, rid)).read()
   112         self.w(rest_publish(self, data))
   112         self.w(rest_publish(self, data))
   113         if node is not None:
   113         if node is not None:
   114             self.subsections_links(node)
   114             self.subsections_links(node)
   115             self.w(u'<div class="hr"></div>')
   115             self.w(u'<div class="hr"></div>')
   122             return
   122             return
   123         brothers = subsections(parent)
   123         brothers = subsections(parent)
   124         self.w(u'<div class="docnav">\n')
   124         self.w(u'<div class="docnav">\n')
   125         previousidx = brothers.index(node) - 1
   125         previousidx = brothers.index(node) - 1
   126         if previousidx >= 0:
   126         if previousidx >= 0:
   127             self.navsection(brothers[previousidx], 'prev')            
   127             self.navsection(brothers[previousidx], 'prev')
   128         self.navsection(parent, 'up')            
   128         self.navsection(parent, 'up')
   129         nextidx = brothers.index(node) + 1
   129         nextidx = brothers.index(node) + 1
   130         if nextidx < len(brothers):
   130         if nextidx < len(brothers):
   131             self.navsection(brothers[nextidx], 'next')            
   131             self.navsection(brothers[nextidx], 'next')
   132         self.w(u'</div>\n')
   132         self.w(u'</div>\n')
   133 
   133 
   134     navinfo = {'prev': ('', 'data/previous.png', _('i18nprevnext_previous')),
   134     navinfo = {'prev': ('', 'data/previous.png', _('i18nprevnext_previous')),
   135                'next': ('', 'data/next.png', _('i18nprevnext_next')),
   135                'next': ('', 'data/next.png', _('i18nprevnext_next')),
   136                'up': ('', 'data/up.png', _('i18nprevnext_up'))}
   136                'up': ('', 'data/up.png', _('i18nprevnext_up'))}
   137                
   137 
   138     def navsection(self, node, navtype):
   138     def navsection(self, node, navtype):
   139         htmlclass, imgpath, msgid = self.navinfo[navtype]
   139         htmlclass, imgpath, msgid = self.navinfo[navtype]
   140         self.w(u'<span class="%s">' % htmlclass)
   140         self.w(u'<span class="%s">' % htmlclass)
   141         self.w(u'%s : ' % self.req._(msgid))
   141         self.w(u'%s : ' % self.req._(msgid))
   142         self.w(u'<a href="%s">%s</a>' % (
   142         self.w(u'<a href="%s">%s</a>' % (
   143             self.req.build_url('doc/'+node.attrib['resource']),
   143             self.req.build_url('doc/'+node.attrib['resource']),
   144             title_for_lang(node, self.req.lang)))
   144             title_for_lang(node, self.req.lang)))
   145         self.w(u'</span>\n')
   145         self.w(u'</span>\n')
   146         
   146 
   147     def subsections_links(self, node, first=True):
   147     def subsections_links(self, node, first=True):
   148         sub = subsections(node)
   148         sub = subsections(node)
   149         if not sub:
   149         if not sub:
   150             return
   150             return
   151         if first:
   151         if first:
   156                 self.req.build_url('doc/'+child.attrib['resource']),
   156                 self.req.build_url('doc/'+child.attrib['resource']),
   157                 title_for_lang(child, self.req.lang)))
   157                 title_for_lang(child, self.req.lang)))
   158             self.subsections_links(child, False)
   158             self.subsections_links(child, False)
   159             self.w(u'</li>')
   159             self.w(u'</li>')
   160         self.w(u'</ul>\n')
   160         self.w(u'</ul>\n')
   161         
   161 
   162 
   162 
   163 
   163 
   164 class InlineHelpImageView(StartupView):
   164 class InlineHelpImageView(StartupView):
   165     id = 'wdocimages'
   165     id = 'wdocimages'
   166     __select__ = match_form_params('fid')
   166     __select__ = match_form_params('fid')
   167     binary = True
   167     binary = True
   168     templatable = False
   168     templatable = False
   169     content_type = 'image/png'
   169     content_type = 'image/png'
   170     
   170 
   171     def call(self):
   171     def call(self):
   172         fid = self.req.form['fid']
   172         fid = self.req.form['fid']
   173         for lang in chain((self.req.lang, self.vreg.property_value('ui.language')),
   173         for lang in chain((self.req.lang, self.vreg.property_value('ui.language')),
   174                           self.config.available_languages()):
   174                           self.config.available_languages()):
   175             rid = join('images', '%s_%s.png' % (fid, lang))
   175             rid = join('images', '%s_%s.png' % (fid, lang))
   183 
   183 
   184 class ChangeLogView(StartupView):
   184 class ChangeLogView(StartupView):
   185     id = 'changelog'
   185     id = 'changelog'
   186     title = _('What\'s new?')
   186     title = _('What\'s new?')
   187     maxentries = 25
   187     maxentries = 25
   188     
   188 
   189     def call(self):
   189     def call(self):
   190         rid = 'ChangeLog_%s' % (self.req.lang)
   190         rid = 'ChangeLog_%s' % (self.req.lang)
   191         allentries = []
   191         allentries = []
   192         title = self.req._(self.title)
   192         title = self.req._(self.title)
   193         restdata = ['.. -*- coding: utf-8 -*-', '', title, '='*len(title), '']
   193         restdata = ['.. -*- coding: utf-8 -*-', '', title, '='*len(title), '']
   232                 i += 1
   232                 i += 1
   233                 if i > self.maxentries:
   233                 if i > self.maxentries:
   234                     break
   234                     break
   235         w('') # blank line
   235         w('') # blank line
   236         self.w(rest_publish(self, '\n'.join(restdata)))
   236         self.w(rest_publish(self, '\n'.join(restdata)))
   237