feat: new cv builder tool

New CV builder tool migrated from the private repository.
This commit is contained in:
Dan Anglin 2023-02-18 21:01:28 +00:00
parent 60227b15b4
commit 11b9d4df15
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
14 changed files with 575 additions and 6 deletions

5
.dockerignore Normal file
View file

@ -0,0 +1,5 @@
*
!magefile.go
!helpers/*
!go.mod
!go.sum

1
.gitignore vendored
View file

@ -42,3 +42,4 @@ tags
# Persistent undo
[._]*.un~
cv.pdf

2
.hadolint.yaml Normal file
View file

@ -0,0 +1,2 @@
ignored:
- DL3015

20
LICENSE
View file

@ -1,9 +1,21 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2023 Dan Anglin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,3 +1,46 @@
# cv-builder
# CV Builder
A CV builder tool that I use to make my CV.
## Overview
This project parses a CV, written in JSON and generates it as a PDF document.
The PDF generation is completed in two steps:
1. The application parses the JSON document and generates a TEX file using the TEX template files located in the [templates](./templates) directory.
2. The application then uses ConTeXt to generate the final PDF document from the generated TEX file.
## Dependencies
If you are interested in generating your own CV, then below is a list of dependencies that you'll need to install:
- **Go:** Please go [here](https://go.dev/dl/) to download the latest version of the Go programming language.
- **ConTeXt:** You can go to the [installation page](https://wiki.contextgarden.net/Installation) to find out how to install ConTeXt for your Operating System.
- **The Carlito font (ttf-carlito):** In previous iterations of my CV I used the Calibri font. Carlito is the free, metric compatible alternative to this and is specified in the TEX template.
- For Ubuntu/Debian installation you can use `apt`:
```bash
$ apt install font-crosextra-carlito
```
- For Arch Linux you can use `pacman`:
```bash
$ pacman -S ttf-carlito
```
- Alternatively you can download the font from https://fontlibrary.org/en/font/carlito
- Once this font is installed you'll need to update ConTeXt so it can find the font when generating the PDF:
```bash
$ OSFONTDIR=/usr/share/fonts
$ mtxrun --script fonts --reload
```
## Generating the example PDF Document
You can generate a PDF from the example CV by running the following command:
```bash
$ go run . --input example/cv.json
```
This will create a file called `cv.pdf` which you can view in your favourite PDF viewer.
## Inspirations
- [The Markdown Resume](https://mszep.github.io/pandoc_resume/) - This project uses ConTeXt and pandoc to convert Markdown based CVs into multiple formats including PDF, HTML and DOCX. This is where I discovered ConTeXt.
- [melkir/resume](https://github.com/melkir/resume) - This project generates CVs using Go and LaTeX.

51
cv.go Normal file
View file

@ -0,0 +1,51 @@
package main
type Cv struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
JobTitle string `json:"jobTitle"`
Contact Contact `json:"contact"`
Links Links `json:"links"`
Summary []string `json:"summary"`
Technologies []Technologies `json:"technologies"`
Employment []Experience `json:"employment"`
Education []Experience `json:"education"`
Interests []string `json:"interests"`
}
type Contact struct {
Email string `json:"email"`
Phone string `json:"phone"`
Location string `json:"location"`
}
type Links struct {
LinkedIn string `json:"linkedin"`
GitHub string `json:"github"`
}
type Technologies struct {
Category string `json:"category"`
Values []string `json:"values"`
}
type Experience struct {
Company string `json:"company,omitempty"`
School string `json:"school,omitempty"`
Location string `json:"location"`
JobTitle string `json:"jobTitle,omitempty"`
Qualification string `json:"qualification,omitempty"`
Duration Duration `json:"duration"`
Details []string `json:"details"`
}
type Duration struct {
Start Date `json:"start"`
End Date `json:"end"`
Present string `json:"present"`
}
type Date struct {
Year string `json:"year"`
Month string `json:"month"`
}

47
docker/Dockerfile Normal file
View file

@ -0,0 +1,47 @@
FROM golang:1.19-buster AS builder
RUN git clone https://github.com/magefile/mage "${GOPATH}/src/mage"
WORKDIR ${GOPATH}/src/mage
RUN go run bootstrap.go
COPY go.mod ${GOPATH}/cv-builder/
COPY go.sum ${GOPATH}/cv-builder/
COPY magefile.go ${GOPATH}/cv-builder/
COPY helpers ${GOPATH}/cv-builder/helpers
WORKDIR ${GOPATH}/cv-builder
RUN mage -compile /usr/local/bin/cv-make
FROM debian:buster
COPY --from=builder /usr/local/bin/cv-make /usr/local/bin
# Install dependencies
RUN \
apt-get update \
&& apt-get install -y \
fonts-crosextra-carlito \
rsync \
curl= \
aspell \
aspell-en \
&& apt-get clean autoclean \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/* /tmp/* \
&& mkdir /opt/context
WORKDIR /opt/context
# Install ConTeXt standalone
RUN \
curl -LO http://minimals.contextgarden.net/setup/first-setup.sh \
&& sh first-setup.sh --context=current --engine=luatex \
&& rm -rf /opt/context/tex/texmf-context/doc
ENV PATH=${PATH}:/opt/context/tex/texmf-linux-64/bin \
OSFONTDIR=/usr/share/fonts
RUN mtxrun --script fonts --reload

121
example/cv.json Normal file
View file

@ -0,0 +1,121 @@
{
"firstName": "John",
"lastName": "Smith",
"jobTitle": "Software Engineer",
"contact": {
"email": "john.smith@example.io",
"location": "England, UK"
},
"links": {
"linkedin": "https://www.linkedin.com/in/john-smith-2YASMKJMP",
"github": "https://github.com/johnsmaith-example-gh"
},
"summary": [
"Rem quia eveniet sit, quis asperiores ea sed repellat non molestias aut accusamus aliquid et dolorem.",
"Natus modi culpa at vel officiis debitis fuga delectus, quo pariatur dicta eum molestiae et, sed maxime delectus quasi enim quasi alias quibusdam.",
"Facere sequi tempora voluptates maxime velit explicabo sed."
],
"technologies": [
{
"category": "Programming Languages",
"values": [ "Go", "Python", "Javascript", "Typescript" ]
},
{
"category": "Databases",
"values": [ "PostgreSQL", "MySQL", "Redis" ]
},
{
"category": "Containerisation",
"values": [ "Docker", "Kubernetes", "LXC/LXD"]
},
{
"category": "Cloud Technologies",
"values": [ "AWS" ]
},
{
"category": "Other",
"values": [ "Distributed Systems", "Agile", "Debugging", "Documentation" ]
}
],
"employment": [
{
"company": "Company C",
"location": "Manchester (Remote)",
"jobTitle": "Software Engineer (Cloud Platform)",
"duration": {
"start": {
"year": "2020",
"month": "August"
},
"present": "yes"
},
"details": [
"Est doloremque molestiae ipsum id occaecati porro repudiandae corporis.",
"Qui fugiat qui et quisquam.",
"Sed excepturi perferendis eius Amet mollitia tenetur voluptatum deserunt."
]
},
{
"company": "Company B",
"location": "London (Hybrid)",
"jobTitle": "Software Engineer (Backend)",
"duration": {
"start": {
"year": "2015",
"month": "September"
},
"end": {
"year": "2021",
"month": "August"
}
},
"details": [
"Et ipsum odio voluptate molestiae odio iusto.",
"Libero quisquam molestiae velit facilis quo. Occaecati non exercitationem aut.",
"Laborum laboriosam sapiente quis beatae provident consequuntur."
]
},
{
"company": "Company A",
"location": "London (Onsite)",
"jobTitle": "Software Engineer (Backend)",
"duration": {
"start": {
"year": "2010",
"month": "January"
},
"end": {
"year": "2015",
"month": "September"
}
},
"details": [
"Voluptatem tempora omnis velit et magnam sunt.",
"Animi et sed nostrum, inventore debitis, quia voluptates dolor cum tempore vero commodi.",
"Distinctio sequi commodi debitis enim voluptas ex ipsum."
]
}
],
"education": [
{
"school": "Example University",
"location": "London",
"qualification": "BSc (Hons) in Computer Science & Artificial Intelligence, 1st",
"duration": {
"start": {
"year": "2012",
"month": "October"
},
"end": {
"year": "2015",
"month": "July"
}
}
}
],
"interests": [
"Consequatur quae dolores repellat velit voluptas, et Quod accusantium sit temporibus quam voluptas magni.",
"Officia numquam accusantium vel eaque est eligendi voluptates nulla est similique et ratione consequatur.",
"Expedita modi voluptatem voluptatibus quis sit qui aut."
]
}

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module codeflow.dananglin.me.uk/apollo/cv-builder
go 1.19

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=

116
main.go Normal file
View file

@ -0,0 +1,116 @@
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"text/template"
)
//go:embed templates/tex/*
var templates embed.FS
func main() {
var input, output string
flag.StringVar(&input, "input", "", "specify the CV JSON file that you want to input to the builder.")
flag.StringVar(&output, "output", "", "specify the name of the output CV file.")
flag.Parse()
tempDir, err := os.MkdirTemp("/tmp", "cv-builder-")
if err != nil {
log.Fatalf("ERROR: Unable to create a temporary directory; %v", err)
}
defer func() {
err := os.RemoveAll(tempDir)
if err != nil {
log.Printf("WARN: An error occurred when removing the temporary directory; %v", err)
}
}()
texFile, err := tex(input, tempDir)
if err != nil {
log.Fatalf("ERROR: %v", err)
}
if err := pdf(tempDir, texFile, output); err != nil {
log.Fatalf("ERROR: %v", err)
}
}
// tex generates the CV document as a Tex file.
func tex(input, tempDir string) (string, error) {
data, err := os.ReadFile(input)
if err != nil {
return "", fmt.Errorf("unable to read data from file; %w", err)
}
var cv Cv
if err = json.Unmarshal(data, &cv); err != nil {
return "", fmt.Errorf("unable to decode JSON data; %w", err)
}
// if CV_CONTACT_PHONE is set then add it to the CV
phone := os.Getenv("CV_CONTACT_PHONE")
if len(phone) > 0 {
cv.Contact.Phone = phone
}
output := filepath.Join(tempDir, "cv.tex")
file, err := os.Create(output)
if err != nil {
return "", fmt.Errorf("unable to create output file %s; %w", output, err)
}
defer file.Close()
fmap := template.FuncMap{
"notLastElement": notLastElement,
"join": join,
"durationToString": durationToString,
}
t := template.Must(template.New("cv.tmpl.tex").
Funcs(fmap).
Delims("<<", ">>").
ParseFS(templates, "templates/tex/*.tmpl.tex"),
)
if err = t.Execute(file, cv); err != nil {
return "", fmt.Errorf("unable to execute the CV template. %w", err)
}
log.Printf("INFO: Tex file %s was successfully created.", output)
return output, nil
}
// pdf generates the CV document as a PDF file from the tex file.
func pdf(tempDir, texFile, output string) error {
pathArg := "--path=" + tempDir
command := exec.Command("mtxrun", pathArg, "--script", "context", texFile)
command.Stderr = os.Stderr
command.Stdout = os.Stdout
if err := command.Run(); err != nil {
return err
}
if output == "" {
output = "./cv.pdf"
}
if err := os.Rename(filepath.Join(tempDir, "cv.pdf"), output); err != nil {
return err
}
return nil
}

34
template_funcs.go Normal file
View file

@ -0,0 +1,34 @@
package main
import (
"fmt"
"strings"
)
// notLastElement returns true if an element of an array
// or a slice is not the last.
func notLastElement(pos, length int) bool {
return pos < length-1
}
// join uses strings.Join to join all string elements into
// a single string.
func join(s []string) string {
return strings.Join(s, " ")
}
// durationToString outputs the employment/education
// duration as a formatted string.
func durationToString(d Duration) string {
start := fmt.Sprintf("%s, %s", d.Start.Month, d.Start.Year)
present := strings.ToLower(d.Present)
end := ""
if present == "yes" || present == "true" {
end = "Present"
} else {
end = fmt.Sprintf("%s, %s", d.End.Month, d.End.Year)
}
return start + " - " + end
}

54
templates/tex/cv.tmpl.tex Normal file
View file

@ -0,0 +1,54 @@
<<- /* Prepend the setup area */ ->>
<< template "cv_setup.tmpl.tex" .>>
\starttext
\starttitleAndContact
\cvtitle{<<.FirstName>> <<.LastName>>}{<<.JobTitle>>}
\titleAndContact
{\bf Email:} <<.Contact.Email>>\blank[none]
<<if .Contact.Phone>>{\bf Phone:} <<.Contact.Phone>>\blank[none]<<end>>
{\bf Location:} <<.Contact.Location>>\blank[medium]
<<if .Links.LinkedIn>>{\bf LinkedIn:} <<.Links.LinkedIn>>\blank[none]<<end>>
<<if .Links.GitHub>>{\bf GitHub:} <<.Links.GitHub>>\blank[none]<<end>>
\stoptitleAndContact
\section{SUMMARY}
<<join .Summary>>
\section{SKILLS SUMMARY}
\starttabulate[|w(0.3\textwidth)lB|lp(0.7\textwidth)|]
<<$lenTech := len .Technologies>>
<<range $i, $tech := .Technologies>>
<<$lenValues := len $tech.Values>>
\NC <<$tech.Category>> \NC <<range $j, $val := $tech.Values>><<$val>><<if notLastElement $j $lenValues>>, <<end>><<end>>\NC\NR
<<if notLastElement $i $lenTech>>\TB[1mm]<<end>>
<<end>>
\stoptabulate
\section{EXPERIENCE}
<<- range .Employment>>
\jobsection{<<.Company>>}{<<.Location>>}{<<.JobTitle>>}{<<durationToString .Duration>>}
\startitemize
<<range .Details>>
\item <<.>>
<<end>>
\stopitemize
<<end>>
\section{EDUCATION}
<<range .Education ->>
\jobsection{<<.School>>}{<<.Location>>}{<<.Qualification>>}{<<durationToString .Duration>>}
\startitemize
<<range .Details>>
\item <<.>>
<<end>>
\stopitemize
<<end>>
\section{INTERESTS AND HOBBIES}
<<join .Interests>>
\section{REFERENCES}
References are available upon request.
\stoptext

