def format(bfo): """ List the 'featured' records """ args = parse_url_string(bfo.user_info['uri']) journal_name = args["journal_name"] featured_records = get_featured_records(journal_name) lines = [] for (recid, img_url) in featured_records: featured_record = BibFormatObject(recid) if bfo.lang == 'fr': title = featured_record.field('246_1a') if title == '': # No French translation, get it in English title = featured_record.field('245__a') else: title = featured_record.field('245__a') lines.append(''' <a href="%s/record/%s?ln=%s" style="display:block"> <img src="%s" alt="" width="100" class="phr" /> %s </a> ''' % (CFG_SITE_URL, recid, bfo.lang, img_url, title)) return '<br/><br/>'.join(lines)
def format_element(bfo): """ List the 'featured' records """ args = parse_url_string(bfo.user_info['uri']) journal_name = args["journal_name"] featured_records = get_featured_records(journal_name) lines = [] for (recid, img_url) in featured_records: featured_record = BibFormatObject(recid) if bfo.lang == 'fr': title = featured_record.field('246_1a') if title == '': # No French translation, get it in English title = featured_record.field('245__a') else: title = featured_record.field('245__a') lines.append(''' <a href="%s/record/%s?ln=%s" style="display:block"> <img src="%s" alt="" width="100" class="phr" /> %s </a> ''' % (CFG_SITE_URL, recid, bfo.lang, img_url, title)) return '<br/><br/>'.join(lines)
def format_element(bfo): """ Formats comments header using the post's name and the date in which the comment was added """ this_recid = bfo.control_field('001') post_recid = get_parent_post(this_recid) post_rec = BibFormatObject(post_recid) try: post_title = post_rec.fields('245__a')[0] except: post_title = 'Untitled' try: addition_date = bfo.fields('269__c')[0] except: addition_date = "" out = '<div id="top"><div id="topbanner"> </div>' out += '<div id="mainmenu"><table width="100%">' out += '<tr><td class="left" style = "font-size: 1em;">Go to post: <a href="%s/record/%s?%s">%s</a>' % \ (CFG_SITE_URL, post_recid, bfo.lang, post_title) out += '<td class="right">%s</td>' % addition_date out += '</td></tr></table></div></div>' out += '<div id="mainphoto"></div>' return out
def testAff(self): """testing Affs""" from bfe_CERN_authors import format_element self.bfo = BibFormatObject('7374') string = format_element(self.bfo, limit="5", print_affiliations="yes") self.assert_(re.search(r'Farhi, E.</a>', string)) self.assert_(re.search(r'</a> \(<a.*MIT', string))
def testDate(self): """testing date""" from bfe_INSPIRE_date import format_element self.bfo = BibFormatObject('6194') string = format_element(self.bfo) print string string2 = format_element(self.bfo, us="no") print string2
def get_bfx_record(recID): ''' Get a record with a specific recID. @param recID: the ID of the record @return: a record in the structure defined here ''' bfo = BibFormatObject(recID) return convert_record(bfo.get_record())
def testarXiv(self): """INSPIRE arXiv format""" from bfe_INSPIRE_arxiv import format_element self.bfo = BibFormatObject('1') string = format_element(self.bfo) self.assert_(re.search(r'0299', string)) self.assert_(not re.search(r'CERN', string)) self.assert_(re.search(r'hep-th', string))
def _get_report_numbers(record_id): from invenio.bibformat_engine import BibFormatObject bfo = BibFormatObject(record_id) fields = bfo.fields('037__') report_numbers = [] for field in fields: if 'a' in field: report_numbers.append(field['a']) return report_numbers
def testarX(self): """testing arXiv""" from bfe_INSPIRE_arxiv import format_element self.bfo = BibFormatObject('37650') string = format_element(self.bfo) print string self.assert_(re.search(r'3066', string)) self.assert_(not re.search(r'CERN', string)) self.assert_(re.search(r'hep-ph', string))
def testLinks(self): """testing Links""" from bfe_INSPIRE_links import format_element self.bfo = BibFormatObject('37650') string = format_element(self.bfo, separator='</li>\n<li>', prefix="<ul><li>", suffix="</li></ul>") print string self.assert_(re.search(r'065201">Journal', string)) self.assert_(re.search(r'\?bibcode=2004', string))
def testLinks(self): """testing INSPIRE Links""" from bfe_INSPIRE_links import format_element self.bfo = BibFormatObject('1') string = format_element(self.bfo, separator='</li>\n<li>', prefix="<ul><li>", suffix="</li></ul>") self.assert_(re.search(r'1012.0299">Abstract<', string)) self.assert_( re.search(r'arXiv:1012.0299">PDF</a> from arXiv.org', string))
def isAuthority(self, recid): record = BibFormatObject(recid) values = [] for value in record.fields_ordered("980"): values.extend(value) leader = record.control_field("000") if "AUTHORITY" in values or (len(leader) > 7 and leader[6] == "z"): return True else: return False
def format_element(bfo): """ Creates a navigation for comments. """ # get variables this_recid = bfo.control_field('001') try: this_content = bfo.fields('520__a')[0] except: return "" try: this_author = bfo.fields('100__a')[0] except: return "" this_limit_content = get_contextual_content(this_content, [], max_lines=2)[0] menu_recids = [] current_language = bfo.lang post_recid = get_parent_post(this_recid) menu_recids = get_comments(post_recid, newest_first=True) try: menu_out = '<h4>%s</h4>' % cfg_messages["in_issue"][current_language] except: menu_out = '<h4>%s</h4>' % cfg_messages["in_issue"]['en'] for recid in menu_recids: if str(this_recid) == str(recid): menu_out += '<div class="active"><div class="litem"><b>%s</b>: %s [...]</div></div>' % (this_author, this_limit_content) else: temp_rec = BibFormatObject(recid) content = temp_rec.fields('520__a')[0] limit_content = get_contextual_content(content, [], max_lines=1)[0] try: author = temp_rec.fields('100__a')[0] except: author = 'Anonymous' menu_out += '<div class="litem"><a href="%s/record/%s%s"><b>%s</b>: %s [...]</a></div>' % (CFG_SITE_URL, recid, (bfo.lang=="fr") and "?ln=fr" or "?ln=en", author, limit_content) return menu_out
def get_eprint(recid): """Get the eprintt number from a record.""" report_fermilab = None eprint = None url = None reports = get_fieldvalues(recid, '037__a') reports = reports + get_fieldvalues(recid, '037__z') if VERBOSE: print reports for report in reports: if re.search("FERMILAB", report): report_fermilab = report if VERBOSE: print report_fermilab if not report_fermilab: return None bfo = BibFormatObject(recid) eprint = bfe_arxiv.get_arxiv(bfo, category="no") if VERBOSE: print eprint if eprint: eprint = eprint[0] print report_fermilab, eprint return None for url_i in get_fieldvalues(recid, '8564_u'): if re.match(r'https?://inspirehep.net.*pdf', url_i): url = url_i for item in BibFormatObject(int(recid)).fields('8564_'): if item.has_key('y') or item.has_key('z') and item.has_key('u'): try: if re.search('fermilab', item['y'].lower()): return None except KeyError: pass if item['u'].endswith('pdf'): url = item['u'] try: if item['y'].lower() == 'fulltext': url = item['u'] if item['y'].lower() == 'poster': url = None if item['y'].lower() == 'slides': url = None except KeyError: pass try: if item['z'].lower() == 'openaccess': url = item['u'] except KeyError: pass if url: print report_fermilab, url
def schemaorg_type(recid=None, bfo=None): if recid: from invenio.bibformat_engine import BibFormatObject bfo = BibFormatObject(recid) if bfo: from invenio.openaire_deposit_config import CFG_OPENAIRE_SCHEMAORG_MAP collections = bfo.fields('980__') for c in collections: a = c.get('a', None) b = c.get('b', None) res = CFG_OPENAIRE_SCHEMAORG_MAP.get(b if b else a, None) if res: return res return 'http://schema.org/CreativeWork'
def answer(self, req, user_info, of, cc, colls_to_search, p, f, search_units, ln): """ Answer question given by context. Return (relevance, html_string) where relevance is integer from 0 to 100 indicating how relevant to the question the answer is (see C{CFG_WEBSEARCH_SERVICE_MAX_SERVICE_ANSWER_RELEVANCE} for details) , and html_string being a formatted answer. """ if f: return (0, '') words = [unit[1].lower() for unit in search_units if unit[2] == ""] if not words: return (0, '') _ = gettext_set_language(ln) if not _("weather").lower() in words and \ not "météo" in words and \ not "meteo" in words: return (0, '') bfo = BibFormatObject(0) if meteoblue_widget_available_p: output = bfe_webjournal_widget_weather_meteoblue.format_element( bfo) else: output = bfe_webjournal_widget_weather.format_element( bfo, display_weather_icon='true') if not output: return (0, '') return (100, output)
def testLinks(self): """testing INSPIRE Links""" from bfe_INSPIRE_links import format_element self.bfo=BibFormatObject('1') string = format_element(self.bfo, separator='</li>\n<li>', prefix="<ul><li>",suffix="</li></ul>") self.assert_(re.search(r'1012.0299">Abstract<',string)) self.assert_(re.search(r'arXiv:1012.0299">PDF</a> from arXiv.org',string))
def eval_bibformat_4suite(ctx, recID, template_code): """ 4suite extension function: Bridge between BibFormat and XSL stylesheets. Returns the evaluation of the given piece of format template Can be used in that way in XSL stylesheet (provided xmlns:fn="http://cdsweb.cern.ch/bibformat/fn" has been declared): <xsl:value-of select="fn:eval_bibformat(marc:controlfield[@tag='001'],'<BFE_SERVER_INFO var="recurl">')" /> if recID is string, value is converted to int if recID is Node, first child node (text node) is taken as value template_code is evaluated as a format template piece of code. '<' and '"' need to be escaped with '<' and '"' """ from invenio.bibformat_engine import \ format_with_format_template, \ BibFormatObject try: if len(recID) > 0 and isinstance(recID[0], Node): recID_int = recID[0].firstChild.nodeValue if recID_int is None: return '' else: recID_int = int(recID_int) bfo = BibFormatObject(recID_int) return format_with_format_template( None, bfo, verbose=0, format_template_code=template_code)[0] except Exception, err: sys.stderr.write("Error during formatting function evaluation: " + \ str(err) + \ '\n') return ''
def get_id(record, id_type=None): """Returns any id with a HEPNames recid""" author_id = None for item in BibFormatObject(record).fields('035__'): if item.has_key('9') and item['9'] == id_type and item.has_key('a'): author_id = item['a'] return author_id
def get_widget_html(language, max_photos, collections, separator, ln): """ Returns the content of the widget """ latest_photo_ids = perform_request_search(c=collections, rg=max_photos, of='id') images_urls = [] for recid in latest_photo_ids[:max_photos]: try: photo_record = BibFormatObject(recid) except: # todo: Exception, no photo in this selection continue if language == "fr": try: title = photo_record.fields('246_1a', escape=1)[0] except KeyError: try: title = photo_record.fields('245__a', escape=1)[0] except: title = "" else: try: title = photo_record.fields('245__a', escape=1)[0] except KeyError: # todo: exception, picture with no title title = "" if CFG_CERN_SITE and photo_record.fields('8567_'): # Get from 8567_ dfs_images = photo_record.fields('8567_') for image_block in dfs_images: if image_block.get("y", '') == "Icon": if image_block.get("u", '').startswith("http://"): images_urls.append((recid, image_block["u"], title)) break # Just one image per record else: # Get from 8564_ images = photo_record.fields('8564_') for image_block in images: if image_block.get("x", '').lower() == "icon": if image_block.get("q", '').startswith("http://"): images_urls.append((recid, image_block["q"], title)) break # Just one image per record # Build output html_out = separator.join([ '<a href="%s/%s/%i?ln=%s"><img class="phr" width="100" height="67" src="%s"/>%s</a>' % (CFG_SITE_URL, CFG_SITE_RECORD, recid, ln, photo_url, title) for (recid, photo_url, title) in images_urls ]) return html_out
def testAff(self): """testing Affs""" from bfe_CERN_authors import format_element self.bfo=BibFormatObject('7374') string = format_element(self.bfo,limit="5",print_affiliations="yes") self.assert_(re.search(r'Farhi, E.</a>',string)) self.assert_(re.search(r'</a> \(<a.*MIT',string))
def testDate(self): """testing date""" from bfe_INSPIRE_date import format_element self.bfo=BibFormatObject('6194') string=format_element(self.bfo) print string string2=format_element(self.bfo,us="no") print string2
def get_eprint_id(recid): """ Find the arxiv number from an INSPIRE record """ osti_id = None for item in BibFormatObject(int(recid)).fields('037__'): if item.has_key('9') and item.has_key('a'): if item['9'].lower() == 'arxiv': arxiv_id = item['a'] return arxiv_id
def testLinks(self): """testing Links""" from bfe_INSPIRE_links import format_element self.bfo=BibFormatObject('37650') string= format_element(self.bfo, separator='</li>\n<li>', prefix="<ul><li>",suffix="</li></ul>") print string self.assert_(re.search(r'065201">Journal',string)) self.assert_(re.search(r'\?bibcode=2004',string))
def testDate(self): """INSPIRE date format""" from bfe_INSPIRE_date import format_element, parse_date # Test parse date function self.assert_(not parse_date(None)) self.assert_(not parse_date("")) self.assert_(not parse_date("This is bad input")) self.assert_(not parse_date([1, 2, 4, "test"])) self.assert_(parse_date("2003-05-02") == (2003, 5, 2)) self.assert_(parse_date("20030502") == (2003, 5, 2)) self.assert_(parse_date("2003-05") == (2003, 5)) self.assert_(parse_date("200305") == (2003, 5)) self.assert_(parse_date("2003") == (2003, )) # Expect date from 269__$$c self.bfo = BibFormatObject('1') string = format_element(self.bfo) self.assert_(re.search(r'Dec 2010', string))
def get_osti_id(recid): """ Find the osti_id from an INSPIRE record """ osti_id = None for item in BibFormatObject(int(recid)).fields('035__'): if item.has_key('9') and item.has_key('a'): if item['9'].lower() == 'osti': osti_id = item['a'] return osti_id
def testarXiv(self): """INSPIRE arXiv format""" from bfe_INSPIRE_arxiv import format_element self.bfo = BibFormatObject('1') string = format_element(self.bfo) self.assert_(re.search(r'0299',string)) self.assert_(not re.search(r'CERN',string)) self.assert_(re.search(r'hep-th',string))
def format_element(bfo): """ Prints the list of papers containing the dataset by title. """ from invenio.bibformat_engine import BibFormatObject from invenio.config import CFG_BASE_URL, CFG_SITE_RECORD parent_recid = bfo.field("786__w") bfo_parent = BibFormatObject(parent_recid) title = bfo_parent.field("245__a") url = CFG_BASE_URL + '/' + CFG_SITE_RECORD + '/' + str(bfo_parent.recID) out = "This dataset complements the following publication: <br />" out += "<a href=\"" + url + "\">" + title + "</a>" return out
def testarX(self): """testing arXiv""" from bfe_INSPIRE_arxiv import format_element self.bfo=BibFormatObject('37650') string=format_element(self.bfo) print string self.assert_(re.search(r'3066',string)) self.assert_(not re.search(r'CERN',string)) self.assert_(re.search(r'hep-ph',string))
def format_element(bfo): """ Prints the list of papers containing the dataset by title. """ from invenio.bibformat_engine import BibFormatObject from invenio.config import CFG_BASE_URL, CFG_SITE_RECORD parent_recid = int(bfo.field("786__w")) bfo_parent = BibFormatObject(parent_recid) title = bfo_parent.field("245__a") url = CFG_BASE_URL + '/' + CFG_SITE_RECORD + '/' + str(bfo_parent.recID) out = "This dataset complements the following publication: <br />" out += "<a href=\"" + url + "\">" + title + "</a>" return out
class TestInspireFormatElements(unittest.TestCase): # # Test case depends on inspires test records #test CERN_authors def testField(self): print """testing bfeField""" self.bfo = BibFormatObject('7374') self.assertEqual(self.bfo.field('100a'), "Farhi, E.") def testAff(self): """testing Affs""" from bfe_CERN_authors import format_element self.bfo = BibFormatObject('7374') string = format_element(self.bfo, limit="5", print_affiliations="yes") self.assert_(re.search(r'Farhi, E.</a>', string)) self.assert_(re.search(r'</a> \(<a.*MIT', string)) #test INSPIRE_arXiv def testarX(self): """testing arXiv""" from bfe_INSPIRE_arxiv import format_element self.bfo = BibFormatObject('37650') string = format_element(self.bfo) print string self.assert_(re.search(r'3066', string)) self.assert_(not re.search(r'CERN', string)) self.assert_(re.search(r'hep-ph', string)) #test INSPIRE_date def testDate(self): """testing date""" from bfe_INSPIRE_date import format_element self.bfo = BibFormatObject('6194') string = format_element(self.bfo) print string string2 = format_element(self.bfo, us="no") print string2 # self.assert_(re.search(r'Jun 1, 1974',string)) # self.assert_(re.search(r'01 Jun 1974',string2)) #test INSPIRE_links def testLinks(self): """testing Links""" from bfe_INSPIRE_links import format_element self.bfo = BibFormatObject('37650') string = format_element(self.bfo, separator='</li>\n<li>', prefix="<ul><li>", suffix="</li></ul>") print string self.assert_(re.search(r'065201">Journal', string)) self.assert_(re.search(r'\?bibcode=2004', string))
def format_element(bfo): """ Creates a navigation for articles in the same issue and category. """ # get variables this_recid = bfo.control_field('001') menu_recids = [] current_language = bfo.lang this_title = "" try: this_title = bfo.fields('245__a')[0] except: return "" blog_recid = get_parent_blog(this_recid) blog_rec = BibFormatObject(blog_recid) try: blog_title = blog_rec.fields('245__a')[0] except: blog_title = 'Untitled' menu_recids = get_posts(blog_recid, newest_first=True) try: menu_out = '<h4>%s</h4>' % cfg_messages["in_issue"][current_language] except: # in english by default menu_out = '<h4>%s</h4>' % cfg_messages["in_issue"]['en'] for recid in menu_recids: if str(this_recid) == str(recid): menu_out += '<div class="active"><div class="litem">%s</div></div>' % this_title else: temp_rec = BibFormatObject(recid) try: title = temp_rec.fields('245__a')[0] except: title = 'Untitled' menu_out += '<div class="litem"><a href="%s/record/%s%s">%s</a></div>' % (CFG_SITE_URL, recid, (bfo.lang=="fr") and "?ln=fr" or "", title) return menu_out
def cite_as(bfo, publisher): """ HepData format example: Cite as: The ATLAS Collaboration (2013) HepData, doi: 10.1234/123456 Dataverse format example: Cranmer, Kyle; Allanach, Ben; Lester, Christopher; Weber, Arne, "Replication data for: "Natural Priors, CMSSM Fits and LHC Weather Forecasts"", http://hdl.handle.net/1902.1/21804 INSPIRE format example: Cite as: The ATLAS Collaboration (2013) INSPIRE, doi: 10.1234/123456 """ from invenio.bibformat_engine import BibFormatObject if publisher == "Dataverse": return dataverse_cite_as(bfo) elif publisher == 'authors': return "" colls = [] for coll in bfo.fields("710__g"): if coll not in colls: colls.append(coll) try: parent_recid = int(bfo.field("786__w")) bfo_parent = BibFormatObject(parent_recid) year = get_year(bfo_parent) except ValueError: # No parent record available year = get_year(bfo) if publisher == 'HEPDATA': publisher = 'HepData' elif publisher == "INSPIRE": publisher == "INSPIRE-HEP" pid_type = bfo.field("0247_2") pid = bfo.field("0247_a") out = '' out += "<b>Cite as: </b>" if colls: out += str(colls[0]) if year: out += ' ( ' + str(year) + ' ) ' out += publisher + ', ' if pid_type == 'DOI': out += '<a href="http://doi.org/' + pid + '" target="_blank" > http://doi.org/' + pid + '</a>' elif pid_type == 'HDL': out += '<a href="http://hdl.handle.net/' + pid + '" target="_blank" > http://hdl.handle.net/' + pid + '</a>' elif pid_type == '': out += '[no persistent identifier assigned]' return out
def create_xml(recid=None, osti_id=None, doi=None): osti_exists = False doi_exists = False osti_mismatch = False mismatches = [] osti_subfields = [('9', 'OSTI'), ('a', osti_id)] record = get_record(recid) record_link = '<a href="http://inspirehep.net/record/%s">%s</a>' % ( str(recid), str(recid)) append_record = {} additions = False errors = None for item in BibFormatObject(recid).fields('035__'): if item.has_key('9') and item.has_key('a'): if item['9'] == 'OSTI' and item['a'] == osti_id: osti_exists = True elif item['9'] == 'OSTI' and item['a'] != osti_id: osti_mismatch = True mismatches.append(item['a']) for item in BibFormatObject(recid).fields('0247_'): if item.has_key('2') and item.has_key('a'): if item['2'] == 'DOI' and item['a'] == doi: doi_exists = True if osti_exists is False and osti_mismatch is True: print str(recid), "already has a different OSTI ID" errors = "doi %s in record %s should match OSTI ID %s, but the record already contains OSTI ID(s) %s<br />" % ( doi, record_link, osti_id, ','.join(mismatches)) return errors if doi_exists is False and osti_exists is True: print str(recid), "contains an OSTI ID but no doi" no_doi = "%s contains OSTI ID %s but not doi %s<br />" % (record_link, osti_id, doi) return no_doi if osti_exists is False and osti_mismatch is False: record_add_field(append_record, '001', controlfield_value=str(recid)) record_add_field(append_record, '035', '', '', subfields=osti_subfields) print "%s: added 035__a:%s" % (str(recid), osti_id) return print_rec(append_record)
def get_widget_html(language, max_photos, collections, separator, ln): """ Returns the content of the widget """ latest_photo_ids = perform_request_search(c=collections, rg=max_photos, of="id") images_urls = [] for recid in latest_photo_ids[:max_photos]: try: photo_record = BibFormatObject(recid) except: # todo: Exception, no photo in this selection continue if language == "fr": try: title = photo_record.fields("246_1a", escape=1)[0] except KeyError: try: title = photo_record.fields("245__a", escape=1)[0] except: title = "" else: try: title = photo_record.fields("245__a", escape=1)[0] except KeyError: # todo: exception, picture with no title title = "" if CFG_CERN_SITE and photo_record.fields("8567_"): # Get from 8567_ dfs_images = photo_record.fields("8567_") for image_block in dfs_images: if image_block.get("y", "") == "Icon": if image_block.get("u", "").startswith("http://"): images_urls.append((recid, image_block["u"], title)) break # Just one image per record else: # Get from 8564_ images = photo_record.fields("8564_") for image_block in images: if image_block.get("x", "").lower() == "icon": if image_block.get("q", "").startswith("http://"): images_urls.append((recid, image_block["q"], title)) break # Just one image per record # Build output html_out = separator.join( [ '<a href="%s/%s/%i?ln=%s"><img class="phr" width="100" height="67" src="%s"/>%s</a>' % (CFG_SITE_URL, CFG_SITE_RECORD, recid, ln, photo_url, title) for (recid, photo_url, title) in images_urls ] ) return html_out
class TestInspireFormatElements(unittest.TestCase): # # Test case depends on inspires test records #test CERN_authors def testField(self): print """testing bfeField""" self.bfo=BibFormatObject('7374') self.assertEqual(self.bfo.field('100a'),"Farhi, E.") def testAff(self): """testing Affs""" from bfe_CERN_authors import format_element self.bfo=BibFormatObject('7374') string = format_element(self.bfo,limit="5",print_affiliations="yes") self.assert_(re.search(r'Farhi, E.</a>',string)) self.assert_(re.search(r'</a> \(<a.*MIT',string)) #test INSPIRE_arXiv def testarX(self): """testing arXiv""" from bfe_INSPIRE_arxiv import format_element self.bfo=BibFormatObject('37650') string=format_element(self.bfo) print string self.assert_(re.search(r'3066',string)) self.assert_(not re.search(r'CERN',string)) self.assert_(re.search(r'hep-ph',string)) #test INSPIRE_date def testDate(self): """testing date""" from bfe_INSPIRE_date import format_element self.bfo=BibFormatObject('6194') string=format_element(self.bfo) print string string2=format_element(self.bfo,us="no") print string2 # self.assert_(re.search(r'Jun 1, 1974',string)) # self.assert_(re.search(r'01 Jun 1974',string2)) #test INSPIRE_links def testLinks(self): """testing Links""" from bfe_INSPIRE_links import format_element self.bfo=BibFormatObject('37650') string= format_element(self.bfo, separator='</li>\n<li>', prefix="<ul><li>",suffix="</li></ul>") print string self.assert_(re.search(r'065201">Journal',string)) self.assert_(re.search(r'\?bibcode=2004',string))
def get_doi_from_record(recid): """ Given a record ID we fetch it from the DB and return the first DOI found as specified by the config variable CFG_APSHARVEST_RECORD_DOI_TAG. @param recid: record id record containing a DOI @type recid: string/int @return: first DOI found in record @rtype: string """ record = BibFormatObject(int(recid)) possible_dois = record.fields(CFG_APSHARVEST_RECORD_DOI_TAG[:-1]) for doi in possible_dois: if '2' in doi and doi.get('2', "") == "DOI": # Valid DOI present, add it try: return doi['a'] except KeyError: continue
def get_hepnames_anyid_from_recid(record, id_type): """ Returns any id with a HEPNames recid """ record = int(record) author_id = None for item in BibFormatObject(record).fields('035__'): if item.has_key('9') and item['9'] == id_type and item.has_key('a'): author_id = item['a'] if VERBOSE and not author_id: print "WARNING: no %s ID found for %s: " % (id_type, record) return author_id
def format_element(bfo, separator='; '): """ Prints the list of the "children" institutions """ from invenio.search_engine import search_pattern from invenio.bibformat_engine import BibFormatObject recID = str(bfo.recID) out = "" children = [] if not recID: #Something is wrong, return empty string return out all_institutions = search_pattern(p="510__0:" + str(recID)) for institution_id in all_institutions: for field in BibFormatObject(institution_id).fields('510__'): if field.get('0') == str(recID) and field.get('w') == 't': children.append(institution_id) if children: out += "Subsidiary Institution: " for item in children: # get the abbreviated name of the institution abbrev = BibFormatObject(item).field('110__t') # if there is no abbreviated name, we try different names if not abbrev: abbrev = BibFormatObject(item).field('110__u') if not abbrev: abbrev = BibFormatObject(item).field('110__a') if not abbrev: # if no name is found, we display record ID as a text of the link abbrev = item out += '<a href="/record/' + str(item) + '">' + str(abbrev) \ + '</a>' + separator # remove last separator and space, then return the string out = out[:-2] return out
def call_function(self, function_name, parameters=None): ''' Call an external element which is a Python file, using BibFormat @param function_name: the name of the function to call @param parameters: a dictionary of the parameters to pass as key=value pairs @return: a string value, which is the result of the function call ''' if parameters is None: parameters = {} bfo = BibFormatObject(self.recID) format_element = get_format_element(function_name) (value, errors) = eval_format_element(format_element, bfo, parameters) #to do: check errors from function call return value
def get_hepnames_aff_from_recid(record, id_type): """ Returns the current affiliation """ record = int(record) affiliation = None for item in BibFormatObject(record).fields('371__'): if item.has_key('z') and item['z'].lower() == id_type.lower() \ and item.has_key('a'): affiliation = item['a'] if VERBOSE and not affiliation: print "WARNING: no %s ID found for %s: " % (id_type, record) return affiliation
def eval_bibformat_lxml(ctx, recID, template_code): """ libxslt extension function: Bridge between BibFormat and XSL stylesheets. Returns the evaluation of the given piece of format template Can be used in that way in XSL stylesheet (provided xmlns:fn="http://cdsweb.cern.ch/bibformat/fn" has been declared): <xsl:value-of select="fn:eval_bibformat(marc:controlfield[@tag='001'],'<BFE_SERVER_INFO var="recurl">')" /> if recID is string, value is converted to int if recID is Node, first child node (text node) is taken as value template_code is evaluated as a format template piece of code. '<' and '"' need to be escaped with '<' and '"' @param ctx: context as passed by lxml @param recID: record ID @param template_code: the code calling a BFE_ as it would be use in format template @return: the evalued call to a format template (usually a call to a format element) @rtype: string """ #' from invenio.bibformat_engine import \ format_with_format_template, \ BibFormatObject try: if isinstance(recID, str): recID_int = int(recID) elif isinstance(recID, (int, long)): recID_int = recID elif isinstance(recID, list): recID = recID[0] if isinstance(recID, str): recID_int = int(recID) else: recID_int = int(recID.text) else: recID_int = int(recID.text) bfo = BibFormatObject(recID_int) return format_with_format_template(None, bfo, verbose=0, format_template_code=template_code) except Exception, err: sys.stderr.write("Error during formatting function evaluation: " + \ str(err) + \ '\n') return ''
def get_author_details(recid, authors, tag): """Get authors broken out as individuals""" for item in BibFormatObject(int(recid)).fields(tag): authors_detail = ET.SubElement(authors, 'authors_detail') author = None last_name = None first_name = None middle_name = None affiliation = None email = None orcid = None if item.has_key('a'): author = item['a'] try: matchobj = re.match(r'(.*)\, (.*)\, (.*)', author) last_name = matchobj.group(1) fore_name = matchobj.group(2) title = matchobj.group(3) fore_name = fore_name + ', ' + title except AttributeError: last_name = re.sub(r'\,.*', '', author) fore_name = re.sub(r'.*\, ', '', author) if re.search(r' ', fore_name): first_name = re.sub(r' .*', '', fore_name) middle_name = re.sub(r'.* ', '', fore_name) elif re.search(r'^\w\.\w\.', fore_name): first_name = re.sub(r'^(\w\.).*', r'\1', fore_name) middle_name = re.sub(r'^\w\.', '', fore_name) else: first_name = fore_name if item.has_key('u'): affiliation = item['u'] if item.has_key('m'): email = item['m'] email = email.replace('email:', '') for item_jk in ['j', 'k']: if item.has_key(item_jk): orcid = item[item_jk] if not re.search(r'ORCID:', orcid): orcid = None else: orcid = re.sub(r'ORCID:', '', orcid) ET.SubElement(authors_detail, 'first_name').text = first_name ET.SubElement(authors_detail, 'middle_name').text = middle_name ET.SubElement(authors_detail, 'last_name').text = last_name ET.SubElement(authors_detail, 'affiliation').text = affiliation ET.SubElement(authors_detail, 'private_email').text = email ET.SubElement(authors_detail, 'orcid_id').text = orcid
class TestInspireFormatElements(unittest.TestCase): """Test cases for INSPIRE format elements. Depends on default inspire test records (~1000)""" def testFieldRetrieval(self): """bfeField retrieval""" self.bfo = BibFormatObject('1') self.assertEqual(self.bfo.field('100a'), "Sachdev, Subir") def testarXiv(self): """INSPIRE arXiv format""" from bfe_INSPIRE_arxiv import format_element self.bfo = BibFormatObject('1') string = format_element(self.bfo) self.assert_(re.search(r'0299', string)) self.assert_(not re.search(r'CERN', string)) self.assert_(re.search(r'hep-th', string)) def testDate(self): """INSPIRE date format""" from bfe_INSPIRE_date import format_element, parse_date # Test parse date function self.assert_(not parse_date(None)) self.assert_(not parse_date("")) self.assert_(not parse_date("This is bad input")) self.assert_(not parse_date([1, 2, 4, "test"])) self.assert_(parse_date("2003-05-02") == (2003, 5, 2)) self.assert_(parse_date("20030502") == (2003, 5, 2)) self.assert_(parse_date("2003-05") == (2003, 5)) self.assert_(parse_date("200305") == (2003, 5)) self.assert_(parse_date("2003") == (2003, )) # Expect date from 269__$$c self.bfo = BibFormatObject('1') string = format_element(self.bfo) self.assert_(re.search(r'Dec 2010', string)) def testLinks(self): """testing INSPIRE Links""" from bfe_INSPIRE_links import format_element self.bfo = BibFormatObject('1') string = format_element(self.bfo, separator='</li>\n<li>', prefix="<ul><li>", suffix="</li></ul>") self.assert_(re.search(r'1012.0299">Abstract<', string)) self.assert_( re.search(r'arXiv:1012.0299">PDF</a> from arXiv.org', string))
def testDate(self): """INSPIRE date format""" from bfe_INSPIRE_date import format_element, parse_date # Test parse date function self.assert_(not parse_date(None)) self.assert_(not parse_date("")) self.assert_(not parse_date("This is bad input")) self.assert_(not parse_date([1,2,4,"test"])) self.assert_(parse_date("2003-05-02") == (2003,5,2)) self.assert_(parse_date("20030502") == (2003,5,2)) self.assert_(parse_date("2003-05") == (2003,5)) self.assert_(parse_date("200305") == (2003,5)) self.assert_(parse_date("2003") == (2003,)) # Expect date from 269__$$c self.bfo = BibFormatObject('1') string = format_element(self.bfo) self.assert_(re.search(r'Dec 2010',string))
class TestInspireFormatElements(unittest.TestCase): """Test cases for INSPIRE format elements. Depends on default inspire test records (~1000)""" def testFieldRetrieval(self): """bfeField retrieval""" self.bfo = BibFormatObject('1') self.assertEqual(self.bfo.field('100a'),"Sachdev, Subir") def testarXiv(self): """INSPIRE arXiv format""" from bfe_INSPIRE_arxiv import format_element self.bfo = BibFormatObject('1') string = format_element(self.bfo) self.assert_(re.search(r'0299',string)) self.assert_(not re.search(r'CERN',string)) self.assert_(re.search(r'hep-th',string)) def testDate(self): """INSPIRE date format""" from bfe_INSPIRE_date import format_element, parse_date # Test parse date function self.assert_(not parse_date(None)) self.assert_(not parse_date("")) self.assert_(not parse_date("This is bad input")) self.assert_(not parse_date([1,2,4,"test"])) self.assert_(parse_date("2003-05-02") == (2003,5,2)) self.assert_(parse_date("20030502") == (2003,5,2)) self.assert_(parse_date("2003-05") == (2003,5)) self.assert_(parse_date("200305") == (2003,5)) self.assert_(parse_date("2003") == (2003,)) # Expect date from 269__$$c self.bfo = BibFormatObject('1') string = format_element(self.bfo) self.assert_(re.search(r'Dec 2010',string)) def testLinks(self): """testing INSPIRE Links""" from bfe_INSPIRE_links import format_element self.bfo=BibFormatObject('1') string = format_element(self.bfo, separator='</li>\n<li>', prefix="<ul><li>",suffix="</li></ul>") self.assert_(re.search(r'1012.0299">Abstract<',string)) self.assert_(re.search(r'arXiv:1012.0299">PDF</a> from arXiv.org',string))
def _get_breaking_news(lang, journal_name): """ Gets the 'Breaking News' articles that are currently active according to start and end dates. """ # CERN Bulletin only if not journal_name.lower() == 'cernbulletin': return '' # Look for active breaking news breaking_news_recids = [recid for recid in search_pattern(p='980__a:BULLETINBREAKING') \ if record_exists(recid) == 1] today = time.mktime(time.localtime()) breaking_news = "" for recid in breaking_news_recids: temp_rec = BibFormatObject(recid) try: end_date = time.mktime(time.strptime(temp_rec.field("925__b"), "%m/%d/%Y")) except: end_date = time.mktime(time.strptime("01/01/1970", "%m/%d/%Y")) if end_date < today: continue try: start_date = time.mktime(time.strptime(temp_rec.field("925__a"), "%m/%d/%Y")) except: start_date = time.mktime(time.strptime("01/01/2050", "%m/%d/%Y")) if start_date > today: continue publish_date = temp_rec.field("269__c") if lang == 'fr': title = temp_rec.field("246_1a") else: title = temp_rec.field("245__a") breaking_news += ''' <h2 class="%s">%s<br/> <strong> <a href="%s/journal/popup?name=%s&type=breaking_news&record=%s&ln=%s" target="_blank">%s</a> </strong> </h2> ''' % ("", publish_date, CFG_SITE_URL, journal_name, recid, lang, title) if breaking_news: breaking_news = '<li>%s</li>' % breaking_news return breaking_news
def format_element(bfo, new_articles_first='yes', subject_to_css_class_kb="WebJournalSubject2CSSClass", display_all_category_articles='no', display_category_title='no'): """ List all articles one after the other, on the same page. Similar to bfe_webjournal_articles_overview, but displays full articles. Note that you cannot use both bfe_webjournal_articles_overview and bfe_webjournal_articles: you have to choose one of them, as they use the same cache location (It would also not make sense to use both...). @param new_articles_first: if 'yes', display new articles before other articles @param subject_to_css_class_kb: knowledge base that maps 595__a to a CSS class @param display_all_category_articles: if yes, display all articles, whatever category is selected @param display_category_title: if yes, display category title (useful if display_all_category_articles is enabled) @see: bfe_webjournal_articles_overview.py """ args = parse_url_string(bfo.user_info['uri']) journal_name = args["journal_name"] this_issue_number = args["issue"] category_name = args["category"] verbose = args["verbose"] ln = bfo.lang _ = gettext_set_language(ln) # Try to get the page from cache. Only if issue is older or equal # to latest release. latest_released_issue = get_current_issue(ln, journal_name) if verbose == 0 and not issue_is_later_than(this_issue_number, latest_released_issue): cached_html = get_index_page_from_cache(journal_name, category_name, this_issue_number, ln) if cached_html: return cached_html # Shall we display current category, or all? categories = [category_name] if display_all_category_articles.lower() == 'yes': categories = get_journal_categories(journal_name, this_issue_number) out = '' for category_name in categories: if display_category_title.lower() == 'yes': out += '<h2>' + _(category_name) + '</h2>' out += '<table border="0" cellpadding="0" cellspacing="0">' # Get the id list ordered_articles = get_journal_articles(journal_name, this_issue_number, category_name, newest_first=new_articles_first.lower() == 'yes') new_articles_only = False if ordered_articles.keys() and max(ordered_articles.keys()) < 0: # If there are only new articles, don't bother marking them as # new new_articles_only = True order_numbers = ordered_articles.keys() order_numbers.sort() for order_number in order_numbers: for article_id in ordered_articles[order_number]: # A record is considered as new if its position is # negative and there are some non-new articles article_is_new = (order_number < 0 and not new_articles_only) temp_rec = BibFormatObject(article_id) title = '' if ln == "fr": title = temp_rec.field('246_1a') if title == '': title = temp_rec.field('245__a') else: title = temp_rec.field('245__a') if title == '': title = temp_rec.field('246_1a') # Get CSS class (if relevant) notes = temp_rec.fields('595__a') css_classes = [temp_rec.kb(subject_to_css_class_kb, note, None) \ for note in notes] css_classes = [css_class for css_class in css_classes \ if css_class is not None] if article_is_new: css_classes.append('new') # Finally create the output. Two different outputs # depending on if we have text to display or not text = [] if ln == "fr": text = temp_rec.fields('590__b') if not text or \ (len(text) == 1 and \ (text[0].strip() in ['', '<br />', '<!--HTML--><br />'])): text = temp_rec.fields('520__b') else: text = temp_rec.fields('520__b') if not text or \ (len(text) == 1 and \ (text[0].strip() in ['', '<br />', '<!--HTML--><br />'])): text = temp_rec.fields('590__b') text = '<br/>'.join(text) out += ''' <tr><td class="article"> <h%(header_tag_size)s class="%(css_classes)s articleTitle" style="clear:both;"> %(title)s </h%(header_tag_size)s> <div class="articleBody"> <div class="articleText"> %(text)s </div> </div> </td></tr> ''' % {'title': title, 'text': text, 'header_tag_size': (display_category_title.lower() == 'yes') and '3' or '2', 'css_classes': ' '.join(css_classes)} out += '</table>' if verbose == 0 and not CFG_ACCESS_CONTROL_LEVEL_SITE == 2 : cache_index_page(out, journal_name, category_name, this_issue_number, ln) return out
def format_element(bfo): """ Displays the description of how users should cite any content of the archive. The citation includes: For blogs: "title". (record_creation_date). record_url Retrieved from the original "original_url" For blog posts: author. "title". Blog: "blog_title". (record_creation_date). record_url Retrieved from the original "original_url" For comments: author. Blog post: "post_title". (record_creation_date). record_url Retrieved from the original "original_url" """ coll = bfo.fields('980__a')[0] recid = bfo.control_field('001') # let's get the fields we want to show if coll in ["BLOGPOST", "COMMENT"]: author = bfo.fields('100__a')[0] try: original_creation_date = bfo.fields('269__c')[0] except: original_creation_date = "" try: title = bfo.fields('245__a')[0] except: title = "Untitled" try: original_url = bfo.fields('520__u')[0] except: raise Exception("URL not found") # creation date of a record record_creation_date = get_creation_date(recid) # url in the archive record_url = CFG_SITE_URL + "/record/" + recid if coll == "BLOGPOST": # we will also show the blog's title of # the corresponding blog post blog_recid = get_parent_blog(recid) blog_bfo = BibFormatObject(blog_recid) try: blog_title = blog_bfo.fields('245__a')[0] except: blog_title = 'Untitled' description = """<table style="border:1px solid black;"><tr><td>\ <span><b>%s</b>. '%s'. Blog: '%s'. </br> \ (%s). <i>'%s'</i> </br> \ Retrieved from the original <i>'%s'</i><span></td></tr></table>""" \ % (author, title, blog_title, record_creation_date, record_url, original_url) elif coll == "COMMENT": # we will also show the post's title of # the corresponding comment post_recid = get_parent_post(recid) post_bfo = BibFormatObject(post_recid) try: post_title = post_bfo.fields('245__a')[0] except: post_title = 'Untitled' description = """<table style="border:1px solid black;"><tr><td>\ <span><b>%s. </b>Blog post: '%s'.</br> \ (%s). <i>'%s'</i> </br> \ Retrieved from the original <i>'%s'</i><span></td></tr></table>""" \ % (author, post_title, record_creation_date, record_url, original_url) else: # coll == "BLOG" description = """<table style="border:1px solid black;"><tr><td>\ <span>'%s' </br> \ (%s). <i>'%s'</i> </br> \ Retrieved from the original <i>'%s'</i><span></td></tr></table>""" \ % (title, record_creation_date, record_url, original_url) out = """ <script type="text/javascript"> function displayCitationDescription(){ var description = document.getElementById('description'); var citation_link = document.getElementById('citation_link'); if (description.style.display == 'none'){ description.style.display = ''; citation_link.innerHTML = "Hide citation description" } else { description.style.display = 'none'; citation_link.innerHTML = "How to cite this" } } </script> """ out += '<span id="description" style="">' + description + '</span>' out += '<a class="moreinfo" id="citation_link" \ href="javascript:void(0)" onclick="displayCitationDescription()""></a>' out += '<script type="text/javascript">displayCitationDescription()</script>' return out
def format_element(bfo, limit, separator='; ', extension='[...]', print_links = "yes", print_affiliations='no', affiliation_prefix = ' (', affiliation_suffix = ')', print_affiliation_first='no', interactive="no", highlight="no", affiliations_separator=" ; ", name_last_first = "yes", collaboration = "yes", id_links = "no", markup = "html", link_extension = "no", suffix = '' ): """ Prints the list of authors of a record. @param limit the maximum number of authors to display @param separator the separator between authors. @param extension a text printed if more authors than 'limit' exist @param print_links if yes, prints the authors as HTML link to their publications @param print_affiliations if yes, make each author name followed by its affiliation @param affiliation_prefix prefix printed before each affiliation @param affiliation_suffix suffix printed after each affiliation @param print_affiliation_first if 'yes', affiliation is printed before the author @param interactive if yes, enable user to show/hide authors when there are too many (html + javascript) @param highlight highlights authors corresponding to search query if set to 'yes' @param affiliations_separator separates affiliation groups @param name_last_first if yes (default) print last, first otherwise first last @param collaboration if yes (default) uses collaboration name in place of long author list, if available @param id_links if yes (default = no) prints link based on INSPIRE IDs if available - only used if print_links = yes @param markup html (default) or latex controls small markup differences @param link_extension if 'yes' link the extension to the detailed record page """ from urllib import quote from cgi import escape import re from invenio.messages import gettext_set_language from invenio.config import CFG_BASE_URL, CFG_SITE_RECORD from invenio.bibformat_engine import BibFormatObject _ = gettext_set_language(bfo.lang) # load the right message language #regex for parsing last and first names and initials re_last_first = re.compile('^(?P<last>[^,]+)\s*,\s*(?P<first_names>[^\,]*)(?P<extension>\,?.*)$') re_initials = re.compile(r'(?P<initial>\w)(\w+|\.)\s*') re_coll = re.compile(r'\s*collaborations?', re.IGNORECASE) bibrec_id = bfo.control_field("001") authors = [] lastauthor = '' # HepData and only-INSPIRE data records inherit the list of authors from the original paper if (bfo.field("520__9") == "HEPDATA") or (bfo.field("520__9") == "INSPIRE"): parent_recid = bfo.field("786__w") bfo_parent = BibFormatObject(int(parent_recid)) authors = [] authors_1 = bfo_parent.fields('100__', repeatable_subfields_p=True) authors_2 = bfo_parent.fields('700__', repeatable_subfields_p=True) # other datasources should have a list of authors else: authors = [] authors_1 = bfo.fields('100__', repeatable_subfields_p=True) authors_2 = bfo.fields('700__', repeatable_subfields_p=True) authors.extend(authors_1) authors.extend(authors_2) # If there are no author check for corporate author in 110__a field if len(authors) == 0: authors = bfo.fields('110__', repeatable_subfields_p=True) # For corporate authors we don't want to reverse names order name_last_first = 'yes' # And we don't want to create links print_links = 'no' # Keep real num of authors. fix + affiliations_separator.join(author['u']) + \ nb_authors = len(authors) # Limit num of authors, so that we do not process # the authors that will not be shown. This can only # be done in non-interactive mode, as interactive mode # allows to show all of them. if limit.isdigit() and nb_authors > int(limit) \ and interactive != "yes": if bfo.field('710g'): # check for colln note authors = authors[:1] else: authors = authors[:int(limit)] # Process authors to add link, affiliation and highlight for author in authors: if author.has_key('a'): author['a'] = author['a'][0] # There should not be # repeatable subfields here. if highlight == 'yes': from invenio import bibformat_utils author['a'] = bibformat_utils.highlight(author['a'], bfo.search_pattern) #check if we need to reverse last, first #we don't try to reverse it if it isn't stored with a comma. first_last_match = re_last_first.search(author['a']) author['display'] = author['a'] if name_last_first.lower() == "no": if first_last_match: author['display'] = first_last_match.group('first_names') + \ ' ' + \ first_last_match.group('last') + \ first_last_match.group('extension') #for latex we do initials only (asn assume first last) if markup == 'latex': if first_last_match: first = re_initials.sub('\g<initial>.~', \ first_last_match.group('first_names')) author['display'] = first + \ first_last_match.group('last') + \ first_last_match.group('extension') if print_links.lower() == "yes": # if there is an ID, search using that. id_link = '' if id_links == "yes" and author.has_key('i'): author['i'] = author['i'][0] #possible to have more IDs? id_link = '<a class="authoridlink" href="' + \ CFG_BASE_URL + \ '/search?' + \ 'ln='+ bfo.lang + \ '&p=100__i' + escape(':' + author['i']) + \ '+or+700__i' + escape(':' + author['i']) +\ '">'+escape("(ID Search)") + '</a> ' author['display'] = '<a class="authorlink" href="' + \ CFG_BASE_URL + \ '/author/'+ quote(author['a']) + \ '?recid=' + bibrec_id + \ '&ln='+ bfo.lang + \ '">' + escape(author['display'])+'</a>' + \ id_link if print_affiliations == "yes": if author.has_key('e'): author['e'] = affiliation_prefix + \ affiliations_separator.join(author['e']) + \ affiliation_suffix if author.has_key('u'): author['ilink'] = ['<a class="afflink" href="' + \ CFG_BASE_URL + '/search?cc=Institutions&p=institution:'+ \ quote('"' + string + '"') + \ '&ln=' + bfo.lang + \ '">' + \ string.lstrip() + \ '</a>' for string in author['u']] author['u'] = affiliation_prefix + \ affiliations_separator.join(author['ilink']) + \ affiliation_suffix # # Consolidate repeated affiliations # last = '' authors.reverse() for author in authors: if not author.has_key('u'): author['u'] = '' #print 'this->'+ author['a']+'\n' if last == author['u']: author['u'] = '' else: last = author['u'] authors.reverse() # Flatten author instances if print_affiliations == 'yes': # 100__a (100__e) 700__a (100__e) (100__u) if print_affiliation_first.lower() != 'yes': authors = [author.get('display', '') + author.get('e', '') + author.get('u', '') for author in authors] else: authors = [author.get('u', '') + author.get('display', '') for author in authors] else: authors = [author.get('display', '') for author in authors] # link the extension to detailed record if link_extension == 'yes' and interactive != 'yes': extension = '<a class="authorlink" href="' + \ CFG_BASE_URL + '/' + CFG_SITE_RECORD + '/' + str(bfo.recID) + '">' + \ extension + '</a>' # Detect Collaborations: if collaboration == "yes": colls = [] for coll in bfo.fields("710__g"): if coll not in colls: colls.append(coll) else: colls = [] if colls: short_coll = False colls = [re_coll.sub('', coll) for coll in colls] if print_links.lower() == "yes": colls = ['<a class="authorlink" href="' + CFG_BASE_URL + '/search' + '?p=collaboration:' + quote("'" + coll + "'") + '&ln=' + bfo.lang + '">'+escape(coll)+'</a>' for coll in colls] coll_display = " and ".join(colls) if not coll_display.endswith("aboration"): coll_display += " Collaboration" if len(colls) > 1: coll_display += 's' if nb_authors > 1: if markup == 'latex': coll_display = authors[0] + extension + " [" + \ coll_display + "]" elif interactive == "yes": coll_display += " (" + authors[0] + " " extension += ")" else: #html coll_display += " (" + authors[0] + extension + ")" elif nb_authors == 1: short_coll = True if markup == 'latex': coll_display = authors[0] + " [" + coll_display + "]" else: #html coll_display += " (" + authors[0] + " for the collaboration)" elif nb_authors == 0: short_coll = True if markup == 'latex': coll_display = "[" + coll_display + "]" # Start outputting, depending on options and number of authors if colls and (interactive != "yes" or short_coll): return coll_display if limit.isdigit() and nb_authors > int(limit) and interactive != "yes": if markup == 'latex': lastauthor = authors.pop() lastauthor = ' and ' + lastauthor limit = int(limit) - 1 return separator.join(authors[:int(limit)]) + lastauthor + \ extension elif interactive == "yes" and ((colls and not short_coll) or (limit.isdigit() and nb_authors > int(limit))): out = ''' <script> function toggle_authors_visibility(){ var more = document.getElementById('more'); var link = document.getElementById('link'); var extension = document.getElementById('extension'); if (more.style.display=='none'){ more.style.display = ''; extension.style.display = 'none'; link.innerHTML = "%(show_less)s" } else { more.style.display = 'none'; extension.style.display = ''; link.innerHTML = "%(show_more)s" } link.style.color = "rgb(204,0,0);" } function set_up(){ var extension = document.getElementById('extension'); extension.innerHTML = '%(extension)s'; toggle_authors_visibility(); } </script> ''' % {'show_less': _("Hide"), 'show_more': _("Show all %i authors") % nb_authors, 'extension': extension} # out += '<a name="show_hide" />' if colls: show = coll_display more = separator + separator.join(authors[1:]) + ')' else: show = separator.join(authors[:int(limit)]) more = separator.join(authors[int(limit):len(authors)]) out += show out += ' <span id="more" style="">' + more + '</span>' out += ' <span id="extension"></span>' out += ' <small><i><a id="link" href="#"' + \ ' style="color:green;background:white;" onclick="toggle_authors_visibility()" ' + \ ' style="color:rgb(204,0,0);"></a></i></small>' out += '<script>set_up()</script>' return out elif nb_authors > 0: if markup == 'latex' and nb_authors > 1: lastauthor = authors.pop() lastauthor = ' and ' + lastauthor output = separator.join(authors) + lastauthor # remove the dot from the end of authors list when the suffix starts with dot # (to avoid two consecutive dots) if suffix and output and output[-1] == suffix[0] == '.': output = output[:-1] return output
def format_element(bfo, number_of_featured_articles="1", number_of_articles_with_image="3", new_articles_first='yes', image_px_width="300", small_image_px_width="200", subject_to_css_class_kb="WebJournalSubject2CSSClass", link_image_to_article='yes', image_alignment='left'): """ Creates an overview of all the articles of a certain category in one specific issue. Note the following: <ul> <li>The element consider only the latest issue: when viewing archives of your journal, readers will see the newest articles of the latest issue, not the ones of the issue they are looking at</li> <li>This is not an index of the articles of the latest issue: it display only <b>new</b> articles, that is articles that have never appeared in a previous issue</li> <li>This element produces a table-based layout, in order to have a more or less readable HTML alert when sent some Email clients (Outlook 2007)</li> <li>When producing the HTML output of images, this element tries to insert the width and height attributes to the img tag: this is necessary in order to produce nice HTML alerts. This dimension therefore overrides any dimension defined in the CSS. The Python Image Library (PIL) should be installed for this element to recognize the size of images.</li> </ul> @param number_of_featured_articles: the max number of records with emphasized title @param number_of_articles_with_image: the max number of records for which their image is displayed @param new_articles_first: if 'yes', display new articles before other articles @param image_px_width: (integer) width of first image featured on this page @param small_image_px_width: (integer) width of small images featured on this page @param subject_to_css_class_kb: knowledge base that maps 595__a to a CSS class @param link_image_to_article: if 'yes', link image (if any) to article @param image_alignment: 'left', 'center' or 'right'. To help rendering in Outlook. """ args = parse_url_string(bfo.user_info['uri']) journal_name = args["journal_name"] this_issue_number = args["issue"] category_name = args["category"] verbose = args["verbose"] ln = bfo.lang _ = gettext_set_language(ln) if image_px_width.isdigit(): image_px_width = int(image_px_width) else: image_px_width = None if small_image_px_width.isdigit(): small_image_px_width = int(small_image_px_width) else: small_image_px_width = None # We want to put emphasis on the n first articles (which are not # new) if number_of_featured_articles.isdigit(): number_of_featured_articles = int(number_of_featured_articles) else: number_of_featured_articles = 0 # Only n first articles will display images if number_of_articles_with_image.isdigit(): number_of_articles_with_image = int(number_of_articles_with_image) else: number_of_articles_with_image = 0 # Help image alignement without CSS, to have better rendering in Outlook img_align = '' if image_alignment: img_align = 'align="%s"' % image_alignment # Try to get the page from cache. Only if issue is older or equal # to latest release. latest_released_issue = get_current_issue(ln, journal_name) if verbose == 0 and not issue_is_later_than(this_issue_number, latest_released_issue): cached_html = get_index_page_from_cache(journal_name, category_name, this_issue_number, ln) if cached_html: return cached_html out = '<table border="0" cellpadding="0" cellspacing="0">' # Get the id list ordered_articles = get_journal_articles(journal_name, this_issue_number, category_name, newest_first=new_articles_first.lower() == 'yes') new_articles_only = False if ordered_articles.keys() and max(ordered_articles.keys()) < 0: # If there are only new articles, don't bother marking them as # new new_articles_only = True order_numbers = ordered_articles.keys() order_numbers.sort() img_css_class = "featuredImageScale" for order_number in order_numbers: for article_id in ordered_articles[order_number]: # A record is considered as new if its position is # negative and there are some non-new articles article_is_new = (order_number < 0 and not new_articles_only) temp_rec = BibFormatObject(article_id) title = '' if ln == "fr": title = temp_rec.field('246_1a') if title == '': title = temp_rec.field('245__a') else: title = temp_rec.field('245__a') if title == '': title = temp_rec.field('246_1a') # Get CSS class (if relevant) notes = temp_rec.fields('595__a') css_classes = [temp_rec.kb(subject_to_css_class_kb, note, None) \ for note in notes] css_classes = [css_class for css_class in css_classes \ if css_class is not None] if article_is_new: css_classes.append('new') # Maybe we want to force image to appear? display_image_on_index = False if 'display_image_on_index' in notes: display_image_on_index = True # Build generic link to this article article_link = make_journal_url(bfo.user_info['uri'], {'recid':str(article_id), 'ln': bfo.lang}) # Build the "more" link more_link = '''<a class="readMore" title="link to the article" href="%s"> >> </a> ''' % (article_link) # If we should display an image along with the text, # prepare it here img = '' if (number_of_articles_with_image > 0 and \ not article_is_new) or display_image_on_index: img = _get_feature_image(temp_rec, ln) if img != "": # Now we will try to identify image size in order # to resize it in the HTML for a nicer rendering # of the HTML alert in email clients (Outlook wants # both height and width) img_width = None img_height = None small_img_width = None small_img_height = None width_and_height = '' if PIL_imported: try: local_img = os.path.join(CFG_TMPDIR, 'webjournal_' + \ ''.join([char for char in img \ if char.isalnum()])) if len(local_img) > 255: # Shorten to 255 chars local_img = local_img[0:100] + '_' + local_img[156:] if not os.path.exists(local_img): # Too bad, must download entire image for PIL (local_img, headers) = urllib.urlretrieve(img, local_img) img_file = Image.open(local_img) except IOError, e: pass else: orig_img_width = img_file.size[0] orig_img_height = img_file.size[1] # Then scale according to user-defined width ## First image ratio = float(orig_img_width) / image_px_width img_width = image_px_width img_height = int(orig_img_height / ratio) ## Other smaller images ratio = float(orig_img_width) / small_image_px_width small_img_width = small_image_px_width small_img_height = int(orig_img_height / ratio) # Note that we cannot reuse the nice phl, ph and # phr classes to put a frame around the image: # this is not supported in Outlook 2007 when HTML # alert is sent. if not img_css_class == "featuredImageScale": # Not first image: display smaller img_width = small_img_width img_height = small_img_height if img_width and img_height: width_and_height = 'width="%i" height="%i"' % \ (img_width, img_height) img = '<img alt="" class="%s" src="%s" %s %s/>' % \ (img_css_class, img, img_align, width_and_height) number_of_articles_with_image -= 1 # Next images will be displayed smaller img_css_class = "featuredImageScaleSmall" # Determine size of the title header_tag_size = '3' if number_of_featured_articles > 0 and \ not article_is_new: # n first articles are especially featured header_tag_size = '2' number_of_featured_articles -= 1 # Finally create the output. Two different outputs # depending on if we have text to display or not text = '' if not article_is_new: text = _get_feature_text(temp_rec, ln) # Link image to article if wanted if link_image_to_article.lower() == 'yes': img = create_html_link(urlbase=article_link, link_label=img, urlargd={}) if text != '': out += ''' <tr><td class="article"> <h%(header_tag_size)s class="%(css_classes)s articleTitle" style="clear:both;"> <a title="link to the article" href="%(article_link)s">%(title)s</a> </h%(header_tag_size)s> <div class="articleBody"> %(img)s %(text)s %(more_link)s </div> </td></tr> ''' % {'article_link': article_link, 'title': title, 'img': img, 'text': text, 'more_link': more_link, 'css_classes': ' '.join(css_classes), 'header_tag_size': header_tag_size} else: out += ''' <tr><td class="article"> <h%(header_tag_size)s class="%(css_classes)s articleTitle" style="clear:both;"> <a title="link to the article" href="%(article_link)s">%(title)s</a> %(more_link)s </h%(header_tag_size)s> %(img)s </td></tr> ''' % {'article_link': article_link, 'title': title, 'more_link': more_link, 'img': img, 'css_classes': ' '.join(css_classes), 'header_tag_size': header_tag_size}
def testFieldRetrieval(self): """bfeField retrieval""" self.bfo = BibFormatObject('1') self.assertEqual(self.bfo.field('100a'),"Sachdev, Subir")
def format_element(bfo, latest_issue_only='yes', newest_articles_only='yes', link_category_headers='yes', display_categories='', hide_when_only_new_records="no"): """ Display the index to the newest articles (of the latest issue, or of the displayed issue) @param latest_issue_only: if 'yes', always display articles of the latest issue, even if viewing a past issue @param newest_articles_only: only display new articles, not those that also appeared in previous issues @param link_category_headers: if yes, category headers link to index page of that category @param display_categories: comma-separated list of categories to display. If none, display all @param hide_when_only_new_records: if 'yes' display new articles only if old articles exist in this issue """ args = parse_url_string(bfo.user_info['uri']) journal_name = args["journal_name"] ln = args["ln"] _ = gettext_set_language(ln) if latest_issue_only.lower() == 'yes': issue_number = get_current_issue(bfo.lang, journal_name) else: issue_number = args["issue"] # Try to get HTML from cache if args['verbose'] == 0: cached_html = _get_whatsNew_from_cache(journal_name, issue_number, ln) if cached_html: return cached_html # No cache? Build from scratch # 1. Get the articles journal_categories = get_journal_categories(journal_name, issue_number) if display_categories: display_categories = display_categories.lower().split(',') journal_categories = [category for category in journal_categories \ if category.lower() in display_categories] whats_new_articles = {} for category in journal_categories: whats_new_articles[category] = get_journal_articles(journal_name, issue_number, category, newest_only=newest_articles_only.lower() == 'yes') # Do we want to display new articles only if they have been added # to an issue that contains non-new records? if hide_when_only_new_records.lower() == "yes": # First gather all articles in this issue all_whats_new_articles = {} for category in journal_categories: all_whats_new_articles[category] = get_journal_articles(journal_name, issue_number, category, newest_first=True, newest_only=False) # Then check if we have some articles at position > -1 has_old_articles = False for articles in all_whats_new_articles.values(): if len([order for order in articles.keys() if order > -1]) > 0: has_old_articles = True break if not has_old_articles: # We don't have old articles? Thend don't consider any for category in journal_categories: whats_new_articles[category] = {} # 2. Build the HTML html_out = u'' html_out += _get_breaking_news(ln, journal_name) for category in journal_categories: articles_in_category = whats_new_articles[category] html_articles_in_category = u"" # Generate the list of articles in this category order_numbers = articles_in_category.keys() order_numbers.sort() for order in order_numbers: articles = articles_in_category[order] for recid in articles: link = make_journal_url(bfo.user_info['uri'], {'journal_name': journal_name, 'issue_number': issue_number.split('/')[0], 'issue_year': issue_number.split('/')[1], 'category': category, 'recid': recid, 'ln': bfo.lang}) temp_rec = BibFormatObject(recid) if ln == 'fr': try: title = temp_rec.fields('246_1a')[0] except: continue else: try: title = temp_rec.field('245__a') except: continue try: html_articles_in_category += u'<li><a href="%s">%s</a></li>' % \ (link, title.decode('utf-8')) except: pass if html_articles_in_category: # Good, we found some new articles for this category. # Then insert the genereated results into a larger list # with category as "parent". html_out += '<li>' if link_category_headers.lower() == 'yes': html_out += '<a href="' html_out += make_journal_url(bfo.user_info['uri'], {'journal_name': journal_name, 'issue_number': issue_number.split('/')[0], 'issue_year': issue_number.split('/')[1], 'category': category, 'recid': '', 'ln': bfo.lang}) html_out += '" class="whatsNewCategory">%s</a>' % category else: html_out += '<span class="whatsNewCategory">%s</span>' % category html_out += '<ul class="whatsNewItem">' html_out += html_articles_in_category html_out += '</ul></li>' if not html_out: html_out = '<i>' + _('There are no new articles for the moment') + '</i>' else: html_out = '<ul class="whatsNew">' + html_out + '</ul>' if args['verbose'] == 0: cache_whatsNew(html_out.encode('utf-8'), journal_name, issue_number, ln) return html_out.encode('utf-8')
def format(bfo, new_articles_first='yes', subject_to_css_class_kb="WebJournalSubject2CSSClass", display_all_category_articles='no'): """ Creates a navigation for articles in the same issue and category. @param new_articles_first: if 'yes', display new articles before other articles @param subject_to_css_class_kb: knowledge base that maps 595__a to a CSS class @param display_all_category_articles: if yes, display all articles, whatever category is selected """ # get variables args = parse_url_string(bfo.user_info['uri']) this_recid = bfo.control_field('001') this_issue_number = args["issue"] category_name = args["category"] journal_name = args["journal_name"] ln = args["ln"] _ = gettext_set_language(ln) this_title = "" if ln == "fr": if bfo.fields('246_1a'): this_title = bfo.fields('246_1a')[0] elif bfo.fields('245__a'): this_title = bfo.fields('245__a')[0] else: if bfo.fields('245__a'): this_title = bfo.fields('245__a')[0] elif bfo.fields('246_1a'): this_title = bfo.fields('246_1a')[0] journal_categories = [category_name] if display_all_category_articles.lower() == 'yes': # Let's retrieve all categories. Ok, we are not supposed to do # that with that element, but if journal editor wants... journal_categories = get_journal_categories(journal_name, this_issue_number) menu_out = '' for category in journal_categories: ordered_articles = get_journal_articles(journal_name, this_issue_number, category, newest_first=new_articles_first.lower() == 'yes') new_articles_only = False if ordered_articles.keys() and max(ordered_articles.keys()) < 0: # If there are only new articles, don't bother marking them as # new new_articles_only = True menu_out += '<div class="subNavigationMenu">' order_numbers = ordered_articles.keys() order_numbers.sort() for order_number in order_numbers: for article_id in ordered_articles[order_number]: # A record is considered as new if its position is # negative and there are some non-new articles article_is_new = (order_number < 0 and not new_articles_only) if str(article_id) == this_recid: # Mark as active # Get CSS class (if relevant) notes = bfo.fields('595__a') css_classes = [bfo.kb(subject_to_css_class_kb, note, None) \ for note in notes] css_classes = [css_class for css_class in css_classes \ if css_class is not None] if article_is_new: css_classes.append('new') menu_out += '''<div class="active"> <div class="subNavigationMenuItem %s">%s</div></div>''' % \ (' '.join(css_classes), this_title) else: temp_rec = BibFormatObject(article_id) title = '' if ln == "fr": title = temp_rec.field('246_1a') if title == '': title = temp_rec.field('245__a') else: title = temp_rec.field('245__a') if title == '': title = temp_rec.field('246_1a') # Get CSS class (if relevant) notes = temp_rec.fields('595__a') css_classes = [temp_rec.kb(subject_to_css_class_kb, note, None) \ for note in notes] css_classes = [css_class for css_class in css_classes \ if css_class is not None] if article_is_new: css_classes.append('new') menu_out += '''<div class="subNavigationMenuItem %s"> <a href="%s">%s</a></div> ''' % (' '.join(css_classes), make_journal_url(bfo.user_info['uri'], {'recid': article_id, 'ln': bfo.lang, 'category': category}), title) menu_out += '</div>' return menu_out
req, "%s/journal/%s/%s/%s?ln=%s" % (CFG_SITE_URL, journal_name, current_issue.split("/")[1], current_issue.split("/")[0], ln), ) try: index_page_template = get_journal_template("index", journal_name, ln) except InvenioWebJournalTemplateNotFoundError, e: register_exception(req=req) return e.user_box(req) temp_marc = """<record> <controlfield tag="001">0</controlfield> </record>""" # create a record and get HTML back from bibformat user_info = collect_user_info(req) bfo = BibFormatObject(0, ln=ln, xml_record=temp_marc, user_info=user_info) bfo.req = req verbosity = 0 if editor: # Increase verbosity only for editors/admins verbosity = verbose html = format_with_format_template(index_page_template, bfo, verbose=verbosity) return html def perform_request_article(req, journal_name, issue_number, ln, category, recid, editor=False, verbose=0): """ Central logic function for article pages. Loads the format template for article display and displays the requested article using BibFormat.
def testField(self): print """testing bfeField""" self.bfo=BibFormatObject('7374') self.assertEqual(self.bfo.field('100a'),"Farhi, E.")