Skip to content

Commit

Permalink
Add params.yml
Browse files Browse the repository at this point in the history
* Add sorting
* add yaml adapter
* add compiler
* modify CI to use compiler
  • Loading branch information
Theo-Hafsaoui committed Nov 11, 2024
1 parent 8f60acc commit 6a7e989
Show file tree
Hide file tree
Showing 16 changed files with 487 additions and 60 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/pdf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
FROM golang:1.23 as build
FROM golang:1.23

WORKDIR /app

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"]
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
run:
echo "Not yet complete"

clean:
rm ./assets/latex/output/*

build:
go build

Expand All @@ -12,4 +15,4 @@ tidy:
go mod tidy

test:
go test ./...
go test -v ./...
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=
4 changes: 3 additions & 1 deletion internal/adapters/input/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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
}
55 changes: 55 additions & 0 deletions internal/adapters/output/compiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package output

import (
"fmt"
"log/slog"
"os"
"os/exec"
"strings"
"sync"
)


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
}
var wg sync.WaitGroup
for _,template := range templates{
wg.Add(1)
go compile(template,root,&wg)
}
wg.Wait()
return nil
}

//Compile the template into a pdf
func compile(template string, root string, wg* sync.WaitGroup){
defer wg.Done()
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 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
}
71 changes: 71 additions & 0 deletions internal/adapters/output/latex_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package output

import (
"anemon/internal/core"
"path/filepath"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -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)
}
}
}
Loading

0 comments on commit 6a7e989

Please sign in to comment.