Skip to content

Commit

Permalink
Almost there...
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Jul 17, 2024
1 parent 7ee4588 commit 427f557
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
2 changes: 2 additions & 0 deletions cmd/sheetsStructs.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func sheetsFields() []CsvColumn {
"LinkedAgent": "field_linked_agent",
"Identifier": "field_identifier",
"Url": "url",
"References": "references",
"FieldAbstract": "field_abstract",
}
for column, field := range f {
fields = append(fields, CsvColumn{
Expand Down
28 changes: 28 additions & 0 deletions cmd/transformCsvCrossref.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ var transformCsvCrossrefCmd = &cobra.Command{
Title: *checkRow.FullTitle,
Year: year,
}
if *checkRow.RightsStatement != "" && !strings.Contains(*checkRow.RightsStatement, ".getty") {
article.LicenseRef = *checkRow.RightsStatement
}
if *checkRow.FieldAbstract != "" {
var abstract model.TypedText
err = json.Unmarshal([]byte(*checkRow.FieldAbstract), &abstract)
if err != nil {
slog.Error("Unable to unmarshal abstract", "err", err)
os.Exit(1)
}
article.Abstract, err = crossref.StrToJATS(abstract.Value)
if err != nil {
slog.Error("Unable to convert abstract to JATS", "err", err)
os.Exit(1)
}
}
for _, agent := range strings.Split(*checkRow.LinkedAgent, "|") {
components := strings.Split(agent, ":")
if len(components) < 3 {
Expand Down Expand Up @@ -142,6 +158,18 @@ var transformCsvCrossrefCmd = &cobra.Command{
break
}
}

for _, doi := range strings.Split(*checkRow.References, "|") {
if doi == "" {
continue
}
reference := crossref.Reference{
crossref.DoiData{
Doi: doi,
},
}
article.References = append(article.References, reference)
}
volume.Articles = append(volume.Articles, article)
}
}
Expand Down
18 changes: 8 additions & 10 deletions crossref/issueless-journal.xml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@
<doi>{{ .DoiData.Doi }}</doi>
<resource>{{ .DoiData.Url }}</resource>
</doi_data>
<component_list>
{{- range .References }}
<component parent_relation="reference">
<doi_data>
<doi>{{ .DoiData.Doi }}</doi>
<resource>{{ .DoiData.Url }}</resource>
</doi_data>
</component>
{{- end }}
</component_list>
{{- if .Abstract }}
{{ .Abstract }}
{{- end }}
{{- if .LicenseRef }}
<ai:program name="AccessIndicators">
<ai:license_ref >{{ .LicenseRef }}</ai:license_ref>
</ai:program>
{{- end }}
</journal_article>
{{- end }}
</journal>
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/oapi-codegen/oapi-codegen/v2 v2.3.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.26.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -25,7 +26,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4d
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
57 changes: 57 additions & 0 deletions model/crossref/jats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package crossref

import (
"bytes"
"encoding/xml"
"log/slog"

"golang.org/x/net/html"
)

type Body struct {
Sections []Section `xml:"sec"`
}

type Section struct {
XMLName xml.Name `xml:"jats:abstract"`
Para []string `xml:"jats:p"`
}

func parseHTML(htmlContent string) (*html.Node, error) {
doc, err := html.Parse(bytes.NewReader([]byte(htmlContent)))
if err != nil {
return nil, err
}
return doc, nil
}

func StrToJATS(htmlContent string) (string, error) {
if len(htmlContent) > 4 && htmlContent[0:4] == "</p>" {
htmlContent = "<p>" + htmlContent[4:]
}
node, err := parseHTML(htmlContent)
if err != nil {
return "", err
}

slog.Info("NODE", "n", node, "html", htmlContent)
var section Section

var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "p" {
section.Para = append(section.Para, n.FirstChild.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(node)

output, err := xml.MarshalIndent(section, "", " ")
if err != nil {
return "", err
}

return string(output), nil
}
1 change: 1 addition & 0 deletions model/crossref/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Article struct {
DoiData DoiData `xml:"doi_data"`
Year int
References []Reference
LicenseRef string
}

type Reference struct {
Expand Down
2 changes: 2 additions & 0 deletions workbench/workbench.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 427f557

Please sign in to comment.