diff --git a/.github/workflows/pdf.yml b/.github/workflows/pdf.yml index 3df69c4..a723bf9 100644 --- a/.github/workflows/pdf.yml +++ b/.github/workflows/pdf.yml @@ -38,4 +38,6 @@ jobs: with: name: compiled-pdf path: | + ls -al + tree ${{ github.workspace }}/assets/latex/output/*.pdf 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/internal/adapters/input/cli.go b/internal/adapters/input/cli.go index cad8d80..1c024d0 100644 --- a/internal/adapters/input/cli.go +++ b/internal/adapters/input/cli.go @@ -11,6 +11,7 @@ func GenerateCVFromMarkDownToLatex(root string)error{ 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, paramsSource,templateReader,templateProccesor) + return service.GenerateTemplates(root,source, paramsSource,templateReader,templateProccesor,compiler) } 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 245917d..e6575c2 100644 --- a/internal/adapters/output/latex_test.go +++ b/internal/adapters/output/latex_test.go @@ -2,6 +2,8 @@ package output import ( "anemon/internal/core" + "path/filepath" + "os" "strings" "testing" ) @@ -290,3 +292,37 @@ func TestApplyInfoToTemplate(t *testing.T) { } } + +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/core/generate.go b/internal/core/generate.go index bc95d69..e1baef3 100644 --- a/internal/core/generate.go +++ b/internal/core/generate.go @@ -8,10 +8,9 @@ import ( 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, paramsSource SourceParams, - templateReader TemplateReader, templateProcessor TemplateProcessor)error{ + templateReader TemplateReader, templateProcessor TemplateProcessor, compiler Compiler)error{ slog.Info("--Generating CVs--") cvs,err := source.GetCVsFrom(root);if err != nil{ return err } params,err := paramsSource.GetParamsFrom(root);if err != nil{ return err } @@ -40,10 +39,13 @@ func (g *CVService) GenerateTemplates(root string, source Source, paramsSource S } } } + 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)