mirror of
				https://github.com/kovidgoyal/calibre.git
				synced 2025-11-04 03:27:00 -05:00 
			
		
		
		
	Implement #239
This commit is contained in:
		
							parent
							
								
									9eee4a67b6
								
							
						
					
					
						commit
						e35e673f44
					
				@ -125,6 +125,8 @@ def option_parser(usage):
 | 
				
			|||||||
                      help='Add a header to all the pages with title and author.')
 | 
					                      help='Add a header to all the pages with title and author.')
 | 
				
			||||||
    laf.add_option('--headerformat', default="%t by %a", dest='headerformat', type='string',
 | 
					    laf.add_option('--headerformat', default="%t by %a", dest='headerformat', type='string',
 | 
				
			||||||
                        help='Set the format of the header. %a is replaced by the author and %t by the title. Default is %default')
 | 
					                        help='Set the format of the header. %a is replaced by the author and %t by the title. Default is %default')
 | 
				
			||||||
 | 
					    laf.add_option('--override-css', default=None, dest='_override_css', type='string',
 | 
				
			||||||
 | 
					                   help='Override the CSS. Can be either a path to a CSS stylesheet or a string. If it is a string it is interpreted as CSS.')
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    page = parser.add_option_group('PAGE OPTIONS')
 | 
					    page = parser.add_option_group('PAGE OPTIONS')
 | 
				
			||||||
    profiles = profile_map.keys()
 | 
					    profiles = profile_map.keys()
 | 
				
			||||||
 | 
				
			|||||||
@ -48,6 +48,13 @@ from libprs500.ebooks.metadata.opf import OPFReader
 | 
				
			|||||||
from libprs500.devices.interface import Device
 | 
					from libprs500.devices.interface import Device
 | 
				
			||||||
from libprs500.ebooks.lrf.html.color_map import lrs_color
 | 
					from libprs500.ebooks.lrf.html.color_map import lrs_color
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def update_css(ncss, ocss):
 | 
				
			||||||
 | 
					    for key in ncss.keys():
 | 
				
			||||||
 | 
					        if ocss.has_key(key):
 | 
				
			||||||
 | 
					            ocss[key].update(ncss[key])
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            ocss[key] = ncss[key]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class HTMLConverter(object):
 | 
					class HTMLConverter(object):
 | 
				
			||||||
    SELECTOR_PAT   = re.compile(r"([A-Za-z0-9\-\_\:\.]+[A-Za-z0-9\-\_\:\.\s\,]*)\s*\{([^\}]*)\}")
 | 
					    SELECTOR_PAT   = re.compile(r"([A-Za-z0-9\-\_\:\.]+[A-Za-z0-9\-\_\:\.\s\,]*)\s*\{([^\}]*)\}")
 | 
				
			||||||
    PAGE_BREAK_PAT = re.compile(r'page-break-(?:after|before)\s*:\s*(\w+)', re.IGNORECASE)
 | 
					    PAGE_BREAK_PAT = re.compile(r'page-break-(?:after|before)\s*:\s*(\w+)', re.IGNORECASE)
 | 
				
			||||||
@ -199,6 +206,23 @@ class HTMLConverter(object):
 | 
				
			|||||||
        self.list_counter = 1
 | 
					        self.list_counter = 1
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        self.book = book                #: The Book object representing a BBeB book
 | 
					        self.book = book                #: The Book object representing a BBeB book
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        self.override_css = {}
 | 
				
			||||||
 | 
					        self.override_pcss = {}
 | 
				
			||||||
 | 
					        if self._override_css is not None:
 | 
				
			||||||
 | 
					            if os.access(self._override_css, os.R_OK):
 | 
				
			||||||
 | 
					                src = open(self._override_css, 'rb').read()
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                src = self._override_css
 | 
				
			||||||
 | 
					            match = self.PAGE_BREAK_PAT.search(src) 
 | 
				
			||||||
 | 
					            if match and not re.match('avoid', match.group(1), re.IGNORECASE):
 | 
				
			||||||
 | 
					                self.page_break_found = True
 | 
				
			||||||
 | 
					            ncss, npcss = self.parse_css(src)
 | 
				
			||||||
 | 
					            if ncss:
 | 
				
			||||||
 | 
					                update_css(ncss, self.override_css)
 | 
				
			||||||
 | 
					            if npcss:
 | 
				
			||||||
 | 
					                update_css(npcss, self.override_pcss)
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        self.start_on_file(path, is_root=True)
 | 
					        self.start_on_file(path, is_root=True)
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
    def is_baen(self, soup):
 | 
					    def is_baen(self, soup):
 | 
				
			||||||
