def citation_links(db, config, i): """ Returns links to a PhiloLogic object and all its ancestors.""" if config.skip_table_of_contents is False: doc_href = make_absolute_object_link(config, i.philo_id[:1]) + "/table-of-contents" else: doc_href = make_absolute_object_link(config, i.philo_id[:1], i.bytes) div1_href = make_absolute_object_link(config, i.philo_id[:2], i.bytes) div2_href = make_absolute_object_link(config, i.philo_id[:3], i.bytes) div3_href = make_absolute_object_link(config, i.philo_id[:4], i.bytes) page_href = make_absolute_object_link(config, i.page.philo_id, i.bytes) try: line_href = make_absolute_object_link(config, i.line.philo_id, i.bytes) except AttributeError: line_href = "" links = { "doc": doc_href, "div1": div1_href, "div2": div2_href, "div3": div3_href, "para": "", "page": page_href, "line": line_href, } for metadata_type in db.locals["metadata_types"].values(): if metadata_type == "para": links["para"] = make_absolute_object_link(config, i.philo_id[:5], i.bytes) break return links
def citation_links(db, config, i): """ Returns links to a PhiloLogic object and all its ancestors.""" if config.skip_table_of_contents is False: doc_href = make_absolute_object_link(config, i.philo_id[:1]) + "/table-of-contents" else: doc_href = make_absolute_object_link(config, i.philo_id[:1], i.bytes) div1_href = make_absolute_object_link(config, i.philo_id[:2], i.bytes) div2_href = make_absolute_object_link(config, i.philo_id[:3], i.bytes) div3_href = make_absolute_object_link(config, i.philo_id[:4], i.bytes) page_href = make_absolute_object_link(config, i.page.philo_id, i.bytes) try: line_href = make_absolute_object_link(config, i.line.philo_id, i.bytes) except AttributeError: line_href = "" links = { "doc": doc_href, "div1": div1_href, "div2": div2_href, "div3": div3_href, "para": "", "page": page_href, "line": line_href, } for field, metadata_type in db.locals["metadata_types"].items(): if metadata_type == "para": links["para"] = make_absolute_object_link(config, i.philo_id[:5], i.bytes) break return links
def cite_linker(hit, citation_object, citation_hrefs, config, report): """Get links""" href = None if citation_object["link"]: if citation_object["object_level"] == "doc": if citation_object["field"] == "title" or citation_object["field"] == "filename": href = citation_hrefs['doc'] elif report == "bibliography" and citation_object["field"] == "head": href = make_absolute_object_link(config, hit.philo_id) else: params = [("report", "bibliography"), (citation_object["field"], '"%s"' % hit[citation_object["field"]])] href = make_absolute_query_link(config, params) else: href = citation_hrefs[citation_object["object_level"]] return href
def cite_linker(hit, citation_object, citation_hrefs, config, report): """Get links""" href = None if citation_object["link"]: if citation_object["object_level"] == "doc": if citation_object["field"] == "title" or citation_object["field"] == "filename": href = citation_hrefs["doc"] elif report == "bibliography" and citation_object["field"] == "head": href = make_absolute_object_link(config, hit.philo_id) else: params = [ ("report", "bibliography"), (citation_object["field"], '"%s"' % hit[citation_object["field"]]), ] href = make_absolute_query_link(config, params) else: href = citation_hrefs[citation_object["object_level"]] return href
def citation_links(db, config, i): """ Returns links to a PhiloLogic object and all its ancestors.""" doc_href = make_absolute_object_link(config, i.philo_id[:1]) + '/table-of-contents' div1_href = make_absolute_object_link(config, i.philo_id[:2], i.bytes) div2_href = make_absolute_object_link(config, i.philo_id[:3], i.bytes) div3_href = make_absolute_object_link(config, i.philo_id[:4], i.bytes) page_href = make_absolute_object_link(config, i.page.philo_id, i.bytes) try: line_href = make_absolute_object_link(config, i.line.philo_id, i.bytes) except AttributeError: line_href = "" links = {"doc": doc_href, "div1": div1_href, "div2": div2_href, "div3": div3_href, "para": "", "page": page_href, "line": line_href} for field, metadata_type in six.iteritems(db.locals["metadata_types"]): if metadata_type == 'para': links['para'] = make_absolute_object_link(config, i.philo_id[:5], i.bytes) break return links
def generate_toc_object(request, config): """This function fetches all philo_ids for div elements within a doc""" db = DB(config.db_path + '/data/') conn = db.dbh cursor = conn.cursor() try: obj = db[request.philo_id] except ValueError: philo_id = ' '.join(request.path_components[:-1]) obj = db[philo_id] doc_id = int(obj.philo_id[0]) next_doc_id = doc_id + 1 # find the starting rowid for this doc cursor.execute('select rowid from toms where philo_id="%d 0 0 0 0 0 0"' % doc_id) start_rowid = cursor.fetchone()[0] # find the starting rowid for the next doc cursor.execute('select rowid from toms where philo_id="%d 0 0 0 0 0 0"' % next_doc_id) try: end_rowid = cursor.fetchone()[0] except TypeError: # if this is the last doc, just get the last rowid in the table. cursor.execute('select max(rowid) from toms;') end_rowid = cursor.fetchone()[0] # use start_rowid and end_rowid to fetch every div in the document. philo_slices = {"doc": 1, "div1": 2, "div2": 3, "div3": 4, "para": 5} text_hierarchy = [] cursor.execute("select * from toms where rowid >= ? and rowid <=? and philo_type>='div' and philo_type<='div3'", (start_rowid, end_rowid)) for row in cursor: philo_id = [int(n) for n in row["philo_id"].split(" ")] text = HitWrapper.ObjectWrapper(philo_id, db, row=row) if text['philo_name'] == '__philo_virtual' and text["philo_type"] != "div1": continue elif text['word_count'] == 0: continue else: philo_id = text['philo_id'] philo_type = text['philo_type'] display_name = "" if text['philo_name'] == "front": display_name = "Front Matter" elif text['philo_name'] == "note": continue else: display_name = text['head'] if display_name: display_name = display_name.strip() if not display_name: if text["type"] and text["n"]: display_name = text['type'] + " " + text["n"] else: display_name = text["head"] or text['type'] or text['philo_name'] or text['philo_type'] if display_name == "__philo_virtual": display_name = text['philo_type'] display_name = display_name[0].upper() + display_name[1:] link = make_absolute_object_link(config, philo_id.split()[:philo_slices[philo_type]]) philo_id = ' '.join(philo_id.split()[:philo_slices[philo_type]]) toc_element = {"philo_id": philo_id, "philo_type": philo_type, "label": display_name, "href": link} text_hierarchy.append(toc_element) metadata_fields = {} for metadata in db.locals['metadata_fields']: if db.locals['metadata_types'][metadata] == "doc": metadata_fields[metadata] = obj[metadata] citation_hrefs = citation_links(db, config, obj) citation = citations(obj, citation_hrefs, config, report="navigation") toc_object = {"query": dict([i for i in request]), "philo_id": obj.philo_id, "toc": text_hierarchy, "metadata_fields": metadata_fields, "citation": citation} return toc_object
def generate_toc_object(request, config): """This function fetches all philo_ids for div elements within a doc""" db = DB(config.db_path + "/data/") conn = db.dbh cursor = conn.cursor() try: obj = db[request.philo_id] except ValueError: philo_id = " ".join(request.path_components[:-1]) obj = db[philo_id] doc_id = int(obj.philo_id[0]) next_doc_id = doc_id + 1 # find the starting rowid for this doc cursor.execute('select rowid from toms where philo_id="%d 0 0 0 0 0 0"' % doc_id) start_rowid = cursor.fetchone()[0] # find the starting rowid for the next doc cursor.execute('select rowid from toms where philo_id="%d 0 0 0 0 0 0"' % next_doc_id) try: end_rowid = cursor.fetchone()[0] except TypeError: # if this is the last doc, just get the last rowid in the table. cursor.execute("select max(rowid) from toms;") end_rowid = cursor.fetchone()[0] # use start_rowid and end_rowid to fetch every div in the document. philo_slices = {"doc": 1, "div1": 2, "div2": 3, "div3": 4, "para": 5} text_hierarchy = [] cursor.execute( "select * from toms where rowid >= ? and rowid <=? and philo_type>='div' and philo_type<='div3'", (start_rowid, end_rowid), ) for row in cursor: philo_id = [int(n) for n in row["philo_id"].split(" ")] text = HitWrapper.ObjectWrapper(philo_id, db, row=row) if text["philo_name"] == "__philo_virtual" and text[ "philo_type"] != "div1": continue elif text["word_count"] == 0: continue else: philo_id = text["philo_id"] philo_type = text["philo_type"] display_name = "" if text["philo_name"] == "front": display_name = "Front Matter" elif text["philo_name"] == "note": continue else: display_name = text["head"] if display_name: display_name = display_name.strip() if not display_name: if text["type"] and text["n"]: display_name = text["type"] + " " + text["n"] else: display_name = text["head"] or text["type"] or text[ "philo_name"] or text["philo_type"] if display_name == "__philo_virtual": display_name = text["philo_type"] display_name = display_name[0].upper() + display_name[1:] link = make_absolute_object_link( config, philo_id.split()[:philo_slices[philo_type]]) philo_id = " ".join(philo_id.split()[:philo_slices[philo_type]]) toc_element = { "philo_id": philo_id, "philo_type": philo_type, "label": display_name, "href": link } text_hierarchy.append(toc_element) metadata_fields = {} for metadata in db.locals["metadata_fields"]: if db.locals["metadata_types"][metadata] == "doc": metadata_fields[metadata] = obj[metadata] citation_hrefs = citation_links(db, config, obj) citation = citations(obj, citation_hrefs, config, report="table_of_contents") toc_object = { "query": dict([i for i in request]), "philo_id": obj.philo_id, "toc": text_hierarchy, "metadata_fields": metadata_fields, "citation": citation, } return toc_object