def convertLaTeX2Unicode(self, source): for latex_entity in _latex2utf8enc_mapping_simple.keys(): source = _encode(_decode(source).replace(latex_entity, _latex2utf8enc_mapping_simple[latex_entity])) for latex_entity in _latex2utf8enc_mapping.keys(): source = _encode(_decode(source).replace(latex_entity, _latex2utf8enc_mapping[latex_entity])) return source
def convertLaTeX2Unicode(self, source): for latex_entity in _latex2utf8enc_mapping_simple.keys(): source = _encode( _decode(source).replace( latex_entity, _latex2utf8enc_mapping_simple[latex_entity])) for latex_entity in _latex2utf8enc_mapping.keys(): source = _encode( _decode(source).replace(latex_entity, _latex2utf8enc_mapping[latex_entity])) return source
def pre_validate(self, REQUEST, errors): reference_catalog = getToolByName(self, 'reference_catalog') authors = REQUEST.get('authors', []) result = [] references = [] # deduce author names from member reference for author in authors: reference = author.get('reference', None) if reference == 'None': author.reference = '' elif reference: reference_object = reference_catalog.lookupObject(reference) if reference_object.isTranslatable(): references.append(reference_object.getCanonical().UID()) reference_object = reference_object.getCanonical() else: references.append(reference) # obtain author data from privileged fields in the reference object data = self.getAuthorDataFromMember(reference_object) only_requested_data = not not author.get('lastname', None) if data: for key in [ key for key in data.keys() if key not in ('middlename', ) ]: if not only_requested_data or (author.get(key, None) == '?'): if key == 'firstname': author.firstnames = _decode( data['firstname']) + ' ' + _decode( data['middlename']) else: exec('author.%s = _decode(data[key])' % key) # if this doesn't help, we try to derive the author name from the Title of reference... (YUK) if not author.get('lastname', None): firstnames, lastname = self._name_from_reference(reference) author.firstnames = firstnames author.lastname = lastname if ''.join([ _decode(_encode(val)) for val in dict(author).values() ]).strip(): result.append(author) REQUEST.form['authors'] = result[:] REQUEST.form['member_publication_authors'] = references[:]
def pre_validate(self, REQUEST, errors): reference_catalog = getToolByName(self, 'reference_catalog') authors = REQUEST.get('authors',[]) result = [] references=[] # deduce author names from member reference for author in authors: reference = author.get('reference', None) if reference == 'None': author.reference = '' elif reference: reference_object = reference_catalog.lookupObject(reference) if reference_object.isTranslatable(): references.append(reference_object.getCanonical().UID()) reference_object = reference_object.getCanonical() else: references.append(reference) # obtain author data from privileged fields in the reference object data = self.getAuthorDataFromMember(reference_object) only_requested_data = not not author.get('lastname', None) if data: for key in [ key for key in data.keys() if key not in ('middlename',) ]: if not only_requested_data or (author.get(key, None) == '?'): if key == 'firstname': author.firstnames = _decode(data['firstname']) + ' ' + _decode(data['middlename']) else: exec('author.%s = _decode(data[key])' % key) # if this doesn't help, we try to derive the author name from the Title of reference... (YUK) if not author.get('lastname', None): firstnames, lastname = self._name_from_reference(reference) author.firstnames = firstnames author.lastname = lastname if ''.join([_decode(_encode(val)) for val in dict(author).values()]).strip(): result.append(author) REQUEST.form['authors'] = result[:] REQUEST.form['member_publication_authors'] = references[:]
def testViewOnBibFolderReturnsJSON(self): ctxt = self.bib_folder view = ctxt.restrictedTraverse('@@citations-json') json_val = view() self.assertTrue(json_val) try: actual = json.loads(json_val) except Exception: self.fail('invalid json: %s' % json_val) for obj in ctxt.objectValues(): expected = _decode(obj.Title()) self.assertTrue(expected in actual)
def __init__(self, context): self.context = context self.__name__ = self.context.getId() self.title = _decode(self.context.Title()) self.publication_year = getattr(self.context, 'publication_year', None) self.publication_month = self.context.getPublication_month() self.abstract = _decode(self.context.getAbstract()) self.subject = _decode(self.context.getKeywords()) self.note = _decode(self.context.getNote()) self.annote = _decode(getattr(self.context, 'annote', None)) self.url = self.context.aq_base.getURL() # identifiers identifiers = dict() for d in self.context.getIdentifiers(): if 'label' in d and d.get('value'): identifiers[d['label']] = d['value'] self.identifiers = identifiers # Authors as a FormattableNames structure self.getAuthors = self.context.getAuthors
def getCookedBibRefId(self, ref, use_pid_on_import=True, **kwargs): """ cook id for ref dict or object, ref dict / object may be a single reference only """ isReferenceObject = (type(ref) != type({})) if isReferenceObject: ref = self._object2ref(ref) new_id = 'nobody1000' if use_pid_on_import and ref.get('pid'): new_id = ref['pid'] else: new_id = self._cookIdCore(ref, new_id=new_id) return _encode(_decode(self._cleanId(new_id)))
def convertLaTeX2Unicode(self, source): # This function converts LaTeX entities to Unicode characters. # This function was improved by JB, 2011-06-05. # # Changes: # 1. Rather than: # for k in d.keys(): # s = s.replace(k, d[k]) # use: # for k, v in d.items(): # s = s.replace(k, v) # # 2. Rather than (decode -> replace -> encode) *every* *iteration*, # decode once at the beginning, and encode again once at the end. # # This yielded an 8x speedup for the function (profiled by cProfile): # 9.615 CPU secs per call on the ~1M benchmark BibTeX file (19.230 secs total), # down to 1.198 CPU secs/call (2.397 secs total). # # Next, the functions '_build_mapping_tree' and '_replace_using_mapping_tree' # were used to optimise-dowm the number of 'string.replace' calls. # (For more info, read the comment immediately above '_build_mapping_tree'.) # # This optimisation yielded a 4.5x speedup for this function: # 1.198 CPU secs per call (on the ~1M benchmark BibTeX file) down to 0.259. # The total speedup of this function was now >35x: 9.615 down to 0.259. source = _decode(source) #for latex_entity, unicode_code_point in _latex2utf8enc_mapping_simple.items(): #source = source.replace(latex_entity, unicode_code_point) source = _replace_using_mapping_tree( source, LATEX2UTF8ENC_MAPPING_SIMPLE_TREE) #for latex_entity, unicode_code_point in _latex2utf8enc_mapping.items(): #source = source.replace(latex_entity, unicode_code_point) source = _replace_using_mapping_tree(source, LATEX2UTF8ENC_MAPPING_TREE) source = _encode(source) return source
def convertLaTeX2Unicode(self, source): # This function converts LaTeX entities to Unicode characters. # This function was improved by JB, 2011-06-05. # # Changes: # 1. Rather than: # for k in d.keys(): # s = s.replace(k, d[k]) # use: # for k, v in d.items(): # s = s.replace(k, v) # # 2. Rather than (decode -> replace -> encode) *every* *iteration*, # decode once at the beginning, and encode again once at the end. # # This yielded an 8x speedup for the function (profiled by cProfile): # 9.615 CPU secs per call on the ~1M benchmark BibTeX file (19.230 secs total), # down to 1.198 CPU secs/call (2.397 secs total). # # Next, the functions '_build_mapping_tree' and '_replace_using_mapping_tree' # were used to optimise-dowm the number of 'string.replace' calls. # (For more info, read the comment immediately above '_build_mapping_tree'.) # # This optimisation yielded a 4.5x speedup for this function: # 1.198 CPU secs per call (on the ~1M benchmark BibTeX file) down to 0.259. # The total speedup of this function was now >35x: 9.615 down to 0.259. source = _decode(source) #for latex_entity, unicode_code_point in _latex2utf8enc_mapping_simple.items(): #source = source.replace(latex_entity, unicode_code_point) source = _replace_using_mapping_tree(source, LATEX2UTF8ENC_MAPPING_SIMPLE_TREE) #for latex_entity, unicode_code_point in _latex2utf8enc_mapping.items(): #source = source.replace(latex_entity, unicode_code_point) source = _replace_using_mapping_tree(source, LATEX2UTF8ENC_MAPPING_TREE) source = _encode(source) return source
def render(self, title_force_uppercase=False, omit_fields=[], msdos_eol_style=None, # not used resolve_unicode=None, # not used output_encoding=None, # not used ): """ renders a BibliographyEntry object in BiBTex format """ entry = self.context omit = [each.lower() for each in omit_fields] bib_key = utils._validKey(entry) f_temp = tempfile.mktemp() fp = codecs.open(f_temp, 'w', 'utf-8', 'ignore') print >>fp, _c(u"\n@%s{%s,", entry.publication_type, bib_key) if entry.editor_flag and self._isRenderableField('editor', omit): print >>fp, _c(u" editor = {%s},", entry.authors) elif not entry.editor_flag and self._isRenderableField('authors', omit): print >>fp, _c(" author = {%s},", entry.authors) if self._isRenderableField('authorurls', omit): aURLs = utils.AuthorURLs(entry) if aURLs.find('http') > -1: print >>fp, _c(u" authorURLs = {%s},", aURLs) if self._isRenderableField('title', omit): if title_force_uppercase: print >>fp, _c(u" title = {%s},", utils._braceUppercase(entry.title)) else: print >>fp, _c(u" title = {%s},", entry.title) if self._isRenderableField('year', omit): print >>fp, _c(u" year = {%s},", entry.publication_year) if self._isRenderableField('month', omit): if entry.publication_month: print >>fp, _c(u" month = {%s},", entry.publication_month) if entry.url and self._isRenderableField('url', omit): print >>fp, _c(u" URL = {%s},", entry.url) if entry.abstract and self._isRenderableField('abstract', omit): print >>fp, _c(u" abstract = {%s},", entry.abstract) for key, val in entry.source_fields: if self._isRenderableField(key, omit) and val: if not isinstance(val, unicode): val = utils._decode(val) print >>fp, _c(u" %s = {%s},", key.lower(), val) if self._isRenderableField('subject', omit): kws = ', '.join(entry.subject) if kws: if not isinstance(kws, unicode): kws = utils._decode(kws) print >>fp, _c(u" keywords = {%s},", kws) if self._isRenderableField('note', omit): note = getattr(entry, 'note', None) if note: print >>fp, _c(u" note = {%s},", note) if self._isRenderableField('annote', omit): annote = getattr(entry, 'annote', None) if annote: print >>fp, _c(u" annote = {%s},", annote) if self._isRenderableField('additional', omit): try: additional = entry.context.getAdditional() except AttributeError: additional = [] for mapping in additional: print >>fp, _c(u" %s = {%s},", mapping['key'], mapping['value']) keys = entry.identifiers.keys() keys.sort() source_fields_keys = [tp[0].lower() for tp in entry.source_fields] for identifier, value in entry.identifiers.items(): if value: print >>fp, _c(u" %s = {%s},", identifier.lower(), value) # remove trailing command fp.seek(-2, 2) print >>fp print >>fp, u"}" print >>fp fp.close() bibtex = file(f_temp, 'r').read() os.unlink(f_temp) return _normalize(bibtex, resolve_unicode=True)
def getEntryDict(self, bibref_item, instance=None, title_link=False, title_link_only_if_owner=False, ): """ transform a BiblioRef Object into python dictionary """ ref_attributes = ('publication_year', 'publication_month', 'publication_url', 'abstract', 'note', 'publisher', 'editor', 'editor_flag', 'organization', 'institution', 'school', 'address', 'booktitle', 'chapter', 'journal', 'volume', 'edition', 'number', 'pages', 'series', 'type', 'howpublished', 'preprint_server', 'pmid', 'isbn', 'annote',) values = {} values['UID'] = bibref_item.UID() tmp_title = _decode(bibref_item.Title()) if tmp_title and (tmp_title[-1] == '.'): tmp_title = tmp_title[:-1] values['title'] = tmp_title uniauthors=[] for author in bibref_item.getAuthors(): uniauthor={} for field in author.keys(): uniauthor[field] = _decode(author.get(field)) uniauthors.append(uniauthor) values['authors'] = uniauthors uniauthors=[] uniauthor={} for attr in ref_attributes: field = bibref_item.getField(attr) if field: value = getattr(bibref_item, field.accessor)() if not value: value = field.getDefault(bibref_item) try: for x in range(value.len()): value[x] = _decode(value[x]) values[attr] = value except: if type(value) == type(''): values[attr] = _decode(value) else: # this is mainly for the editor flag (not a string, no unicode transformation possible values[attr] = value values['source'] = _decode(bibref_item.Source()) values['description'] = _decode(bibref_item.Description()) values['meta_type'] = bibref_item.meta_type values['reference_type'] = bibref_item.getReference_type() # should be same as portal_type if title_link: mtool = getToolByName(self, 'portal_membership') bibref_item_roles = mtool.getAuthenticatedMember().getRolesInContext(bibref_item) if not title_link_only_if_owner or [ role for role in ['Owner', 'Manager'] if role in bibref_item_roles ]: values['title_link'] = bibref_item.absolute_url() wf_tool = getToolByName(self, 'portal_workflow') values['review_state'] = wf_tool.getInfoFor(bibref_item, 'review_state', '') if instance is not None: values['came_from'] = instance.absolute_url() values['came_from_title'] = _decode(instance.Title()) values['came_from_description'] = _decode(instance.Description()) return values
def getEntryDict( self, bibref_item, instance=None, title_link=False, title_link_only_if_owner=False, ): """ transform a BiblioRef Object into python dictionary """ ref_attributes = ( 'publication_year', 'publication_month', 'publication_url', 'abstract', 'note', 'publisher', 'editor', 'editor_flag', 'organization', 'institution', 'school', 'address', 'booktitle', 'chapter', 'journal', 'volume', 'edition', 'number', 'pages', 'series', 'type', 'howpublished', 'preprint_server', 'pmid', 'isbn', 'annote', ) values = {} values['UID'] = bibref_item.UID() tmp_title = _decode(bibref_item.Title()) if tmp_title and (tmp_title[-1] == '.'): tmp_title = tmp_title[:-1] values['title'] = tmp_title uniauthors = [] for author in bibref_item.getAuthors(): uniauthor = {} for field in author.keys(): uniauthor[field] = _decode(author.get(field)) uniauthors.append(uniauthor) values['authors'] = uniauthors uniauthors = [] uniauthor = {} for attr in ref_attributes: field = bibref_item.getField(attr) if field: value = getattr(bibref_item, field.accessor)() if not value: value = field.getDefault(bibref_item) try: for x in range(value.len()): value[x] = _decode(value[x]) values[attr] = value except: if type(value) == type(''): values[attr] = _decode(value) else: # this is mainly for the editor flag (not a string, no unicode transformation possible values[attr] = value values['source'] = _decode(bibref_item.Source()) values['description'] = _decode(bibref_item.Description()) values['meta_type'] = bibref_item.meta_type values['reference_type'] = bibref_item.getReference_type( ) # should be same as portal_type if title_link: mtool = getToolByName(self, 'portal_membership') bibref_item_roles = mtool.getAuthenticatedMember( ).getRolesInContext(bibref_item) if not title_link_only_if_owner or [ role for role in ['Owner', 'Manager'] if role in bibref_item_roles ]: values['title_link'] = bibref_item.absolute_url() wf_tool = getToolByName(self, 'portal_workflow') values['review_state'] = wf_tool.getInfoFor(bibref_item, 'review_state', '') if instance is not None: values['came_from'] = instance.absolute_url() values['came_from_title'] = _decode(instance.Title()) values['came_from_description'] = _decode(instance.Description()) return values
def render( self, title_force_uppercase=False, omit_fields=[], msdos_eol_style=None, # not used resolve_unicode=None, # not used output_encoding=None, # not used ): """ renders a BibliographyEntry object in BiBTex format """ entry = self.context omit = [each.lower() for each in omit_fields] bib_key = utils._validKey(entry) f_temp = tempfile.mktemp() fp = codecs.open(f_temp, 'w', 'utf-8', 'ignore') print >> fp, _c(u"\n@%s{%s,", entry.publication_type, bib_key) if entry.editor_flag and self._isRenderableField('editor', omit): print >> fp, _c(u" editor = {%s},", entry.authors) elif not entry.editor_flag and self._isRenderableField( 'authors', omit): print >> fp, _c(" author = {%s},", entry.authors) if self._isRenderableField('authorurls', omit): aURLs = utils.AuthorURLs(entry) if aURLs.find('http') > -1: print >> fp, _c(u" authorURLs = {%s},", aURLs) if self._isRenderableField('title', omit): if title_force_uppercase: print >> fp, _c(u" title = {%s},", utils._braceUppercase(entry.title)) else: print >> fp, _c(u" title = {%s},", entry.title) if self._isRenderableField('year', omit): print >> fp, _c(u" year = {%s},", entry.publication_year) if self._isRenderableField('month', omit): if entry.publication_month: print >> fp, _c(u" month = {%s},", entry.publication_month) if entry.url and self._isRenderableField('url', omit): print >> fp, _c(u" URL = {%s},", entry.url) if entry.abstract and self._isRenderableField('abstract', omit): print >> fp, _c(u" abstract = {%s},", entry.abstract) for key, val in entry.source_fields: if self._isRenderableField(key, omit) and val: if not isinstance(val, unicode): val = utils._decode(val) print >> fp, _c(u" %s = {%s},", key.lower(), val) if self._isRenderableField('subject', omit): kws = ', '.join(entry.subject) if kws: if not isinstance(kws, unicode): kws = utils._decode(kws) print >> fp, _c(u" keywords = {%s},", kws) if self._isRenderableField('note', omit): note = getattr(entry, 'note', None) if note: print >> fp, _c(u" note = {%s},", note) if self._isRenderableField('annote', omit): annote = getattr(entry, 'annote', None) if annote: print >> fp, _c(u" annote = {%s},", annote) if self._isRenderableField('additional', omit): try: additional = entry.context.getAdditional() except AttributeError: additional = [] for mapping in additional: print >> fp, _c(u" %s = {%s},", mapping['key'], mapping['value']) keys = entry.identifiers.keys() keys.sort() source_fields_keys = [tp[0].lower() for tp in entry.source_fields] for identifier, value in entry.identifiers.items(): if value: print >> fp, _c(u" %s = {%s},", identifier.lower(), value) # remove trailing command fp.seek(-2, 2) print >> fp print >> fp, u"}" print >> fp fp.close() bibtex = file(f_temp, 'r').read() os.unlink(f_temp) return _normalize(bibtex, resolve_unicode=True)