@ -1236,12 +1260,6 @@ class HTMLConverter(object):
 | 
				
			|||||||
            else:
 | 
					            else:
 | 
				
			||||||
                self.logger.debug("Failed to process: %s", str(tag))
 | 
					                self.logger.debug("Failed to process: %s", str(tag))
 | 
				
			||||||
        elif tagname in ['style', 'link']:
 | 
					        elif tagname in ['style', 'link']:
 | 
				
			||||||
            def update_css(ncss, ocss):
 | 
					 | 
				
			||||||
                for key in ncss.keys():
 | 
					 | 
				
			||||||
                    if ocss.has_key(key):
 | 
					 | 
				
			||||||
                        ocss[key].update(ncss[key])
 | 
					 | 
				
			||||||
                    else:
 | 
					 | 
				
			||||||
                        ocss[key] = ncss[key]
 | 
					 | 
				
			||||||
            ncss, npcss = {}, {}
 | 
					            ncss, npcss = {}, {}
 | 
				
			||||||
            if tagname == 'style':
 | 
					            if tagname == 'style':
 | 
				
			||||||
                for c in tag.contents:
 | 
					                for c in tag.contents:
 | 
				
			||||||
@ -1265,8 +1283,10 @@ class HTMLConverter(object):
 | 
				
			|||||||
                    pass
 | 
					                    pass
 | 
				
			||||||
            if ncss:
 | 
					            if ncss:
 | 
				
			||||||
                update_css(ncss, self.css)
 | 
					                update_css(ncss, self.css)
 | 
				
			||||||
 | 
					                self.css.update(self.override_css)
 | 
				
			||||||
            if npcss:
 | 
					            if npcss:
 | 
				
			||||||
                update_css(npcss, self.pseudo_css)
 | 
					                update_css(npcss, self.pseudo_css)
 | 
				
			||||||
 | 
					                self.pseudo_css.update(self.override_pcss)
 | 
				
			||||||
        elif tagname == 'pre':
 | 
					        elif tagname == 'pre':
 | 
				
			||||||
            self.end_current_para()
 | 
					            self.end_current_para()
 | 
				
			||||||
            self.end_current_block()
 | 
					            self.end_current_block()
 | 
				
			||||||
 | 
				
			|||||||
@ -15,7 +15,8 @@
 | 
				
			|||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from PyQt4.QtCore import QObject, SIGNAL, Qt
 | 
					from PyQt4.QtCore import QObject, SIGNAL, Qt
 | 
				
			||||||
from PyQt4.QtGui import QAbstractSpinBox, QLineEdit, QCheckBox, QDialog, QPixmap
 | 
					from PyQt4.QtGui import QAbstractSpinBox, QLineEdit, QCheckBox, QDialog, \
 | 
				
			||||||
 | 
					                        QPixmap, QTextEdit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from libprs500.gui2.dialogs.lrf_single_ui import Ui_LRFSingleDialog
 | 
					from libprs500.gui2.dialogs.lrf_single_ui import Ui_LRFSingleDialog
 | 
				
			||||||
from libprs500.gui2.dialogs.choose_format import ChooseFormatDialog
 | 
					from libprs500.gui2.dialogs.choose_format import ChooseFormatDialog
 | 
				
			||||||
