Skip to content

Commit

Permalink
Cleanup some of the gdocs --> md export output (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtstern authored Aug 17, 2018
1 parent 2924b37 commit 8fc47cf
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 100 deletions.
39 changes: 20 additions & 19 deletions claat/parser/md/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ import (
"github.com/russross/blackfriday"
)

// Metadata constants for the YAML header
const (
metaAuthor = "author"
metaSummary = "summary"
metaID = "id"
metaCategories = "categories"
metaEnvironments = "environments"
metaStatus = "status"
metaFeedbackLink = "feedback link"
metaAnalyticsAccount = "analytics account"
metaTags = "tags"
MetaAuthor = "author"
MetaSummary = "summary"
MetaID = "id"
MetaCategories = "categories"
MetaEnvironments = "environments"
MetaStatus = "status"
MetaFeedbackLink = "feedback link"
MetaAnalyticsAccount = "analytics account"
MetaTags = "tags"
)

const (
Expand Down Expand Up @@ -351,40 +352,40 @@ func standardSplit(s string) []string {
func addMetadataToCodelab(m map[string]string, c *types.Codelab) error {
for k, v := range m {
switch k {
case metaAuthor:
case MetaAuthor:
// Directly assign the summary to the codelab field.
c.Author = v
case metaSummary:
case MetaSummary:
// Directly assign the summary to the codelab field.
c.Summary = v
break
case metaID:
case MetaID:
// Directly assign the ID to the codelab field.
c.ID = v
break
case metaCategories:
case MetaCategories:
// Standardize the categories and append to codelab field.
c.Categories = append(c.Categories, standardSplit(v)...)
break
case metaEnvironments:
case MetaEnvironments:
// Standardize the tags and append to the codelab field.
c.Tags = append(c.Tags, standardSplit(v)...)
break
case metaStatus:
case MetaStatus:
// Standardize the statuses and append to the codelab field.
statuses := standardSplit(v)
statusesAsLegacy := types.LegacyStatus(statuses)
c.Status = &statusesAsLegacy
break
case metaFeedbackLink:
case MetaFeedbackLink:
// Directly assign the feedback link to the codelab field.
c.Feedback = v
break
case metaAnalyticsAccount:
case MetaAnalyticsAccount:
// Directly assign the GA id to the codelab field.
c.GA = v
break
case metaTags:
case MetaTags:
// Standardize the tags and append to the codelab field.
c.Tags = append(c.Tags, standardSplit(v)...)
break
Expand Down Expand Up @@ -651,7 +652,7 @@ func image(ds *docState) types.Node {
}

if ws := nodeAttr(ds.cur, "width"); ws != "" {
w,err := strconv.ParseFloat(ws, 64)
w, err := strconv.ParseFloat(ws, 64)
if err != nil {
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions claat/render/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (mw *mdWriter) write(nodes ...types.Node) error {

func (mw *mdWriter) text(n *types.TextNode) {
if n.Bold {
mw.writeString("__")
mw.writeString("**")
}
if n.Italic {
mw.writeString(" *")
Expand All @@ -142,7 +142,7 @@ func (mw *mdWriter) text(n *types.TextNode) {
mw.writeString("* ")
}
if n.Bold {
mw.writeString("__")
mw.writeString("**")
}
}

Expand Down Expand Up @@ -242,6 +242,9 @@ func (mw *mdWriter) infobox(n *types.InfoboxNode) {
// TODO: This should use the "detail item" syntax so that it can be pure MD and not HTML
// kind
// : <content>
//
// The main issue is that when you do write(n.Content.Nodes...) it always adds two newlines
// at the beginning.
mw.newBlock()
mw.writeString(`<aside class="`)
mw.writeString(string(n.Kind))
Expand Down
34 changes: 31 additions & 3 deletions claat/render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ import (
"path/filepath"
"sort"
textTemplate "text/template"
"time"

mdParse "github.com/googlecodelabs/tools/claat/parser/md"

"github.com/googlecodelabs/tools/claat/types"
"strings"

"github.com/googlecodelabs/tools/claat/types"
)

// Context is a template context during execution.
Expand Down Expand Up @@ -72,8 +76,32 @@ var funcMap = map[string]interface{}{
"renderLite": Lite,
"renderHTML": HTML,
"renderMD": MD,
"commaSep": func(arr []string) string {
return strings.Join(arr, ",")
"durationStr": func(d time.Duration) string {
m := d / time.Minute
return fmt.Sprintf("%02d:00", m)
},
"metaHeaderYaml": func(meta *types.Meta) string {
kvLine := func(k string, v string) string {
if tv := strings.TrimSpace(v); tv != "" {
return fmt.Sprintf("%s: %s\n", k, tv)
}

return ""
}

res := ""
res += kvLine(mdParse.MetaID, meta.ID)
res += kvLine(mdParse.MetaSummary, meta.Summary)
if meta.Status != nil {
res += kvLine(mdParse.MetaStatus, meta.Status.String())
}
res += kvLine(mdParse.MetaAuthor, meta.Author)
res += kvLine(mdParse.MetaCategories, strings.Join(meta.Categories, ","))
res += kvLine(mdParse.MetaTags, strings.Join(meta.Tags, ","))
res += kvLine(mdParse.MetaFeedbackLink, meta.Feedback)
res += kvLine(mdParse.MetaAnalyticsAccount, meta.GA)

return res
},
"matchEnv": func(tags []string, t string) bool {
if len(tags) == 0 || t == "" {
Expand Down
12 changes: 2 additions & 10 deletions claat/render/template.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
---
id: {{.Meta.ID}}
{{if .Meta.Status}}status: {{.Meta.Status}}{{end}}
{{if .Meta.Summary}}summary: {{.Meta.Summary}}{{end}}
{{if .Meta.Author}}author: {{.Meta.Author}}{{end}}
{{if .Meta.Categories}}categories: {{commaSep .Meta.Categories}}{{end}}
{{if .Meta.Tags}}tags: {{commaSep .Meta.Tags}}{{end}}
{{if .Meta.Feedback}}feedback link: {{.Meta.Feedback}}{{end}}
{{if .Meta.GA}}analytics account: {{.Meta.GA}}{{end}}

{{metaHeaderYaml .Meta}}
---

# {{.Meta.Title}}
Expand All @@ -16,6 +8,6 @@ id: {{.Meta.ID}}

{{range .Steps}}{{if matchEnv .Tags $.Env}}
## {{.Title}}
{{if .Duration}}Duration: {{.Duration.Minutes}}{{end}}
{{if .Duration}}Duration: {{durationStr .Duration}}{{end}}
{{.Content | renderMD $.Env}}
{{end}}{{end}}
94 changes: 28 additions & 66 deletions claat/render/tmpldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,73 +334,35 @@ var tmpldata = map[string]*template{
"md": &template{
html: false,
bytes: []byte{
0x2d,0x2d,0x2d,0xa,0x69,0x64,0x3a,0x20,0x7b,0x7b,
0x2e,0x4d,0x65,0x74,0x61,0x2e,0x49,0x44,0x7d,0x7d,
0xa,0x7b,0x7b,0x69,0x66,0x20,0x2e,0x4d,0x65,0x74,
0x61,0x2e,0x53,0x74,0x61,0x74,0x75,0x73,0x7d,0x7d,
0x73,0x74,0x61,0x74,0x75,0x73,0x3a,0x20,0x7b,0x7b,
0x2e,0x4d,0x65,0x74,0x61,0x2e,0x53,0x74,0x61,0x74,
0x75,0x73,0x7d,0x7d,0x7b,0x7b,0x65,0x6e,0x64,0x7d,
0x7d,0xa,0x7b,0x7b,0x69,0x66,0x20,0x2e,0x4d,0x65,
0x74,0x61,0x2e,0x53,0x75,0x6d,0x6d,0x61,0x72,0x79,
0x7d,0x7d,0x73,0x75,0x6d,0x6d,0x61,0x72,0x79,0x3a,
0x20,0x7b,0x7b,0x2e,0x4d,0x65,0x74,0x61,0x2e,0x53,
0x75,0x6d,0x6d,0x61,0x72,0x79,0x7d,0x7d,0x7b,0x7b,
0x65,0x6e,0x64,0x7d,0x7d,0xa,0x7b,0x7b,0x69,0x66,
0x20,0x2e,0x4d,0x65,0x74,0x61,0x2e,0x41,0x75,0x74,
0x68,0x6f,0x72,0x7d,0x7d,0x61,0x75,0x74,0x68,0x6f,
0x72,0x3a,0x20,0x7b,0x7b,0x2e,0x4d,0x65,0x74,0x61,
0x2e,0x41,0x75,0x74,0x68,0x6f,0x72,0x7d,0x7d,0x7b,
0x7b,0x65,0x6e,0x64,0x7d,0x7d,0xa,0x7b,0x7b,0x69,
0x66,0x20,0x2e,0x4d,0x65,0x74,0x61,0x2e,0x43,0x61,
0x74,0x65,0x67,0x6f,0x72,0x69,0x65,0x73,0x7d,0x7d,
0x63,0x61,0x74,0x65,0x67,0x6f,0x72,0x69,0x65,0x73,
0x3a,0x20,0x7b,0x7b,0x63,0x6f,0x6d,0x6d,0x61,0x53,
0x65,0x70,0x20,0x2e,0x4d,0x65,0x74,0x61,0x2e,0x43,
0x61,0x74,0x65,0x67,0x6f,0x72,0x69,0x65,0x73,0x7d,
0x7d,0x7b,0x7b,0x65,0x6e,0x64,0x7d,0x7d,0xa,0x7b,
0x7b,0x69,0x66,0x20,0x2e,0x4d,0x65,0x74,0x61,0x2e,
0x54,0x61,0x67,0x73,0x7d,0x7d,0x74,0x61,0x67,0x73,
0x3a,0x20,0x7b,0x7b,0x63,0x6f,0x6d,0x6d,0x61,0x53,
0x65,0x70,0x20,0x2e,0x4d,0x65,0x74,0x61,0x2e,0x54,
0x61,0x67,0x73,0x7d,0x7d,0x7b,0x7b,0x65,0x6e,0x64,
0x7d,0x7d,0xa,0x7b,0x7b,0x69,0x66,0x20,0x2e,0x4d,
0x2d,0x2d,0x2d,0xa,0x7b,0x7b,0x6d,0x65,0x74,0x61,
0x48,0x65,0x61,0x64,0x65,0x72,0x59,0x61,0x6d,0x6c,
0x20,0x2e,0x4d,0x65,0x74,0x61,0x7d,0x7d,0xa,0x2d,
0x2d,0x2d,0xa,0xa,0x23,0x20,0x7b,0x7b,0x2e,0x4d,
0x65,0x74,0x61,0x2e,0x54,0x69,0x74,0x6c,0x65,0x7d,
0x7d,0xa,0xa,0x7b,0x7b,0x69,0x66,0x20,0x2e,0x4d,
0x65,0x74,0x61,0x2e,0x46,0x65,0x65,0x64,0x62,0x61,
0x63,0x6b,0x7d,0x7d,0x66,0x65,0x65,0x64,0x62,0x61,
0x63,0x6b,0x20,0x6c,0x69,0x6e,0x6b,0x3a,0x20,0x7b,
0x7b,0x2e,0x4d,0x65,0x74,0x61,0x2e,0x46,0x65,0x65,
0x64,0x62,0x61,0x63,0x6b,0x7d,0x7d,0x7b,0x7b,0x65,
0x6e,0x64,0x7d,0x7d,0xa,0x7b,0x7b,0x69,0x66,0x20,
0x2e,0x4d,0x65,0x74,0x61,0x2e,0x47,0x41,0x7d,0x7d,
0x61,0x6e,0x61,0x6c,0x79,0x74,0x69,0x63,0x73,0x20,
0x61,0x63,0x63,0x6f,0x75,0x6e,0x74,0x3a,0x20,0x7b,
0x7b,0x2e,0x4d,0x65,0x74,0x61,0x2e,0x47,0x41,0x7d,
0x7d,0x7b,0x7b,0x65,0x6e,0x64,0x7d,0x7d,0xa,0xa,
0x2d,0x2d,0x2d,0xa,0xa,0x23,0x20,0x7b,0x7b,0x2e,
0x4d,0x65,0x74,0x61,0x2e,0x54,0x69,0x74,0x6c,0x65,
0x7d,0x7d,0xa,0xa,0x7b,0x7b,0x69,0x66,0x20,0x2e,
0x4d,0x65,0x74,0x61,0x2e,0x46,0x65,0x65,0x64,0x62,
0x61,0x63,0x6b,0x7d,0x7d,0x5b,0x43,0x6f,0x64,0x65,
0x6c,0x61,0x62,0x20,0x46,0x65,0x65,0x64,0x62,0x61,
0x63,0x6b,0x5d,0x28,0x7b,0x7b,0x2e,0x4d,0x65,0x74,
0x61,0x2e,0x46,0x65,0x65,0x64,0x62,0x61,0x63,0x6b,
0x7d,0x7d,0x29,0x7b,0x7b,0x65,0x6e,0x64,0x7d,0x7d,
0xa,0xa,0x7b,0x7b,0x72,0x61,0x6e,0x67,0x65,0x20,
0x2e,0x53,0x74,0x65,0x70,0x73,0x7d,0x7d,0x7b,0x7b,
0x69,0x66,0x20,0x6d,0x61,0x74,0x63,0x68,0x45,0x6e,
0x76,0x20,0x2e,0x54,0x61,0x67,0x73,0x20,0x24,0x2e,
0x45,0x6e,0x76,0x7d,0x7d,0xa,0x23,0x23,0x20,0x7b,
0x7b,0x2e,0x54,0x69,0x74,0x6c,0x65,0x7d,0x7d,0xa,
0x7b,0x7b,0x69,0x66,0x20,0x2e,0x44,0x75,0x72,0x61,
0x74,0x69,0x6f,0x6e,0x7d,0x7d,0x44,0x75,0x72,0x61,
0x74,0x69,0x6f,0x6e,0x3a,0x20,0x7b,0x7b,0x2e,0x44,
0x75,0x72,0x61,0x74,0x69,0x6f,0x6e,0x2e,0x4d,0x69,
0x6e,0x75,0x74,0x65,0x73,0x7d,0x7d,0x7b,0x7b,0x65,
0x6e,0x64,0x7d,0x7d,0xa,0x7b,0x7b,0x2e,0x43,0x6f,
0x6e,0x74,0x65,0x6e,0x74,0x20,0x7c,0x20,0x72,0x65,
0x6e,0x64,0x65,0x72,0x4d,0x44,0x20,0x24,0x2e,0x45,
0x6e,0x76,0x7d,0x7d,0xa,0x7b,0x7b,0x65,0x6e,0x64,
0x7d,0x7d,0x7b,0x7b,0x65,0x6e,0x64,0x7d,0x7d,0xa,
0x63,0x6b,0x7d,0x7d,0x5b,0x43,0x6f,0x64,0x65,0x6c,
0x61,0x62,0x20,0x46,0x65,0x65,0x64,0x62,0x61,0x63,
0x6b,0x5d,0x28,0x7b,0x7b,0x2e,0x4d,0x65,0x74,0x61,
0x2e,0x46,0x65,0x65,0x64,0x62,0x61,0x63,0x6b,0x7d,
0x7d,0x29,0x7b,0x7b,0x65,0x6e,0x64,0x7d,0x7d,0xa,
0xa,0x7b,0x7b,0x72,0x61,0x6e,0x67,0x65,0x20,0x2e,
0x53,0x74,0x65,0x70,0x73,0x7d,0x7d,0x7b,0x7b,0x69,
0x66,0x20,0x6d,0x61,0x74,0x63,0x68,0x45,0x6e,0x76,
0x20,0x2e,0x54,0x61,0x67,0x73,0x20,0x24,0x2e,0x45,
0x6e,0x76,0x7d,0x7d,0xa,0x23,0x23,0x20,0x7b,0x7b,
0x2e,0x54,0x69,0x74,0x6c,0x65,0x7d,0x7d,0xa,0x7b,
0x7b,0x69,0x66,0x20,0x2e,0x44,0x75,0x72,0x61,0x74,
0x69,0x6f,0x6e,0x7d,0x7d,0x44,0x75,0x72,0x61,0x74,
0x69,0x6f,0x6e,0x3a,0x20,0x7b,0x7b,0x64,0x75,0x72,
0x61,0x74,0x69,0x6f,0x6e,0x53,0x74,0x72,0x20,0x2e,
0x44,0x75,0x72,0x61,0x74,0x69,0x6f,0x6e,0x7d,0x7d,
0x7b,0x7b,0x65,0x6e,0x64,0x7d,0x7d,0xa,0x7b,0x7b,
0x2e,0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x20,0x7c,
0x20,0x72,0x65,0x6e,0x64,0x65,0x72,0x4d,0x44,0x20,
0x24,0x2e,0x45,0x6e,0x76,0x7d,0x7d,0xa,0x7b,0x7b,
0x65,0x6e,0x64,0x7d,0x7d,0x7b,0x7b,0x65,0x6e,0x64,
0x7d,0x7d,0xa,
},
},
"offline": &template{
Expand Down
11 changes: 11 additions & 0 deletions claat/types/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
)

Expand Down Expand Up @@ -138,3 +139,13 @@ func (s *LegacyStatus) UnmarshalJSON(b []byte) error {
*s = LegacyStatus(v)
return nil
}

// String turns a status into a string
func (s LegacyStatus) String() string {
ss := []string(s)
if len(ss) == 0 {
return ""
}

return "[" + strings.Join(ss, ",") + "]"
}

0 comments on commit 8fc47cf

Please sign in to comment.