View file

@ -0,0 +1,78 @@
\version [final]
% set main language to English
\mainlanguage [en]
% set paper size to A4
\setuppapersize [A4]
% define the page layout
\setuplayout
[backspace=20mm,
bottomspace=20mm,
cutspace=0mm,
footer=0mm,
header=0mm,
height=middle,
topspace=10mm,
width=middle]
% \showframe
% remove page numbers by setting an empty location
\setuppagenumbering[location=]
% Colour definitions for headings
\definecolor [bluetheme][h=1155cc]
\definecolor [greytheme][h=434343]
% Font setup
% https://wiki.contextgarden.net/Simplefonts
\definefontfamily [cvfontfamily][rm][LiberationSerif]
\definefontfamily [cvfontfamily][ss][Carlito]
\definefontfamily [cvfontfamily][tt][LiberationMono]
\definefontfamily [cvfontfamily][mm][LibertinusMath]
\setupbodyfont [cvfontfamily,ss,10pt]
% define the font scaling when using commands such as \tfa, \tfb, etc
\definebodyfontenvironment [10pt][a=11pt,b=20pt,c=32pt]
% cvtitle outputs the title of the CV
% arguments:
% #1 - name: name of the individual
% #2 - role: job role of the individual
\define[2]\cvtitle{
{\bfc#1}\blank[small]{\tfb#2}
}
% jobsection outputs the summary of a work experience
% arguments:
% #1 - company: the company where the work experience took place
% #2 - location: the location of the work experience
% #3 - role: the role of the work experience
% #4 - duration: the duration of the work experience (e.g. March, 2017 - April, 2018)
\define[4]\jobsection{
{\bfa#1,} {\tfa#2}{\tfa\em#3}\blank[small]\color[greytheme]{#4}\blank[line]
}
\definehead [skillssection][subsection]
\setuphead [section][color=bluetheme]
\setuphead [skillssection][color=black]
\setuphead [section, skillssection][number=no, align=right, style=\bfa]
% setup the format of items (bullet points)
\setupitemize
[1]
[packed]
[align=right,
symbol=1,
margin=no,
distance=1mm]
% defineparagraphs and setupparagraphs allows us to create custom
% columns. Here we define and configure a paragraph environment for the CV title
% and contact columns.
% https://wiki.contextgarden.net/Command/defineparagraphs
% https://wiki.contextgarden.net/Command/setupparagraphs
\defineparagraphs[titleAndContact][n=2,after={\blank}]
\setupparagraphs [titleAndContact][1][width=0.5\textwidth]
\setupparagraphs [titleAndContact][2][width=0.5\textwidth]