diff --git a/.github/workflows/pdf.yml b/.github/workflows/pdf.yml index 3df69c4..cf97433 100644 --- a/.github/workflows/pdf.yml +++ b/.github/workflows/pdf.yml @@ -31,7 +31,10 @@ jobs: run: docker build . -t anemon - name: Run Docker container - run: docker run --rm -v ${{ github.workspace }}/assets/latex/output:/output anemon + run: docker run --rm -v ${{ github.workspace }}/assets/latex/output:/app/assets/latex/output anemon + + - name: LSINFO + run: ls -al && tree - name: Upload compiled PDF uses: actions/upload-artifact@v4 diff --git a/Dockerfile b/Dockerfile index c9bd18b..bb62c8c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.23 as build +FROM golang:1.23 WORKDIR /app @@ -6,20 +6,18 @@ COPY go.mod go.sum ./ RUN go mod download && go mod verify COPY . . -RUN make build -RUN ./anemon generate -FROM debian:latest -COPY --from=build /app/assets/latex/output/ /internal_output ENV DEBIAN_FRONTEND=noninteractive - -RUN apt-get update && apt-get install -y \ +RUN apt-get update && \ + apt-get install -y \ texlive \ texlive-latex-extra \ texlive-fonts-extra \ texlive-xetex \ texlive-font-utils \ - fonts-font-awesome \ - && apt-get clean && rm -rf /var/lib/apt/lists/* + fonts-font-awesome && \ + apt-get clean && rm -rf /var/lib/apt/lists/* + +RUN make build -CMD mkdir -p /tmp_output && cd /internal_output && for i in *.tex; do pdflatex $i -output-directory=/tmp_output || true; done && ls && pwd && ls /tmp_output && cp /internal_output/*.pdf /output/ +CMD ["./anemon", "generate"] diff --git a/Makefile b/Makefile index 3dd0a92..c7728fa 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,9 @@ run: echo "Not yet complete" +clean: + rm ./assets/latex/output/* + build: go build @@ -12,4 +15,4 @@ tidy: go mod tidy test: - go test ./... + go test -v ./... diff --git a/assets/latex/template/template.tex b/assets/latex/template/template.tex index 5eab7f9..45cff28 100644 --- a/assets/latex/template/template.tex +++ b/assets/latex/template/template.tex @@ -80,9 +80,7 @@ } \newcommand{\resumeSubItem}[1]{\resumeItem{#1}\vspace{-4pt}} - \renewcommand\labelitemii{$\vcenter{\hbox{\tiny$\bullet$}}$} - \newcommand{\resumeSubHeadingListStart}{\begin{itemize}[leftmargin=0.15in, label={}]} \newcommand{\resumeSubHeadingListEnd}{\end{itemize}} \newcommand{\resumeItemListStart}{\begin{itemize}} @@ -94,11 +92,12 @@ \begin{document} + %VARS% \begin{center} - \textbf{\Huge \scshape \textcolor{nblue}{Anemon Vincent}} \\ \vspace{1pt} - \small +33 6 26 26 50 07 $|$ \href{mailto:anemon@pm.me}{\underline{anemon@pm.me}} \\ - \href{https://linkedin.com/in/anemon/}{\underline{linkedin.com/in/anemon}} $|$ - \href{https://github.com/anemon}{\underline{github.com/anemon}} + \textbf{\Huge \scshape \textcolor{nblue}{\Name \ \Firstname}} \\ \vspace{1pt} + \small \Number \ $|$ \href{mailto:\Mail}{\underline{\Mail}} \\ + \href{https:\Linkedin}{\underline{linkedin.com/in/\Name}} $|$ + \href{https:\Github}{\underline{github.com/\Name}} \end{center} %-----------EDUCATION----------- diff --git a/go.mod b/go.mod index 7510094..0c4bfec 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module anemon go 1.22.0 -require github.com/spf13/cobra v1.8.1 +require ( + github.com/spf13/cobra v1.8.1 + gopkg.in/yaml.v3 v3.0.1 +) require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect diff --git a/go.sum b/go.sum index 912390a..a01295b 100644 --- a/go.sum +++ b/go.sum @@ -6,5 +6,7 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/adapters/input/cli.go b/internal/adapters/input/cli.go index ccdd3b9..1c024d0 100644 --- a/internal/adapters/input/cli.go +++ b/internal/adapters/input/cli.go @@ -8,8 +8,10 @@ import ( //Use the implementation for markdown and latex to generate latex CV from a tree dir of mardown document func GenerateCVFromMarkDownToLatex(root string)error{ var source core.Source = &MarkdownSource{} + var paramsSource core.SourceParams = &YamlSource{} var templateReader core.TemplateReader = &output.LatexReader{} var templateProccesor core.TemplateProcessor = &output.LatexProccesor{} + var compiler core.Compiler = &output.LatexCompiler{} service := &core.CVService{} - return service.GenerateTemplates(root,source,templateReader,templateProccesor) + return service.GenerateTemplates(root,source, paramsSource,templateReader,templateProccesor,compiler) } diff --git a/internal/adapters/input/input_test.go b/internal/adapters/input/input_test.go index 8645f13..18fd73b 100644 --- a/internal/adapters/input/input_test.go +++ b/internal/adapters/input/input_test.go @@ -9,6 +9,21 @@ import ( ) var( + yamlContent = ` +info: + name: Doe + firstname: John + number: "12345" + mail: john.doe@example.com + github: johndoe + linkedin: john-doe-linkedin +variante: + optionA: + - "value1" + - "value2" + optionB: + - "value3" +` work_input = ` # Back-End Intern ## February 2024 -- August 2024 @@ -152,3 +167,44 @@ func TestSections(t *testing.T) { }) } + + +func TestGetParamsFrom(t *testing.T) { + tempDir, err := os.MkdirTemp("", "test") + if err != nil { t.Fatalf("Failed to create temp directory: %v", err) } + defer os.RemoveAll(tempDir) + + yamlFilePath := filepath.Join(tempDir, "params.yml") + err = os.WriteFile(yamlFilePath, []byte(yamlContent), 0644) + if err != nil { t.Fatalf("Failed to write YAML file: %v", err) } + + source := &YamlSource{} + params, err := source.GetParamsFrom(tempDir) + if err != nil { + t.Fatalf("GetParamsFrom returned an error: %v", err) + } + expectedParams := core.Params{ + Info: struct { + Name string `yaml:"name"` + FirstName string `yaml:"firstname"` + Number string `yaml:"number"` + Mail string `yaml:"mail"` + GitHub string `yaml:"github"` + LinkedIn string `yaml:"linkedin"` + }{ + Name: "Doe", + FirstName: "John", + Number: "12345", + Mail: "john.doe@example.com", + GitHub: "johndoe", + LinkedIn: "john-doe-linkedin", + }, + Variante: map[string][]string{ + "optionA": {"value1", "value2"}, + "optionB": {"value3"}, + }, + } + if !reflect.DeepEqual(params, expectedParams) { + t.Errorf("Expected %+v, but got %+v", expectedParams, params) + } +} diff --git a/internal/adapters/input/yaml.go b/internal/adapters/input/yaml.go new file mode 100644 index 0000000..a9e480f --- /dev/null +++ b/internal/adapters/input/yaml.go @@ -0,0 +1,22 @@ +package input + +import ( + core "anemon/internal/core" + "os" + "gopkg.in/yaml.v3" +) + +type YamlSource struct{} + +func (*YamlSource) GetParamsFrom(root string) (core.Params, error) { + params := core.Params{} + yamlFile, err := os.ReadFile(root + "/params.yml") + if err != nil { + return params, err + } + err = yaml.Unmarshal(yamlFile, ¶ms) + if err != nil { + return params, err + } + return params, nil +} diff --git a/internal/adapters/output/compiler.go b/internal/adapters/output/compiler.go new file mode 100644 index 0000000..738e479 --- /dev/null +++ b/internal/adapters/output/compiler.go @@ -0,0 +1,45 @@ +package output + +import ( + "fmt" + "log/slog" + "os" + "os/exec" + "strings" +) + + +const COMPILER = "pdflatex" +type LatexCompiler struct{} + +//Compile the template into PDF +func (*LatexCompiler) CompileTemplate(root string)error{ + templates,err := getListOfTemplate(root);if err != nil { + return err + } + for _,template := range templates{ + cmd := exec.Command(COMPILER,"-interaction=nonstopmode", + "-output-directory="+root+"/assets/latex/output", template ) + log, err := cmd.Output(); if err != nil { + slog.Info("---FROM pdflatex ---\n"+string(log)) + slog.Error("failed to compile file:"+template) + } + } + return nil +} + +//Return the path of latex file inside the template directory +func getListOfTemplate(root string)([]string, error){ + var res []string + templates, err := os.ReadDir(root + "/assets/latex/output"); if err != nil { + slog.Error("failed to read directory because: "+ err.Error()) + return res,err + } + for _, template := range templates { + if strings.HasSuffix(template.Name(),".tex"){ + res = append(res, root+"/assets/latex/output/"+template.Name()) + } + } + fmt.Println(res) + return res,nil +} diff --git a/internal/adapters/output/latex_test.go b/internal/adapters/output/latex_test.go index f501033..e6575c2 100644 --- a/internal/adapters/output/latex_test.go +++ b/internal/adapters/output/latex_test.go @@ -1,6 +1,9 @@ package output import ( + "anemon/internal/core" + "path/filepath" + "os" "strings" "testing" ) @@ -255,3 +258,71 @@ func TestSanitize(t *testing.T) { t.Run("happy path - should sanitize special ch } }) } + +func TestApplyInfoToTemplate(t *testing.T) { + template := "%VARS%" + params := core.Params{ + Info: struct { + Name string `yaml:"name"` + FirstName string `yaml:"firstname"` + Number string `yaml:"number"` + Mail string `yaml:"mail"` + GitHub string `yaml:"github"` + LinkedIn string `yaml:"linkedin"` + }{ + Name: "John Doe", + FirstName: "John", + Number: "12345", + Mail: "john.doe@example.com", + GitHub: "https://github.com/johndoe", + LinkedIn: "https://linkedin.com/in/johndoe", + }, + Variante: map[string][]string{}, + } + want := `\def\Name{John Doe} +\def\FirstName{John} +\def\Number{12345} +\def\Mail{john.doe@example.com} +\def\GitHub{https://github.com/johndoe} +\def\LinkedIn{https://linkedin.com/in/johndoe}` + got := ApplyInfoToTemplate(template, params) + + if removeWhitespace(got) != removeWhitespace(want) { + t.Errorf("expected:\n%s\ngot:\n%s", want, got) + } +} + + +func TestGetListOfTemplate(t *testing.T) { + root := "testdata" + templateDir := filepath.Join(root, "assets", "latex", "output") + err := os.MkdirAll(templateDir, os.ModePerm) + if err != nil { + t.Fatalf("setup failed: %v", err) + } + defer os.RemoveAll(root) + + files := []string{"foo.tex", "bar.tex","garbage"} + for _, file := range files { + f, err := os.Create(filepath.Join(templateDir, file)) + if err != nil { + t.Fatalf("failed to create test file: %v", err) + } + f.Close() + } + + got, err := getListOfTemplate(root) + if err != nil { + t.Fatalf("getListOfTemplate returned an error: %v", err) + } + expected := files + if len(got) != len(expected)-1{//Should ommit garbage + t.Errorf("expected %d files, got %d", len(expected), len(got)) + } + for _, filePath := range got { + _, err := os.Stat(filePath) + if err != nil { + t.Errorf("expected path %s not found: %v", filePath, err) + } + } +} diff --git a/internal/adapters/output/latex_writer.go b/internal/adapters/output/latex_writer.go index 682519e..5192412 100644 --- a/internal/adapters/output/latex_writer.go +++ b/internal/adapters/output/latex_writer.go @@ -1,11 +1,13 @@ package output import ( - "fmt" + "anemon/internal/core" "errors" - "strings" - "regexp" + "fmt" "os" + "reflect" + "regexp" + "strings" ) type LatexReader struct{} @@ -19,13 +21,25 @@ func (*LatexProccesor)MakeNewTemplate(path string, template string, name string) return err } -//Read the template file in the assets directory from the root dir -func (*LatexReader)ReadCVTemplate(root string)(string, error){ +//Read the template file in the assets directory from the root dir and apply the params given to it +func (*LatexReader)ReadCVTemplate(root string,params core.Params)(string, error){ file, err := os.ReadFile(root+"/assets/latex/template/template.tex") if err != nil { return "", err } - return string(file), nil + return ApplyInfoToTemplate(string(file),params), nil +} + +//Apply general information(name, mail..) to a template +func ApplyInfoToTemplate(template string, params core.Params) (string){ + var varsBuilder strings.Builder + infoValue := reflect.ValueOf(params.Info) + for i := 0; i < infoValue.NumField(); i++ { + field := infoValue.Type().Field(i) + fieldValue := infoValue.Field(i) + varsBuilder.WriteString("\\def\\"+field.Name+"{"+fieldValue.String()+"}\n") + } + return replaceVars(template,varsBuilder.String()) } //Apply a section to a section type on a latex template @@ -60,6 +74,12 @@ func replaceWithSectionTemplate(template string,SectionTemplate TemplateStruct,h return updated_template } +//Replace the %vars$ with the vars +func replaceVars(template string,vars string)string{ + updated_template := strings.Replace(template,"%VARS%",vars,1) + return updated_template +} + //Search and replace the headers in the template by their replacement func replace_headers(sec_template string, replacements []string)string{ for i := 0; i < len(replacements); i++ { diff --git a/internal/core/generate.go b/internal/core/generate.go index dc00164..e1baef3 100644 --- a/internal/core/generate.go +++ b/internal/core/generate.go @@ -1,34 +1,63 @@ package core import ( - "log/slog" + "log/slog" + "sort" + "strings" ) type CVService struct{} //generate the template for the cvs defined in the assets directory -func (g *CVService) GenerateTemplates(root string, source Source, templateReader TemplateReader, templateProcessor TemplateProcessor)error{ +func (g *CVService) GenerateTemplates(root string, source Source, paramsSource SourceParams, + templateReader TemplateReader, templateProcessor TemplateProcessor, compiler Compiler)error{ slog.Info("--Generating CVs--") - cvs,err := source.GetCVsFrom(root) - if err != nil{ return err } - GenTemplate,err := templateReader.ReadCVTemplate(root) + cvs,err := source.GetCVsFrom(root);if err != nil{ return err } + params,err := paramsSource.GetParamsFrom(root);if err != nil{ return err } + GenTemplate,err := templateReader.ReadCVTemplate(root,params) if err != nil{ return err } for _, cv := range cvs { - slog.Info("Generating for:"+cv.Lang) - cvTemplate := GenTemplate - for _, section := range cv.Sections { - for _, paragraph := range section.Paragraphes { - headers := []string{ paragraph.H1, paragraph.H2, - paragraph.H3, paragraph.H4} - items:=paragraph.Items - cvTemplate,err = templateProcessor.ApplySectionToTemplate( - cvTemplate,headers,items,section.Title) + if len(params.Variante)==0{ + params.Variante = map[string][]string{"simple": nil} + } + for vari, keywords:= range params.Variante { + cvName := "CV-"+cv.Lang+"-"+vari+".tex" + slog.Info("Generating for:"+cvName) + cvTemplate := GenTemplate + for _, section := range cv.Sections { + for _, paragraph := range section.Paragraphes { + headers := []string{ paragraph.H1, paragraph.H2, + paragraph.H3, paragraph.H4} + items:=paragraph.Items + sortByScore(items,keywords) + cvTemplate,err = templateProcessor.ApplySectionToTemplate( + cvTemplate,headers,items,section.Title) + if err != nil{ return err } + } + err = templateProcessor.MakeNewTemplate(root,cvTemplate,cvName) if err != nil{ return err } } - cvName := "CV-"+cv.Lang+".tex" - err = templateProcessor.MakeNewTemplate(root,cvTemplate,cvName) - if err != nil{ return err } } } + err = compiler.CompileTemplate(root); if err != nil{ return err } return nil } + +//Sorte a slice of items by the number of keyword +// +//The sort is done in ascending order as the section append work like a stack(Lifo) +func sortByScore(items []string, keywords []string){ + sort.Slice(items, func(i, j int) bool { + return getScore(items[i],keywords)