@ -108,6 +109,8 @@ class LRFSingleDialog(QDialog, Ui_LRFSingleDialog):
 | 
				
			|||||||
                    obj.setValue(cmdline[i+1])
 | 
					                    obj.setValue(cmdline[i+1])
 | 
				
			||||||
                elif isinstance(obj, QLineEdit):
 | 
					                elif isinstance(obj, QLineEdit):
 | 
				
			||||||
                    obj.setText(cmdline[i+1])
 | 
					                    obj.setText(cmdline[i+1])
 | 
				
			||||||
 | 
					                elif isinstance(obj, QTextEdit):
 | 
				
			||||||
 | 
					                    obj.setPlainText(cmdline[i+1])
 | 
				
			||||||
            profile = cmdline[cmdline.index('--profile')+1]            
 | 
					            profile = cmdline[cmdline.index('--profile')+1]            
 | 
				
			||||||
            self.gui_profile.setCurrentIndex(self.gui_profile.findText(profile))
 | 
					            self.gui_profile.setCurrentIndex(self.gui_profile.findText(profile))
 | 
				
			||||||
            for prepro in self.PREPROCESS_OPTIONS:
 | 
					            for prepro in self.PREPROCESS_OPTIONS:
 | 
				
			||||||
@ -197,6 +200,8 @@ class LRFSingleDialog(QDialog, Ui_LRFSingleDialog):
 | 
				
			|||||||
                obj.setValue(default)
 | 
					                obj.setValue(default)
 | 
				
			||||||
            elif isinstance(obj, QLineEdit) and default:
 | 
					            elif isinstance(obj, QLineEdit) and default:
 | 
				
			||||||
                obj.setText(default)
 | 
					                obj.setText(default)
 | 
				
			||||||
 | 
					            elif isinstance(obj, QTextEdit) and default:
 | 
				
			||||||
 | 
					                obj.setPlainText(default)
 | 
				
			||||||
            elif isinstance(obj, QCheckBox):
 | 
					            elif isinstance(obj, QCheckBox):
 | 
				
			||||||
                state = Qt.Checked if default else Qt.Unchecked
 | 
					                state = Qt.Checked if default else Qt.Unchecked
 | 
				
			||||||
                obj.setCheckState(state)
 | 
					                obj.setCheckState(state)
 | 
				
			||||||
@ -266,6 +271,10 @@ class LRFSingleDialog(QDialog, Ui_LRFSingleDialog):
 | 
				
			|||||||
                val = qstring_to_unicode(obj.text())
 | 
					                val = qstring_to_unicode(obj.text())
 | 
				
			||||||
                if val:
 | 
					                if val:
 | 
				
			||||||
                    cmd.extend([opt, val])
 | 
					                    cmd.extend([opt, val])
 | 
				
			||||||
 | 
					            elif isinstance(obj, QTextEdit):
 | 
				
			||||||
 | 
					                val = qstring_to_unicode(obj.toPlainText())
 | 
				
			||||||
 | 
					                if val:
 | 
				
			||||||
 | 
					                    cmd.extend([opt, val])
 | 
				
			||||||
            elif isinstance(obj, QCheckBox):
 | 
					            elif isinstance(obj, QCheckBox):
 | 
				
			||||||
                if obj.checkState() == Qt.Checked:
 | 
					                if obj.checkState() == Qt.Checked:
 | 
				
			||||||
                    cmd.append(opt)
 | 
					                    cmd.append(opt)
 | 
				
			||||||
 | 
				
			|||||||
