2
2
__license__ = 'GPL v3'
3
3
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
5
import textwrap, os, glob, functools
5
import textwrap, os, glob, functools, re
6
from calibre import guess_type
6
7
from calibre.customize import FileTypePlugin, MetadataReaderPlugin, \
7
8
MetadataWriterPlugin, PreferencesPlugin, InterfaceActionBase
8
9
from calibre.constants import numeric_version
9
10
from calibre.ebooks.metadata.archive import ArchiveExtract, get_cbz_metadata
11
from calibre.ebooks.metadata.opf2 import metadata_to_opf
12
from calibre.ebooks.oeb.base import OEB_IMAGES
11
14
# To archive plugins {{{
12
15
class HTML2ZIP(FileTypePlugin):
88
class TXT2TXTZ(FileTypePlugin):
90
author = 'John Schember'
91
description = _('Create a TXTZ archive when a TXT file is imported '
92
'containing Markdown or Textile references to images. The referenced '
93
'images as well as the TXT file are added to the archive.')
94
version = numeric_version
95
file_types = set(['txt', 'text'])
96
supported_platforms = ['windows', 'osx', 'linux']
99
def _get_image_references(self, txt, base_dir):
103
for m in re.finditer(ur'(?mu)(?:[\[{])?\!(?:\. )?(?P<path>[^\s(!]+)\s?(?:\(([^\)]+)\))?\!(?::(\S+))?(?:[\]}]|(?=\s|$))', txt):
104
path = m.group('path')
105
if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
109
for m in re.finditer(ur'(?mu)\!\[([^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*)\]\s*\((?P<path>[^\)]*)\)', txt):
110
path = m.group('path')
111
if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
116
for m in re.finditer(ur'(?mu)^(\ ?\ ?\ ?)\[(?P<id>[^\]]*)\]:\s*(?P<path>[^\s]*)$', txt):
117
if m.group('id') and m.group('path'):
118
refs[m.group('id')] = m.group('path')
119
for m in re.finditer(ur'(?mu)\!\[([^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*)\]\s*\[(?P<id>[^\]]*)\]', txt):
120
path = refs.get(m.group('id'), None)
121
if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
125
return list(set(images))
127
def run(self, path_to_ebook):
128
with open(path_to_ebook, 'rb') as ebf:
130
base_dir = os.path.dirname(path_to_ebook)
131
images = self._get_image_references(txt, base_dir)
134
# Create TXTZ and put file plus images inside of it.
136
of = self.temporary_file('_plugin_txt2txtz.txtz')
137
txtz = zipfile.ZipFile(of.name, 'w')
138
# Add selected TXT file to archive.
139
txtz.write(path_to_ebook, os.path.basename(path_to_ebook), zipfile.ZIP_DEFLATED)
141
if os.path.exists(os.path.join(base_dir, 'metadata.opf')):
142
txtz.write(os.path.join(base_dir, 'metadata.opf'), 'metadata.opf', zipfile.ZIP_DEFLATED)
144
from calibre.ebooks.metadata.txt import get_metadata
145
with open(path_to_ebook, 'rb') as ebf:
146
mi = get_metadata(ebf)
147
opf = metadata_to_opf(mi)
148
txtz.writestr('metadata.opf', opf, zipfile.ZIP_DEFLATED)
151
txtz.write(os.path.join(base_dir, image), image)
156
# No images so just import the TXT file.
87
161
# Metadata reader plugins {{{
325
399
from calibre.ebooks.metadata.txt import get_metadata
326
400
return get_metadata(stream)
402
class TXTZMetadataReader(MetadataReaderPlugin):
404
name = 'Read TXTZ metadata'
405
file_types = set(['txtz'])
406
description = _('Read metadata from %s files') % 'TXTZ'
407
author = 'John Schember'
409
def get_metadata(self, stream, ftype):
410
from calibre.ebooks.metadata.txtz import get_metadata
411
return get_metadata(stream)
328
413
class ZipMetadataReader(MetadataReaderPlugin):
330
415
name = 'Read ZIP metadata'
474
571
from calibre.devices.hanvon.driver import N516, EB511, ALEX, AZBOOKA, THEBOOK
475
572
from calibre.devices.edge.driver import EDGE
476
573
from calibre.devices.teclast.driver import TECLAST_K3, NEWSMY, IPAPYRUS, \
477
SOVOS, PICO, SUNSTECH_EB700, ARCHOS7O
574
SOVOS, PICO, SUNSTECH_EB700, ARCHOS7O, STASH, WEXLER
478
575
from calibre.devices.sne.driver import SNE
479
from calibre.devices.misc import PALMPRE, AVANT, SWEEX, PDNOVEL, KOGAN, \
480
GEMEI, VELOCITYMICRO, PDNOVEL_KOBO, Q600, LUMIREAD, ALURATEK_COLOR, \
576
from calibre.devices.misc import PALMPRE, AVANT, SWEEX, PDNOVEL, \
577
GEMEI, VELOCITYMICRO, PDNOVEL_KOBO, LUMIREAD, ALURATEK_COLOR, \
481
578
TREKSTOR, EEEREADER, NEXTBOOK
482
579
from calibre.devices.folder_device.driver import FOLDER_DEVICE_FOR_CONFIG
483
580
from calibre.devices.kobo.driver import KOBO
484
581
from calibre.devices.bambook.driver import BAMBOOK
486
583
from calibre.ebooks.metadata.fetch import GoogleBooks, ISBNDB, Amazon, \
488
585
from calibre.ebooks.metadata.douban import DoubanBooks
489
586
from calibre.ebooks.metadata.nicebooks import NiceBooks, NiceBooksCovers
490
587
from calibre.ebooks.metadata.covers import OpenLibraryCovers, \
491
LibraryThingCovers, DoubanCovers
588
AmazonCovers, DoubanCovers
492
589
from calibre.library.catalog import CSV_XML, EPUB_MOBI, BIBTEX
493
590
from calibre.ebooks.epub.fix.unmanifested import Unmanifested
494
591
from calibre.ebooks.epub.fix.epubcheck import Epubcheck
496
plugins = [HTML2ZIP, PML2PMLZ, ArchiveExtract, GoogleBooks, ISBNDB, Amazon,
497
LibraryThing, DoubanBooks, NiceBooks, CSV_XML, EPUB_MOBI, BIBTEX, Unmanifested,
498
Epubcheck, OpenLibraryCovers, LibraryThingCovers, DoubanCovers,
593
plugins = [HTML2ZIP, PML2PMLZ, TXT2TXTZ, ArchiveExtract, GoogleBooks, ISBNDB, Amazon,
594
KentDistrictLibrary, DoubanBooks, NiceBooks, CSV_XML, EPUB_MOBI, BIBTEX, Unmanifested,
595
Epubcheck, OpenLibraryCovers, AmazonCovers, DoubanCovers,
917
1023
config_widget = 'calibre.gui2.preferences.misc'
918
1024
description = _('Miscellaneous advanced configuration')
920
plugins += [LookAndFeel, Behavior, Columns, Toolbar, InputOptions,
1026
plugins += [LookAndFeel, Behavior, Columns, Toolbar, Search, InputOptions,
921
1027
CommonOptions, OutputOptions, Adding, Saving, Sending, Plugboard,
922
1028
Email, Server, Plugins, Tweaks, Misc, TemplateFunctions]
1032
# New metadata download plugins {{{
1033
from calibre.ebooks.metadata.sources.google import GoogleBooks
1034
from calibre.ebooks.metadata.sources.amazon import Amazon
1036
plugins += [GoogleBooks, Amazon]