示例#1
0
    def get_rendered_for_toc(self, path_override=None):
        """Returns an html 'ul' element, that represents the entire table of contents for this category.
        Parameters
        ---------
        path_override : str
          If not None, gets urls for this path. Intended for use by parent categories getting the toc of their children.
        """
        root_elm = E.UL(E.CLASS("toc_category"))
        # Children categories.
        nav = self.navigation
        for child in self.children:
            if path_override != None:
                child_path = get_url(path_override, child.file_path)
            else:
                child_path = get_url(self.file_path, child.file_path)

            child_elm = E.LI(
                E.A(child.name, href=child_path),
                child.get_rendered_for_toc(path_override=self.path)
            )
            root_elm.append(child_elm)
        # Assets.
        for asset in self.assets:
            asset_path = get_new_ext(asset.path, 'html')
            if path_override != None:
                asset_path = get_url(path_override, asset_path)
            else:
                asset_path = get_url(self.file_path, asset_path)
            asset_elm = E.LI(
                E.A(asset.title, href=asset_path)
            )
            root_elm.append(asset_elm)
        return root_elm
 def test_get_url_same(self):
     source_path = 'Src\\source\\abc.recipe'
     dest_path = 'Src\\source\\def.md'
     goal = 'def.md'
     url = get_url(source_path, dest_path)
     print('[test_get_url_same] ' + url)
     self.assertEqual(url, goal)
 def test_get_url_adir(self):
     source_path = 'Src\\source\\OtherDir\\ASubDir\\def.md'
     dest_path = 'Src\\source\\ADir\\abc.recipe'
     goal = '../../ADir/abc.recipe'
     url = get_url(source_path, dest_path)
     print('[test_get_url_adir] ' + url)
     self.assertEqual(url, goal)
 def test_get_url_different(self):
     source_path = 'Src\\source\\ADir\\abc.recipe'
     dest_path = 'Src\\source\\OtherDir\\def.md'
     goal = '../OtherDir/def.md'
     url = get_url(source_path, dest_path)
     print('[test_get_url_different] ' + url)
     self.assertEqual(url, goal)
示例#5
0
    def get_rendered(self, generator):
        """Returns an html representation of this category.
        Parameters
        ---------
        generator : Generator
          The generator object this category belongs to.
        """
        categories_toc = self.get_rendered_for_toc()
        categories_toc = lxml.html.tostring(categories_toc, pretty_print=True)
        categories_toc = categories_toc.decode(sys.getdefaultencoding())
        title = self.name
        titleimage = None # TODO

        categories = []
        for category in generator.root_category.children:
            categories.append({ 'name': category.name,
                                'url': get_url(self.file_path,
                                               category.file_path) })

        neighbor_previous = None
        neighbor_next = None
        if len(self.navigation.neighbors_prev) > 0:
            neighbor = self.navigation.neighbors_prev[0]
            neighbor_previous = {
                'title': neighbor.name,
                'url': get_url(self.file_path,
                               neighbor.file_path)
            }
        if len(self.navigation.neighbors_next) > 0:
            neighbor = self.navigation.neighbors_next[0]
            neighbor_next = {
                'title': neighbor.name,
                'url': get_url(self.file_path,
                               neighbor.file_path)
            }

        rendered = generator.templates['template_category'].render(
            language = generator.language,
            styles = generator.get_style_urls(self),
            categories_toc = categories_toc,
            categories = categories,
            title = title,
            titleimage = titleimage,
            neighbor_previous = neighbor_previous,
            neighbor_next = neighbor_next
        )
        return rendered
示例#6
0
 def get_neighbor_prev(self):
     """Returns the first previous neighbor,
     or None, if there isn't one."""
     if len(self.navigation.neighbors_prev) > 0:
         neighbor = self.navigation.neighbors_prev[0]
         return {
             'title': neighbor.title,
             'url': get_url(self.path, get_new_ext(neighbor.path, 'html'))
         }
     else:
         return None
示例#7
0
 def get_categories(self, generator):
     """Returns a list of root categories.
     Parameters
     ---------
     generator : classes.Generator
         Generator object whose root categories to get.
     """
     categories = []
     for category in generator.root_category.children:
         categories.append({
             'name': category.name,
             'url': get_url(self.path, category.file_path)
         })
     return categories
示例#8
0
    def get_style_urls(self, obj):
        """Returns a list of styles urls for use in template rendering.
        Parameters
        ---------
        obj : Asset or Category
          The object for which to get the urls.
        """
        path = obj.path

        if isinstance(obj, Category):
            path += '/index.html'

        style_urls = []
        for style in self.styles:
            url = get_url(path, style.path)
            style_urls.append({ 'url': url })
        return style_urls