def get_tags_config(config): """Fetch needs config from our config file""" # Probably "citation" unless this file gets renamed function = config.get("rank_method", "function") write_message("config function %s" % function, verbose=9) tags = {} # 037a: contains (often) the "hep-ph/0501084" tag of THIS record try: tag = config.get(function, "primary_report_number") except ConfigParser.NoOptionError: tags['record_pri_number'] = None else: tags['record_pri_number'] = tagify(parse_tag(tag)) # 088a: additional short identifier for the record try: tag = config.get(function, "additional_report_number") except ConfigParser.NoOptionError: tags['record_add_number'] = None else: tags['record_add_number'] = tagify(parse_tag(tag)) # 999C5r. this is in the reference list, refers to other records. # Looks like: hep-ph/0408002 try: tag = config.get(function, "reference_via_report_number") except ConfigParser.NoOptionError: tags['refs_report_number'] = None else: tags['refs_report_number'] = tagify(parse_tag(tag)) # 999C5s. this is in the reference list, refers to other records. # Looks like: Phys.Rev.,A21,78 try: tag = config.get(function, "reference_via_pubinfo") except ConfigParser.NoOptionError: tags['refs_journal'] = None else: tags['refs_journal'] = tagify(parse_tag(tag)) # 999C5a. this is in the reference list, refers to other records. # Looks like: 10.1007/BF03170733 try: tag = config.get(function, "reference_via_doi") except ConfigParser.NoOptionError: tags['refs_doi'] = None else: tags['refs_doi'] = tagify(parse_tag(tag)) # Fields needed to construct the journals for this record try: tag = { 'pages': config.get(function, "pubinfo_journal_page"), 'year': config.get(function, "pubinfo_journal_year"), 'journal': config.get(function, "pubinfo_journal_title"), 'volume': config.get(function, "pubinfo_journal_volume"), } except ConfigParser.NoOptionError: tags['publication'] = None else: tags['publication'] = { 'pages': tagify(parse_tag(tag['pages'])), 'year': tagify(parse_tag(tag['year'])), 'journal': tagify(parse_tag(tag['journal'])), 'volume': tagify(parse_tag(tag['volume'])), } # Fields needed to lookup the DOIs tags['doi'] = get_field_tags('doi') # 999C5s. A standardized way of writing a reference in the reference list. # Like: Nucl. Phys. B 710 (2000) 371 try: tags['publication_format'] = config.get(function, "pubinfo_journal_format") except ConfigParser.NoOptionError: tags['publication_format'] = CFG_JOURNAL_PUBINFO_STANDARD_FORM # Print values of tags for debugging write_message("tag values: %r" % [tags], verbose=9) return tags
def format_element(bfo, name, tag_name='', tag='', kb='', kb_default_output='', var='', protocol='googlescholar'): """Prints a custom field in a way suitable to be used in HTML META tags. In particular conforms to Google Scholar harvesting protocol as defined http://scholar.google.com/intl/en/scholar/inclusion.html and Open Graph http://ogp.me/ @param tag_name: the name, from tag table, of the field to be exported looks initially for names prefixed by "meta-"<tag_name> then looks for exact name, then falls through to "tag" @param tag: the MARC tag to be exported (only if not defined by tag_name) @param name: name to be displayed in the meta headers, labelling this value. @param kb: a knowledge base through which to process the retrieved value if necessary. @param kb: when a '<code>kb</code>' is specified and no match for value is found, what shall we return? Either return the given parameter or specify "{value}" to return the retrieved value before processing though kb. @param var: the name of a variable to output instead of field from metadata. Allowed values are those supported by bfe_server_info and bfe_client_info. Overrides <code>name</code> and <code>tag_name</code> @param protocol: the protocol this tag is aimed at. Can be used to switch on/off support for a given "protocol". Can take values among 'googlescholar', 'opengraph' @see: bfe_server_info.py, bfe_client_info.py """ if protocol == 'googlescholar' and not CFG_WEBSEARCH_ENABLE_GOOGLESCHOLAR: return "" elif protocol == 'opengraph' and not CFG_WEBSEARCH_ENABLE_OPENGRAPH: return "" tags = [] if var: # delegate to bfe_server_info or bfe_client_info: value = server_info(bfo, var) if value.startswith("Unknown variable: "): # Oops variable was not defined there value = client_info(bfo, var) return not value.startswith("Unknown variable: ") and \ create_metatag(name=name, content=cgi.escape(value, True)) \ or "" elif tag_name: # First check for special meta named tags tags = get_field_tags("meta-" + tag_name) if not tags: # then check for regular tags tags = get_field_tags(tag_name) if not tags and tag: # fall back to explicit marc tag tags = [tag] if not tags: return '' out = [] values = [bfo.fields(marctag, escape=9) for marctag in tags] for value in values: if isinstance(value, list): for val in value: if isinstance(val, dict): out.extend(val.values()) else: out.append(val) elif isinstance(value, dict): out.extend(value.values()) else: out.append(value) out = dict(zip(out, len(out) * [''])).keys() # Remove duplicates if name == 'citation_date': for idx in range(len(out)): out[idx] = out[idx].replace('-', '/') if kb: if kb_default_output == "{value}": out = [bfo.kb(kb, value, value) for value in out] else: out = [bfo.kb(kb, value, kb_default_output) for value in out] return '\n'.join( [create_metatag(name=name, content=value) for value in out])
def format_element(bfo, name, tag_name='', tag='', kb='', kb_default_output='', var='', protocol='googlescholar'): """Prints a custom field in a way suitable to be used in HTML META tags. In particular conforms to Google Scholar harvesting protocol as defined http://scholar.google.com/intl/en/scholar/inclusion.html and Open Graph http://ogp.me/ @param tag_name: the name, from tag table, of the field to be exported looks initially for names prefixed by "meta-"<tag_name> then looks for exact name, then falls through to "tag" @param tag: the MARC tag to be exported (only if not defined by tag_name) @param name: name to be displayed in the meta headers, labelling this value. @param kb: a knowledge base through which to process the retrieved value if necessary. @param kb: when a '<code>kb</code>' is specified and no match for value is found, what shall we return? Either return the given parameter or specify "{value}" to return the retrieved value before processing though kb. @param var: the name of a variable to output instead of field from metadata. Allowed values are those supported by bfe_server_info and bfe_client_info. Overrides <code>name</code> and <code>tag_name</code> @param protocol: the protocol this tag is aimed at. Can be used to switch on/off support for a given "protocol". Can take values among 'googlescholar', 'opengraph' @see: bfe_server_info.py, bfe_client_info.py """ if protocol == 'googlescholar' and not CFG_WEBSEARCH_ENABLE_GOOGLESCHOLAR: return "" elif protocol == 'opengraph' and not CFG_WEBSEARCH_ENABLE_OPENGRAPH: return "" tags = [] if var: # delegate to bfe_server_info or bfe_client_info: value = server_info(bfo, var) if value.startswith("Unknown variable: "): # Oops variable was not defined there value = client_info(bfo, var) return not value.startswith("Unknown variable: ") and \ create_metatag(name=name, content=cgi.escape(value, True)) \ or "" elif tag_name: # First check for special meta named tags tags = get_field_tags("meta-" + tag_name) if not tags: # then check for regular tags tags = get_field_tags(tag_name) if not tags and tag: # fall back to explicit marc tag tags = [tag] if not tags: return '' out = [] values = [bfo.fields(marctag, escape=9) for marctag in tags] for value in values: if isinstance(value, list): for val in value: if isinstance(val, dict): out += val.values() else: out.append(val) out += [isinstance(val, dict) and val.values() or val for val in value] elif isinstance(value, dict): out += value.values() else: out.append(value) out = dict(zip(out, len(out)*[''])).keys() # Remove duplicates if name == 'citation_date': for idx in range(len(out)): out[idx] = out[idx].replace('-', '/') if kb: if kb_default_output == "{value}": out = [bfo.kb(kb, value, value) for value in out] else: out = [bfo.kb(kb, value, kb_default_output) for value in out] return '\n'.join([create_metatag(name=name, content=value) for value in out])
def format_element(bfo, name, tag_name='', tag = '', respect_file_visiblity=False, author_only=True, escape=4): """Prints a custom field in a way suitable to be used in HTML META tags. In particular conforms to Google Scholar harvesting protocol as defined http://scholar.google.com/intl/en/scholar/inclusion.html @param tag_name: the name, from tag table, of the field to be exported looks initially for names prefixed by "meta-"<tagname> then looks for exact name, then falls through to "tag" @param tag: the MARC tag to be exported (only if not defined by tag_name) @param name: name to be displayed in the meta headers, labelling this value @param respect_file_visiblity: check the 8564_z if we are allowed to show a file @param author_only: as google scholar, the author field is for authors only, not editor nor director """ # for files case, make different rule if respect_file_visiblity: values = [] files = bfo.fields('8564_') for file_dict in files: # show only public if file_dict.get('x'): try: if file_dict['x'] == 'PUBLIC': values.append(file_dict['u']) except KeyError: continue else: tags = [] if tag_name: # First check for special meta named tags tags = get_field_tags("meta-" + tag_name) if not tags: #then check for regular tags tags = get_field_tags(tag_name) if not tags and tag: # fall back to explicit marc tag tags = [tag] if not tags: return '' # load values to be printed values = [] for marctag in tags: ###################################### # Special cases if marctag == '700__a' and author_only: # authors # no editor or director authors_info = bfo.fields('700',escape=2) for author_info in authors_info: if author_info.has_key('a') and not author_info.has_key('e'): values.append(author_info['a']) # doi elif marctag == '0247_a': # dont show anything that is not an doi doi_infos = bfo.fields('0247_',escape=2) for doi_info in doi_infos: if doi_info.has_key('a'): values.append(doi_info['a']) #### else: values.append(bfo.fields(marctag,escape=2)) out = [] for value in values: if isinstance(value, list): for val in value: if isinstance(val, dict): out.extend(val.values()) else: out.append(val) elif isinstance(value, dict): out.extend(value.values()) else: out.append(value) # out = dict(zip(out, len(out)*[''])).keys() # uniquify noDupes = [] [noDupes.append(i) for i in out if not noDupes.count(i)] return '\n'.join([create_html_tag('meta', name=name, content=value) for value in noDupes])