Skip to content

Commit

Permalink
Merge pull request jsonresume#69 from ArmanMazdaee/add-render
Browse files Browse the repository at this point in the history
Add render
  • Loading branch information
stp-ip authored Dec 1, 2018
2 parents e634a0c + 259b017 commit e2e2242
Show file tree
Hide file tree
Showing 22 changed files with 591 additions and 4,626 deletions.
52 changes: 0 additions & 52 deletions Gopkg.lock

This file was deleted.

26 changes: 0 additions & 26 deletions Gopkg.toml

This file was deleted.

25 changes: 25 additions & 0 deletions cmd/resumic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"

"github.com/resumic/schema/jsonschema"
"github.com/resumic/schema/render"
"github.com/resumic/schema/schema"
"github.com/xeipuuv/gojsonschema"
)
Expand Down Expand Up @@ -86,10 +87,28 @@ func GenerateExample(exampleFile string) error {
return nil
}

func RenderResume(resumeFile, htmlFile, themeName string) error {
resume, err := ioutil.ReadFile(resumeFile)
if err != nil {
return fmt.Errorf("couldn't read the resume %s", err)
}
html, err := render.RenderHTML(resume, themeName)
if err != nil {
return fmt.Errorf("couldn't render the resume: %s", err)
}
err = ioutil.WriteFile(htmlFile, html, 0600)
if err != nil {
return fmt.Errorf("couldn't write the html output: %s", err)
}
return nil
}

func main() {
resumeFile := flag.String("resume", "", "Resume file")
schemaFile := flag.String("schema", "./schema.json", "Generate JSON Schema")
exampleFile := flag.String("example", "./example.json", "Generate example JSON")
htmlFile := flag.String("html", "", "html output file")
themeName := flag.String("theme", "", "theme name")
flag.Parse()

// Verify that a subcommand has been provided
Expand All @@ -116,6 +135,12 @@ func main() {
log.Fatalln(err)
}
log.Println("Example file created successfully")
case "render":
err := RenderResume(*resumeFile, *htmlFile, *themeName)
if err != nil {
log.Fatalln(err)
}
log.Println("Resume HTML rendered successfully")
default:
log.Fatalln("Unsupported subcommands. Please check --help for commands list")
}
Expand Down
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/resumic/schema

require (
github.com/gobuffalo/packd v0.0.0-20181124090624-311c6248e5fb
github.com/gobuffalo/packr/v2 v2.0.0-rc.8
github.com/gohugoio/hugo v0.49.2
github.com/markbates/inflect v1.0.4 // indirect
github.com/tidwall/gjson v1.1.3
github.com/tidwall/match v1.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v0.0.0-20181112162635-ac52e6811b56
)
419 changes: 419 additions & 0 deletions go.sum

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions render/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package render

import (
"encoding/json"
"io/ioutil"
"os"
"path"

"github.com/gohugoio/hugo/commands"
)

type siteConfig struct {
DisableKinds []string `json:"disableKinds"`
Theme string `json:"theme"`
}

type frontmatter struct {
Layout string `json:"layout"`
}

func build(root string) error {
resp := commands.Execute([]string{"--quiet", "-s", root})
return resp.Err
}

func RenderHTML(resume []byte, theme string) ([]byte, error) {
sitePath, err := ioutil.TempDir(os.TempDir(), "resumic")
if err != nil {
return nil, err
}
defer os.RemoveAll(sitePath)

themePath := path.Join(sitePath, "themes")
err = extractTheme(themePath, theme)
if err != nil {
return nil, err
}

config := siteConfig{}
config.Theme = theme
config.DisableKinds = []string{"taxonomy", "taxonomyTerm", "category", "sitemap", "RSS", "404", "robotsTXT", "home", "section"}

configJSON, err := json.MarshalIndent(config, "", " ")
if err != nil {
return nil, err
}
configPath := path.Join(sitePath, "config.json")
err = ioutil.WriteFile(configPath, configJSON, 0600)
if err != nil {
return nil, err
}

dataPath := path.Join(sitePath, "data", "resumic", "resume.json")
err = os.MkdirAll(path.Dir(dataPath), 0700)
if err != nil {
return nil, err
}
err = ioutil.WriteFile(dataPath, resume, 0600)
if err != nil {
return nil, err
}

content := frontmatter{}
content.Layout = "resumic"

contentJSON, err := json.MarshalIndent(content, "", " ")
if err != nil {
return nil, err
}
contentPath := path.Join(sitePath, "content", "resumic", "resume.md")
err = os.MkdirAll(path.Dir(contentPath), 0700)
if err != nil {
return nil, err
}
err = ioutil.WriteFile(contentPath, contentJSON, 0600)
if err != nil {
return nil, err
}

err = build(sitePath)
if err != nil {
return nil, err
}

htmlPath := path.Join(sitePath, "public", "resumic", "resume", "index.html")
return ioutil.ReadFile(htmlPath)
}
41 changes: 41 additions & 0 deletions render/theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package render

import (
"io"
"os"
"path"

"github.com/gobuffalo/packd"
packr "github.com/gobuffalo/packr/v2"
)

var themesList = map[string]bool{
"test-theme": true,
}

type InvalidThemeError struct {
name string
}

func (e *InvalidThemeError) Error() string {
return "render: " + e.name + " is not a valid theme."
}

func extractTheme(root, theme string) error {
if !themesList[theme] {
return &InvalidThemeError{name: theme}
}

box := packr.New("themes", "./render/themes")
return box.WalkPrefix(theme+"/", func(name string, file packd.File) error {
p := path.Join(root, name)
os.MkdirAll(path.Dir(p), 0777)
dst, err := os.Create(p)
defer dst.Close()
if err != nil {
return err
}
_, err = io.Copy(dst, file)
return err
})
}
6 changes: 6 additions & 0 deletions render/themes/test-theme/layouts/resumic/single.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
{{ $resume := index $.Site.Data.resumic .File.BaseFileName }}
{{ $resume.core.name }} - {{ $resume.core.title }}
</body>
</html>
1 change: 0 additions & 1 deletion vendor/github.com/tidwall/gjson/.travis.yml

This file was deleted.

20 changes: 0 additions & 20 deletions vendor/github.com/tidwall/gjson/LICENSE

This file was deleted.

Loading

0 comments on commit e2e2242

Please sign in to comment.