Skip to content

Commit

Permalink
WIP latex apply
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Aug 30, 2024
1 parent 9921e9b commit 34fe74f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 16 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module anemon

go 1.22.0

require github.com/google/go-cmp v0.6.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
38 changes: 32 additions & 6 deletions internal/parser/latex.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package parser

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

//Apply a section to a section type on a latex template
//func (section section, type section_type, template string)
type SectionName string

//to write this template
//func write(template string, name string)
//Todo

//Read the template file in the assets directory
func read_template()(string,error) {
Expand All @@ -24,3 +25,28 @@ func writeTemplate(template string, name string)error{
[]byte(template), 0644)
return err
}

//Apply a section to a section type on a latex template
func applyToSection(section Section, section_type string)(string,error){
replacements := []string{section.first, section.second, section.third, section.fourth}
template := ""
if section.params_nb == 0 {
return "",errors.New("No nb of params given")
}
switch{
case section_type == "Professional":
template = pro_template
for i := 0; i < section.params_nb; i++ {
position := fmt.Sprintf("%d", i+1)
template = strings.Replace(template,
position, replacements[i], 1)
}
items := ""
for _,item := range section.description{
items += strings.Replace(pro_item,"%ITEM%",item,1)
}
template = strings.Replace(template,
"%ITEMS%", items, 1)
}
return template,nil
}
37 changes: 34 additions & 3 deletions internal/parser/latex_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package parser

import (
"os"
"path/filepath"
"testing"
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestReadLatex(t *testing.T) {
Expand Down Expand Up @@ -42,6 +44,7 @@ func TestReadLatex(t *testing.T) {
}
}
}

func TestWriteLatex(t *testing.T) {
err := writeTemplate("Hello, world", "hello")
if err != nil {
Expand All @@ -54,3 +57,31 @@ func TestWriteLatex(t *testing.T) {
}
}

func TestApplySection(t *testing.T) {
pro_section := Section{
first: "first",
second: "second",
third: "third",
fourth: "fourth",
description: []string{"item1","item2"},
params_nb: 4,
}
want := `
\resumeSubheading
{first}{second}
{\href{third}{fourth}}{ }
\resumeItemListStart
\resumeItem{item1}
\resumeItem{item2}
\resumeItemListEnd
`
got,err := applyToSection(pro_section,
"Professional")
if err!=nil{
t.Fatalf("error when apply template: %v", err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("TestApplySection mismatch (-expected +got):\n%s", diff)
}
}
7 changes: 4 additions & 3 deletions internal/parser/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Section struct {
second string
third string
fourth string
description string
description []string
params_nb int
}

Expand All @@ -40,8 +40,9 @@ func Parse(paragraph string) (Section,error){
section.third=line[nb_hashtag+1:]
case nb_hashtag == 4:
section.fourth=line[nb_hashtag+1:]
case nb_hashtag == 0:
section.description += line
case nb_hashtag == 0 && len(line)>1:
items := strings.Split(line, "\n")
section.description = append(section.description, items...)
case nb_hashtag > 4:
return section, errors.New("Err: cannot parse this md line{"+line+"}")
}
Expand Down
12 changes: 8 additions & 4 deletions internal/parser/markdown_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package parser

import (
"testing"
"fmt"
"reflect"
"testing"
)

func TestParseHappyPath(t *testing.T) {
Expand All @@ -10,7 +12,8 @@ func TestParseHappyPath(t *testing.T) {
## Skill
### Date
#### Url
Description`
Item
Item2`
result, err := Parse(input)
if err != nil {
t.Fatalf("Unexecpted eroor :%s", err.Error())
Expand Down Expand Up @@ -38,8 +41,9 @@ Description`
}


description := "Description"
if result.description != description {
description := []string{"Item","Item2"}
if !reflect.DeepEqual(result.description, description){
fmt.Printf("want: %q, got: %q\n", description, result.description)
t.Fatalf("want %s got %s", description, result.description)
}
}
Expand Down
11 changes: 11 additions & 0 deletions internal/parser/template_sections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package parser

const pro_item = "\\resumeItem{%ITEM%}\n"
const pro_template = `
\resumeSubheading
{1}{2}
{\href{3}{4}}{ }
\resumeItemListStart
%ITEMS%
\resumeItemListEnd
`

0 comments on commit 34fe74f

Please sign in to comment.