Commits

Nikolaj Sjujskij committed d3d1753

Strip trailing spaces everywhere

Comments (0)

Files changed (6)

rst2epub/container.py

 
 class Container(Document):
     """Represents container.xml see 3.5.1 in [OCF10]_
-    
+
        .. [OCF10] OEBPS Container Format (OCF) 1.0
           http://www.idpf.org/ocf/ocf1.0/download/ocf10.htm
 
-    
+
         This file is required.
     """
 
     path = "META-INF/container.xml"
     """Path of the container.xml specified by OCF1.0"""
-    
+
     namespace = "urn:oasis:names:tc:opendocument:xmlns:container"
     """Namespace for container.xml"""
-    
+
     version = "1.0"
     """Container file version supported by this implemntation."""
-    
+
     def __init__(self):
         Document.__init__(self)
 
         self.appendChild(container)
         container.setAttribute("xmlns", self.namespace)
         container.setAttribute("version", self.version)
-        
+
         self.rootfiles = self.createElement("rootfiles")
         container.appendChild(self.rootfiles)
-        
+
     def add_rootfile(self, full_path, media_type):
         """Creates and adds a rootfile elemnt to the container.
-        
+
             full_path   must be relative to the physical container root
         """
         rootfile = self.createElement("rootfile")
         rootfile.setAttribute("full-path", full_path)
         rootfile.setAttribute("media-type", media_type)
-        
+
         self.rootfiles.appendChild(rootfile)
-    
+
     def to_xml(self):
         return self.toprettyxml(indent="    ", encoding="utf-8")

rst2epub/converter.py

     identifier = "TODO"
     language = "en_TODO"
     writer = EpubWriter(sys.argv[2], title, identifier, language)
-    
+
     writer.container.add_rootfile(Package.path, Package.media_type)
-   
+
     writer.package.manifest.add_item("item1", "content/content.xhtml", "application/xhtml+xml")
     writer.package.spine.add_itemref("item1")
-    
+
     writer.ncx.nav_map.add_nav_point("content", 1, "Content", "content/content.xhtml")
-    
+
     writer.add_content(rst.to_xhtml())
     writer.write()
-    
+
 
 if __name__ == "__main__":
     main()
 
     media_type = "application/epub+zip"
     """EPUB media type see 3.4 in OCF1.0"""
-    
+
     extension = ".epub"
     """EPUB file extension see 3.4 in OCF1.0"""
 
         self.title = title
         self.identifier = identifier
         self.language = language
-        
+
         # create the zip container with correct mimetype
         self.file = ZipFile(to_file, "w", ZIP_STORED)
         self.file.writestr("mimetype", EpubWriter.media_type)
-        
+
         # create required container
         # user must add required rootfile
         self.container = Container()
-        
+
         # create required package / OPF file
         self.package = Package()
-        
+
         # ...add required metadata fields
         self.package.metadata.add_title(self.title)
         self.package.metadata.add_identifier(self.identifier, True)
         self.package.metadata.add_language("en")
-        
+
         # create required NCX file
         # ... add required header fields
         # TODO: '1' should not be hard coded
         self.ncx = NavigationControl(self.identifier, 1, self.title)
-        
+
     def add_content(self, content):
-        self.content = content 
-        
+        self.content = content
+
     def write(self):
         self.file.writestr(self.container.path, self.container.to_xml())
         self.file.writestr(Package.path, self.package.to_xml())
         self.file.writestr(NavigationControl.path, self.ncx.to_xml())
-        
-        self.write_content()       
-        
+
+        self.write_content()
+
         self.file.close()
- 
-        
-        
+
+
+
     def write_content(self):
         self.file.writestr("content/content.xhtml", self.content)
-  
-  
+
+
 if __name__ == "__main__":
     writer = EpubWriter("ebook.epub",   # name of generated file
                         "My Title",     # required infos
                         "unique-id",
                         "en"
                        )
-    
+
     # add root file
     writer.container.add_rootfile(Package.path, Package.media_type)
