+
+ def snip(self, words, before=None, sub=False):
+ if sub and self.ASIDE:
+ return words, []
+
+ snippet = []
+ if before is not None:
+ i = self.index(before)
+ else:
+ i = len(self)
+
+ while i > 0:
+ i -= 1
+ if self[i].tail:
+ if words:
+ words, text = last_words(self[i].tail, words)
+ snippet = [('text', text)] + snippet
+
+ if words:
+ words, subsnip = self[i].snip(words, sub=True)
+ snippet = subsnip + snippet
+
+ if words and self.text:
+ words, text = last_words(self.text, words)
+ snippet = [('text', text)] + snippet
+
+ snippet = [('start', self.tag, self.attrib)] + snippet + [('end',)]
+
+ if not sub and words and not self.ASIDE:
+ # do we dare go up?
+ parent = self.getparent()
+ if parent is not None and parent.CAN_HAVE_TEXT:
+ words, parsnip = parent.snip(words, before=self)
+ return words, parsnip[:-1] + snippet + parsnip[-1:]
+
+ return words, snippet
+
+ def get_snippet(self, words=15):
+ from librarian.parser import parser
+
+ words, snippet = self.getparent().snip(words=words, before=self)
+
+ cursor = snipelem = parser.makeelement('snippet')
+ snipelem._meta_object = self.meta
+ for s in snippet:
+ if s[0] == 'start':
+ elem = parser.makeelement(s[1], **s[2])
+ cursor.append(elem)
+ cursor = elem
+ elif s[0] == 'end':
+ cursor = cursor.getparent()
+ else:
+ if len(cursor):
+ cursor[-1].tail = (cursor[-1].tail or '') + s[1]
+ else:
+ cursor.text = (cursor.text or '') + s[1]
+
+ return snipelem
+
+ @property
+ def numbering(self):
+ numbering = self.NUMBERING
+ if numbering is None or self.in_context_of('DISABLE_NUMBERING'):
+ return None
+ numbering = self.get_context_map('SUPPRESS_NUMBERING', numbering, numbering)
+ return numbering
+
+ @property
+ def id_prefix(self):
+ prefix = self.numbering
+ if prefix == 'main':
+ # TODO: self.context.main_numbering_prefix
+ prefix = 'f' # default numbering prefix
+ return prefix
+
+ def assign_id(self, document):
+ numbering = self.numbering
+ if numbering:
+ number = str(document.counters[numbering])
+ self.attrib['_id'] = self.id_prefix + number
+ document.counters[numbering] += 1
+
+ if numbering == 'main':
+ self.attrib['_visible_numbering'] = str(document.counters['_visible'])
+ document.counters['_visible'] += 1
+
+ if numbering == 'fn':
+ self.attrib['_visible_numbering'] = number
+
+ def get_link(self):
+ return self.attrib.get('_id') or self.getparent().get_link()
+
+
+class Snippet(WLElement):
+ pass