| 
 | 1 | +-- Checks if any contributor information is available  | 
 | 2 | + | 
 | 3 | +local roles = {  | 
 | 4 | +    ["conceptualization"] = {  | 
 | 5 | +        name = "Conceptualization",  | 
 | 6 | +        id = "8b73531f-db56-4914-9502-4cc4d4d8ed73",  | 
 | 7 | +        uri = "https://credit.niso.org/contributor-roles/conceptualization/"  | 
 | 8 | +    },  | 
 | 9 | +    ["data-curation"] = {  | 
 | 10 | +        name = "Data curation",  | 
 | 11 | +        id = "f93e0f44-f2a4-4ea1-824a-4e0853b05c9d",  | 
 | 12 | +        uri = "https://credit.niso.org/contributor-roles/data-curation/"  | 
 | 13 | +    },  | 
 | 14 | +    ["formal-analysis"] = {  | 
 | 15 | +        name = "Formal analysis",  | 
 | 16 | +        id = "95394cbd-4dc8-4735-b589-7e5f9e622b3f",  | 
 | 17 | +        uri = "https://credit.niso.org/contributor-roles/formal-analysis/"  | 
 | 18 | +    },  | 
 | 19 | +    ["funding-acquisition"] = {  | 
 | 20 | +        name = "Funding acquisition",  | 
 | 21 | +        id = "34ff6d68-132f-4438-a1f4-fba61ccf364a",  | 
 | 22 | +        uri = "https://credit.niso.org/contributor-roles/funding-acquisition/"  | 
 | 23 | +    },  | 
 | 24 | +    ["investigation"] = {  | 
 | 25 | +        name = "Investigation",  | 
 | 26 | +        id = "2451924d-425e-4778-9f4c-36c848ca70c2",  | 
 | 27 | +        uri = "https://credit.niso.org/contributor-roles/investigation/"  | 
 | 28 | +    },  | 
 | 29 | +    ["methodology"] = {  | 
 | 30 | +        name = "Methodology",  | 
 | 31 | +        id = "f21e2be9-4e38-4ab7-8691-d6f72d5d5843",  | 
 | 32 | +        uri = "https://credit.niso.org/contributor-roles/methodology/"  | 
 | 33 | +    },  | 
 | 34 | +    ["project-administration"] = {  | 
 | 35 | +        name = "Project administration",  | 
 | 36 | +        id = "a693fe76-ea33-49ad-9dcc-5e4f3ac5f938",  | 
 | 37 | +        uri = "https://credit.niso.org/contributor-roles/project-administration/"  | 
 | 38 | +    },  | 
 | 39 | +    ["resources"] = {  | 
 | 40 | +        name = "Resources",  | 
 | 41 | +        id = "ebd781f0-bf79-492c-ac21-b31b9c3c990c",  | 
 | 42 | +        uri = "https://credit.niso.org/contributor-roles/resources/"  | 
 | 43 | +    },  | 
 | 44 | +    ["software"] = {  | 
 | 45 | +        name = "Software",  | 
 | 46 | +        id = "f89c5233-01b0-4778-93e9-cc7d107aa2c8",  | 
 | 47 | +        uri = "https://credit.niso.org/contributor-roles/software/"  | 
 | 48 | +    },  | 
 | 49 | +    ["supervision"] = {  | 
 | 50 | +        name = "Supervision",  | 
 | 51 | +        id = "0c8ca7d4-06ad-4527-9cea-a8801fcb8746",  | 
 | 52 | +        uri = "https://credit.niso.org/contributor-roles/supervision/"  | 
 | 53 | +    },  | 
 | 54 | +    ["validation"] = {  | 
 | 55 | +        name = "Validation",  | 
 | 56 | +        id = "4b1bf348-faf2-4fc4-bd66-4cd3a84b9d44",  | 
 | 57 | +        uri = "https://credit.niso.org/contributor-roles/validation/"  | 
 | 58 | +    },  | 
 | 59 | +    ["visualization"] = {  | 
 | 60 | +        name = "Visualization",  | 
 | 61 | +        id = "76b9d56a-e430-4e0a-84c9-59c11be343ae",  | 
 | 62 | +        uri = "https://credit.niso.org/contributor-roles/visualization/"  | 
 | 63 | +    },  | 
 | 64 | +    ["writing-original-draft"] = {  | 
 | 65 | +        name = "Writing – original draft",  | 
 | 66 | +        id = "43ebbd94-98b4-42f1-866b-c930cef228ca",  | 
 | 67 | +        uri = "https://credit.niso.org/contributor-roles/writing-original-draft/"  | 
 | 68 | +    },  | 
 | 69 | +    ["writing-review-editing"] = {  | 
 | 70 | +        name = "Writing – review & editing",  | 
 | 71 | +        id = "d3aead86-f2a2-47f7-bb99-79de6421164d",  | 
 | 72 | +        uri = "https://credit.niso.org/contributor-roles/writing-review-editing/"  | 
 | 73 | +    }  | 
 | 74 | +}  | 
 | 75 | + | 
 | 76 | +degrees = pandoc.List {  | 
 | 77 | +    "Lead",  | 
 | 78 | +    "Supporting",  | 
 | 79 | +    "Equal"  | 
 | 80 | +}  | 
 | 81 | + | 
 | 82 | +function invalidRole(str)  | 
 | 83 | +    return roles[str] == nil  | 
 | 84 | +end  | 
 | 85 | + | 
 | 86 | +function invalidDegree(str)  | 
 | 87 | +    return not degrees:includes(str)  | 
 | 88 | +end  | 
 | 89 | + | 
 | 90 | +function join_with_commas_and(list)  | 
 | 91 | +    local len = #list  | 
 | 92 | +    if len == 0 then  | 
 | 93 | +        return ""  | 
 | 94 | +    elseif len == 1 then  | 
 | 95 | +        return list[1]  | 
 | 96 | +    elseif len == 2 then  | 
 | 97 | +        return list[1] .. " and " .. list[2]  | 
 | 98 | +    else  | 
 | 99 | +        local result = table.concat(list, ", ", 1, len - 1)  | 
 | 100 | +        return result .. ", and " .. list[len]  | 
 | 101 | +    end  | 
 | 102 | +end  | 
 | 103 | + | 
 | 104 | +function capitalize_first_letter(str)  | 
 | 105 | +    return str:sub(1, 1):upper() .. str:sub(2)  | 
 | 106 | +end  | 
 | 107 | + | 
 | 108 | +function clean_role_dict(d)  | 
 | 109 | +    if d.type then  | 
 | 110 | +        return d  | 
 | 111 | +    else  | 
 | 112 | +        return { ["type"] = pandoc.utils.stringify(d) }  | 
 | 113 | +    end  | 
 | 114 | +end  | 
 | 115 | + | 
 | 116 | +local function prepare_credit (meta)  | 
 | 117 | +    meta.hasRoles = false  | 
 | 118 | +    for _, author in ipairs(meta.authors or {}) do  | 
 | 119 | +        if author.roles then  | 
 | 120 | +            roleList = {}  | 
 | 121 | +            for _, roleDict in ipairs(author.roles) do  | 
 | 122 | +                roleDict = clean_role_dict(roleDict)  | 
 | 123 | +                role = pandoc.utils.stringify(roleDict.type)  | 
 | 124 | +                if invalidRole(role) then  | 
 | 125 | +                    print("invalid role for author " .. author.name .. ": " .. role)  | 
 | 126 | +                elseif roleDict.degree then  | 
 | 127 | +                    degree = capitalize_first_letter(pandoc.utils.stringify(roleDict.degree))  | 
 | 128 | +                    if invalidDegree(degree) then  | 
 | 129 | +                        print("invalid degree for author " .. author.name .. ": " .. degree)  | 
 | 130 | +                        -- even though the degree is invalid, add the role anyway  | 
 | 131 | +                        table.insert(roleList, roles[role].name)  | 
 | 132 | +                    else  | 
 | 133 | +                        table.insert(roleList, roles[role].name .. " (" .. degree .. ")")  | 
 | 134 | +                    end  | 
 | 135 | +                else  | 
 | 136 | +                    table.insert(roleList, roles[role].name)  | 
 | 137 | +                end  | 
 | 138 | +            end  | 
 | 139 | +            if #roleList > 0 then  | 
 | 140 | +                meta.hasRoles = true  | 
 | 141 | +                author.rolesString = join_with_commas_and(roleList)  | 
 | 142 | +            end  | 
 | 143 | +        end  | 
 | 144 | +    end  | 
 | 145 | +    return meta  | 
 | 146 | +end  | 
 | 147 | + | 
 | 148 | +function Meta (meta)  | 
 | 149 | +    local ok, result = pcall(prepare_credit, meta)  | 
 | 150 | +    if ok then  | 
 | 151 | +        return result  | 
 | 152 | +    end  | 
 | 153 | +end  | 
 | 154 | + | 
 | 155 | +function assertEqual(expected, actual)  | 
 | 156 | +    assert(expected == actual, "got \"" .. actual .. "\", expected \"" .. expected .. "\"")  | 
 | 157 | +end  | 
 | 158 | + | 
 | 159 | +function tests()  | 
 | 160 | +    assert("" == join_with_commas_and({ }))  | 
 | 161 | +    assert("foo" == join_with_commas_and({ "foo" }))  | 
 | 162 | +    assert("foo and bar" == join_with_commas_and({ "foo", "bar" }))  | 
 | 163 | +    assert("foo, bar, and baz" == join_with_commas_and({ "foo", "bar", "baz" }))  | 
 | 164 | + | 
 | 165 | +    local m1 = {  | 
 | 166 | +        ["authors"] = {  | 
 | 167 | +            {  | 
 | 168 | +                ["name"] = "Author 1",  | 
 | 169 | +                ["roles"] = {  | 
 | 170 | +                    "methodology"  | 
 | 171 | +                }  | 
 | 172 | +            },  | 
 | 173 | +            {  | 
 | 174 | +                ["name"] = "Author 2",  | 
 | 175 | +                ['roles'] = {  | 
 | 176 | +                    { ["type"] = "methodology" }  | 
 | 177 | +                }  | 
 | 178 | +            },  | 
 | 179 | +            {  | 
 | 180 | +                ["name"] = "Author 3",  | 
 | 181 | +                ['roles'] = {  | 
 | 182 | +                    { ["type"] = "methodology" },  | 
 | 183 | +                    { ["type"] = "data-curation" },  | 
 | 184 | +                    { ["type"] = "conceptualization" },  | 
 | 185 | +                }  | 
 | 186 | +            },  | 
 | 187 | +            {  | 
 | 188 | +                ["name"] = "Author 4",  | 
 | 189 | +                ['roles'] = {  | 
 | 190 | +                    { ["type"] = "methodology", ["degree"] = "lead" },  | 
 | 191 | +                    { ["type"] = "data-curation", ["degree"] = "supporting" },  | 
 | 192 | +                    { ["type"] = "conceptualization" },  | 
 | 193 | +                }  | 
 | 194 | +            },  | 
 | 195 | +        }  | 
 | 196 | +    }  | 
 | 197 | +    local m1t = prepare_credit(m1)  | 
 | 198 | +    assert(m1t.hasRoles, "hasRoles should be set to true")  | 
 | 199 | +    assertEqual("Methodology", m1t['authors'][1].rolesString)  | 
 | 200 | +    assertEqual("Methodology", m1t['authors'][2].rolesString)  | 
 | 201 | +    assertEqual("Methodology, Data curation, and Conceptualization", m1t['authors'][3].rolesString)  | 
 | 202 | +    assertEqual("Methodology (Lead), Data curation (Supporting), and Conceptualization", m1t['authors'][4].rolesString)  | 
 | 203 | + | 
 | 204 | +    local m2 = {  | 
 | 205 | +        ["authors"] = {  | 
 | 206 | +            {  | 
 | 207 | +                ["name"] = "Author 1"  | 
 | 208 | +            },  | 
 | 209 | +            {  | 
 | 210 | +                ["name"] = "Author 2"  | 
 | 211 | +            }  | 
 | 212 | +        }  | 
 | 213 | +    }  | 
 | 214 | +    local m2t = prepare_credit(m2)  | 
 | 215 | +    assert(not m2t.hasRoles, "hasRoles should be set to false")  | 
 | 216 | +end  | 
 | 217 | + | 
 | 218 | +tests()  | 
0 commit comments