-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbib-fns.jq
99 lines (84 loc) · 3.73 KB
/
bib-fns.jq
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
# Module of JQ functions for manipulating JSON returned from Zotero API
def nonBlankKey($keyName):
has($keyName) and (.[$keyName] | tostring | length >= 1);
def blankKey($keyName):
(has($keyName) and (.[$keyName] | tostring | length >= 1)) | not;
def unwrapDiv:
sub("^<div.+?>(?<body>.*)</div>$"; "\(.body)"; "gm");
def moveURL_to_url:
select(nonBlankKey("URL")) | (setpath(["url"]; .URL) | del(.URL)) // .;
def raise_issued_date_parts:
if nonBlankKey("issued") and (.issued | nonBlankKey("date-parts")) then setpath(["issuedDateParts"]; .issued."date-parts"[0]) else . end;
def make_DOI_to_url($doi):
if ($doi | startswith("https:")) then $doi else "https://doi.org/" + ($doi | ltrimstr("/")) end ;
def cleanAbstracts:
if blankKey("abstract") and nonBlankKey("abstractNote") then
setpath(["abstract"]; .abstractNote) | del(.abstractNote)
elif blankKey("abstractNote") or (nonBlankKey("abstract") and (.abstract == .abstractNote)) then
del(.abstractNote)
else
.
end;
def getTargetInfo:
{target: (.parentItem // .key)} + .;
def reconstituteGroupedEntries: # assumes that array of all entries in a group (effectively by year) is the input
if length == 1 then .[0] else ( { key: (.[0].key), value: (map(.value ) | flatten(1) ) } ) end | .value |= sort_by(.title);
def updateParentUrl:
. as $parent
| (.children | map(select(nonBlankKey("url")) | .url)) as $childurls
# For now, use first child url to set for blank parent url
| if ($childurls | length >= 1) then
#only 1
if blankKey("url") then # parent has no url
. * {url: $childurls[0]}
else
. # For now, don't do anything here
end
else # For now, don't do anything here, either
# none or many
.
end;
def applyChildrenAmendments:
if (.children | length < 1) then
.
else
updateParentUrl
# . as $parent |
# if (blankKey("url")) then
# .children
# | map(select()) as $url | .url |= $url
# else
# .children
# | map(select()) as $url | .url |= $url
# .
# end;
end;
def getNeededUrls: # assumes that array of all current items is the input
map(.key) as $keys # extract all existing keys and keep 'em handy
| map( # collect all of the "up" link urls and their keys
.links.up.href # get the "up" link url
| select(. != null) # only if there IS an up url
| [(split("/")|last), .]) # make an array with the key, and the url; an entry in the collected array
| map( # for each key/url pair
.[0] as $k # get the key
| select(($keys | all(. != $k))) # if this key isn't already in the $keys
| .[1]?) # include this url in this collected array
;
# def intersection(x;y):
# if (y|length) == 0 then
# []
# else (x|unique) as $x
# | $x - ($x - y)
# end;
def semiflatten: # assumes that only one item is the input
. as $item
| (keys - ["csljson","data"]) as $topKeys
| ((.csljson // {}) * .data) as $inner
| (($inner | keys) - ["key","version"]) as $innerKeys
| ( ($topKeys | map(. as $tKey | {"key": $tKey, "value": ($item | getpath([$tKey]))}))
+ ($innerKeys | map(. as $iKey | {"key": $iKey, "value": ($inner | getpath([$iKey]))})) )
| from_entries;
def bibItem: # assumes that only one item is the input
. as $item
| (keys - ["key","title","target"]) as $tailKeys
| {"key": .key, "title": .title, "target": .target } + ($tailKeys | map(. as $tKey | {"key": $tKey, "value": ($item | getpath([$tKey]))}) | from_entries);