Skip to content

Commit

Permalink
Merge pull request #17 from mamezou-tech/feature/extract-with-user-di…
Browse files Browse the repository at this point in the history
…splayname

feat: extract page with user display name
  • Loading branch information
kondoumh authored May 12, 2024
2 parents bc2f799 + 8b02e0c commit 5530d67
Showing 1 changed file with 33 additions and 3 deletions.
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
}

0 comments on commit 5530d67

Please sign in to comment.