Example #1
0
  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
Example #2
0
  def to_html(self,style={}):
    """
    Converts the item to html format with the given style The style is a pair (before,
    after) surrounding the corresponding field (except for authors)
    """

    fields = ['title', 'author', 'journal', 'volume','number', 'month', 'booktitle', 'chapter',
                     'address', 'edition', 'howpublished','school', 'institution', 'organization',
                     'publisher', 'series','firstpage','lastpage', 'note', 'crossref', 'issn','isbn',
                     'year', 'keywords' ,'annote', '_code', 'doi',  'url',  'abstract']

    st= dict(self.html_style)
    st.update(style)
    if not st.has_key('fields'): st['fields'] = fields 

    # Format the title
    title = self.get_field('title','')
    title= helper.handle_math(title)
    title= helper.removebraces(title)
    if title != '' and st.get('title',['','']) !=  None:
      title= title.strip().join(st.get('title',['','']))
    else:
      title = ''

    # Format authors
    if self.has_key('author') and st.get('author')!= None:
      form_aut=''
      list_aut= self.get_authorsList()
      if len(list_aut) > 10:
        list_aut=list_aut[:10]
        list_aut.append('<i>et al</i>')
      for a in list_aut:
        form_aut += '<span class="author">'+ a +'</span>, '
      form_aut= form_aut[:-2]
      autores= form_aut.join(st.get('author',['','']))
    
    # Put all fields together
    s = ''
    for field in st['fields']:
      if field == 'author':  value = autores
      elif field == 'title':  value = title
      elif self.get_field(field,'') != '' and st.get(field,(' ',' ')) != None:
        value= helper.handle_math(self.get_field(field,'').strip())
        value= helper.removebraces(value).join(st.get(field,[' ',' ']))
      else: value = ''
      s+= value

    # Aca agrego algo al principio y al final del item completo (por ejemplo '<li>' )
    if st.get('_type',['','']) !=  None:
      s= s.strip().join(st.get('_type',['','']))

    s= s.decode('latex','replace')  # Convert from latex some characters using encoding
    return unicode(s)
Example #3
0
def bibtexauthor(data):
  """ Returns a list of authors where each author is a list of the form:
  [von, Last, First, Jr]
  """
  return map(helper.process_name,helper.removebraces(data).split(' and '))
Example #4
0
    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
Example #5
0
def bibtexauthor(data):
    """ Returns a list of authors where each author is a list of the form:
  [von, Last, First, Jr]
  """
    return map(helper.process_name, helper.removebraces(data).split(' and '))