Esempio n. 1
0
    def generate_annotation_html(self, bookmark):
        from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag, NavigableString
        # Returns <div class="user_annotations"> ... </div>
        last_read_location = bookmark.last_read_location
        timestamp = datetime.datetime.utcfromtimestamp(bookmark.timestamp)
        percent_read = bookmark.percent_read

        ka_soup = BeautifulSoup()
        dtc = 0
        divTag = Tag(ka_soup, 'div')
        divTag['class'] = 'user_annotations'

        # Add the last-read location
        spanTag = Tag(ka_soup, 'span')
        spanTag['style'] = 'font-weight:bold'
        if bookmark.book_format == 'pdf':
            spanTag.insert(
                0,
                NavigableString(
                    _("%(time)s<br />Last Page Read: %(loc)d (%(pr)d%%)") %
                    dict(time=strftime(u'%x', timestamp.timetuple()),
                         loc=last_read_location,
                         pr=percent_read)))
        else:
            spanTag.insert(
                0,
                NavigableString(
                    _("%(time)s<br />Last Page Read: Location %(loc)d (%(pr)d%%)"
                      ) % dict(time=strftime(u'%x', timestamp.timetuple()),
                               loc=last_read_location,
                               pr=percent_read)))

        divTag.insert(dtc, spanTag)
        dtc += 1
        divTag.insert(dtc, Tag(ka_soup, 'br'))
        dtc += 1

        if bookmark.user_notes:
            user_notes = bookmark.user_notes
            annotations = []

            # Add the annotations sorted by location
            # Italicize highlighted text
            for location in sorted(user_notes):
                if user_notes[location]['text']:
                    annotations.append(
                        _('<b>Location %(dl)d &bull; %(typ)s</b><br />%(text)s<br />'
                          ) %
                        dict(
                            dl=user_notes[location]['displayed_location'],
                            typ=user_notes[location]['type'],
                            text=(user_notes[location]['text'] if
                                  user_notes[location]['type'] == 'Note' else
                                  '<i>%s</i>' % user_notes[location]['text'])))
                else:
                    if bookmark.book_format == 'pdf':
                        annotations.append(
                            _('<b>Page %(dl)d &bull; %(typ)s</b><br />') %
                            dict(dl=user_notes[location]['displayed_location'],
                                 typ=user_notes[location]['type']))
                    else:
                        annotations.append(
                            _('<b>Location %(dl)d &bull; %(typ)s</b><br />') %
                            dict(dl=user_notes[location]['displayed_location'],
                                 typ=user_notes[location]['type']))

            for annotation in annotations:
                divTag.insert(dtc, annotation)
                dtc += 1

        ka_soup.insert(0, divTag)
        return ka_soup