diff --git a/go.mod b/go.mod index fd1e805..f9b9f73 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module anemon go 1.22.0 + +require github.com/google/go-cmp v0.6.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5a8d551 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/internal/parser/latex.go b/internal/parser/latex.go index 483cbd3..666dcbc 100644 --- a/internal/parser/latex.go +++ b/internal/parser/latex.go @@ -1,13 +1,14 @@ package parser -import "os" +import ( + "errors" + "fmt" + "os" + "strings" +) -//Apply a section to a section type on a latex template -//func (section section, type section_type, template string) +type SectionName string -//to write this template -//func write(template string, name string) -//Todo //Read the template file in the assets directory func read_template()(string,error) { @@ -24,3 +25,28 @@ func writeTemplate(template string, name string)error{ []byte(template), 0644) return err } + +//Apply a section to a section type on a latex template +func applyToSection(section Section, section_type string)(string,error){ + replacements := []string{section.first, section.second, section.third, section.fourth} + template := "" + if section.params_nb == 0 { + return "",errors.New("No nb of params given") + } + switch{ + case section_type == "Professional": + template = pro_template + for i := 0; i < section.params_nb; i++ { + position := fmt.Sprintf("%d", i+1) + template = strings.Replace(template, + position, replacements[i], 1) + } + items := "" + for _,item := range section.description{ + items += strings.Replace(pro_item,"%ITEM%",item,1) + } + template = strings.Replace(template, + "%ITEMS%", items, 1) + } + return template,nil +} diff --git a/internal/parser/latex_test.go b/internal/parser/latex_test.go index d98b549..f6f4132 100644 --- a/internal/parser/latex_test.go +++ b/internal/parser/latex_test.go @@ -1,9 +1,11 @@ package parser import ( - "os" - "path/filepath" - "testing" + "os" + "path/filepath" + "testing" + + "github.com/google/go-cmp/cmp" ) func TestReadLatex(t *testing.T) { @@ -42,6 +44,7 @@ func TestReadLatex(t *testing.T) { } } } + func TestWriteLatex(t *testing.T) { err := writeTemplate("Hello, world", "hello") if err != nil { @@ -54,3 +57,31 @@ func TestWriteLatex(t *testing.T) { } } +func TestApplySection(t *testing.T) { + pro_section := Section{ + first: "first", + second: "second", + third: "third", + fourth: "fourth", + description: []string{"item1","item2"}, + params_nb: 4, + } + want := ` +\resumeSubheading + {first}{second} + {\href{third}{fourth}}{ } +\resumeItemListStart + \resumeItem{item1} +\resumeItem{item2} + +\resumeItemListEnd +` + got,err := applyToSection(pro_section, + "Professional") + if err!=nil{ + t.Fatalf("error when apply template: %v", err) + } + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("TestApplySection mismatch (-expected +got):\n%s", diff) + } +} diff --git a/internal/parser/markdown.go b/internal/parser/markdown.go index d1848d6..b72714b 100644 --- a/internal/parser/markdown.go +++ b/internal/parser/markdown.go @@ -15,7 +15,7 @@ type Section struct { second string third string fourth string - description string + description []string params_nb int } @@ -40,8 +40,9 @@ func Parse(paragraph string) (Section,error){ section.third=line[nb_hashtag+1:] case nb_hashtag == 4: section.fourth=line[nb_hashtag+1:] - case nb_hashtag == 0: - section.description += line + case nb_hashtag == 0 && len(line)>1: + items := strings.Split(line, "\n") + section.description = append(section.description, items...) case nb_hashtag > 4: return section, errors.New("Err: cannot parse this md line{"+line+"}") } diff --git a/internal/parser/markdown_test.go b/internal/parser/markdown_test.go index ad5b3fd..3596c64 100644 --- a/internal/parser/markdown_test.go +++ b/internal/parser/markdown_test.go @@ -1,7 +1,9 @@ package parser import ( - "testing" + "fmt" + "reflect" + "testing" ) func TestParseHappyPath(t *testing.T) { @@ -10,7 +12,8 @@ func TestParseHappyPath(t *testing.T) { ## Skill ### Date #### Url -Description` +Item +Item2` result, err := Parse(input) if err != nil { t.Fatalf("Unexecpted eroor :%s", err.Error()) @@ -38,8 +41,9 @@ Description` } - description := "Description" - if result.description != description { + description := []string{"Item","Item2"} + if !reflect.DeepEqual(result.description, description){ + fmt.Printf("want: %q, got: %q\n", description, result.description) t.Fatalf("want %s got %s", description, result.description) } } diff --git a/internal/parser/template_sections.go b/internal/parser/template_sections.go new file mode 100644 index 0000000..ae6aae8 --- /dev/null +++ b/internal/parser/template_sections.go @@ -0,0 +1,11 @@ +package parser + +const pro_item = "\\resumeItem{%ITEM%}\n" +const pro_template = ` +\resumeSubheading + {1}{2} + {\href{3}{4}}{ } +\resumeItemListStart + %ITEMS% +\resumeItemListEnd +`