Skip to content

Commit

Permalink
WIP add compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Nov 11, 2024
1 parent 5f42a1c commit 70ad222
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 6 deletions.
27 changes: 27 additions & 0 deletions .log
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
This is pdfTeX, Version 3.141592653-2.6-1.40.25 (TeX Live 2023/Fedora 40) (preloaded format=pdflatex 2024.11.11) 11 NOV 2024 15:42
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
**/home/theo/projects/Anemon/assets/latex/output/.gitignore
(/home/theo/projects/Anemon/assets/latex/output/.gitignore
LaTeX2e <2022-11-01> patch level 1
L3 programming layer <2023-02-22>
! You can't use `macro parameter character #' in vertical mode.
l.1 #
Ignore everything in this directory
?
! Emergency stop.
l.1

End of file on the terminal!


Here is how much of TeX's memory you used:
17 strings out of 476041
526 string characters out of 5793163
1849388 words of memory out of 6000000
20570 multiletter control sequences out of 15000+600000
512287 words of font info for 32 fonts, out of 8000000 for 9000
1137 hyphenation exceptions out of 8191
13i,0n,12p,126b,10s stack positions out of 10000i,1000n,20000p,200000b,200000s
! ==> Fatal error occurred, no output PDF file produced!
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 ./...
3 changes: 2 additions & 1 deletion internal/adapters/input/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
45 changes: 45 additions & 0 deletions internal/adapters/output/compiler.go
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 36 additions & 0 deletions internal/adapters/output/latex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package output

import (
"anemon/internal/core"
"path/filepath"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -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)
}
}
}
6 changes: 4 additions & 2 deletions internal/core/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -40,10 +39,13 @@ func (g *CVService) GenerateTemplates(root string, source Source, paramsSource S
}
}
}
compiler.CompileTemplate(root)

Check failure on line 42 in internal/core/generate.go

View workflow job for this annotation

GitHub Actions / Run Linter

Error return value of `compiler.CompileTemplate` is not checked (errcheck)
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)<getScore(items[j],keywords)
Expand Down
9 changes: 7 additions & 2 deletions internal/core/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func (s *MockParamsSource) GetParamsFrom(root string) (Params, error) {
return s.Params, nil
}

type MockCompiler struct {}
func (c *MockCompiler) CompileTemplate(root string) error { return nil }

type MockSource struct {
CVs []CV
Err error
Expand Down Expand Up @@ -106,8 +109,9 @@ func TestGenerateTemplates(t *testing.T) {
paramsSource := &MockParamsSource{ Params: params, }
templateReader := &MockTemplateReader{ Template: baseTemplate }
templateProcessor := &MockTemplateProcessor{ GeneratedFiles: make(map[string]string) }
compiler := &MockCompiler{}
service := CVService{}
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor)
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor, compiler )

if err != nil { t.Fatalf("expected no error, got %v", err) }
if len(templateProcessor.GeneratedFiles) != 2 { t.Fatalf("expected 2 generated file, got %d", len(templateProcessor.GeneratedFiles)) }
Expand All @@ -126,8 +130,9 @@ func TestGenerateTemplates(t *testing.T) {
paramsSource := &MockParamsSource{ Params: Params{}, }
templateReader := &MockTemplateReader{ Template: baseTemplate }
templateProcessor := &MockTemplateProcessor{ GeneratedFiles: make(map[string]string) }
compiler := &MockCompiler{}
service := CVService{}
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor)
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor, compiler)

if err != nil { t.Fatalf("expected no error, got %v", err) }
if len(templateProcessor.GeneratedFiles) != 1 { t.Fatalf("expected 1 generated file, got %d", len(templateProcessor.GeneratedFiles)) }
Expand Down
4 changes: 4 additions & 0 deletions internal/core/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type ICVservice interface {
generateTemplates(root string, source Source, templateReader TemplateReader, templateProcessor TemplateProcessor)error
}

type Compiler interface {
CompileTemplate(root string) error
}

type SourceParams interface {
GetParamsFrom(root string) (Params, error)
}
Expand Down

0 comments on commit 70ad222

Please sign in to comment.