mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Port TOCOffsetMap to rapydscript
This commit is contained in:
parent
d4f9c559c3
commit
d720400d77
@ -51,6 +51,40 @@ def text_to_regex(text):
|
||||
return ans.join('')
|
||||
|
||||
|
||||
class ToCOffsetMap:
|
||||
|
||||
def __init__(self, toc_nodes, offset_map, previous_toc_node, parent_map):
|
||||
self.toc_nodes = toc_nodes or v'[]'
|
||||
self.offset_map = offset_map or {}
|
||||
self.previous_toc_node = previous_toc_node or None
|
||||
self.parent_map = parent_map or {}
|
||||
|
||||
def toc_nodes_for_offset(self, offset):
|
||||
matches = v'[]'
|
||||
for node in self.toc_nodes:
|
||||
q = self.offset_map[node.id]
|
||||
if q?:
|
||||
if q > offset:
|
||||
break
|
||||
matches.push(node)
|
||||
if not matches and self.previous_toc_node:
|
||||
matches.push(self.previous_toc_node)
|
||||
ans = v'[]'
|
||||
if matches:
|
||||
ancestors = v'[]'
|
||||
node = matches[-1]
|
||||
parent = self.parent_map[node.id]
|
||||
while parent?:
|
||||
ancestors.push(parent)
|
||||
parent = self.parent_map[parent.id]
|
||||
if len(ancestors) > 1:
|
||||
ancestors.pop() # root node
|
||||
for v'var i = ancestors.length; i-- > 0;':
|
||||
ans.push(ancestors[i])
|
||||
ans.push(node)
|
||||
return ans
|
||||
|
||||
|
||||
class Worker:
|
||||
|
||||
def __init__(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user