From 92ed938369de85444afc51b250aef12d95e5d555 Mon Sep 17 00:00:00 2001 From: Ayush Dubey Date: Mon, 20 Jun 2016 10:53:25 -0700 Subject: [PATCH 1/8] no-address flag to remove conf/book/techreport/misc address from bib --- crosstex/__init__.py | 3 +++ crosstex/cmd.py | 4 ++++ crosstex/style/homepage.py | 8 ++++---- crosstex/style/plain.py | 8 ++++---- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/crosstex/__init__.py b/crosstex/__init__.py index 1fa7423..a4a8834 100644 --- a/crosstex/__init__.py +++ b/crosstex/__init__.py @@ -356,6 +356,9 @@ def __init__(self, xtx_path=None): def no_pages(self): self._flags.add('no-pages') + def no_address(self): + self._flags.add('no-address') + def add_in(self): self._flags.add('add-in') diff --git a/crosstex/cmd.py b/crosstex/cmd.py index 882df78..853d80b 100644 --- a/crosstex/cmd.py +++ b/crosstex/cmd.py @@ -132,6 +132,8 @@ def __call__(self, parser, args, values, option_string=None): help='Write the bibliography to the specified output file.') parser.add_argument('--no-pages', action='store_const', const=True, default=False, help='Skip pages.') +parser.add_argument('--no-address', action='store_const', const=True, default=False, + help='Skip address.') parser.add_argument('--add-in', action='store_const', const=True, default=False, help='Add "In" for articles.') parser.add_argument('--add-proc', dest='add_proc', @@ -155,6 +157,8 @@ def main(argv): xtx.set_titlecase(args.titlecase) if args.no_pages: xtx.no_pages() + if args.no_address: + xtx.no_address() if args.add_in: xtx.add_in() if args.add_proc == 'proc': diff --git a/crosstex/style/homepage.py b/crosstex/style/homepage.py index 1a197da..63bf609 100644 --- a/crosstex/style/homepage.py +++ b/crosstex/style/homepage.py @@ -104,7 +104,7 @@ def render_book(self, book, context=None, history=None): # XXX need to handle editors title = self.render_title(book.title) publisher = self.render_str(book.publisher, 'publisher') if book.publisher else None - address = self.render_address(book.address) if book.address else None + address = self.render_address(book.address) if book.address and 'no-address' not in self._flags else None year = self.render_year(book.year) if book.year else None first = '' second = '' @@ -129,7 +129,7 @@ def render_inproceedings(self, inproceedings, context=None, history=None): title = self.render_title(inproceedings.title) booktitle = self.render_booktitle(inproceedings.booktitle) pages = self.render_pages(inproceedings.pages.value) if inproceedings.pages and 'no-pages' not in self._flags else None - address = self.render_address(inproceedings.address) if inproceedings.address else None + address = self.render_address(inproceedings.address) if inproceedings.address and 'no-address' not in self._flags else None year = self.render_year(inproceedings.year) if inproceedings.year else None month = self.render_month(inproceedings.month) if inproceedings.month else None first = '' @@ -167,7 +167,7 @@ def render_misc(self, misc, context=None, history=None): title = self.render_title(misc.title) if misc.title else None howpub = unicode(misc.howpublished.value) if misc.howpublished else None booktitle = self.render_booktitle(misc.booktitle) if misc.booktitle else None - address = self.render_address(misc.address) if misc.address else None + address = self.render_address(misc.address) if misc.address and 'no-address' not in self._flags else None year = self.render_year(misc.year) if misc.year else None first = '' second = '' @@ -194,7 +194,7 @@ def render_techreport(self, techreport, context=None, history=None): title = self.render_title(techreport.title) number = unicode(techreport.number.value) if techreport.number else None insti = unicode(techreport.institution.value) if techreport.institution else None - address = self.render_address(techreport.address) if techreport.address else None + address = self.render_address(techreport.address) if techreport.address and 'no-address' not in self._flags else None year = self.render_year(techreport.year) month = self.render_month(techreport.month) if techreport.month else None first = '' diff --git a/crosstex/style/plain.py b/crosstex/style/plain.py index 4f84cb4..7814720 100644 --- a/crosstex/style/plain.py +++ b/crosstex/style/plain.py @@ -343,7 +343,7 @@ def render_book(self, book, context=None, history=None): # XXX need to handle editors title = self.render_title(book.title) publisher = self.render_str(book.publisher, 'publisher') if book.publisher else None - address = self.render_address(book.address) if book.address else None + address = self.render_address(book.address) if book.address and 'no-address' not in self._flags else None year = self.render_year(book.year) if book.year else None first = '' second = '' @@ -367,7 +367,7 @@ def render_inproceedings(self, inproceedings, context=None, history=None): title = self.render_title(inproceedings.title) booktitle = self.render_booktitle(inproceedings.booktitle) pages = self.render_pages(inproceedings.pages.value) if inproceedings.pages and 'no-pages' not in self._flags else None - address = self.render_address(inproceedings.address) if inproceedings.address else None + address = self.render_address(inproceedings.address) if inproceedings.address and 'no-address' not in self._flags else None year = self.render_year(inproceedings.year) if inproceedings.year else None month = self.render_month(inproceedings.month) if inproceedings.month else None first = '' @@ -404,7 +404,7 @@ def render_misc(self, misc, context=None, history=None): title = self.render_title(misc.title) if misc.title else None howpub = unicode(misc.howpublished.value) if misc.howpublished else None booktitle = self.render_booktitle(misc.booktitle) if misc.booktitle else None - address = self.render_address(misc.address) if misc.address else None + address = self.render_address(misc.address) if misc.address and 'no-address' not in self._flags else None year = self.render_year(misc.year) if misc.year else None first = '' second = '' @@ -430,7 +430,7 @@ def render_techreport(self, techreport, context=None, history=None): title = self.render_title(techreport.title) number = unicode(techreport.number.value) if techreport.number else None insti = unicode(techreport.institution.value) if techreport.institution else None - address = self.render_address(techreport.address) if techreport.address else None + address = self.render_address(techreport.address) if techreport.address and 'no-address' not in self._flags else None year = self.render_year(techreport.year) month = self.render_month(techreport.month) if techreport.month else None first = '' From 83895a3948c1cb1fd6b5af490087d605699a2560 Mon Sep 17 00:00:00 2001 From: Ayush Dubey Date: Wed, 7 Dec 2016 15:44:35 -0500 Subject: [PATCH 2/8] acm journal format --- .../style/ACM-Reference-Format-Journals.py | 495 ++++++++++++++++++ crosstex/style/__init__.py | 60 +++ 2 files changed, 555 insertions(+) create mode 100644 crosstex/style/ACM-Reference-Format-Journals.py diff --git a/crosstex/style/ACM-Reference-Format-Journals.py b/crosstex/style/ACM-Reference-Format-Journals.py new file mode 100644 index 0000000..ea70eed --- /dev/null +++ b/crosstex/style/ACM-Reference-Format-Journals.py @@ -0,0 +1,495 @@ +import math + +import crosstex.latex +import crosstex.style + + +class ACMJournalBbl(object): + + etalchar = '{\etalchar{+}}' + + def header(self, longest): + return '\\newcommand{\etalchar}[1]{$^{#1}$}\n' + \ + '\\begin{thebibliography}{%s}\n' % longest + + def heading(self, name, sep): + return '' + + def footer(self): + return '\n\end{thebibliography}\n' + + def list_begin(self): + return '' + + def list_end(self): + return '' + + def item(self, key, label, rendered_obj): + if label: + label = '[%s]' % label + return '\n' + ('\\bibitem%s{%s}\n' % (label, key)) + crosstex.latex.to_latex(rendered_obj) + '\n' + + def block(self, text): + return text.strip() + + def block_sep(self): + return '\n\\newblock ' + + def emph(self, text): + return r'\emph{' + text.strip() + '}' + + +class ACMJournalTxt(object): + + etalchar = '+' + + def header(self, longest): + return '' + + def heading(self, name, sep): + if sep: + return '\n%s\n' % name + else: + return '%s\n' % name + + def footer(self): + return '' + + def list_begin(self): + return '' + + def list_end(self): + return '' + + def item(self, key, label, rendered_obj): + return rendered_obj + '\n' + + def block(self, text): + return text.strip() + + def block_sep(self): + return ' ' + + def emph(self, text): + return text.strip() + + +class ACMJournalHtml(object): + + etalchar = '+' + + def header(self, longest): + return '' + + def heading(self, name, sep): + return '\n\n' % name + + def footer(self): + return '
%s
' + + def list_begin(self): + return '' + + def list_end(self): + return '' + + def item(self, key, label, rendered_obj): + return '' \ + + '%s' % (label, label) \ + + rendered_obj + '\n' + + def block(self, text): + return '' + text.strip() + '' + + def block_sep(self): + return '\n' + + def emph(self, text): + return '' + text.strip() + '' + + +class Style(crosstex.style.Style): + + formatters = {'bbl': ACMJournalBbl, + 'txt': ACMJournalTxt, + 'html': ACMJournalHtml} + + @classmethod + def formats(cls): + return set(Style.formatters.keys()) + + def __init__(self, fmt, flags, options, db): + fmtr = self.formatters.get(fmt, None) + assert fmtr + self._fmt = fmtr() + self._db = db + self._flags = flags or set([]) + self._options = options or {} + + def sort_key(self, citation): + cite, obj = citation + author = None + if 'author' in obj.allowed and obj.author: + author = self.get_field(obj, 'author') + title = None + if 'title' in obj.allowed and obj.title: + title = obj.title.value + where = None + if 'booktitle' in obj.allowed and obj.booktitle: + where = self.render_booktitle(obj.booktitle) + elif 'journal' in obj.allowed and obj.journal: + where = self.render_journal(obj.journal) + when = None + if 'year' in obj.allowed and obj.year: + when = unicode(obj.year.value) + return author, title, where, when + + def get_field(self, obj, field): + # return or set attr + if field == 'author': + author = [a.name.value if hasattr(a, 'name') else a.value for a in obj.author] + author = [crosstex.style.name_sort_last_first(a) for a in author] + author = tuple(author) + return author + elif field == 'monthno' and not hasattr(obj, 'monthno'): + attr = getattr(obj, 'month', None) + if hasattr(attr, 'monthno'): + attr = attr.monthno + else: + attr = None + else: + attr = getattr(obj, field, None) + # do something with attr + if attr is not None: + return attr.name.value if hasattr(attr, 'name') else attr.value + return None + + def render(self, citations): + label_dict, longest = self.get_label_dict(citations) + bib = self._fmt.header(longest) + in_list = False + for idx, citation in enumerate(citations): + if isinstance(citation, crosstex.style.Heading): + if in_list: + bib += self._fmt.list_end() + bib += self._fmt.heading(citation.name, in_list) + bib += self._fmt.list_begin() + in_list = True + continue + cite, obj = citation + cb = self._callback(obj.kind) + if cb is None: + raise crosstex.style.UnsupportedCitation(obj.kind) + item = cb(obj) + label = label_dict[cite] + if not in_list: + bib += self._fmt.list_begin() + in_list = True + bib += self._fmt.item(cite, label, item) + if in_list: + in_list = False + bib += self._fmt.list_end() + bib += self._fmt.footer() + return label_dict, bib + + def get_label_dict(self, citations): + citations = [c for c in citations + if not isinstance(c, crosstex.style.Heading)] + labels = crosstex.style.label_generate_lastnames(citations) + longest = '00' #XXX what is this? + label_dict = dict(zip([c for c, o in citations], labels)) + return label_dict, longest + + def _callback(self, kind): + print 'RENDER', kind + if not hasattr(self, 'render_' + kind): + return None + else: + return getattr(self, 'render_' + kind) + + # Stuff for rendering + + def render_str(self, string, which): + if isinstance(string, crosstex.parse.Value): + string = unicode(string.value) + elif 'short-' + which in self._flags: + string = unicode(string.shortname.value) + elif 'short-' + which not in self._flags: + string = unicode(string.longname.value) + return string + + def render_author(self, author, context=None, history=None): + author = [a.name.value if hasattr(a, 'name') else a.value for a in author] + if 'short-author' in self._flags: + author = crosstex.style.names_shortfirst_last(author) + else: + author = crosstex.style.names_first_last(author) + author = [a.strip('{}') for a in author] + author = crosstex.style.list_comma_and(author) + return author + + def render_title(self, title, context=None, history=None): + title = title.value + if 'titlecase-default' in self._flags: + return title + elif 'titlecase-upper' in self._flags: + return crosstex.style.title_uppercase(title) + elif 'titlecase-title' in self._flags: + return crosstex.style.title_titlecase(title, self._db.titlephrases()) + elif 'titlecase-lower' in self._flags: + return crosstex.style.title_lowercase(title, self._db.titlesmalls()) + return title + + def render_booktitle(self, booktitle, context=None, history=None): + if isinstance(booktitle, crosstex.objects.workshop): + return self.render_str(booktitle, 'workshop') + elif isinstance(booktitle, crosstex.objects.conference): + return self.render_str(booktitle, 'conference') + elif isinstance(booktitle, crosstex.parse.Value): + return self.render_str(booktitle, 'booktitle') + + def render_journal(self, journal, context=None, history=None): + return self.render_str(journal, 'journal') + + def render_pages(self, pages, context=None, history=None): + pages = unicode(pages) + if '-' in pages: + return 'pages %s' % pages + else: + return 'page %s' % pages + + def render_address(self, address, context=None, history=None): + city, state, country = None, None, None + if isinstance(address, crosstex.objects.location): + if address.city: + city = self.render_str(address.city, 'city') + if address.state: + state = self.render_str(address.state, 'state') + if address.country: + country = self.render_str(address.country, 'country') + elif isinstance(address, crosstex.objects.country): + country = self.render_str(address, 'country') + elif isinstance(address, crosstex.objects.state): + state = self.render_str(address, 'state') + if address.country: + country = self.render_str(address.country, 'country') + elif isinstance(address, crosstex.parse.Value): + return self.render_str(address, 'address') + return ', '.join([x for x in (city, state, country) if x is not None]) + + def render_year(self, year, context=None, history=None): + if isinstance(year, crosstex.parse.Value): + return self.render_str(year, 'year') + + def render_month(self, month, context=None, history=None): + return self.render_str(month, 'month') + + def render_article(self, article, context=None, history=None): + author = self.render_author(article.author) + title = self.render_title(article.title) + journal = self.render_journal(article.journal) + year = self.render_year(article.year) + volume = unicode(article.volume.value) if article.volume else None + number = unicode(article.number.value) if article.number else None + pages = unicode(article.pages.value) if article.pages and 'no-pages' not in self._flags else None + first = '' + second = '' + third = '' + if author: + first = self._fmt.block(crosstex.style.punctuate(author, '.', '')) + if year: + first += ' ' + crosstex.style.punctuate(year, '.', '') + if title: + second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) + if journal: + if 'add-in' in self._flags: + third += 'In ' + third += self._fmt.emph(journal) + volnumpages = '' + if number or volume or pages: + if volume: + volnumpages += unicode(volume) + if number: + volnumpages += '(%s)' % number + if pages: + if volume or number: + volnumpages += ':%s' % pages + else: + volnumpages += self.render_pages(pages) + if volnumpages: + third = crosstex.style.punctuate(third, ',', ' ') + third += volnumpages + third = crosstex.style.punctuate(third, '.', '') + third = self._fmt.block(third) + return self._fmt.block_sep().join([b for b in (first, second, third) if b]) + + def render_book(self, book, context=None, history=None): + author = self.render_author(book.author) + # XXX need to handle editors + title = self.render_title(book.title) + publisher = self.render_str(book.publisher, 'publisher') if book.publisher else None + address = self.render_address(book.address) if book.address and 'no-address' not in self._flags else None + year = self.render_year(book.year) if book.year else None + first = '' + second = '' + third = '' + if author: + first = self._fmt.block(crosstex.style.punctuate(author, '.', '')) + if year: + first += ' ' + crosstex.style.punctuate(year, '.', '') + if title: + second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) + if publisher: + third = publisher + if address: + third = crosstex.style.punctuate(third, ',', ' ') + address + third = crosstex.style.punctuate(third, '.', '') + third = self._fmt.block(third) + return self._fmt.block_sep().join([b for b in (first, second, third) if b]) + + def render_inproceedings(self, inproceedings, context=None, history=None): + author = self.render_author(inproceedings.author) + title = self.render_title(inproceedings.title) + booktitle = self.render_booktitle(inproceedings.booktitle) + pages = self.render_pages(inproceedings.pages.value) if inproceedings.pages and 'no-pages' not in self._flags else None + address = self.render_address(inproceedings.address) if inproceedings.address and 'no-address' not in self._flags else None + year = self.render_year(inproceedings.year) if inproceedings.year else None + month = self.render_month(inproceedings.month) if inproceedings.month else None + first = '' + second = '' + third = '' + if author: + first = self._fmt.block(crosstex.style.punctuate(author, '.', '')) + if year: + first += ' ' + crosstex.style.punctuate(year, '.', '') + if title: + second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) + if booktitle: + if 'add-in' in self._flags: + third += 'In ' + if 'add-proceedings' in self._flags: + third += 'Proceedings of the ' + elif 'add-proc' in self._flags: + third += 'Proc. of ' + third += crosstex.style.punctuate(self._fmt.emph(booktitle), ',', ' ') + if pages: + third = crosstex.style.punctuate(third, ',', ' ') + pages + if address: + third = crosstex.style.punctuate(third, ',', ' ') + address + #if month and year: + # third = crosstex.style.punctuate(third, ',', ' ') + # third += month + ' ' + year + #elif year: + # third = crosstex.style.punctuate(third, ',', ' ') + # third += year + third = crosstex.style.punctuate(third, '.', '') + third = self._fmt.block(third) + return self._fmt.block_sep().join([b for b in (first, second, third) if b]) + + def render_misc(self, misc, context=None, history=None): + author = self.render_author(misc.author) if misc.author else None + title = self.render_title(misc.title) if misc.title else None + howpub = unicode(misc.howpublished.value) if misc.howpublished else None + booktitle = self.render_booktitle(misc.booktitle) if misc.booktitle else None + address = self.render_address(misc.address) if misc.address and 'no-address' not in self._flags else None + year = self.render_year(misc.year) if misc.year else None + first = '' + second = '' + third = '' + if author: + first = self._fmt.block(crosstex.style.punctuate(author, '.', '')) + if year: + first += ' ' + crosstex.style.punctuate(year, '.', '') + if title: + second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) + if howpub: + third += howpub + if booktitle: + third = crosstex.style.punctuate(third, ',', ' ') + self._fmt.emph(booktitle) + if address: + third = crosstex.style.punctuate(third, ',', ' ') + address + third = crosstex.style.punctuate(third, '.', '') + third = self._fmt.block(third) + return self._fmt.block_sep().join([b for b in (first, second, third) if b]) + + def render_techreport(self, techreport, context=None, history=None): + author = self.render_author(techreport.author) + title = self.render_title(techreport.title) + number = unicode(techreport.number.value) if techreport.number else None + insti = unicode(techreport.institution.value) if techreport.institution else None + address = self.render_address(techreport.address) if techreport.address and 'no-address' not in self._flags else None + year = self.render_year(techreport.year) + month = self.render_month(techreport.month) if techreport.month else None + first = '' + second = '' + third = '' + if author: + first = self._fmt.block(crosstex.style.punctuate(author, '.', '')) + if year: + first += ' ' + crosstex.style.punctuate(year, '.', '') + if title: + second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) + if insti: + third = insti + if address: + third = crosstex.style.punctuate(third, ',', ' ') + address + if number: + third = crosstex.style.punctuate(third, ',', ' ') + third += 'Technical Report ' + number + else: + third = crosstex.style.punctuate(third, ',', ' ') + third += 'Technical Report' + third = crosstex.style.punctuate(third, '.', '') + third = self._fmt.block(third) + return self._fmt.block_sep().join([b for b in (first, second, third) if b]) + + def render_phdthesis(self, phdthesis, context=None, history=None): + author = self.render_author(phdthesis.author) + title = self.render_title(phdthesis.title) + school = unicode(phdthesis.school.value) if phdthesis.school else None + year = self.render_year(phdthesis.year) + first = '' + second = '' + third = 'PhD thesis' + if author: + first = self._fmt.block(crosstex.style.punctuate(author, '.', '')) + if year: + first += ' ' + crosstex.style.punctuate(year, '.', '') + if title: + second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) + if school: + third = crosstex.style.punctuate(third, ',', ' ') + school + third = crosstex.style.punctuate(third, '.', '') + third = self._fmt.block(third) + return self._fmt.block_sep().join([b for b in (first, second, third) if b]) + + def render_url(self, url, context=None, history=None): + author = self.render_author(url.author) if url.author else None + title = self.render_title(url.title) if url.title else None + link = unicode(url.url.value) + month = self.render_month(url.accessmonth) if url.accessmonth else None + day = self.render_str(url.accessday, 'day') if url.accessday else None + year = self.render_year(url.accessyear) if url.accessyear else None + first = '' + second = '' + third = '' + if author: + first = self._fmt.block(crosstex.style.punctuate(author, '.', '')) + if year: + first += ' ' + crosstex.style.punctuate(year, '.', '') + if title: + second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) + if url: + third = '\url{%s}' % link + if month and day and year: + third = self._fmt.block(crosstex.style.punctuate(third, '.', '')) + third += ' Accessed ' + month + ' ' + day + ', ' + year + elif month and year: + third = self._fmt.block(crosstex.style.punctuate(third, '.', '')) + third += ' Accessed ' + month + ' ' + year + elif year: + third = self._fmt.block(crosstex.style.punctuate(third, '.', '')) + third += ' Accessed ' + year + third = self._fmt.block(crosstex.style.punctuate(third, '.', '')) + third = self._fmt.block(third) + return self._fmt.block_sep().join([b for b in (first, second, third) if b]) diff --git a/crosstex/style/__init__.py b/crosstex/style/__init__.py index 4d9fb3f..2bb2ef6 100644 --- a/crosstex/style/__init__.py +++ b/crosstex/style/__init__.py @@ -452,9 +452,34 @@ def label_fullnames(authors): value += ' et al.' return value +def label_lastnames_list(authors): + broken_names = [break_name(a) for a in authors] + last_names = [' '.join(mname + lname) for (fname, mname, lname, sname) in broken_names] + return last_names + +def label_lastnames_all(authors): + value = '' + last_names = label_lastnames_list(authors) + if len(last_names) == 1: + value = last_names[0] + elif len(last_names) == 2: + value = '%s and %s' % (last_names[0], last_names[1]) + elif len(last_names) > 0: + value = '%s, and %s' % (', '.join(last_names[:-1]), last_names[-1]) + return value + +def label_lastnames_first(authors): + last_names = label_lastnames_list(authors) + if len(last_names) > 0: + return last_names[0] + else: + return '' + def label_generate_initials(citations): by_label = collections.defaultdict(list) for cite, obj in citations: + if not obj.author: + continue author = [a.name.value if hasattr(a, 'name') else a.value for a in obj.author] year = getattr(obj, 'year', None) label = crosstex.style.label_initials(author) @@ -479,3 +504,38 @@ def label_generate_initials(citations): def label_generate_fullnames(citations): assert False # XXX + +def label_generate_lastnames(citations): + by_label = collections.defaultdict(list) + for cite, obj in citations: + if not obj.author: + print 'author', cite + assert obj.author + author = [a.name.value if hasattr(a, 'name') else a.value for a in obj.author] + year = getattr(obj, 'year', None) + if not year: + year = getattr(obj, 'accessyear', None) + if not year: + print 'year', cite + assert year + + label = '\protect\citeauthoryear{%s}{%s et~al\mbox{.}}' % (label_lastnames_all(author), label_lastnames_first(author)) + if year: + if isinstance(year, crosstex.parse.Value): + label += '{%i}' % year.value + else: + label += '{%i}' % year + by_label[label].append(cite) + by_cite = {} + for label, citelist in by_label.iteritems(): + suffixes = itertools.repeat('') + if len(citelist) > 1: + alpha = ['', ''] + list('abcdefghijklmnopqrstuvwxyz') + suffixes = itertools.imap(lambda y: ''.join(y), itertools.combinations(alpha, 3)) + try: + for suffix, cite in itertools.izip(suffixes, citelist): + by_cite[cite] = label + suffix + except StopIteration: + raise crosstex.CrossTeXError('Way too many citations with the same author initials in the same year') + return [by_cite[c] for c, o in citations] + From fde16a2fe60cb1235c0fb1da1d9b8ade4df6828c Mon Sep 17 00:00:00 2001 From: Ayush Dubey Date: Wed, 7 Dec 2016 15:45:02 -0500 Subject: [PATCH 3/8] remove debug printf --- crosstex/style/ACM-Reference-Format-Journals.py | 1 - 1 file changed, 1 deletion(-) diff --git a/crosstex/style/ACM-Reference-Format-Journals.py b/crosstex/style/ACM-Reference-Format-Journals.py index ea70eed..0c33834 100644 --- a/crosstex/style/ACM-Reference-Format-Journals.py +++ b/crosstex/style/ACM-Reference-Format-Journals.py @@ -201,7 +201,6 @@ def get_label_dict(self, citations): return label_dict, longest def _callback(self, kind): - print 'RENDER', kind if not hasattr(self, 'render_' + kind): return None else: From 9554f9135e08fbabe25e0c0a477bcae667ed2ca3 Mon Sep 17 00:00:00 2001 From: Ayush Dubey Date: Wed, 7 Dec 2016 16:03:33 -0500 Subject: [PATCH 4/8] fix render_address --- crosstex/style/ACM-Reference-Format-Journals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crosstex/style/ACM-Reference-Format-Journals.py b/crosstex/style/ACM-Reference-Format-Journals.py index 0c33834..1fa78b9 100644 --- a/crosstex/style/ACM-Reference-Format-Journals.py +++ b/crosstex/style/ACM-Reference-Format-Journals.py @@ -274,7 +274,7 @@ def render_address(self, address, context=None, history=None): country = self.render_str(address.country, 'country') elif isinstance(address, crosstex.parse.Value): return self.render_str(address, 'address') - return ', '.join([x for x in (city, state, country) if x is not None]) + return ', '.join([x for x in (city, state, country) if x is not None and x != '']) def render_year(self, year, context=None, history=None): if isinstance(year, crosstex.parse.Value): From 4467e6d67d5ee68dc24f5517b5a919a8cee7b980 Mon Sep 17 00:00:00 2001 From: Ayush Dubey Date: Wed, 7 Dec 2016 16:11:36 -0500 Subject: [PATCH 5/8] ignore build --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0d20b64..0fe2c40 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +build/ From 18b72b11a7ca3891e10a9d201074abce936a4fb6 Mon Sep 17 00:00:00 2001 From: Ayush Dubey Date: Fri, 9 Dec 2016 12:46:53 -0500 Subject: [PATCH 6/8] et al only if more than 1 author --- crosstex/style/__init__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crosstex/style/__init__.py b/crosstex/style/__init__.py index 2bb2ef6..c67c2d6 100644 --- a/crosstex/style/__init__.py +++ b/crosstex/style/__init__.py @@ -470,10 +470,12 @@ def label_lastnames_all(authors): def label_lastnames_first(authors): last_names = label_lastnames_list(authors) - if len(last_names) > 0: + if len(last_names) == 0: + return '' + elif len(last_names) == 1: return last_names[0] else: - return '' + return '%s et~al\mbox{.}' % last_names[0] def label_generate_initials(citations): by_label = collections.defaultdict(list) @@ -519,7 +521,7 @@ def label_generate_lastnames(citations): print 'year', cite assert year - label = '\protect\citeauthoryear{%s}{%s et~al\mbox{.}}' % (label_lastnames_all(author), label_lastnames_first(author)) + label = '\protect\citeauthoryear{%s}{%s}' % (label_lastnames_all(author), label_lastnames_first(author)) if year: if isinstance(year, crosstex.parse.Value): label += '{%i}' % year.value From 6d5027484c5744c2aeaf7f36b81886e61fb904fd Mon Sep 17 00:00:00 2001 From: Ayush Dubey Date: Sun, 8 Oct 2017 20:57:30 -0400 Subject: [PATCH 7/8] pass --- crosstex/parse.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crosstex/parse.py b/crosstex/parse.py index 4924a47..cdcd237 100755 --- a/crosstex/parse.py +++ b/crosstex/parse.py @@ -186,7 +186,15 @@ def _parse_ext_xtx(self, path): logger.debug('Processing database %s.' % path) db = XTXFileInfo() stream = open(path) - contents = stream.read().decode('utf8') + coded = stream.read() + try: + contents = coded.decode('utf8') + except UnicodeDecodeError: + try: + contents = coded.decode('utf16') + except UnicodeDecodeError: + print coded + contents = coded.decode() if contents: lexer = ply.lex.lex(reflags=re.UNICODE) lexer.path = path From ef949396c65242af17ea9fbc33fd24209d5e683f Mon Sep 17 00:00:00 2001 From: Ayush Dubey Date: Sun, 19 Nov 2017 11:33:31 -0500 Subject: [PATCH 8/8] properly format urls in plain --- .../style/ACM-Reference-Format-Journals.py | 7 +++- crosstex/style/plain.py | 34 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/crosstex/style/ACM-Reference-Format-Journals.py b/crosstex/style/ACM-Reference-Format-Journals.py index 1fa78b9..750c829 100644 --- a/crosstex/style/ACM-Reference-Format-Journals.py +++ b/crosstex/style/ACM-Reference-Format-Journals.py @@ -1,4 +1,5 @@ import math +import re import crosstex.latex import crosstex.style @@ -125,6 +126,7 @@ def __init__(self, fmt, flags, options, db): self._db = db self._flags = flags or set([]) self._options = options or {} + self._url_pattern = re.compile(r"^\\url{.*}$") def sort_key(self, citation): cite, obj = citation @@ -479,7 +481,10 @@ def render_url(self, url, context=None, history=None): if title: second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) if url: - third = '\url{%s}' % link + if self._url_pattern.match(link): + third = link + else: + third = "\url{%s}" % link if month and day and year: third = self._fmt.block(crosstex.style.punctuate(third, '.', '')) third += ' Accessed ' + month + ' ' + day + ', ' + year diff --git a/crosstex/style/plain.py b/crosstex/style/plain.py index 7814720..dba188b 100644 --- a/crosstex/style/plain.py +++ b/crosstex/style/plain.py @@ -1,4 +1,5 @@ import math +import re import crosstex.latex import crosstex.style @@ -125,6 +126,7 @@ def __init__(self, fmt, flags, options, db): self._db = db self._flags = flags or set([]) self._options = options or {} + self._url_pattern = re.compile(r"^\\url{.*}$") def sort_key(self, citation): cite, obj = citation @@ -491,10 +493,40 @@ def render_url(self, url, context=None, history=None): if title: second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) if url: - third = link + if self._url_pattern.match(link): + third = link + else: + third = "\url{%s}" % link if month and day and year: third = self._fmt.block(crosstex.style.punctuate(third, '.', '')) third += 'Accessed ' + month + ' ' + day + ', ' + year third = self._fmt.block(crosstex.style.punctuate(third, '.', '')) third = self._fmt.block(third) return self._fmt.block_sep().join([b for b in (first, second, third) if b]) + + def render_patent(self, patent, context=None, history=None): + author = self.render_author(patent.author) if patent.author else None + title = self.render_title(patent.title) if patent.title else None + number = unicode(patent.number.value) if patent.number else None + month = self.render_month(patent.month) if patent.month else None + year = self.render_year(patent.year) if patent.year else None + first = '' + second = '' + third = '' + if author: + first = self._fmt.block(crosstex.style.punctuate(author, '.', '')) + if title: + second = self._fmt.block(crosstex.style.punctuate(title, '.', '')) + if number: + third += crosstex.style.punctuate("US Patent %s" % number, ",", " ") + if month and year: + third = crosstex.style.punctuate(third, ',', ' ') + third += month + ' ' + year + elif year: + third = crosstex.style.punctuate(third, ',', ' ') + third += year + third = crosstex.style.punctuate(third, '.', '') + third = self._fmt.block(third) + return self._fmt.block_sep().join([b for b in (first, second, third) if b]) + +