MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / test_write_stabilized_with_links

Function test_write_stabilized_with_links

tests/test_story.py:156–230  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

154
155
156def test_write_stabilized_with_links():
157
158 def rectfn(rect_num, filled):
159 '''
160 We return one rect per page.
161 '''
162 rect = pymupdf.Rect(10, 20, 290, 380)
163 mediabox = pymupdf.Rect(0, 0, 300, 400)
164 #print(f'rectfn(): rect_num={rect_num} filled={filled}')
165 return mediabox, rect, None
166
167 def contentfn(positions):
168 ret = ''
169 ret += textwrap.dedent('''
170 <!DOCTYPE html>
171 <body>
172 <h2>Contents</h2>
173 <ul>
174 ''&#x27;)
175 for position in positions:
176 if position.heading and (position.open_close & 1):
177 text = position.text if position.text else ''
178 if position.id:
179 ret += f' <li><a href="#{position.id}">{text}</a>'
180 else:
181 ret += f' <li>{text}'
182 ret += f' page={position.page_num}\n'
183 ret += '</ul>\n'
184 ret += textwrap.dedent(f''&#x27;
185 <h1>First section</h1>
186 <p>Contents of first section.
187 <ul>
188 <li>External <a href="https://artifex.com/">link to https://artifex.com/</a>.
189 <li><a href="#idtest">Link to IDTEST</a>.
190 <li><a href="#nametest">Link to NAMETEST</a>.
191 </ul>
192
193 <h1>Second section</h1>
194 <p>Contents of second section.
195 <h2>Second section first subsection</h2>
196
197 <p>Contents of second section first subsection.
198 <p id="idtest">IDTEST
199
200 <h1>Third section</h1>
201 <p>Contents of third section.
202 <p><a name="nametest">NAMETEST</a>.
203
204 </body>
205 ''&#x27;)
206 return ret.strip()
207
208 document = pymupdf.Story.write_stabilized_with_links(contentfn, rectfn)
209
210 # Check links.
211 links = list()
212 for page in document:
213 links += page.get_links()

Callers

nothing calls this directly

Calls 4

get_linksMethod · 0.80
getMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…