| 15820 | |
| 15821 | |
| 15822 | class Story: |
| 15823 | |
| 15824 | def __init__( self, html='', user_css=None, em=12, archive=None): |
| 15825 | buffer_ = mupdf.fz_new_buffer_from_copied_data( html.encode('utf-8')) |
| 15826 | if archive and not isinstance(archive, Archive): |
| 15827 | archive = Archive(archive) |
| 15828 | arch = archive.this if archive else mupdf.FzArchive( None) |
| 15829 | if hasattr(mupdf, 'FzStoryS'): |
| 15830 | self.this = mupdf.FzStoryS( buffer_, user_css, em, arch) |
| 15831 | else: |
| 15832 | self.this = mupdf.FzStory( buffer_, user_css, em, arch) |
| 15833 | |
| 15834 | def add_header_ids(self): |
| 15835 | ''' |
| 15836 | Look for `<h1..6>` items in `self` and adds unique `id` |
| 15837 | attributes if not already present. |
| 15838 | ''' |
| 15839 | dom = self.body |
| 15840 | i = 0 |
| 15841 | x = dom.find(None, None, None) |
| 15842 | while x: |
| 15843 | name = x.tagname |
| 15844 | if len(name) == 2 and name[0]=="h" and name[1] in "123456": |
| 15845 | attr = x.get_attribute_value("id") |
| 15846 | if not attr: |
| 15847 | id_ = f"h_id_{i}" |
| 15848 | #log(f"{name=}: setting {id_=}") |
| 15849 | x.set_attribute("id", id_) |
| 15850 | i += 1 |
| 15851 | x = x.find_next(None, None, None) |
| 15852 | |
| 15853 | @staticmethod |
| 15854 | def add_pdf_links(document_or_stream, positions): |
| 15855 | """ |
| 15856 | Adds links to PDF document. |
| 15857 | Args: |
| 15858 | document_or_stream: |
| 15859 | A PDF `Document` or raw PDF content, for example an |
| 15860 | `io.BytesIO` instance. |
| 15861 | positions: |
| 15862 | List of `ElementPosition`'s for `document_or_stream`, |
| 15863 | typically from Story.element_positions(). We raise an |
| 15864 | exception if two or more positions have same id. |
| 15865 | Returns: |
| 15866 | `document_or_stream` if a `Document` instance, otherwise a |
| 15867 | new `Document` instance. |
| 15868 | We raise an exception if an `href` in `positions` refers to an |
| 15869 | internal position `#<name>` but no item in `positions` has `id = |
| 15870 | name`. |
| 15871 | """ |
| 15872 | if isinstance(document_or_stream, Document): |
| 15873 | document = document_or_stream |
| 15874 | else: |
| 15875 | document = Document("pdf", document_or_stream) |
| 15876 | |
| 15877 | # Create dict from id to position, which we will use to find |
| 15878 | # link destinations. |
| 15879 | # |
no outgoing calls
no test coverage detected
searching dependent graphs…