def to_xml(self,p='',indent=2): """ Converts the item to xml format. The prefix is added to each entry """ sp= indent spc=indent*' ' s='%s<%sentry id="%s">\n' %(sp*spc,p,self.get_field('_code','')) sp += 1 s+='%s<%s%s>\n' %(sp*spc,p,self.get('_type','')) for k,e in self.iteritems(): if k == 'author': sp+=1 space=sp*spc+'\n' v= space.join(['%s<%sauthor>%s</%sauthor>'%(sp*spc,p,x,p) for x in self.get_authorsList()]) v= helper.removebraces(v) v= helper.replace_tags(v,'other') sp-=1 s+= '%s<%s%s>\n%s\n%s</%s%s>\n' %(sp*spc,p,'authors',v,sp*spc,p,'authors') else: if helper.is_string_like(e): v= helper.replace_tags(e,'xml') v= helper.handle_math(v) if k=='title': v=helper.capitalizestring(v) v= helper.removebraces(v) v= helper.replace_tags(v,'other') s+= '%s<%s%s>%s</%s%s>\n' %(sp*spc,p,k,v,p,k) sp-=1 s+= '%s</%s%s>\n' %(sp*spc,p,self.get('_type','')) s+= '%s</%sentry>\n' %(sp*spc,p) return s
def replace_abbrevs(strs,bitem): """ Resolve all abbreviations found in the value fields of one entry""" b=bitem for f,v in b.iteritems(): if helper.is_string_like(v): b[f]= helper.replace_abbrevs(strs,v) return b
def to_xml(self, p='', indent=2): """ Converts the item to xml format. The prefix is added to each entry """ from xml.sax.saxutils import escape from string import capwords sp = indent * 3 spc = indent * ' ' entry_type = self.get('_type', '') # check it's one of our approved types! if (entry_type != "article" and\ entry_type != "book" and \ entry_type != "incollection" and \ entry_type != "inbook"): return None if (entry_type == 'inbook'): entry_type = 'in_book' if (entry_type == 'incollection'): entry_type = 'in_collection' s = '%s<bibliographic_information>\n' % (sp * spc) sp += 1 s += '%s<%s%s>\n' % (sp * spc, p, entry_type) # We need to pull items out in the right order, which is defined # the the Relax NG schema. Note the schema has , as element seperators, # not &, so order *IS* important. Correct order is: authors, title, # year, journal, pages, number, doi # this varies by type, so lot's of is in here! # Optional fields are checked for None and if so # they are *not* added sp += 1 v = "" for x in self.get_authorsList(): v += '%s<author>\n' % (sp * spc) sp += 1 try: other_names, fam_name = x.strip().rsplit(' ', 1) except ValueError: # only family name fam_name = x.strip() other_names = "--" v += '%s<surname>\n' % (sp * spc) sp += 1 v += '%s<string_value lines="1">%s</string_value>\n' % ( sp * spc, capwords(fam_name)) sp -= 1 v += '%s</surname>\n' % (sp * spc) v += '%s<other_names>\n' % (sp * spc) sp += 1 v += '%s<string_value lines="1">%s</string_value>\n' % ( sp * spc, capwords(other_names)) sp -= 1 v += '%s</other_names>\n' % (sp * spc) sp -= 1 v += '%s</author>\n' % (sp * spc) v = helper.removebraces(v) v = helper.replace_tags(v, 'other') sp -= 1 s += '%s<%s%s>\n%s%s</%s%s>\n' % (sp * spc, p, 'authors', v, sp * spc, p, 'authors') # title k = self.get_field('title') if (helper.is_string_like(k)): k = helper.replace_tags(k, 'xml') sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % (sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<title>%s</title>\n' % (sp * spc, v) # year k = self.get_field('year') sp += 1 v = '\n%s<integer_value rank="0">%s</integer_value>\n%s' % ( sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<year>%s</year>\n' % (sp * spc, v) # book, in book and incollection have editors here if (entry_type == "book" or entry_type == "in_book" or entry_type == "in_collection"): v = "" for x in self.get_authorsList(who='editor'): try: other_names, fam_name = x.rsplit(' ', 1) except AttributeError: continue v += '%s<editor>\n' % (sp * spc) sp += 1 v += '%s<surname>\n' % (sp * spc) sp += 1 v += '%s<string_value lines="1">%s</string_value>\n' % ( sp * spc, fam_name) sp -= 1 v += '%s</surname>\n' % (sp * spc) v += '%s<other_names>\n' % (sp * spc) sp += 1 v += '%s<string_value lines="1">%s</string_value>\n' % ( sp * spc, other_names) sp -= 1 v += '%s</other_names>\n' % (sp * spc) sp -= 1 v += '%s</editor>\n' % (sp * spc) v = helper.removebraces(v) v = helper.replace_tags(v, 'other') sp -= 1 s += '%s<%s%s>\n%s%s</%s%s>\n' % (sp * spc, p, 'editors', v, sp * spc, p, 'editors') # journal - if article if (entry_type == "article"): k = self.get_field('journal') if (helper.is_string_like(k)): k = helper.replace_tags(k, 'xml') sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<journal>%s</journal>\n' % (sp * spc, v) # booktitle - if incollection if (entry_type == "in_collection"): k = self.get_field('booktitle') if (helper.is_string_like(k)): k = helper.replace_tags(k, 'xml') sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<booktitle>%s</booktitle>\n' % (sp * spc, v) ## All entries are now optional - if they aren't in the bib file, don't add ## empty tags, just ignore completely. # series - everything but article if (entry_type == "book" or entry_type == "in_book" or entry_type == "in_collection"): k = self.get_field('series') if (helper.is_string_like(k)): k = helper.replace_tags(k, 'xml') if (k != None): sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<series>%s</series>\n' % (sp * spc, v) # publisher - everything but article if (entry_type == "book" or entry_type == "in_book" or entry_type == "in_collection"): k = self.get_field('publisher') if (helper.is_string_like(k)): k = helper.replace_tags(k, 'xml') if (k != None): sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<publisher>%s</publisher>\n' % (sp * spc, v) # volume if (entry_type == "article"): volume = self.get_field('volume') if (helper.is_string_like(volume)): volume = helper.replace_tags(volume, 'xml') if (volume != None): sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, volume, (sp - 1) * spc) sp -= 1 s += '%s<volume>%s</volume>\n' % (sp * spc, v) # pages - unless in a book if (entry_type != "book"): pages = "" lastpage = self.get_field('lastpage') firstpage = self.get_field('firstpage') if (lastpage != None and firstpage != None): if (lastpage == ''): pages = firstpage else: pages = firstpage + "-" + lastpage # escape pages = escape(pages) sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, pages, (sp - 1) * spc) sp -= 1 s += '%s<pages>%s</pages>\n' % (sp * spc, v) # number - article only if (entry_type == "article"): k = self.get_field('number') if (k != None): sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<issue>%s</issue>\n' % (sp * spc, v) # doi - everyone! k = self.get_field('doi') if (k != None): if (helper.is_string_like(k)): k = helper.replace_tags(k, 'xml') sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<doi>%s</doi>\n' % (sp * spc, v) # everyone k = self.get_field('url') if (k != None): if (helper.is_string_like(k)): k = helper.replace_tags(k, 'xml') sp += 1 v = '\n%s<string_value lines="1">%s</string_value>\n%s' % ( sp * spc, k, (sp - 1) * spc) sp -= 1 s += '%s<url>%s</url>\n' % (sp * spc, v) sp -= 1 s += '%s</%s%s>\n' % (sp * spc, p, entry_type) s += '%s</%sbibliographic_information>\n' % (sp * spc, p) return s
def export_html(self, fname=None, style={}, head='',tail='', separate_css='biblio.css', css_style=None, encoding='utf-8'): """ Export a bibliography (set of items) to a file in bibtex format: style is a dictionary (like in bibitem objects) where the values is a pair (open,close) to insert around the data. head and tail are html code to insert before and after the list of publications separate_css may have the """ # default style def_css_style=""" .title a, .title {font-weight: bold; color : #416DFF; } ol.bibliography li{ margin-bottom:0.5em;} .journal { font-style: italic;} .book .series { font-style: italic;} .journal:after {content:" ";} .series:after {content:" ";} li.article .publisher {display:none;} .publisher:before {content:" (";} .publisher:after {content:") ";} .year:before {content:" (";} .year:after {content:").";} .authors {font-weight:bold; display:list;} .authors:after {content:". ";} .volume { font-weight: bold;} .book .volume: before { content: "Vol. ";} .number:before {content:":";} .button {display:inline; border: 3px ridge;line-height:2.2em;margin: 0pt 10pt 0pt 0pt;padding:1pt;} .masterthesis:before{font-weight: bold;content:"Master Thesis"} .phdthesis:before{font-weight: bold;content:"Phd Thesis"} div.abstracts {display: inline; font-weight: bold; text-decoration : none; border: 3px ridge;} div.abstract {display: none;padding: 0em 1% 0em 1%; border: 3px double rgb(130,100,110); text-align: justify;} """ if css_style == None: css_style= def_css_style if helper.is_string_like(separate_css): the_path, fname_css= os.path.split(separate_css) fpath= os.path.dirname(fname) the_path= os.path.normpath(os.path.join(fpath,the_path)) fname_css= os.path.join(the_path,fname_css) css=' <link title="new" rel="stylesheet" href="' + separate_css + '" type="text/css">' fi=helper.openfile(fname_css,'w'); fi.write(css_style); helper.closefile(fi) else: css= '<style type="text/css">' +css_style + '</style>' if head=='': head=''' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset='''+encoding.upper()+'''"> ''' + css + ''' <title>Publicaciones</title> <script language="JavaScript" type="text/javascript"> //<![CDATA[ function toggle(thisid) { var thislayer=document.getElementById(thisid); if (thislayer.style.display == 'block') { thislayer.style.display='none'; } else { thislayer.style.display='block';} } //]]> </script> </head> <body> <h2>Publicaciones</h2> <ol class="bibliography"> ''' if tail == '': tail=""" </ol> </body> </html> """ s= head + self.to_html(style=style) + tail fi= helper.openfile(fname,'w'); fi.write(s.encode(encoding,'xmlcharrefreplace')) helper.closefile(fi)
def export_html(self, fname=None, style={}, head='',tail='', separate_css=None,encoding='utf-8'): """ Export a bibliography (set of items) to a file in bibtex format: style is a dictionary (like in bibitem objects) where the values is a pair (open,close) to insert around the data. head and tail are html code to insert before and after the list of publications separate_css may have the """ # default style css_style=""" .title a, .title {font-weight: bold; color : #416DFF; } ol.bibliography li{ margin-bottom:0.5em;} .journal { font-style: italic;} .book .series { font-style: italic;} .journal:after {content:" ";} .series:after {content:" ";} li.article .publisher {display:none;} .publisher:before {content:" (";} .publisher:after {content:") ";} .year:before {content:" (";} .year:after {content:").";} .authors {font-weight:bol; display:list;} .authors:after {content:". ";} .volume { font-weight: bold;} .book .volume: before { content: "Vol. ";} .number:before {content:":";} .button {display:inline; border: 3px ridge;line-height:2.2em;margin: 0pt 10pt 0pt 0pt;padding:1pt;} .masterthesis{content:"Master Thesis"} .phdthesis{content:"Phd Thesis"} div.abstracts {display: inline; font-weight: bold; text-decoration : none; border: 3px ridge;} div.abstract {display: none;padding: 0em 1% 0em 1%; border: 3px double rgb(130,100,110); text-align: justify;} """ if helper.is_string_like(separate_css): fi=helper.openfile(separate_css,'w'); fi.write(css_style); helper.closefile(fi) name= os.path.commonprefix([os.path.dirname(fname),os.path.dirname(separate_css)]) name= os.path.join(name,separate_css[len(name):]) css=' <link title="new" rel="stylesheet" href="' + name + '" type="text/css">' else: css= '<style type="text/css">' +css_style + '</style>' if head=='': head=''' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset='''+encoding.upper()+'''"> ''' + css + ''' <title>Bibliography</title> <script language="JavaScript" type="text/javascript"> //<![CDATA[ function toggle(thisid) { var thislayer=document.getElementById(thisid); if (thislayer.style.display == 'block') { thislayer.style.display='none'; } else { thislayer.style.display='block';} } //]]> </script> </head> <body> <h2>Bibliography</h2> <ol class="bibliography"> ''' if tail == '': tail=""" </ol> </body> </html> """ s= head + self.to_html(style=style) + tail fi= helper.openfile(fname,'w'); fi.write(s.encode(encoding,'xmlcharrefreplace')) helper.closefile(fi)
def replace_abbrevs(strs, bitem): """ Resolve all abbreviations found in the value fields of one entry""" b = bitem for f, v in b.iteritems(): if helper.is_string_like(v): b[f] = helper.replace_abbrevs(strs, v) return b