mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Refactoring in html2lrf and pylrs
This commit is contained in:
parent
42c4acd360
commit
35344ebceb
@ -15,7 +15,7 @@ Var MUI_TEMP
|
|||||||
|
|
||||||
!define PRODUCT_NAME "libprs500"
|
!define PRODUCT_NAME "libprs500"
|
||||||
!define XPUI_BRANDINGTEXT "${PRODUCT_NAME} created by Kovid Goyal"
|
!define XPUI_BRANDINGTEXT "${PRODUCT_NAME} created by Kovid Goyal"
|
||||||
!define PRODUCT_VERSION "0.3.13"
|
!define PRODUCT_VERSION "0.3.14"
|
||||||
!define WEBSITE "https://libprs500.kovidgoyal.net"
|
!define WEBSITE "https://libprs500.kovidgoyal.net"
|
||||||
!define PY2EXE_DIR "C:\libprs500"
|
!define PY2EXE_DIR "C:\libprs500"
|
||||||
!define LIBUSB_DIR "C:\libusb-prs500"
|
!define LIBUSB_DIR "C:\libusb-prs500"
|
||||||
|
@ -29,16 +29,6 @@ __author__ = "Kovid Goyal <kovid@kovidgoyal.net>"
|
|||||||
class ConversionError(Exception):
|
class ConversionError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_text(elem):
|
|
||||||
''' Return the textual content of a pylrs element '''
|
|
||||||
txt = ''
|
|
||||||
if hasattr(elem, 'text'):
|
|
||||||
txt += elem.text
|
|
||||||
if hasattr(elem, 'contents'):
|
|
||||||
for child in elem.contents:
|
|
||||||
txt += get_text(child)
|
|
||||||
return txt
|
|
||||||
|
|
||||||
def option_parser(usage):
|
def option_parser(usage):
|
||||||
parser = OptionParser(usage=usage, version='libprs500 '+VERSION)
|
parser = OptionParser(usage=usage, version='libprs500 '+VERSION)
|
||||||
parser.add_option('--header', action='store_true', default=False, dest='header',
|
parser.add_option('--header', action='store_true', default=False, dest='header',
|
||||||
@ -59,7 +49,7 @@ def Book(font_delta=0, header=None, **settings):
|
|||||||
ps = dict(textwidth=575, textheight=747)
|
ps = dict(textwidth=575, textheight=747)
|
||||||
if header:
|
if header:
|
||||||
hdr = Header()
|
hdr = Header()
|
||||||
hb = TextBlock(TextStyle(align='foot', fontsize=50))
|
hb = TextBlock(TextStyle(align='foot', fontsize=60))
|
||||||
hb.append(header)
|
hb.append(header)
|
||||||
hdr.PutObj(hb)
|
hdr.PutObj(hb)
|
||||||
ps['headheight'] = 30
|
ps['headheight'] = 30
|
||||||
|
@ -33,7 +33,7 @@ from libprs500.lrf.pylrs.pylrs import Paragraph, CR, Italic, ImageStream, TextBl
|
|||||||
ImageBlock, JumpButton, CharButton, \
|
ImageBlock, JumpButton, CharButton, \
|
||||||
Page, Bold, Space, Plot, TextStyle, Image
|
Page, Bold, Space, Plot, TextStyle, Image
|
||||||
from libprs500.lrf.pylrs.pylrs import Span as _Span
|
from libprs500.lrf.pylrs.pylrs import Span as _Span
|
||||||
from libprs500.lrf import ConversionError, option_parser, Book, get_text
|
from libprs500.lrf import ConversionError, option_parser, Book
|
||||||
from libprs500 import extract
|
from libprs500 import extract
|
||||||
|
|
||||||
def ImagePage():
|
def ImagePage():
|
||||||
@ -351,11 +351,11 @@ class HTMLConverter(object):
|
|||||||
self.top = self.current_block
|
self.top = self.current_block
|
||||||
|
|
||||||
self.process_children(self.soup, {})
|
self.process_children(self.soup, {})
|
||||||
if self.current_para and get_text(self.current_para).strip():
|
if self.current_para and self.current_block:
|
||||||
self.current_block.append(self.current_para)
|
self.current_para.append_to(self.current_block)
|
||||||
if self.current_block and get_text(self.current_block).strip():
|
if self.current_block and self.current_page:
|
||||||
self.current_page.append(self.current_block)
|
self.current_block.append_to(self.current_page)
|
||||||
if self.current_page and get_text(self.current_page).strip():
|
if self.current_page and self.current_page.get_text().strip():
|
||||||
self.book.append(self.current_page)
|
self.book.append(self.current_page)
|
||||||
|
|
||||||
|
|
||||||
@ -436,13 +436,11 @@ class HTMLConverter(object):
|
|||||||
End the current page, ensuring that any further content is displayed
|
End the current page, ensuring that any further content is displayed
|
||||||
on a new page.
|
on a new page.
|
||||||
"""
|
"""
|
||||||
if get_text(self.current_para).strip():
|
self.current_para.append_to(self.current_block)
|
||||||
self.current_block.append(self.current_para)
|
self.current_para = Paragraph()
|
||||||
self.current_para = Paragraph()
|
self.current_block.append_to(self.current_page)
|
||||||
if get_text(self.current_block).strip():
|
self.current_block = TextBlock()
|
||||||
self.current_page.append(self.current_block)
|
if self.current_page.get_text().strip():
|
||||||
self.current_block = TextBlock()
|
|
||||||
if get_text(self.current_page).strip():
|
|
||||||
self.book.append(self.current_page)
|
self.book.append(self.current_page)
|
||||||
self.current_page = Page()
|
self.current_page = Page()
|
||||||
|
|
||||||
@ -486,10 +484,8 @@ class HTMLConverter(object):
|
|||||||
align = "center"
|
align = "center"
|
||||||
css.pop('text-align')
|
css.pop('text-align')
|
||||||
if align != self.current_block.textStyle.attrs['align']:
|
if align != self.current_block.textStyle.attrs['align']:
|
||||||
if get_text(self.current_para).strip():
|
self.current_para.append_to(self.current_block)
|
||||||
self.current_block.append(self.current_para)
|
self.current_block.append_to(self.current_page)
|
||||||
if get_text(self.current_block).strip():
|
|
||||||
self.current_page.append(self.current_block)
|
|
||||||
self.current_block = TextBlock(TextStyle(align=align))
|
self.current_block = TextBlock(TextStyle(align=align))
|
||||||
self.current_para = Paragraph()
|
self.current_para = Paragraph()
|
||||||
try:
|
try:
|
||||||
@ -505,9 +501,10 @@ class HTMLConverter(object):
|
|||||||
test = key.lower()
|
test = key.lower()
|
||||||
if test.startswith('margin') or 'indent' in test or \
|
if test.startswith('margin') or 'indent' in test or \
|
||||||
'padding' in test or 'border' in test or 'page-break' in test \
|
'padding' in test or 'border' in test or 'page-break' in test \
|
||||||
or test.startswith('mso') \
|
or test.startswith('mso') or test.startswith('background')\
|
||||||
or test in ['color', 'display', 'text-decoration', \
|
or test in ['color', 'display', 'text-decoration', \
|
||||||
'letter-spacing', 'text-autospace', 'text-transform']:
|
'letter-spacing', 'text-autospace', 'text-transform',
|
||||||
|
'font-variant']:
|
||||||
css.pop(key)
|
css.pop(key)
|
||||||
return css
|
return css
|
||||||
|
|
||||||
@ -516,7 +513,7 @@ class HTMLConverter(object):
|
|||||||
End current paragraph with a paragraph break after it. If the current
|
End current paragraph with a paragraph break after it. If the current
|
||||||
paragraph has no non whitespace text in it do nothing.
|
paragraph has no non whitespace text in it do nothing.
|
||||||
'''
|
'''
|
||||||
if not get_text(self.current_para).strip():
|
if not self.current_para.get_text().strip():
|
||||||
return
|
return
|
||||||
if self.current_para.contents:
|
if self.current_para.contents:
|
||||||
self.current_block.append(self.current_para)
|
self.current_block.append(self.current_para)
|
||||||
@ -551,10 +548,8 @@ class HTMLConverter(object):
|
|||||||
pass
|
pass
|
||||||
elif tagname == 'a':
|
elif tagname == 'a':
|
||||||
if tag.has_key('name'):
|
if tag.has_key('name'):
|
||||||
if get_text(self.current_para).strip():
|
self.current_para.append_to(self.current_block)
|
||||||
self.current_block.append(self.current_para)
|
self.current_block.append_to(self.current_page)
|
||||||
if get_text(self.current_block).strip():
|
|
||||||
self.current_page.append(self.current_block)
|
|
||||||
previous = self.current_block
|
previous = self.current_block
|
||||||
tb = TextBlock()
|
tb = TextBlock()
|
||||||
self.current_block = tb
|
self.current_block = tb
|
||||||
@ -563,11 +558,11 @@ class HTMLConverter(object):
|
|||||||
self.process_children(tag, tag_css)
|
self.process_children(tag, tag_css)
|
||||||
if tb.parent == None:
|
if tb.parent == None:
|
||||||
if self.current_block == tb:
|
if self.current_block == tb:
|
||||||
if get_text(self.current_para):
|
self.current_para.append_to(self.current_block)
|
||||||
self.current_block.append(self.current_para)
|
self.current_para = Paragraph()
|
||||||
self.current_para = Paragraph()
|
if not self.current_block.get_text().strip():
|
||||||
self.current_page.append(self.current_block)
|
self.current_page.append(self.current_block)
|
||||||
self.current_block = TextBlock()
|
self.current_block = TextBlock()
|
||||||
else:
|
else:
|
||||||
found, marked = False, False
|
found, marked = False, False
|
||||||
for item in self.current_page.contents:
|
for item in self.current_page.contents:
|
||||||
@ -663,7 +658,7 @@ class HTMLConverter(object):
|
|||||||
self.end_current_para()
|
self.end_current_para()
|
||||||
elif tagname == 'li':
|
elif tagname == 'li':
|
||||||
prepend = str(self.in_ol)+'. ' if self.in_ol else u'\u2022' + ' '
|
prepend = str(self.in_ol)+'. ' if self.in_ol else u'\u2022' + ' '
|
||||||
if get_text(self.current_para).strip():
|
if self.current_para.get_text().strip():
|
||||||
self.current_para.append(CR())
|
self.current_para.append(CR())
|
||||||
self.current_block.append(self.current_para)
|
self.current_block.append(self.current_para)
|
||||||
self.current_para = Paragraph()
|
self.current_para = Paragraph()
|
||||||
@ -680,12 +675,6 @@ class HTMLConverter(object):
|
|||||||
self.end_current_para()
|
self.end_current_para()
|
||||||
self.current_block.append(CR())
|
self.current_block.append(CR())
|
||||||
elif tagname in ['p', 'div']:
|
elif tagname in ['p', 'div']:
|
||||||
# TODO: Implement ol
|
|
||||||
#indent = tag_css.pop('text-indent', '')
|
|
||||||
#if indent:
|
|
||||||
# TODO: If indent is different from current textblock's parindent
|
|
||||||
# start a new TextBlock
|
|
||||||
#pass
|
|
||||||
self.end_current_para()
|
self.end_current_para()
|
||||||
self.process_children(tag, tag_css)
|
self.process_children(tag, tag_css)
|
||||||
self.end_current_para()
|
self.end_current_para()
|
||||||
|
@ -275,6 +275,24 @@ class LrsContainer(object):
|
|||||||
self.validChildren = validChildren
|
self.validChildren = validChildren
|
||||||
|
|
||||||
|
|
||||||
|
def get_text(self):
|
||||||
|
''' Return the textual content of this container'''
|
||||||
|
txt = ''
|
||||||
|
if hasattr(self, 'text'):
|
||||||
|
txt += self.text
|
||||||
|
if hasattr(self, 'contents'):
|
||||||
|
for child in self.contents:
|
||||||
|
txt += child.get_text()
|
||||||
|
return txt
|
||||||
|
|
||||||
|
def append_to(self, parent):
|
||||||
|
'''
|
||||||
|
Append self to C{parent} iff self has non whitespace textual content
|
||||||
|
@type parent: LrsContainer
|
||||||
|
'''
|
||||||
|
if self.get_text().strip():
|
||||||
|
parent.append(self)
|
||||||
|
|
||||||
def appendReferencedObjects(self, parent):
|
def appendReferencedObjects(self, parent):
|
||||||
for c in self.contents:
|
for c in self.contents:
|
||||||
c.appendReferencedObjects(parent)
|
c.appendReferencedObjects(parent)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user