-    
+
     content = """<?xml version="1.0" encoding="UTF-8" ?>
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
                            "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
                      </head>
                      <body><p>This is a test epub file.</p></body>
                  </html>
-              """    
+              """
     writer.add_content(content)
-                       
+
     # list content items in manifest
     # TODO should be generated when content is added
     writer.package.manifest.add_item("item1", "content/content.xhtml", "application/xhtml+xml")
-    
+
     # add content reference to spine
     # TODO should be generated when content is added
     writer.package.spine.add_itemref("item1")
-    
+
     # reference start of content from guide -- optional
     guide = writer.package.create_guide()
     guide.add_reference("text", "Content", "content/content.xhtml")
-    
+
     # add content to nav map
     # TODO should be generated when content is added
     writer.ncx.nav_map.add_nav_point("content", 1, "Content", "content/content.xhtml")
-    
-    writer.write()
+
+    writer.write()
 
 class NavigationControl(Document):
     """Represents NCX filet see [OPF20]_ and [NCX]_.
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
        .. [NCX] Navigation Control File (NCX) (in ANSI/NISO Z39.86-2005)
           http://www.niso.org/workrooms/daisy/Z39-86-2005.html#NCX
 
-    
+
         This file is required exactly once.
     """
-    
+
     extension = ".ncx"
     """File extension."""
-    
+
     media_type = "application/x-dtbncx+xml"
     """Media typ as specified in [OPF20]_."""
-    
+
     path = "toc" + extension    # conventions used in this implementation
     """Path of the OPF Package Document within the container."""
-    
+
     namespace = "http://www.daisy.org/z3986/2005/ncx/"
     """Namespace for OPF Package Document."""
-    
+
     version = "2005-1"
     """NCX file version supported by this implemntation."""
 
-    
+
     def __init__(self, dtb_uid, dtb_depth, title):
         Document.__init__(self)
-        
+
         self.ncx = self.createElement("ncx")
         self.appendChild(self.ncx)
         self.ncx.setAttribute("version", self.version)
         self.ncx.setAttribute("xmlns", self.namespace)
-        
+
         self.head = self.create_head(dtb_uid, dtb_depth)
         self.doc_title = self.create_doc_title(title)
         self.nav_map = self.create_nav_map()
-        
+
     def create_head(self, dtb_uid, dtb_depth, dtb_totalPageCount=0,
                     dtb_bacPageNumber=0):
         head = Head(self, dtb_uid, str(dtb_depth), str(dtb_totalPageCount),
                     str(dtb_bacPageNumber))
         self.ncx.appendChild(head)
         return head
-        
+
     def create_doc_title(self, title):
         doc_title = DocTitle(self, title)
         self.ncx.appendChild(doc_title)
         return doc_title
-    
+
     def create_nav_map(self):
         nav_map = NavMap(self)
         self.ncx.appendChild(nav_map)
         return nav_map
-    
+
     def create_text_element(self, value):
         text = self.createElement("text")
         text.appendChild(self.createTextNode(value))
         return text
-    
+
     def to_xml(self):
         return self.toprettyxml(indent="    ", encoding="utf-8")
 
- 
+
 class Head(Element):
     """Represents head element see [NCX]_.
-    
+
        Only meta elements are used by OPF.
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
        .. [NCX] Navigation Control File (NCX) (in ANSI/NISO Z39.86-2005)
           http://www.niso.org/workrooms/daisy/Z39-86-2005.html#NCX
-    
+
         This element is required.
     """
 
     def __init__(self, nxc_document, dtb_uid, dtb_depth,
-                 dtb_totalPageCount, dtb_bacPageNumber):    
+                 dtb_totalPageCount, dtb_bacPageNumber):
         Element.__init__(self, "head")
         self.ownerDocument = nxc_document
-        
+
         # add required meta elements
         self.add_meta("dtb:uid", dtb_uid)
         self.add_meta("dtb:depth", dtb_depth)
         # not used by OPF - shoulg be 0
-        self.add_meta("dtb:totalPageCount", dtb_totalPageCount) 
+        self.add_meta("dtb:totalPageCount", dtb_totalPageCount)
         self.add_meta("dtb:maxPageNumber", dtb_bacPageNumber)
-        
+
     def add_meta(self, name, content):
         element = self.ownerDocument.createElement("meta")
         self.appendChild(element)
         element.setAttribute("name", name)
         element.setAttribute("content", content)
-        
+
 class DocTitle(Element):
     """Represents docTitle element see [NCX]_.
-    
+
        audio element is not used in OPF.
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
        .. [NCX] Navigation Control File (NCX) (in ANSI/NISO Z39.86-2005)
           http://www.niso.org/workrooms/daisy/Z39-86-2005.html#NCX
