File: _fmt.py

package info (click to toggle)
isbnlib 3.9.3-1.1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 596 kB
  • sloc: python: 4,575; makefile: 4
file content (142 lines) | stat: -rw-r--r-- 3,971 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
# flake8: noqa
# pylint: skip-file
"""Format canonical in bibliographic formats."""

import re
import uuid
from string import Template

from ._helpers import last_first

bibtex = r"""@book{$ISBN,
     title = {$Title},
    author = {$AUTHORS},
      isbn = {$ISBN},
      year = {$Year},
 publisher = {$Publisher}
}"""

endnote = r"""%0 Book
%T $Title
%A $AUTHORS
%@ $ISBN
%D $Year
%I $Publisher """

refworks = r"""TY  - BOOK
T1  - $Title
A1  - $AUTHORS
SN  - $ISBN
Y1  - $Year
PB  - $Publisher
ER  - """

msword = r'''<b:Source xmlns:b="http://schemas.microsoft.com/office/'''\
         r'''word/2004/10/bibliography">
<b:Tag>$uid</b:Tag>
<b:SourceType>Book</b:SourceType>
<b:Author>
<b:NameList>$AUTHORS
</b:NameList>
</b:Author>
<b:Title>$Title</b:Title>
<b:Year>$Year</b:Year>
<b:City></b:City>
<b:Publisher>$Publisher</b:Publisher>
</b:Source>'''

json = r'''{"type": "book",
     "title": "$Title",
    "author": [$AUTHORS],
      "year": "$Year",
"identifier": [{"type": "ISBN", "id": "$ISBN"}],
 "publisher": "$Publisher"}'''

csl = r'''{"type":"book",
        "id":"$ISBN",
     "title":"$Title",
    "author": [$AUTHORS],
    "issued": {"date_parts": [[$Year]]},
      "ISBN":"$ISBN",
 "publisher":"$Publisher"}'''

opf = r"""<?xml version='1.0' encoding='utf-8'?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="uuid_id">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
    <dc:type>Book</dc:type>
    <dc:identifier opf:scheme="uuid" id="uuid_id">$uid</dc:identifier>
    <dc:identifier opf:scheme="ISBN" id="isbn_id">$ISBN</dc:identifier>
    <dc:title>$Title</dc:title>
    $AUTHORS
    <dc:publisher>$Publisher</dc:publisher>
    <dc:date>$Year</dc:date>
    <dc:contributor opf:file-as="isbnlib" opf:role="mdc">isbnlib [http://github.com/xlcnd/isbnlib]</dc:contributor>
  </metadata>
</package>"""

labels = r"""Type:      BOOK
Title:     $Title
Author:    $AUTHORS
ISBN:      $ISBN
Year:      $Year
Publisher: $Publisher"""

templates = {
    'labels': labels,
    'bibtex': bibtex,
    'endnote': endnote,
    'refworks': refworks,
    'msword': msword,
    'json': json,
    'csl': csl,
    'opf': opf
}

_fmts = list(templates.keys())


def _gen_proc(name, canonical):
    if 'ISBN-13' in canonical:
        canonical['ISBN'] = canonical.pop('ISBN-13')
    tpl = templates[name]
    return Template(tpl).safe_substitute(canonical)


def _spec_proc(name, fmtrec, authors):
    """Fix the Authors records."""
    if name not in _fmts:
        return
    if name == 'labels':
        AUTHORS = '\nAuthor:    '.join(authors)
    elif name == 'bibtex':
        AUTHORS = ' and '.join(authors)
    elif name == 'refworks':
        AUTHORS = '\nA1  - '.join(authors)
    elif name == 'endnote':
        AUTHORS = '\n%A '.join(authors)
    elif name == 'msword':
        fmtrec = fmtrec.replace('$uid', str(uuid.uuid4()))
        person = r"<b:Person><b:Last>$last</b:Last>"\
                 r"<b:First>$first</b:First></b:Person>"
        AUTHORS = '\n'.join(
            Template(person).safe_substitute(last_first(a)) for a in authors)
    elif name == 'json':
        AUTHORS = ', '.join('{"name": "$"}'.replace("$", a) for a in authors)
    elif name == 'csl':
        AUTHORS = ', '.join(
            '{"literal": "$"}'.replace("$", a) for a in authors)
    elif name == 'opf':
        fmtrec = fmtrec.replace('$uid', str(uuid.uuid4()))
        creator = r'<dc:creator opf:file-as="$last, $first"'\
                  r' opf:role="aut">$first $last</dc:creator>'
        AUTHORS = '\n    '.join(
            Template(creator).safe_substitute(last_first(author))
            for author in authors)
    return re.sub(r'\$AUTHORS', AUTHORS, fmtrec)


def _fmtbib(fmtname, canonical):
    """Return a canonical record in the selected format."""
    return _spec_proc(fmtname, _gen_proc(fmtname, canonical),
                      canonical['Authors'])