@ -468,7 +468,7 @@
 | 
				
			|||||||
                </property>
 | 
					                </property>
 | 
				
			||||||
               </widget>
 | 
					               </widget>
 | 
				
			||||||
              </item>
 | 
					              </item>
 | 
				
			||||||
              <item row="0" column="3" >
 | 
					              <item row="0" column="2" >
 | 
				
			||||||
               <widget class="QDoubleSpinBox" name="gui_font_delta" >
 | 
					               <widget class="QDoubleSpinBox" name="gui_font_delta" >
 | 
				
			||||||
                <property name="buttonSymbols" >
 | 
					                <property name="buttonSymbols" >
 | 
				
			||||||
                 <enum>QAbstractSpinBox::PlusMinus</enum>
 | 
					                 <enum>QAbstractSpinBox::PlusMinus</enum>
 | 
				
			||||||
@ -503,7 +503,7 @@
 | 
				
			|||||||
                </property>
 | 
					                </property>
 | 
				
			||||||
               </widget>
 | 
					               </widget>
 | 
				
			||||||
              </item>
 | 
					              </item>
 | 
				
			||||||
              <item row="1" column="3" >
 | 
					              <item row="1" column="2" >
 | 
				
			||||||
               <widget class="QDoubleSpinBox" name="gui_wordspace" >
 | 
					               <widget class="QDoubleSpinBox" name="gui_wordspace" >
 | 
				
			||||||
                <property name="buttonSymbols" >
 | 
					                <property name="buttonSymbols" >
 | 
				
			||||||
                 <enum>QAbstractSpinBox::PlusMinus</enum>
 | 
					                 <enum>QAbstractSpinBox::PlusMinus</enum>
 | 
				
			||||||
@ -538,7 +538,7 @@
 | 
				
			|||||||
                </property>
 | 
					                </property>
 | 
				
			||||||
               </widget>
 | 
					               </widget>
 | 
				
			||||||
              </item>
 | 
					              </item>
 | 
				
			||||||
              <item row="2" column="2" colspan="2" >
 | 
					              <item row="2" column="1" colspan="2" >
 | 
				
			||||||
               <widget class="QComboBox" name="preprocess" />
 | 
					               <widget class="QComboBox" name="preprocess" />
 | 
				
			||||||
              </item>
 | 
					              </item>
 | 
				
			||||||
              <item row="3" column="0" colspan="4" >
 | 
					              <item row="3" column="0" colspan="4" >
 | 
				
			||||||
@ -597,18 +597,15 @@
 | 
				
			|||||||
                </property>
 | 
					                </property>
 | 
				
			||||||
               </widget>
 | 
					               </widget>
 | 
				
			||||||
              </item>
 | 
					              </item>
 | 
				
			||||||
 | 
					              <item row="7" column="0" >
 | 
				
			||||||
 | 
					               <widget class="QLabel" name="label_21" >
 | 
				
			||||||
 | 
					                <property name="text" >
 | 
				
			||||||
 | 
					                 <string>Override<br>CSS</string>
 | 
				
			||||||
 | 
					                </property>
 | 
				
			||||||
 | 
					               </widget>
 | 
				
			||||||
 | 
					              </item>
 | 
				
			||||||
              <item row="7" column="1" >
 | 
					              <item row="7" column="1" >
 | 
				
			||||||
               <spacer>
 | 
					               <widget class="QTextEdit" name="gui_override_css" />
 | 
				
			||||||
                <property name="orientation" >
 | 
					 | 
				
			||||||
                 <enum>Qt::Vertical</enum>
 | 
					 | 
				
			||||||
                </property>
 | 
					 | 
				
			||||||
                <property name="sizeHint" >
 | 
					 | 
				
			||||||
                 <size>
 | 
					 | 
				
			||||||
                  <width>20</width>
 | 
					 | 
				
			||||||
                  <height>41</height>
 | 
					 | 
				
			||||||
                 </size>
 | 
					 | 
				
			||||||
                </property>
 | 
					 | 
				
			||||||
               </spacer>
 | 
					 | 
				
			||||||
              </item>
 | 
					              </item>
 | 
				
			||||||
             </layout>
 | 
					             </layout>
 | 
				
			||||||
            </widget>
 | 
					            </widget>
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user