Skip to content

Commit

Permalink
WIP add yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Nov 10, 2024
1 parent 78f7aaf commit 5f42a1c
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 55 deletions.
11 changes: 5 additions & 6 deletions assets/latex/template/template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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-----------
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
3 changes: 2 additions & 1 deletion internal/adapters/input/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ 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{}
service := &core.CVService{}
return service.GenerateTemplates(root,source,templateReader,templateProccesor)
return service.GenerateTemplates(root,source, paramsSource,templateReader,templateProccesor)
}
56 changes: 56 additions & 0 deletions internal/adapters/input/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
22 changes: 22 additions & 0 deletions internal/adapters/input/yaml.go
Original file line number Diff line number Diff line change
@@ -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, &params)
if err != nil {
return params, err
}
return params, nil
}
35 changes: 35 additions & 0 deletions internal/adapters/output/latex_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package output

import (
"anemon/internal/core"
"strings"
"testing"
)
Expand Down Expand Up @@ -255,3 +256,37 @@ 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)
}
}

32 changes: 26 additions & 6 deletions internal/adapters/output/latex_writer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package output

import (
"fmt"
"anemon/internal/core"
"errors"
"strings"
"regexp"
"fmt"
"os"
"reflect"
"regexp"
"strings"
)

type LatexReader struct{}
Expand All @@ -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
Expand Down Expand Up @@ -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++ {
Expand Down
46 changes: 27 additions & 19 deletions internal/core/generate.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
package core

import (
"log/slog"
"sort"
"strings"
"log/slog"
"sort"
"strings"
)

type CVService struct{}

//TODO apply information in header of CV
//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)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 }
}
}
return nil
Expand Down
Loading

0 comments on commit 5f42a1c

Please sign in to comment.