Get a unique reference if there are duplicates.
(self, reference: str, found: bool = False)
| 136 | self.last_refs = {} |
| 137 | |
| 138 | def unique_ref(self, reference: str, found: bool = False) -> str: |
| 139 | """ Get a unique reference if there are duplicates. """ |
| 140 | if not found: |
| 141 | return reference |
| 142 | |
| 143 | original_ref = reference |
| 144 | # Resume from the last reference issued for this footnote. Every reference |
| 145 | # before it is already in `used_refs`, so restarting from `original_ref` would |
| 146 | # only rebuild them all again, which is quadratic in the number of duplicates. |
| 147 | reference = self.last_refs.get(original_ref, original_ref) |
| 148 | while reference in self.used_refs: |
| 149 | ref, rest = reference.split(self.get_separator(), 1) |
| 150 | m = RE_REF_ID.match(ref) |
| 151 | if m: |
| 152 | reference = '%s%d%s%s' % (m.group(1), int(m.group(2))+1, self.get_separator(), rest) |
| 153 | else: |
| 154 | reference = '%s%d%s%s' % (ref, 2, self.get_separator(), rest) |
| 155 | |
| 156 | self.used_refs.add(reference) |
| 157 | self.last_refs[original_ref] = reference |
| 158 | if original_ref in self.found_refs: |
| 159 | self.found_refs[original_ref] += 1 |
| 160 | else: |
| 161 | self.found_refs[original_ref] = 1 |
| 162 | return reference |
| 163 | |
| 164 | def findFootnotesPlaceholder( |
| 165 | self, root: etree.Element |
no test coverage detected