def make_text(doc): """ Export doc into a simple text format. if complex, go through nodes depth first, at each node, output name of node if node is leaf, run flatten on it """ index = model.library.get_index(doc["title"]) text = "\n".join([doc["title"], doc.get("heTitle", ""), doc["versionTitle"], doc["versionSource"]]) version = Version().load({'title': doc["title"], 'versionTitle': doc["versionTitle"], 'language': doc["language"]}) if "versions" in doc: if not len(doc["versions"]): return None # Occurs when text versions don't actually have content version = Version().load({'title': doc["title"], 'versionTitle': doc["versions"][0][0], 'language': doc["language"]}) text += "\nThis file contains merged sections from the following text versions:" for v in doc["versions"]: text += "\n-%s\n-%s" % (v[0], v[1]) def make_node(node, depth, **kwargs): if not node.children: content = "\n\n%s" % node.primary_title(doc["language"]) content += flatten(version.content_node(node), node.sectionNames) return "\n\n%s" % content else: return "\n\n%s" % node.primary_title(doc["language"]) def flatten(text, sectionNames): text = text or "" if len(sectionNames) == 1: text = [t if t else "" for t in text] # Bandaid for mismatch between text structure, join recursively if text # elements are lists instead of strings. return "\n".join([t if isinstance(t, basestring) else "\n".join(t) for t in text]) flat = "" for i in range(len(text)): section = section_to_daf(i + 1) if sectionNames[0] == "Daf" else str(i + 1) flat += "\n\n%s %s\n\n%s" % (sectionNames[0], section, flatten(text[i], sectionNames[1:])) return flat text += index.nodes.traverse_to_string(make_node) return text
def he_version(value): """ Returns the Hebrew translation of a version title, if it exists. """ version = Version().load({"versionTitle": value}) if not version: return value return getattr(version, "versionTitleInHebrew", value)
def _validate(self): from sefaria.model.text import library, Version if self.type == "index": assert self.content.get("index") assert library.get_index(self.content.get("index")) elif self.type == "version": i = self.content.get("index") v = self.content.get("version") l = self.content.get("language") assert i and v and l version = Version().load({ "title": i, "versionTitle": v, "language": l, }) assert version, "No Version Found: {}/{}/{}".format(i, v, l) elif self.type == "general": assert self.content.get("en"), "Please provide an English message." assert self.content.get("he"), "Please provide a Hebrew message." else: raise InputError("Unknown type for GlobalNotification: {}".format(self.type))