Debian Codegraph @

examples/citeproc_json.py

raw
1 #!/usr/bin/env python
2
3 # We'll use json.loads for parsing the JSON data.
4 import json
5
6 # Import the citeproc-py classes we'll use below.
7 from citeproc import CitationStylesStyle, CitationStylesBibliography
8 from citeproc import Citation, CitationItem
9 from citeproc import formatter
10 from citeproc.source.json import CiteProcJSON
11
12
13 # The following JSON data describes 5 references picked from the CSL test suite.
14
15 json_input = """
16 [
17 {
18 "id": "ITEM-1",
19 "issued": {
20 "date-parts": [[1987, 8, 3],
21 [2003, 10, 23]]
22 },
23 "title": "Ignore me",
24 "type": "book"
25 },
26 {
27 "id" : "ITEM-2",
28 "page" : "1-7",
29 "type" : "article-journal",
30 "issued" : {
31 "date-parts": [[2006]]
32 }
33 },
34 {
35 "author": [
36 {
37 "family": "Doe",
38 "given": "John"
39 }
40 ],
41 "id": "ITEM-3",
42 "issued": {
43 "date-parts": [["1965", "6", "1"]]
44 },
45 "title": "His Anonymous Life",
46 "type": "book"
47 },
48 {
49 "author": [
50 {
51 "family": "Grignon",
52 "given": "Cyril"
53 },
54 {
55 "family": "Sentenac",
56 "given": "Corey"
57 }
58 ],
59 "id": "ITEM-4",
60 "issued": {
61 "date-parts": [[2000]]
62 },
63 "type": "book"
64 },
65 {
66 "id": "ITEM-5",
67 "title":"Boundaries of Dissent: Protest and State Power in the Media Age",
68 "author": [
69 {
70 "family": "D'Arcus",
71 "given": "Bruce",
72 "static-ordering": false
73 }
74 ],
75 "publisher": "Routledge",
76 "publisher-place": "New York",
77 "issued": {
78 "date-parts":[[2006]]
79 },
80 "type": "book",
81 "URL": "http://www.test01.com"
82 }
83 ]
84 """
85
86
87 # Parse the JSON input using json.loads()
88 # (parsing from a file object can be done with json.load)
89
90 json_data = json.loads(json_input)
91
92 # Process the JSON data to generate a citeproc-py BibliographySource.
93
94 bib_source = CiteProcJSON(json_data)
95 ##for key, entry in bib_source.items():
96 ## print(key)
97 ## for name, value in entry.items():
98 ## print(' {}: {}'.format(name, value))
99
100 # load a CSL style (from the current directory)
101
102 bib_style = CitationStylesStyle('harvard-cite-them-right', validate=False)
103
104 # Create the citeproc-py bibliography, passing it the:
105 # * CitationStylesStyle,
106 # * BibliographySource (CiteProcJSON in this case), and
107 # * a formatter (plain, html, or you can write a custom formatter)
108
109 bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.html)
110
111
112 # Processing citations in a document needs to be done in two passes as for some
113 # CSL styles, a citation can depend on the order of citations in the
114 # bibliography and thus on citations following the current one.
115 # For this reason, we first need to register all citations with the
116 # CitationStylesBibliography.
117
118 citation1 = Citation([CitationItem('ITEM-3')])
119 citation2 = Citation([CitationItem('ITEM-1'), CitationItem('ITEM-2')])
120 citation3 = Citation([CitationItem('ITEM-4')])
121 citation4 = Citation([CitationItem('ITEM-5')])
122 citation5 = Citation([CitationItem('MISSING')])
123
124 bibliography.register(citation1)
125 bibliography.register(citation2)
126 bibliography.register(citation3)
127 bibliography.register(citation4)
128 bibliography.register(citation5)
129
130
131 # In the second pass, CitationStylesBibliography can generate citations.
132 # CitationStylesBibliography.cite() requires a callback function to be passed
133 # along to be called in case a CitationItem's key is not present in the
134 # bibliography.
135
136 def warn(citation_item):
137 print("WARNING: Reference with key '{}' not found in the bibliography."
138 .format(citation_item.key))
139
140 print('Citations')
141 print('---------')
142
143 print(bibliography.cite(citation1, warn))
144 print(bibliography.cite(citation2, warn))
145 print(bibliography.cite(citation3, warn))
146 print(bibliography.cite(citation4, warn))
147 print(bibliography.cite(citation5, warn))
148
149
150 # And finally, the bibliography can be rendered.
151
152 print('')
153 print('Bibliography')
154 print('------------')
155
156 for item in bibliography.bibliography():
157 print(str(item))