Guest User

Untitled

a guest
Apr 23rd, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.42 KB | None | 0 0
  1. def toc_structure_from_headers(headers, toc = [])
  2.   if headers.empty?
  3.     return []
  4.   end
  5.  
  6.   depth = headers[0][:depth]
  7.  
  8.   while header = headers[0]
  9.     if header[:depth] == depth
  10.       toc << { id: header[:id], text: header[:text], children: [] }
  11.       headers.shift
  12.     elsif header[:depth] < depth
  13.       break;
  14.     else
  15.       toc_structure_from_headers(headers, toc[-1][:children])
  16.     end
  17.   end
  18.  
  19.   toc
  20. end
Advertisement
Add Comment
Please, Sign In to add comment