Skip to content

Commit

Permalink
WIP refacto to hexa latex
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Oct 27, 2024
1 parent e265514 commit 71f68a2
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
100 changes: 100 additions & 0 deletions internal/adapters/output/latex_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package output

import (
"fmt"
"errors"
"strings"
"regexp"
"os"
)


//Fill if existing the section of the template with the section given
func applySectionToTemplate(template string,section string, headers []string, items []string)error{ return nil }

Check failure on line 13 in internal/adapters/output/latex_writer.go

View workflow job for this annotation

GitHub Actions / Run Linter

func `applySectionToTemplate` is unused (unused)

//Read the template file in the assets directory
func readCVTemplate(path string)(string, error){

Check failure on line 16 in internal/adapters/output/latex_writer.go

View workflow job for this annotation

GitHub Actions / Run Linter

func `readCVTemplate` is unused (unused)
file, err := os.ReadFile(path+"/assets/latex/template/template.tex")
if err != nil {
return "", err
}
return string(file), nil
}

//Write the template file in the assets directory
func makeNewCV(path string, template string, name string)error{

Check failure on line 25 in internal/adapters/output/latex_writer.go

View workflow job for this annotation

GitHub Actions / Run Linter

func `makeNewCV` is unused (unused)
err := os.WriteFile(path+"/assets/latex/output/"+name,
[]byte(template), 0644)
return err
}

//In the core we will read the template
//iterate by section
//Apply to template
//Write

//Apply a section to a section type on a latex template
func ApplyToSectionToTemplate(template string, headers []string,item []string, section string) (string,error){
switch{
case section == "Professional":
template = strings.Replace(template,"%EXPERIENCE_SECTIONS%",
"%EXPERIENCE_SECTIONS%\n"+replace_param(prof_template,headers),1)
template = replace_items(template,item)

case section == "Project":
template = strings.Replace(template,"%PROJECTS_SECTIONS%",
"%PROJECTS_SECTIONS%\n"+replace_param(proj_template,headers),1)
template = replace_items(template, item)

case section == "Education":
template = strings.Replace(template,"%EDUCATION_SECTIONS%",
"%EDUCATION_SECTIONS%\n"+replace_param(edu_template,headers),1)

case section == "Skill"://TODO https://github.com/Theo-Hafsaoui/Anemon/issues/1
template = strings.Replace(template,"%SKILL_SECTIONS%",
"%SKILL_SECTIONS%\n"+replace_param(sk_template,headers),1)
default:
return "",errors.New("Don't know type "+section)
}
return template,nil
}


//Search and replace the number in range of `nb_params` by their replacement
func replace_param(sec_template string, replacements []string)string{
for i := 0; i < len(replacements); i++ {
position := fmt.Sprintf("$%d", i+1)
sec_template = strings.Replace(sec_template,
position, replacements[i], 1)
}
return sanitize(sec_template)
}


func replace_items(template string, section_items []string)string{
items := ""
for _,item := range section_items{
items += strings.Replace(pro_item,"%ITEM%",item,1)
}
template = strings.Replace(template,
"%ITEMS%", items, 1)
return sanitize(template)
}

//Sanitize the special charactere
func sanitize(template string)(string){
replacements := []struct {
pattern string
replacement string
}{
{`[0-9]\%`, `\\%`},
{`\*\*(.*?)\*\*`, `\textbf{$1}`},
{`\*(.*?)\*`, `\emph{$1}`},
//{`\[(.*?)\]\((.*?)\)`, `\href{$2}{$1}`},
}
for _, r := range replacements {
re := regexp.MustCompile(r.pattern)
template = re.ReplaceAllString(template, r.replacement)
}
return template
}
34 changes: 34 additions & 0 deletions internal/adapters/output/template_sections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package output

const pro_item = "\\resumeItem{%ITEM%}\n"

const prof_template = `
\resumeSubheading
{$1}{$2}
{\href{$3}{$4}}{ }
\resumeItemListStart
%ITEMS%
\resumeItemListEnd
`
const NB_P_PROF = 4

const proj_template = `
\resumeProjectHeading
{\textbf{$1} | \emph{$2 \href{$3}{\faIcon{github}}}}{}
\resumeItemListStart
%ITEMS%
\resumeItemListEnd
`
const NB_P_PROJ = 3

const edu_template = `
\resumeSubheading
{\href{$2}{$1}}{}
{$3}{$4}
`
const NB_P_EDU = 4

const sk_template = `
\item \textbf{$1}{: $2} \\
`
const NB_P_SK = 2
6 changes: 6 additions & 0 deletions internal/core/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"fmt"
"strings"
)
//TODO replace with interface
//
//type geometry interface {
// area() float64
// perim() float64
//}

//CV with Language and Sections
type CV struct {
Expand Down
2 changes: 0 additions & 2 deletions internal/markup_languages/latex.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"strings"
)

type SectionName string

//Read the template file in the assets directory
func read_template(path string)(string,error) {
file, err := os.ReadFile(path+"/assets/latex/template/template.tex")
Expand Down

0 comments on commit 71f68a2

Please sign in to comment.