-    
+
         This element is required.
     """
 
-    def __init__(self, nxc_document, text):    
+    def __init__(self, nxc_document, text):
         Element.__init__(self, "docTitle")
         self.ownerDocument = nxc_document
-        
+
         self.appendChild(self.ownerDocument.create_text_element(text))
 
 class NavMap(Element):
     """Represents navMap element see [NCX]_.
 
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
        .. [NCX] Navigation Control File (NCX) (in ANSI/NISO Z39.86-2005)
           http://www.niso.org/workrooms/daisy/Z39-86-2005.html#NCX
-    
+
         This element is required.
     """
 
-    def __init__(self, nxc_document):    
+    def __init__(self, nxc_document):
         Element.__init__(self, "navMap")
         self.ownerDocument = nxc_document
 
         self.appendChild(point)
         point.setAttribute("id", id)
         point.setAttribute("playOrder", str(play_order))
-        
+
         label = self.ownerDocument.createElement("navLabel")
         point.appendChild(label)
         label.appendChild(self.ownerDocument.create_text_element(nav_label))
-        
+
         content = self.ownerDocument.createElement("content")
         point.appendChild(content)
         content.setAttribute("src", content_src)

rst2epub/package.py

 
 class Package(Document):
     """Represents OPF Package Document see [OPF20]_.
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
 
-    
+
         This file is required exactly once.
     """
-    
+
     extension = ".opf"
     """File extension as specified in [OPF20]_."""
-    
+
     media_type = "application/oebps-package+xml"
     """Media typ as specified in [OPF20]_."""
-    
+
     path = "content" + extension    # conventions used in this implementation
     """Path of the OPF Package Document within the container."""
-    
+
     namespace = "http://www.idpf.org/2007/opf"
     """Namespace for OPF Package Document."""
-    
+
     version = "2.0"
     """OPF Package Document file version supported by this implemntation."""
 
     primary_identifier = "primaryID"
     """ID to identifier primaryID."""
-    
+
     def __init__(self):
         Document.__init__(self)
 
         self.package.setAttribute("version", self.version)
         self.package.setAttribute("xmlns", self.namespace)
         self.package.setAttribute("unique-identifier", self.primary_identifier)
-        
+
         self.metadata = self.create_metadata()
         self.manifest = self.create_manifest()
         self.spine = self.create_spine()
-        
+
 
     def create_metadata(self):
         """Creata and append a new metadata element."""
         manifest = Manifest(self)
         self.package.appendChild(manifest)
         return manifest
-        
+
     def create_spine(self):
         """Creata and append a new spine element."""
         spine = Spine(self, self.manifest.get_ncx_id())
         self.package.appendChild(spine)
         return spine
-    
+
     def create_guide(self):
         """Creata and append a new guide element."""
         self.guide = Guide(self)
         self.package.appendChild(self.guide)
         return self.guide
-    
+
     def to_xml(self):
         return self.toprettyxml(indent="    ", encoding="utf-8")
- 
- 
+
+
 class Metadata(Element):
     """Represents Metadata element see 2.2 in [OPF20]_.
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
-    
-        This element is required. Must have at least one of each title, 
+
+        This element is required. Must have at least one of each title,
         identifier, language.
     """
-    
+
     dc_namespace = "http://purl.org/dc/elements/1.1/"
     """Namespace for Dublin Core elemnts."""
-    
+
     def __init__(self, package_document):
         """Should be called by Package.create_metadata."""
-    
+
         Element.__init__(self, "metadata")
         self.ownerDocument = package_document
         self.setAttribute("xmlns:dc", self.dc_namespace)
         self.setAttribute("xmlns:opf", Package.namespace)
 
     def _create_dc_metadata_element(self, name, value):
-        """Creates and append a Doublin Core metadataelement."""    
+        """Creates and append a Doublin Core metadataelement."""
         element = self.ownerDocument.createElement("dc:" + name)
         element.appendChild(self.ownerDocument.createTextNode(value))
         self.appendChild(element)
         return element
-    
+
     def add_title(self, title):
         """See 2.2.1 in [OPF20]_."""
         self._create_dc_metadata_element("title", title)
-        
+
     def add_creator(self):
         """See 2.2.2 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_subject(self):
         """See 2.2.3 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_description(self):
         """See 2.2.4 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_publisher(self):
         """See 2.2.5 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_contributor(self):
         """See 2.2.6 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_date(self):
         """See 2.2.7 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_type(self):
         """See 2.2.8 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_format(self):
         """See 2.2.9 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_identifier(self, identifier, primary=False, scheme=None):
         """See 2.2.10 in [OPF20]_."""
         element = self._create_dc_metadata_element("identifier", "identifier")
-        
+
         if primary:
             element.setAttribute("id", Package.primary_identifier)
-            
+
         if scheme:
             element.setAttribute("opf:scheme", scheme)
-        
+
     def add_source(self):
         """See 2.2.11 in [OPF20]_."""
         raise NotImplementedError
-            
+
     def add_language(self, language):
         """See 2.2.12 in [OPF20]_."""
         self._create_dc_metadata_element("language", language)
-        
+
     def add_relation(self):
         """See 2.2.13 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_coverage(self):
         """See 2.2.14 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_rights(self):
         """See 2.2.15 in [OPF20]_."""
         raise NotImplementedError
-        
+
     def add_meta(self, name, content):
         element = self.ownerDocument.createElement("meta")
         element.setAttribute("name", name)
         element.setAttribute("content", content)
         self.appendChild(element)
 
-        
+
 class Manifest(Element):
     """Represents Manifest element see 2.3 in [OPF20]_.
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
-    
+
         This element is required.
     """
 
     def __init__(self, package_document):
         Element.__init__(self, "manifest")
         self.ownerDocument = package_document
-        
+
         # add item for required ncx file
         self.add_item("ncx", "toc.ncx", "application/x-dtbncx+xml")
 
         item.setAttribute("href", href)
         item.setAttribute("media-type", media_type)
         # TODO support fallback mechanism
-    
+
     def get_ncx_id(self):
         ncx_media_type = "application/x-dtbncx+xml"
         for item in self.getElementsByTagName("item"):
 
 class Spine(Element):
     """Represents Spine element see 2.4 in [OPF20]_.
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
-    
+
         This element is required after manifest.
     """
 
         Element.__init__(self, "spine")
         self.ownerDocument = package_document
         self.setAttribute("toc", ncx_id)
- 
+
     def add_itemref(self, idref, linear=True):
         itemref = self.ownerDocument.createElement("itemref")
         self.appendChild(itemref)
         itemref.setAttribute("idref", idref)
-        
+
         if not linear:
             itemref.setAttribute("linear", "no")
         # default if no linear attribute present
 
 class Guide(Element):
     """Represents Guide element see 2.6 in [OPF20]_.
