jsonresume/themes/theme.go
2021-02-07 21:10:42 +01:00

321 lines
7.6 KiB
Go

/*
* 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"
"strings"
"text/template"
"time"
)
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
In string
At string
}
type Theme struct {
Name string
Directory string
Functions template.FuncMap
Template string
}
var Themes map[string]*Theme
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",
In: "in",
At: "at",
},
}
var CommonFuncs = map[string]template.FuncMap{
"en": template.FuncMap{
"iconClass": iconClass,
"formatDateY": formatDateY,
"formatDateMY": formatDateMY,
"formatDateDMY": formatDateDMY,
"formatDateShortMY": formatDateShortMY,
"formatDateDShortMY": formatDateDShortMY,
},
}
func init() {
Themes = make(map[string]*Theme)
setupFrenchTranslation()
}
type TemplateData struct {
*model.Resume
Lang LangString
}
func (t *Theme) Render(r *model.Resume, w io.Writer) error {
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})
}
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
}
func iconClass(network string) string {
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"
case "gitea":
return "fa fa-git"
default:
return "fa fa-" + network
}
return "fa fa-" + network
}
func formatDateY(date model.ResumeDate) string {
if date.IsZero() {
return "Present"
}
return fmt.Sprintf("%d", date.Year())
}
func formatDateMY(date model.ResumeDate) string {
if date.IsZero() {
return "Present"
}
return fmt.Sprintf("%s %d", date.Month().String(), date.Year())
}
func formatDateDMY(date model.ResumeDate) string {
if date.IsZero() {
return "Present"
}
return fmt.Sprintf("%d %s %d", date.Day(), date.Month().String(), date.Year())
}
func formatDateShortMY(date model.ResumeDate) string {
if date.IsZero() {
return "Present"
}
return fmt.Sprintf("%s %d", date.Format("Jan"), date.Year())
}
func formatDateDShortMY(date model.ResumeDate) string {
if date.IsZero() {
return "Present"
}
return fmt.Sprintf("%d %s %d", date.Day(), date.Format("Jan"), date.Year())
}
func setupFrenchTranslation() {
Langs["fr"] = LangString{
TitlePrefix: "CV de",
About: "À propos",
Contact: "Coordonnées",
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",
In: "en",
At: "à",
}
CommonFuncs["fr"] = template.FuncMap{
"iconClass": iconClass,
"formatDateY": formatDateYFR,
"formatDateMY": formatDateMYFR,
"formatDateDMY": formatDateDMYFR,
"formatDateShortMY": formatDateShortMYFR,
"formatDateDShortMY": formatDateDShortMYFR,
}
}
func getMonthFRShort(date model.ResumeDate) string {
switch date.Month() {
case time.January:
return "Janv."
case time.February:
return "Fév."
case time.March:
return "Mars"
case time.April:
return "Avril"
case time.May:
return "Mai"
case time.June:
return "Juin"
case time.July:
return "Juil."
case time.August:
return "Août"
case time.September:
return "Sept."
case time.October:
return "Oct."
case time.November:
return "Nov."
case time.December:
return "Déc."
}
return ""
}
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())
}
func formatDateShortMYFR(date model.ResumeDate) string {
if date.IsZero() {
return "Présent"
}
return fmt.Sprintf("%s %d", getMonthFRShort(date), date.Year())
}
func formatDateDShortMYFR(date model.ResumeDate) string {
if date.IsZero() {
return "Présent"
}
return fmt.Sprintf("%d %s %d", date.Day(), getMonthFRShort(date), date.Year())
}