Example #1
0
 def _set_content(self, parent_tag, content_list):
     """Convert the content_list to proper HTML and enclose with the given parent_tag."""
     if not isinstance(content_list, list):
         content_list = [content_list]
     parent_tag.clear()
     for item in content_list:
         if isinstance(item, dict):
             hyperlinks = {'link', 'file', 'email'}
             formats = {'bold', 'italics', 'underline'}
             navigation = {'jump', 'anchor'}
             lists = {'numbers', 'bullets'}
             # Using set intersection ((keys_to_search_for) & item.keys()),
             # The set will be empty when the keys are not found.
             # This takes advantage of the fact that empty == False and non-empty == True.
             if hyperlinks & item.keys():
                 self._add_hyperlink(parent_tag, item)
             elif 'image' in item:
                 self._add_image(parent_tag, item)
             elif formats & item.keys():
                 self._add_formatted(parent_tag, item)
             elif navigation & item.keys():
                 self._add_navigation(parent_tag, item)
             elif lists & item.keys():
                 self._add_list(parent_tag, item)
             else:
                 raise exceptions.UnknownTransform(item,
                                                   hyperlinks + formats + navigation +
                                                   lists + {'image'})
         else:
             parent_tag.append(str(item))
Example #2
0
    def _add_list(self, parent_tag, descriptor):
        """Add an ol or a ul tag representing the list specified by the descriptor."""
        try:  # Try numbers descriptor
            list_items = descriptor['numbers']
            list_tag = self._data.new_tag('ol')
        except KeyError:
            try:  # Try bullets descriptor
                list_items = descriptor['bullets']
                list_tag = self._data.new_tag('ul')
            except KeyError:
                raise exceptions.UnknownTransform(descriptor, ['numbers', 'bullets'])

        for item in list_items:
            item_tag = self._data.new_tag('li')
            self._set_content(item_tag, item)
            list_tag.append(item_tag)
        parent_tag.append(list_tag)
Example #3
0
    def _add_navigation(self, parent_tag, descriptor):
        """Add an 'a' tag that drops an anchor or jumps to one."""
        a_tag = self._data.new_tag('a')
        try:  # Try anchor descriptor
            default_text = a_tag['name'] = descriptor['anchor']
        except KeyError:
            try:  # Try jump descriptor
                default_text = descriptor['jump']
                a_tag['href'] = '#' + descriptor['jump']
            except KeyError:
                raise exceptions.UnknownTransform(descriptor, ['anchor', 'jump'])

        try:
            self._set_content(a_tag, descriptor['text'])
        except KeyError:
            a_tag.string = default_text
        parent_tag.append(a_tag)
Example #4
0
    def _add_formatted(self, parent_tag, descriptor):
        """Add an appropriate text formmating tag according to the descriptor."""
        try:  # Try bold descriptor
            content_list = descriptor['bold']
            format_tag = self._data.new_tag('strong')
        except KeyError:
            try:  # Try italics descriptor
                content_list = descriptor['italics']
                format_tag = self._data.new_tag('em')
            except KeyError:
                try:  # Try underline descriptor
                    content_list = descriptor['underline']
                    format_tag = self._data.new_tag('u')
                except KeyError:
                    raise exceptions.UnknownTransform(descriptor, ['bold', 'italics', 'underline'])

        self._set_content(format_tag, content_list)
        parent_tag.append(format_tag)
Example #5
0
    def _add_hyperlink(self, parent_tag, descriptor):
        """Add an 'a' tag as specified by the descriptor."""
        try:  # Try link descriptor
            default_text = descriptor['link']
            link_url = descriptor['link']
        except KeyError:
            try:  # Try file descriptor
                default_text = descriptor['file'].rsplit('/')[-1]
                link_url = requests.compat.urljoin(self.BASE_URL,
                                                   self._ensure_quoted(descriptor['file']))
            except KeyError:
                try:  # Try email descriptor
                    default_text = descriptor['email']
                    link_url = 'mailto:' + descriptor['email']
                except:
                    raise exceptions.UnknownTransform(descriptor, ['link', 'file', 'email'])

        # Create 'a' tag that opens in new window
        a_tag = self._data.new_tag('a', href=link_url, target='_blank')
        try:
            self._set_content(a_tag, descriptor['text'])
        except KeyError:
            a_tag.string = default_text
        parent_tag.append(a_tag)