-    
+
        .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
           http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
-    
+
         This element is optional.
     """
- 
+
     def __init__(self, package_document):
         Element.__init__(self, "guide")
         self.ownerDocument = package_document
- 
+
     def add_reference(self, type, title, href):
         reference = self.ownerDocument.createElement("reference")
         self.appendChild(reference)
         reference.setAttribute("type", type)
         reference.setAttribute("title", title)
         reference.setAttribute("href", href)
- 
 from xml.etree import ElementTree
 
 class RstDocument:
-    
+
     def __init__(self, source):
         self.source = source
         with open(self.source, "r") as txt:
             document.settings.tab_width = 4
             document.settings.pep_references = 1
             document.settings.rfc_references = 1
-            
+
             parser = docutils.parsers.rst.Parser()
-            
+
             parser.parse(txt.read(), document)
             self.dom = document.asdom()
-        
+
     def _get_field_value(self, field_name, path_to_value):
         element = ElementTree.fromstring(self.dom.toxml())
         for element in element.findall("*//field_list/field"):
             if element.findtext("./field_name") == field_name:
-                return element.findtext(path_to_value)   
-    
+                return element.findtext(path_to_value)
+
     # def get_author(self):
         # return self._get_field_value("Author", "./field_body//*")
 
     # def get_contact(self):
         # return self._get_field_value("Contact", "./field_body/paragraph//*")
-    
+
     def get_title(self):
         element = ElementTree.fromstring(self.dom.toxml())
         return element.findtext("section/title")
-        
+
     def to_xhtml(self):
         content = """<?xml version="1.0" encoding="UTF-8" ?>
                      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
                      </html>
                   """
         with open(self.source, "r") as source:
-        
+
             # disable external data insertion
             # http://docutils.sourceforge.net/docs/howto/security.html
             settings = dict(file_insertion_enabled=False, raw_enabled=False)
-            
+
             body = publish_parts(source.read(), writer_name='html',
                                     settings_overrides=settings)['html_body']
             content = content % (self.get_title(), body)
-            
+
             # FIXME name attributes are not allowed
             dom = parseString(content)
             for element in dom.getElementsByTagName("a"):
                 if element.hasAttribute("name"):
                     element.removeAttribute("name")
-                    
-            return dom.toprettyxml(indent="    ")
+
+            return dom.toprettyxml(indent="    ")