Skip to content

Commit

Permalink
Add section removla in case of pageOverflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Nov 23, 2024
1 parent 30acaec commit 25dfa90
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 87 deletions.
1 change: 1 addition & 0 deletions internal/adapters/output/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (*LatexCompiler) CompileTemplate(root string)(int,error){
wg.Add(1)
go compile(template,root,&wg, page_nb)
pdf_pages_nb := <-page_nb
slog.Info("Number of pages", "pages", pdf_pages_nb)
max_nb_of_page = max(pdf_pages_nb,max_nb_of_page)
}
close(page_nb)
Expand Down
78 changes: 41 additions & 37 deletions internal/core/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *BuilderService) GetService() CVService {
}


//generate the template for the cvs defined in the assets directory
//generate the templates for the cvs defined in the assets directory
func (s *CVService) GenerateTemplates()error{
slog.Info("--Generating CVs--")
cvs,err := s.source.GetCVsFrom(s.root);if err != nil{ return err }
Expand All @@ -69,21 +69,30 @@ func (s *CVService) GenerateTemplates()error{
return err
}
for _, cv := range cvs {
err = generateCVFrom(cv,params,s.root,generiqueTemplate,s.templateProcessor)
err = generateCVFrom(cv,params,s.root,generiqueTemplate,s.templateProcessor,false)
if err != nil{
return err
}
}
max_nb_page,err := s.compiler.CompileTemplate(s.root); if err != nil{ return err }
println("---")
println(max_nb_page)
println("---")
max_nb_page,err := s.compiler.CompileTemplate(s.root);
if err != nil{ }

Check failure on line 78 in internal/core/generate.go

View workflow job for this annotation

GitHub Actions / Run Linter

SA9003: empty branch (staticcheck)
for max_nb_page > 1{
for _, cv := range cvs {
err = generateCVFrom(cv,params,s.root,generiqueTemplate,s.templateProcessor,true)
if err != nil{
return err
}
}
max_nb_page,err = s.compiler.CompileTemplate(s.root);
if err != nil{ return nil }
}
return nil
}


//generate a template for the cv with the given params
func generateCVFrom(cv CV, params Params, root string,
template string, processor TemplateProcessor)(error){
template string, processor TemplateProcessor, shouldBeShorter bool)(error){
var err error
if len(params.Variante)==0{//if no variante create simple CV
params.Variante = map[string][]string{"simple": nil}
Expand All @@ -92,7 +101,9 @@ func generateCVFrom(cv CV, params Params, root string,
cvName := "CV-"+cv.Lang+"-"+vari+".tex"
slog.Info("Generating for:"+cvName)
cvTemplate := template

if shouldBeShorter {
removeLowestSection(&cv,keywords)
}
for _, section := range cv.Sections {
for _, paragraph := range section.Paragraphes {
headers := []string{ paragraph.H1, paragraph.H2,
Expand All @@ -111,49 +122,42 @@ func generateCVFrom(cv CV, params Params, root string,
}

//Sort items and after remove the last items
func sortAndRemoveLast(items []string, keywords []string) []string {
if len(items) == 0 {
return items
}
sortByScore(items, keywords)
return items[:len(items)-1]
func removeLowestSection(cv *CV, keywords []string){
if len(cv.Sections)== 0{ return }
l_i := getLowestSection(*cv,keywords)
cv.Sections = append(cv.Sections[:l_i], cv.Sections[l_i+1:]...)
}

//Return the index of the section from the CV with the lowest score
// Return the index of the section from the CV with the lowest score
func getLowestSection(cv CV, keywords []string) int {
min_score := getScoreSection(cv.Sections[0],keywords)
min_idx := 0
for idx, section := range cv.Sections[1:] {
current_score := getScoreSection(section,keywords)
if current_score <= min_score{
min_idx = idx
min_score = current_score
}
}
return min_idx
return getLowestIndex(cv.Sections, keywords, getScoreSection)
}

//Return the index of the section from the CV with the lowest score
// Return the index of the paragraph from the section with the lowest score
func getLowestParagraphe(section Section, keywords []string) int {
min_score := getScoreParagraphe(section.Paragraphes[0],keywords)
min_idx := 0
for idx, para := range section.Paragraphes[1:] {
current_score := getScoreParagraphe(para,keywords)
if current_score <= min_score{
min_idx = idx
min_score = current_score
}
return getLowestIndex(section.Paragraphes, keywords, getScoreParagraphe)
}

//generic function to get the index of the element with the lowest score
//TODO ? an interface to avoid the any ?
func getLowestIndex[T any](items []T, keywords []string, getScore func(T, []string) int) int {
min_score := getScore(items[0], keywords)
min_idx := 0
for idx, item := range items[1:] {
current_score := getScore(item, keywords)
if current_score < min_score {
min_idx = idx + 1
min_score = current_score
}
}
return min_idx
}

//Get items of a section and sum they score to get global score of the items
func getScoreSection (section Section, keywords []string)int{
res := 0
for _, paragraph := range section.Paragraphes {
for _,item := range paragraph.Items{
res += getScore(item,keywords)
}
res+=getScoreParagraphe(paragraph,keywords)
}
return res
}
Expand Down
53 changes: 3 additions & 50 deletions internal/core/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,53 +229,6 @@ func TestSortByScore(t *testing.T) {
}


func TestSortAndRemoveLast(t *testing.T) {
keywords := []string{"foo", "bar"}
tests := []struct {
items []string
expected []string
}{
{
items: []string{" with foo", " with foo and bar", " with neither", " with bar"},
expected: []string{" with foo and bar", " with foo", " with bar"},
},
{
items: []string{" with foo bar foo bar", " with foo", " with bar bar bar", " with foo bar"},
expected: []string{" with foo bar foo bar", " with bar bar bar", " with foo bar"},
},
{
items: []string{" without keywords", " with bar", "Another without keywords"},
expected: []string{" with bar", " without keywords"},
},
{
items: []string{"single item"},
expected: []string{},
},
{
items: []string{},
expected: []string{},
},
}

for _, tt := range tests {
items := make([]string, len(tt.items))
copy(items, tt.items)
result := sortAndRemoveLast(items, keywords)

if len(result) != len(tt.expected) {
t.Errorf("expected length %v but got %v", len(tt.expected), len(result))
continue
}

for i := range result {
if result[i] != tt.expected[i] {
t.Errorf("expected %v but got %v", tt.expected, result)
break
}
}
}
}

func TestGetLowestSection(t *testing.T) {
keywords := []string{"experience", "skills", "projects"}
tests := []struct {
Expand Down Expand Up @@ -306,7 +259,7 @@ func TestGetLowestSection(t *testing.T) {
},
},
},
expected: 0,
expected: 1,
},
{
cv: CV{
Expand Down Expand Up @@ -379,7 +332,7 @@ func TestGetLowestParagraphe(t *testing.T) {
{H1: "Experience 3", Items: []string{"project"}},
},
},
expected: 1,
expected: 2,
},
{
section: Section{
Expand All @@ -390,7 +343,7 @@ func TestGetLowestParagraphe(t *testing.T) {
{H1: "Project C", Items: []string{"project", "experience"}},
},
},
expected: 1,
expected: 0,
},
{
section: Section{
Expand Down

0 comments on commit 25dfa90

Please sign in to comment.