jsonresume/themes/theme.go

251 lines
6.0 KiB
Go
Raw Normal View History

2018-05-07 00:58:03 +02:00
/*
* Copyright (c) 2018 Arnaud Ysmal. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package themes
import (
"fmt"
"io"
"jsonresume/model"
2018-05-08 15:23:46 +02:00
"strings"
2018-05-07 00:58:03 +02:00
"text/template"
2018-05-09 23:30:40 +02:00
"time"
2018-05-07 00:58:03 +02:00
)
2018-05-09 23:30:40 +02:00
type LangString struct {
TitlePrefix string
About string
Contact string
WorkExperience string
Awards string
Volunteer string
Education string
Skills string
Publications string
Languages string
Interests string
References string
Projects string
}
2018-05-07 00:58:03 +02:00
type Theme struct {
Name string
2018-05-07 22:24:43 +02:00
Directory string
2018-05-07 00:58:03 +02:00
Functions template.FuncMap
Template string
}
var Themes map[string]*Theme
2018-05-09 23:30:40 +02:00
var Langs = map[string]LangString{
"en": LangString{
TitlePrefix: "Resume of",
About: "About",
Contact: "Contact",
WorkExperience: "Work Experience",
Awards: "Awards",
Volunteer: "Volunteer",
Education: "Education",
Skills: "Skills",
Publications: "Publications",
Languages: "Languages",
Interests: "Interests",
References: "References",
Projects: "Projects",
},
}
var CommonFuncs = map[string]template.FuncMap{
"en": template.FuncMap{
"iconClass": iconClass,
"formatDateY": formatDateY,
"formatDateMY": formatDateMY,
"formatDateDMY": formatDateDMY,
},
}
2018-05-07 00:58:03 +02:00
func init() {
Themes = make(map[string]*Theme)
2018-05-09 23:30:40 +02:00
setupFrenchTranslation()
}
type TemplateData struct {
*model.Resume
Lang LangString
2018-05-07 00:58:03 +02:00
}
func (t *Theme) Render(r *model.Resume, w io.Writer) error {
2018-05-09 23:30:40 +02:00
if r.Language == "" {
r.Language = "en"
}
l, exists := Langs[r.Language]
if !exists {
return fmt.Errorf("Unsupported language %s", r.Language)
}
f, _ := CommonFuncs[r.Language]
tmpl := template.Must(template.New(t.Name).Funcs(f).Funcs(t.Functions).Parse(t.Template))
return tmpl.Execute(w, &TemplateData{r, l})
2018-05-07 00:58:03 +02:00
}
func (t *Theme) Register() error {
if _, exists := Themes[t.Name]; exists {
return fmt.Errorf("Package %s already registered", t.Name)
}
Themes[t.Name] = t
return nil
}
2018-05-08 15:23:46 +02:00
2018-05-09 23:30:40 +02:00
func iconClass(network string) string {
2018-05-08 15:23:46 +02:00
network = strings.ToLower(network)
switch network {
case "google-plus":
case "googleplus":
return "fa fa-google-plus"
case "flickr":
case "flicker":
return "fa fa-flickr"
case "dribbble":
case "dribble":
return "fa fa-dribbble"
case "codepen":
return "fa fa-codepen"
case "soundcloud":
return "fa fa-soundcloud"
case "reddit":
return "fa fa-reddit"
case "tumblr":
case "tumbler":
return "fa fa-tumblr"
case "stack-overflow":
case "stackoverflow":
return "fa fa-stack-overflow"
case "blog":
case "rss":
return "fa fa-rss"
case "gitlab":
return "fa fa-gitlab"
case "keybase":
return "fa fa-key"
default:
return "fa fa-" + network
}
return "fa fa-" + network
}
2018-05-09 23:30:40 +02:00
func formatDateY(date model.ResumeDate) string {
2018-05-08 15:23:46 +02:00
if date.IsZero() {
return "Present"
}
2018-05-09 23:30:40 +02:00
return fmt.Sprintf("%d", date.Year())
2018-05-08 15:23:46 +02:00
}
2018-05-09 23:30:40 +02:00
func formatDateMY(date model.ResumeDate) string {
2018-05-08 15:23:46 +02:00
if date.IsZero() {
return "Present"
}
2018-05-09 23:30:40 +02:00
return fmt.Sprintf("%s %d", date.Month().String(), date.Year())
2018-05-08 15:23:46 +02:00
}
2018-05-09 23:30:40 +02:00
func formatDateDMY(date model.ResumeDate) string {
2018-05-08 15:23:46 +02:00
if date.IsZero() {
return "Present"
}
return fmt.Sprintf("%d %s %d", date.Day(), date.Month().String(), date.Year())
}
2018-05-09 23:30:40 +02:00
func setupFrenchTranslation() {
Langs["fr"] = LangString{
TitlePrefix: "CV de",
About: "À propos",
Contact: "Contact",
WorkExperience: "Expérience Professionnelle",
Awards: "Récompenses",
Volunteer: "Bénévolat",
Education: "Formation",
Skills: "Compétences",
Publications: "Publications",
Languages: "Langues",
Interests: "Intérêts",
References: "Références",
Projects: "Projets",
}
CommonFuncs["fr"] = template.FuncMap{
"iconClass": iconClass,
"formatDateY": formatDateYFR,
"formatDateMY": formatDateMYFR,
"formatDateDMY": formatDateDMYFR,
}
}
func getMonthFR(date model.ResumeDate) string {
switch date.Month() {
case time.January:
return "Janvier"
case time.February:
return "Février"
case time.March:
return "Mars"
case time.April:
return "Avril"
case time.May:
return "Mai"
case time.June:
return "Juin"
case time.July:
return "Juillet"
case time.August:
return "Août"
case time.September:
return "Septembre"
case time.October:
return "Octobre"
case time.November:
return "Novembre"
case time.December:
return "Décembre"
}
return ""
}
func formatDateYFR(date model.ResumeDate) string {
if date.IsZero() {
return "Présent"
}
return fmt.Sprintf("%d", date.Year())
}
func formatDateMYFR(date model.ResumeDate) string {
if date.IsZero() {
return "Présent"
}
return fmt.Sprintf("%s %d", getMonthFR(date), date.Year())
}
func formatDateDMYFR(date model.ResumeDate) string {
if date.IsZero() {
return "Présent"
}
return fmt.Sprintf("%d %s %d", date.Day(), getMonthFR(date), date.Year())
}