adsarxiv.py 8.56 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
ADSArxiv PybTeX style
"""
# Copyright Emmanuel Bertin CFHT/CNRS/SorbonneU
# Licensed under GPL v3

from packaging import version as vers
from pybtex import __version__ as pybtex_version
from pybtex.style.formatting.unsrt import Style as UnsrtStyle, date, pages, toplevel
from pybtex.style.template import * # ... and anything else needed
from pybtex.plugin import register_plugin

pybtex_new_version = vers.parse(pybtex_version) >= vers.parse("0.22")

if pybtex_new_version:
    def _format_data(node, data):
        try:
            f = node.format_data
        except AttributeError:
            return node
        else:
            return f(data)


    def _format_list(list_, data):
        return (_format_data(part, data) for part in list_)

    @node
    def href2(children, data):
        parts = _format_list(children, data)
        if "http" in list(parts)[0]:
            parts = _format_list(children, data)
            return richtext.HRef(*parts)
        else:
            parts = _format_list(children, data)
            return richtext.Tag('strong', *parts)

class ADSArxivStyle(UnsrtStyle):

    if pybtex_new_version:
        def format_names(self, role, as_sentence=True):
            formatted_names = names(role, sep=', ', sep2 = ' and ', last_sep=', and ')
            if as_sentence:
                return sentence[ formatted_names ]
            else:
               return formatted_names

        def get_article_template(self, e):
            volume_and_pages = first_of[
            # volume and pages, with optional issue number
                optional[
                    join[
                        field('volume'),
                        optional[ '(', field('number'),')' ],
                        ':', pages
                    ],
                ],
                # pages only
                words[ 'pages', pages ],
            ]
            myurl = first_of[
                    optional_field('adsurl'),
                    optional[ join[ 'http://arxiv.org/abs/', field('eprint') ]],
                    optional_field('url'),
                    optional[ join ['http://dx.doi.org/', field('doi') ]]
            ]
            template = toplevel[
                sentence[ self.format_names('author', as_sentence=False), field('year') ],
                href2[ myurl, self.format_title(e, 'title') ],
                sentence(capfirst=False) [
                    tag('emph')[ field('journal') ],
                    optional[ volume_and_pages ]],
                sentence(capfirst=False) [ optional_field('note') ],
            ]
            return template

        def get_book_template(self, e):
            myurl = first_of[
                optional_field('adsurl'),
                optional[ join ['http://arxiv.org/abs/', field('eprint') ]],
                optional_field('url'),
                optional[ join ['http://dx.doi.org/', field('doi') ]]
            ]
            template = toplevel[
                self.format_author_or_editor(e),
                href2[ myurl, self.format_btitle(e, 'title') ],
                self.format_volume_and_series(e),
                sentence [
                    field('publisher'),
                    optional_field('address'),
                    self.format_edition(e),
                    date
                ],
                optional[ sentence [ self.format_isbn(e) ] ],
                sentence [ optional_field('note') ],
            ]
            return template

        def get_inproceedings_template(self, e):
            myurl = first_of[
                optional_field('adsurl'),
                optional[ join ['http://arxiv.org/abs/', field('eprint') ]],
                optional_field('url'),
                optional[ join ['http://dx.doi.org/', field('doi') ]]
            ]
            template = toplevel[
                sentence[ self.format_names('author', as_sentence=False), field('year') ],
                href2[ myurl, self.format_title(e, 'title') ],
                words[
                    'In',
                    sentence(capfirst=False)[
                        optional[ self.format_editor(e, as_sentence=False) ],
                        self.format_btitle(e, 'booktitle', as_sentence=False),
                        self.format_volume_and_series(e, as_sentence=False),
                        optional[ pages ],
                    ],
                    self.format_address_organization_publisher_date(e),
                ],
                sentence(capfirst=False)[ optional_field('note') ],
            ]
            return template

        def get_misc_template(self, e):
            myurl = first_of[
                    optional_field('adsurl'),
                    optional[ join ['http://arxiv.org/abs/', field('eprint') ]],
                    optional_field('url'),
                    optional[ join ['http://dx.doi.org/', field('doi') ]]
            ]
            template = toplevel[
                optional[ sentence[ self.format_names('author', as_sentence=False), optional [ field('year') ]]],
                optional[ href2[ myurl, self.format_title(e, 'title') ]],
                sentence[ optional[ field('howpublished') ]],
                sentence[ optional_field('note') ],
            ]
            return template
    else:

        def format_article(self, e):
            volume_and_pages = first_of [
            # volume and pages, with optional issue number
                optional [
                    join [
                        field('volume'),
                        optional['(', field('number'),')'],
                        ':', pages
                    ],
                ],
                # pages only
                words ['pages', pages],
            ]
            myurl = first_of [
                    optional_field('adsurl'),
                    optional [join ['http://arxiv.org/abs/'], field('eprint')],
                    optional_field('url'),
                    optional [join ['http://dx.doi.org/', field('doi')]]
            ]
            template = toplevel [
                self.format_names('author'),
                href [myurl, self.format_title(e, 'title')],
                sentence(capfirst=False) [
                    tag('emph') [field('journal')],
                    optional[ volume_and_pages ],
                    field('year')],
                sentence(capfirst=False) [ optional_field('note') ],
            ]
            return template.format_data(e)

        def format_book(self, e):
            myurl = first_of [
                optional_field('adsurl'),
                optional [join ['http://arxiv.org/abs/'], field('eprint')],
                optional_field('url'),
                optional [join ['http://dx.doi.org/', field('doi')]]
            ]
            template = toplevel [
                self.format_author_or_editor(e),
                href [myurl, self.format_btitle(e, 'title')] \
                    if len(myurl.format_data(e)) > 0 \
                    else tag('strong') [self.format_btitle(e, 'title')],
                self.format_volume_and_series(e),
                sentence [
                    field('publisher'),
                    optional_field('address'),
                    self.format_edition(e),
                    date
                ],
                optional[ sentence [ self.format_isbn(e) ] ],
                optional_field('note'),
            ]
            return template.format_data(e)

        def format_inproceedings(self, e):
            myurl = first_of [
                optional_field('adsurl'),
                optional [join ['http://arxiv.org/abs/', field('eprint')]],
                optional_field('url'),
                optional [join ['http://dx.doi.org/', field('doi')]]
            ]
            template = toplevel [
                sentence [self.format_names('author')],
                href [myurl, self.format_title(e, 'title')] \
                    if len(myurl.format_data(e)) > 0 \
                    else tag('strong') [self.format_title(e, 'title')],
                words [
                    'In',
                    sentence(capfirst=False) [
                        optional[ self.format_editor(e, as_sentence=False) ],
                        self.format_btitle(e, 'booktitle', as_sentence=False),
                        self.format_volume_and_series(e, as_sentence=False),
                        optional[ pages ],
                    ],
                    self.format_address_organization_publisher_date(e),
                ],
                sentence(capfirst=False) [ optional_field('note') ],
            ]
            return template.format_data(e)


register_plugin('pybtex.style.formatting', 'adsarxiv', ADSArxivStyle)