MCPcopy Create free account
hub / github.com/PageBot/PageBot / typesetFile

Method typesetFile

Lib/pagebot/base/typesetter.py:738–782  ·  view source on GitHub ↗

Read the XML document and parse it into a tree of document-chapter nodes. Make the typesetter start at page pageNumber and find the name of the flow in the page template. The optional filter can be a list of tag names that need to be included in the composition, ignoring the

(self, fileName, e=None, xPath=None, patterns=None)

Source from the content-addressed store, hash-verified

736 return self.galley
737
738 def typesetFile(self, fileName, e=None, xPath=None, patterns=None):
739 """Read the XML document and parse it into a tree of document-chapter
740 nodes. Make the typesetter start at page pageNumber and find the name
741 of the flow in the page template. The optional filter can be a list of
742 tag names that need to be included in the composition, ignoring the
743 rest.
744
745 The optional rootStyle can be defined as style for the root tag,
746 cascading force all child elements. Answer the root node."""
747 fileExtension = fileName.split('.')[-1]
748 if fileExtension.lower() == 'md':
749 # If we have MarkDown content, convert to XML (XHTML)
750 f = codecs.open(fileName, mode="r", encoding="utf-8")
751 mdText = f.read() # Read the raw MarkDown source
752 f.close()
753
754 # Pre-filtering, to replace easier (for authors) patterns by ~~~...~~~
755 # Python instructions.
756 if patterns is not None:
757 for pattern, pythonCode in patterns:
758 mdText = mdText.replace(pattern, pythonCode)
759
760 fileName = self.markDown2XmlFile(fileName, mdText) # Translate MarkDown to HTML and save in file.
761
762 tree = ET.parse(fileName)
763 self.root = tree.getroot() # Get the root element of the tree and store for later retrieval.
764
765 # If XSL filtering is defined, they get the filtered nodes.
766 if xPath is not None:
767 filteredNodes = self.root.findall(xPath)
768 if filteredNodes:
769 # How to handle if we got multiple result nodes?
770 self.typesetNode(filteredNodes[0], e)
771 else:
772 # Collect all flowing text in one formatted string, while
773 # simulating the page/flow, because we need to keep track on which
774 # page/flow nodes results get positioned (e.g. for toc-head
775 # reference, image index and footnote placement.
776 self.typesetNode(self.root, e)
777
778 # Remember this galley where it came from.
779 self.galley.name = fileName
780
781 # Answer the self.galley.
782 return self.galley
783
784if __name__ == '__main__':
785 import doctest

Callers 3

typesetMarkdownMethod · 0.95
typesetMethod · 0.95
produceMethod · 0.95

Calls 3

markDown2XmlFileMethod · 0.95
typesetNodeMethod · 0.95
openMethod · 0.45

Tested by

no test coverage detected