Skip to content

Commit

Permalink
Add section removal in case of pageOverflow
Browse files Browse the repository at this point in the history
* Change service to builder pattern
* Add threshold
* Add log to md parser
  • Loading branch information
Theo-Hafsaoui committed Nov 24, 2024
1 parent 4de166a commit a43f198
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN go mod download && go mod verify
COPY . .
RUN make build

CMD ["./anemon", "generate"]
CMD ["./anemon", "-g"]
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

run:
echo "Not yet complete"

Expand Down
24 changes: 0 additions & 24 deletions cmd/generate.go

This file was deleted.

38 changes: 31 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,41 @@ package cmd
import (
"os"
"github.com/spf13/cobra"
"anemon/internal/adapters/input"
)

var rootCmd = &cobra.Command{
Use: "anemon",
Short: "a CV genrator",
Long: `This CLI tool, written in Go, automates the generation of customized CVs from Markdown files based on a specified configuration. It parses CV sections in
multiple languages, prioritizes key skills or features as defined in an output.yml file, and outputs LaTeX files for each CV version, ready for compilation.`,
Use: "anemon",
Short: "A CV generator",
Long: `This CLI tool, automates the generation of customized CVs from Markdown files based on a specified configuration.`,
RunE: func(cmd *cobra.Command, args []string) error {
threshold, err := cmd.Flags().GetInt("threshold")
if err != nil {
return err
}
input.ChangeOverflowThreshold(threshold)

generate, err := cmd.Flags().GetBool("generate")
if err != nil {
return err
}
if generate {
root, err := os.Getwd()
if err != nil {
return err
}
return input.GenerateCVFromMarkDownToLatex(root)
}

return nil
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
rootCmd.Flags().IntP("threshold", "t", 1, "Set the page overflow threshold (default 1)")
rootCmd.Flags().BoolP("generate", "g", false, "Generate a CV")
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

21 changes: 14 additions & 7 deletions internal/adapters/input/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ 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, paramsSource,templateReader,templateProccesor,compiler)
var builder core.BuilderService = core.BuilderService{}
builder.SetRoot(root)
builder.SetSource(&MarkdownSource{})
builder.SetParamsSource(&YamlSource{})
builder.SetTemplateReader(&output.LatexReader{})
builder.SetTemplateProcessor(&output.LatexProccesor{})
builder.SetCompiler(&output.LatexCompiler{})
service := builder.GetService()
return service.GenerateTemplates()
}

//Change the threshold for the regeration of the PDF
func ChangeOverflowThreshold(newThreshold int){
core.SetOverflowThreshold(newThreshold)
}
39 changes: 25 additions & 14 deletions internal/adapters/input/markdown_tree_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
core "anemon/internal/core"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -127,23 +128,33 @@ func parse_paragraphe(paragraph string) (core.Paragraphe, error) {

// handleLine processes a line based on the number of leading hashtags
func handleLine(n_paragraphe *core.Paragraphe, line string, nbHashtags int) error {
switch {
case nbHashtags > 0 && line[nbHashtags] != ' ':
if nbHashtags > 0 && line[nbHashtags] != ' '{
return fmt.Errorf("Err: cannot parse this md line {%s}, '#' should be followed by space", line)
case nbHashtags == 1:
n_paragraphe.H1 = line[nbHashtags+1:]
case nbHashtags == 2:
n_paragraphe.H2 = line[nbHashtags+1:]
case nbHashtags == 3:
n_paragraphe.H3 = line[nbHashtags+1:]
case nbHashtags == 4:
n_paragraphe.H4 = line[nbHashtags+1:]
case nbHashtags == 0 && len(line) > 1:
if strings.HasPrefix(line, "- ") {
}
switch nbHashtags {
case 1:
processesHeader(&n_paragraphe.H1,line,nbHashtags)
case 2:
processesHeader(&n_paragraphe.H2,line,nbHashtags)
case 3:
processesHeader(&n_paragraphe.H3,line,nbHashtags)
case 4:
processesHeader(&n_paragraphe.H4,line,nbHashtags)
case 0 :
if strings.HasPrefix(line, "- ") && len(line) > 1 {
n_paragraphe.Items = append(n_paragraphe.Items, strings.TrimLeft(line, "- "))
}
case nbHashtags > 4:
return fmt.Errorf("Err: cannot parse this md line {%s}", line)
default:
return fmt.Errorf("cannot parse this md line {%s}", line)
}
return nil
}

//Affect to the Header the line without the `-`
func processesHeader (pt_header *string, line string, nbHashtags int){
if *pt_header != ""{
slog.Warn("Trying to overload Header","oldHeader",
*pt_header,"newHeader",line[nbHashtags+1:])
}
*pt_header = line[nbHashtags+1:]
}
42 changes: 30 additions & 12 deletions internal/adapters/output/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,55 @@ import (
"log/slog"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
)


const COMPILER = "pdflatex"
var REGEX = regexp.MustCompile(`(\d+) page`)
type LatexCompiler struct{}

//Compile the template into PDF
func (*LatexCompiler) CompileTemplate(root string)error{
//Compile the templates into PDFs and return the number of page of the longest one
func (*LatexCompiler) CompileTemplate(root string)(int,error){
templates,err := getListOfTemplate(root);if err != nil {
return err
return 0,err
}
max_nb_of_page := 0
page_nb := make(chan int, len(templates))
var wg sync.WaitGroup
for _,template := range templates{
wg.Add(1)
go compile(template,root,&wg)
go compile(template,root,&wg, page_nb)
pdf_pages_nb := <-page_nb
slog.Info("Number of pages", "pages", pdf_pages_nb)
max_nb_of_page = max(pdf_pages_nb,max_nb_of_page)
}
close(page_nb)
wg.Wait()
return nil
return max_nb_of_page,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)
func compile(template string, root string, wg* sync.WaitGroup, c_page_nb chan int){
defer wg.Done()
cmd := exec.Command(COMPILER,"-interaction=nonstopmode",
"-output-directory="+root+"/assets/latex/output", template )
log, err := cmd.Output(); if err != nil {
slog.Warn("error(s) to compile file:"+template)
}
page_nb := -1
log_page := REGEX.FindStringSubmatch(string(log))
if len(log_page)<1{
slog.Error("failed to compile file didnt get the number of pages:"+template)
fmt.Println(log_page)
}else{
page_nb,err = strconv.Atoi(log_page[1]);if err != nil{
slog.Error(err.Error())}
}
c_page_nb <- page_nb
}

//Return the path of latex file inside the template directory
Expand Down
Loading

0 comments on commit a43f198

Please sign in to comment.