Fix news not uploading to device automatically

This commit is contained in:
Kovid Goyal 2008-09-27 14:45:07 -07:00
parent b51e6b73a7
commit 16ab9fbf02
3 changed files with 32 additions and 20 deletions

View File

@ -66,6 +66,11 @@ class HTMLProcessor(Processor):
if opts.verbose > 2: if opts.verbose > 2:
self.debug_tree('nocss') self.debug_tree('nocss')
def save(self):
for meta in list(self.root.xpath('//meta')):
meta.getparent().remove(meta)
Processor.save(self)
#self.collect_font_statistics() #self.collect_font_statistics()

View File

@ -28,9 +28,8 @@ from calibre.utils.zipfile import ZipFile
def tostring(root, pretty_print=False): def tostring(root, pretty_print=False):
return html.tostring(root, encoding='utf-8', method='xml', return html.tostring(root, encoding='utf-8', method='xml',
pretty_print=pretty_print, include_meta_content_type=True,
include_meta_content_type=True) pretty_print=pretty_print)
class Link(object): class Link(object):
''' '''
@ -313,6 +312,14 @@ class PreProcessor(object):
return html return html
class Parser(PreProcessor, LoggingInterface): class Parser(PreProcessor, LoggingInterface):
# SELF_CLOSING_TAGS = 'hr|br|link|img|meta|input|area|base|basefont'
# SELF_CLOSING_RULES = [re.compile(p[0]%SELF_CLOSING_TAGS, re.IGNORECASE) for p in
# [
# (r'<(?P<tag>%s)(?P<attrs>(\s+[^<>]*){0,1})(?<!/)>',
# '<\g<tag>\g<attrs> />'),
# (),
# ]
# ]
def __init__(self, htmlfile, opts, tdir, resource_map, htmlfiles, name='htmlparser'): def __init__(self, htmlfile, opts, tdir, resource_map, htmlfiles, name='htmlparser'):
LoggingInterface.__init__(self, logging.getLogger(name)) LoggingInterface.__init__(self, logging.getLogger(name))
@ -347,10 +354,10 @@ class Parser(PreProcessor, LoggingInterface):
Save processed HTML into the content directory. Save processed HTML into the content directory.
Should be called after all HTML processing is finished. Should be called after all HTML processing is finished.
''' '''
with open(self.save_path(), 'wb') as f:
ans = tostring(self.root, pretty_print=self.opts.pretty_print) ans = tostring(self.root, pretty_print=self.opts.pretty_print)
ans = re.compile(r'<html>', re.IGNORECASE).sub('<html xmlns="http://www.w3.org/1999/xhtml">', ans) ans = re.compile(r'<html>', re.IGNORECASE).sub('<html xmlns="http://www.w3.org/1999/xhtml">', ans[:1000]) + ans[1000:]
ans = re.compile(r'<head[^<>]*?>', re.IGNORECASE).sub('<head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n', ans)
with open(self.save_path(), 'wb') as f:
f.write(ans) f.write(ans)
return f.name return f.name
@ -369,15 +376,14 @@ class Parser(PreProcessor, LoggingInterface):
if self.opts.verbose: if self.opts.verbose:
self.log_exception('lxml based parsing failed') self.log_exception('lxml based parsing failed')
self.root = soupparser.fromstring(src) self.root = soupparser.fromstring(src)
self.head = self.body = None head = self.root.xpath('./head')
head = self.root.xpath('//head') self.head = head[0] if head else etree.SubElement(self.root, 'head')
if head: self.body = self.root.body
self.head = head[0]
body = self.root.xpath('//body')
if body:
self.body = body[0]
for a in self.root.xpath('//a[@name]'): for a in self.root.xpath('//a[@name]'):
a.set('id', a.get('name')) a.set('id', a.get('name'))
if not self.head.xpath('./title'):
title = etree.SubElement(self.head, 'title')
title.text = _('Unknown')
def debug_tree(self, name): def debug_tree(self, name):
''' '''
@ -455,11 +461,11 @@ class Processor(Parser):
def save(self): def save(self):
head = self.head if self.head is not None else self.body
style_path = os.path.basename(self.save_path())+'.css' style_path = os.path.basename(self.save_path())+'.css'
style = etree.SubElement(head, 'link', attrib={'type':'text/css', 'rel':'stylesheet', style = etree.SubElement(self.head, 'link', attrib={'type':'text/css', 'rel':'stylesheet',
'href':'resources/'+style_path}) 'href':'resources/'+style_path,
style.tail = '\n\n' 'charset':'UTF-8'})
style.tail = '\n'
style_path = os.path.join(os.path.dirname(self.save_path()), 'resources', style_path) style_path = os.path.join(os.path.dirname(self.save_path()), 'resources', style_path)
open(style_path, 'wb').write(self.css.encode('utf-8')) open(style_path, 'wb').write(self.css.encode('utf-8'))
return Parser.save(self) return Parser.save(self)
@ -584,6 +590,7 @@ class Processor(Parser):
if cn: cn += ' ' if cn: cn += ' '
cn += classname cn += classname
font.set('class', cn) font.set('class', cn)
font.tag = 'span'
for elem in self.root.xpath('//*[@style]'): for elem in self.root.xpath('//*[@style]'):
setting = elem.get('style') setting = elem.get('style')

View File

@ -806,7 +806,7 @@ class Main(MainWindow, Ui_MainWindow):
if job.exception is not None: if job.exception is not None:
self.job_exception(job) self.job_exception(job)
return return
to_device = self.device_connected and fmt in self.device_manager.device_class.FORMATS to_device = self.device_connected and fmt.lower() in self.device_manager.device_class.FORMATS
self._add_books([pt.name], to_device) self._add_books([pt.name], to_device)
if to_device: if to_device:
self.status_bar.showMessage(_('News fetched. Uploading to device.'), 2000) self.status_bar.showMessage(_('News fetched. Uploading to device.'), 2000)