~stub/ubuntu/precise/calibre/devel

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/fb2/fb2ml.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-04-12 11:29:25 UTC
  • mfrom: (42.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110412112925-c7171kt2bb5rmft4
Tags: 0.7.50+dfsg-2
* debian/control: Build with libpodofo-dev to enable PDF metadata.
  (Closes: #619632)
* debian/control: Add libboost1.42-dev build dependency. Apparently it is
  needed in some setups. (Closes: #619807)
* debian/rules: Call dh_sip to generate a proper sip API dependency, to
  prevent crashes like #616372 for partial upgrades.
* debian/control: Bump python-qt4 dependency to >= 4.8.3-2, which reportedly
  fixes crashes on startup. (Closes: #619701, #620125)

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
 
73
73
    def clean_text(self, text):
74
74
        # Condense empty paragraphs into a line break. 
75
 
        text = re.sub(r'(?miu)(<p>\s*</p>\s*){3,}', '<p><empty-line /></p>', text)
 
75
        text = re.sub(r'(?miu)(<p>\s*</p>\s*){3,}', '<empty-line />', text)
76
76
        # Remove empty paragraphs.
77
77
        text = re.sub(r'(?miu)<p>\s*</p>', '', text)
78
78
        # Clean up pargraph endings.
101
101
 
102
102
    def fb2_header(self):
103
103
        metadata = {}
104
 
        metadata['author_first'] = u''
105
 
        metadata['author_middle'] = u''
106
 
        metadata['author_last'] = u''
107
104
        metadata['title'] = self.oeb_book.metadata.title[0].value
108
105
        metadata['appname'] = __appname__
109
106
        metadata['version'] = __version__
115
112
        metadata['id'] = None
116
113
        metadata['cover'] = self.get_cover()
117
114
 
118
 
        author_parts = self.oeb_book.metadata.creator[0].value.split(' ')
119
 
        if len(author_parts) == 1:
120
 
            metadata['author_last'] = author_parts[0]
121
 
        elif len(author_parts) == 2:
122
 
            metadata['author_first'] = author_parts[0]
123
 
            metadata['author_last'] = author_parts[1]
124
 
        else:
125
 
            metadata['author_first'] = author_parts[0]
126
 
            metadata['author_middle'] = ' '.join(author_parts[1:-2])
127
 
            metadata['author_last'] = author_parts[-1]
 
115
        metadata['author'] = u''
 
116
        for auth in self.oeb_book.metadata.creator:
 
117
            author_first = u''
 
118
            author_middle = u''
 
119
            author_last = u''
 
120
            author_parts = auth.value.split(' ')
 
121
            if len(author_parts) == 1:
 
122
                author_last = author_parts[0]
 
123
            elif len(author_parts) == 2:
 
124
                author_first = author_parts[0]
 
125
                author_last = author_parts[1]
 
126
            else:
 
127
                author_first = author_parts[0]
 
128
                author_middle = ' '.join(author_parts[1:-1])
 
129
                author_last = author_parts[-1]
 
130
            metadata['author'] += '<author>'
 
131
            metadata['author'] += '<first-name>%s</first-name>' % prepare_string_for_xml(author_first)
 
132
            if author_middle:
 
133
                metadata['author'] += '<middle-name>%s</middle-name>' % prepare_string_for_xml(author_middle)
 
134
            metadata['author'] += '<last-name>%s</last-name>' % prepare_string_for_xml(author_last)
 
135
            metadata['author'] += '</author>'
 
136
        if not metadata['author']:
 
137
            metadata['author'] = u'<author><first-name></first-name><last-name><last-name></author>'
 
138
 
 
139
        metadata['sequence'] = u''
 
140
        if self.oeb_book.metadata.series:
 
141
            index = '1'
 
142
            if self.oeb_book.metadata.series_index:
 
143
                index = self.oeb_book.metadata.series_index[0]
 
144
            metadata['sequence'] = u'<sequence name="%s" number="%s" />' % (prepare_string_for_xml(u'%s' % self.oeb_book.metadata.series[0]), index)
128
145
 
129
146
        identifiers = self.oeb_book.metadata['identifier']
130
147
        for x in identifiers:
136
153
            metadata['id'] = str(uuid.uuid4())
137
154
 
138
155
        for key, value in metadata.items():
139
 
            if not key == 'cover':
 
156
            if key not in ('author', 'cover', 'sequence'):
140
157
                metadata[key] = prepare_string_for_xml(value)
141
158
 
142
159
        return u'<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">' \
143
160
                '<description>' \
144
161
                    '<title-info>' \
145
162
                        '<genre>antique</genre>' \
146
 
                        '<author>' \
147
 
                            '<first-name>%(author_first)s</first-name>' \
148
 
                            '<middle-name>%(author_middle)s</middle-name>' \
149
 
                            '<last-name>%(author_last)s</last-name>' \
150
 
                        '</author>' \
 
163
                            '%(author)s' \
151
164
                        '<book-title>%(title)s</book-title>' \
152
165
                        '%(cover)s' \
153
166
                        '<lang>%(lang)s</lang>' \
 
167
                        '%(sequence)s' \
154
168
                    '</title-info>' \
155
169
                    '<document-info>' \
156
 
                        '<author>' \
157
 
                            '<first-name></first-name>' \
158
 
                            '<middle-name></middle-name>' \
159
 
                            '<last-name></last-name>' \
160
 
                        '</author>' \
 
170
                        '%(author)s' \
161
171
                        '<program-used>%(appname)s %(version)s</program-used>' \
162
172
                        '<date>%(date)s</date>' \
163
173
                        '<id>%(id)s</id>' \
350
360
        # Number of blank lines above tag
351
361
        try:
352
362
            ems = int(round((float(style.marginTop) / style.fontSize) - 1))
 
363
            if ems < 0:
 
364
                ems = 0
353
365
        except:
354
366
            ems = 0
355
367
 
397
409
                    fb2_out += p_txt
398
410
                    tags += p_tag
399
411
                    fb2_out.append('<image xlink:href="#%s" />' % self.image_hrefs[page.abshref(elem_tree.attrib['src'])])
400
 
        if tag in ('br', 'hr') or ems:
 
412
        if tag in ('br', 'hr') or ems >= 1:
401
413
            if ems < 1:
402
414
                multiplier = 1
403
415
            else: