Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract page with user display name #17

Merged
merged 1 commit into from
May 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions cmd/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ import (
)

type pageSimple struct {
ID string `json:"id"`
Title string `json:"title"`
Lines []string `json:"lines"`
ID string `json:"id"`
Title string `json:"title"`
Authors []author `json:"authors"`
Lines []string `json:"lines"`
}

type author struct {
ID string `json:"id"`
Name string `json:"name"`
}

// extractCmd represents the extract command
Expand Down Expand Up @@ -55,6 +61,12 @@ func doExtract(cmd *cobra.Command) {

bar := pb.StartNew(proj.Count)

var authors types.Authors
if file.Exists(projectName+"_authors.json", config.WorkDir) {
err := authors.ReadFrom(projectName, config.WorkDir)
CheckErr(err)
}

outputPath := config.WorkDir + "/" + outputDir
file.CreateDir(outputPath)
for _, idx := range proj.Pages {
Expand All @@ -66,6 +78,13 @@ func doExtract(cmd *cobra.Command) {
var simplePage pageSimple
simplePage.ID = page.ID
simplePage.Title = page.Title
simplePage.Authors = []author{}
displayName := getDisplayName(authors, page.Author.ID, page.Author.Name)
simplePage.Authors = append(simplePage.Authors, author{ID: page.Author.ID, Name: displayName})
for _, collaborator := range page.Collaborators {
displayName := getDisplayName(authors, collaborator.ID, collaborator.Name)
simplePage.Authors = append(simplePage.Authors, author{ID: collaborator.ID, Name: displayName})
}
simplePage.Lines = toLines(&page)
data, _ := json.Marshal(simplePage)
err = file.WriteBytes(data, simplePage.ID+".json", outputPath)
Expand Down Expand Up @@ -112,3 +131,14 @@ func isExtractable(lines []string, includes []string, excludes []string) bool {
func isEmpty(arr []string) bool {
return len(arr) == 0 || (len(arr) == 1 && arr[0] == "")
}

func getDisplayName(authors types.Authors, id string, name string) string {
username := name
for _, a := range authors.Authors {
if id == a.ID {
username = a.Name
break
}
}
return username
}
Loading