Skip to content

Commit

Permalink
claat, render: support of an "offline" out format
Browse files Browse the repository at this point in the history
The offline output creates a set of files, each per step,
to support a codelab consumption directly from an offline storage, such
as a USB thumbdrive.
  • Loading branch information
Alex Vaghin committed Aug 27, 2016
1 parent 556d8e8 commit 58cc95b
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 36 deletions.
2 changes: 1 addition & 1 deletion claat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $(OUTDIR)/claat-%: GOARCH=$(subst .exe,,$(word 2,$(subst -, ,$*)))
$(OUTDIR)/claat-%: $(SRCS) VERSION
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $@ $(LDFLAGS)

%/tmpldata.go: %/gen-tmpldata.go %/template.html %/template.md
%/tmpldata.go: %/gen-tmpldata.go %/template.html %/template.md %/template-offline.html
cd $* && go generate

release: $(RELEASES)
Expand Down
2 changes: 1 addition & 1 deletion claat/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.1
0.6.0
73 changes: 49 additions & 24 deletions claat/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -106,23 +105,7 @@ func exportCodelab(src string) (*types.Meta, error) {
// writeCodelab stores codelab main content in ctx.Format and its metadata
// in JSON format on disk.
func writeCodelab(dir string, clab *types.Codelab, ctx *types.Context) error {
// render main codelab content to a tmp buffer,
// which will also verify output format is valid,
// and avoid creating empty files in case this goes wrong
data := &render.Context{
Env: ctx.Env,
Prefix: ctx.Prefix,
GlobalGA: ctx.MainGA,
Meta: &clab.Meta,
Steps: clab.Steps,
Extra: extraVars,
}
var buf bytes.Buffer
if err := render.Execute(&buf, ctx.Format, data); err != nil {
return err
}
// output to stdout does not include metadata
w := os.Stdout
if !isStdout(dir) {
// make sure codelab dir exists
if err := os.MkdirAll(dir, 0755); err != nil {
Expand All @@ -134,16 +117,58 @@ func writeCodelab(dir string, clab *types.Codelab, ctx *types.Context) error {
if err := writeMeta(f, cm); err != nil {
return err
}
// main content file
f = filepath.Join(dir, contentFile(ctx.Format))
var err error
if w, err = os.Create(f); err != nil {
}

// main content file(s)
data := &struct {
render.Context
Current *types.Step
StepNum int
Prev bool
Next bool
}{Context: render.Context{
Env: ctx.Env,
Prefix: ctx.Prefix,
GlobalGA: ctx.MainGA,
Meta: &clab.Meta,
Steps: clab.Steps,
Extra: extraVars,
}}
if ctx.Format != "offline" {
w := os.Stdout
if !isStdout(dir) {
f, err := os.Create(filepath.Join(dir, "index."+ctx.Format))
if err != nil {
return err
}
w = f
defer f.Close()
}
return render.Execute(w, ctx.Format, data)
}
for i, step := range clab.Steps {
data.Current = step
data.StepNum = i + 1
data.Prev = i > 0
data.Next = i < len(clab.Steps)-1
w := os.Stdout
if !isStdout(dir) {
name := "index.html"
if i > 0 {
name = fmt.Sprintf("step-%d.html", i+1)
}
f, err := os.Create(filepath.Join(dir, name))
if err != nil {
return err
}
w = f
defer f.Close()
}
if err := render.Execute(w, ctx.Format, data); err != nil {
return err
}
defer w.Close()
}
_, err := w.Write(buf.Bytes())
return err
return nil
}

func slurpImages(client *http.Client, dir string, steps []*types.Step) (map[string]string, error) {
Expand Down
12 changes: 4 additions & 8 deletions claat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ const (
// imgDirname is where a codelab images are stored,
// relative to the codelab dir.
imgDirname = "img"
// contentFilename is the name of file for codelab content output,
// without the format extension.
contentFilename = "index"
// metaFilename is codelab metadata file.
metaFilename = "codelab.json"
// stdout is a special value for -o cli arg to identify stdout writer.
Expand Down Expand Up @@ -77,11 +74,6 @@ func isStdout(filename string) bool {
return filename == stdout
}

// contentFile returns codelab main output file given the specified format.
func contentFile(format string) string {
return contentFilename + "." + format
}

// printf prints formatted string fmt with args to stderr.
func printf(format string, args ...interface{}) {
log.Printf(format, args...)
Expand Down Expand Up @@ -153,13 +145,17 @@ Export takes one or more 'src' documents and converts them
to the format specified with -f option.
The following formats are built-in:
- html (Polymer-based app)
- md (Markdown)
- offline (plain HTML markup for offline consumption)
To use a custom format, specify a local file path to a Go template file.
More info on Go templates: https://golang.org/pkg/text/template/.
Each 'src' can be either a remote HTTP resource or a local file.
Source formats currently supported are:
- Google Doc (Codelab Format, go/codelab-guide)
- Markdown
Expand Down
5 changes: 3 additions & 2 deletions claat/render/gen-tmpldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ var files = map[string]struct {
file string
html bool
}{
"html": {"template.html", true},
"md": {"template.md", false},
"html": {"template.html", true},
"md": {"template.md", false},
"offline": {"template-offline.html", true},
}

func main() {
Expand Down
78 changes: 78 additions & 0 deletions claat/render/template-offline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!--
Copyright (c) 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-->

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>{{.Meta.Title}}</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Code+Pro:400|Roboto:400,300,400italic,500,700|Roboto+Mono">
<link rel="stylesheet" href="{{.Prefix}}styles/codelab.css">
<style>
html {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>

<body class="codelab-takeover">
<div class="codelab__toc">{{range $i, $t := .Steps}}
<a href="{{inc $i | stepLink}}" class="{{inc $i | tocItemClass $.StepNum}}">
<span class="toc-item__index">{{inc $i}}</span>
<span class="toc-item__title">{{$t.Title}}</span>
</a>{{end}}
</div>

<div class="codelab__step">

<div class="step__header">
<a href="{{dec .StepNum | stepLink}}"{{if not .Prev}} class="invis"{{end}}>
<svg fill="#FFFFFF" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"/>
</svg>
</a>

<a href="{{.Prefix}}index.html" title="Return to home page">
<svg fill="#FFFFFF" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>
<path d="M0 0h24v24H0z" fill="none"/>
</svg>
</a>

<a href="{{inc .StepNum | stepLink}}"{{if not .Next}} class="invis"{{end}}>
<svg fill="#FFFFFF" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M12 4l-1.41 1.41L16.17 11H4v2h12.17l-5.58 5.59L12 20l8-8z"/>
</svg>
</a>

<h1>{{.Meta.Title}}</h1>
</div>

<div class="step__body">
<h1>{{.Meta.Title}}</h1>
<h2>{{.StepNum}}. {{.Current.Title}}</h2>
{{.Current.Content | renderLite .Env}}
</div>

</div><!-- codelab__toc -->
</body>
</html>
23 changes: 23 additions & 0 deletions claat/render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package render

import (
"fmt"
htmlTemplate "html/template"
"io"
"io/ioutil"
Expand Down Expand Up @@ -77,6 +78,28 @@ var funcMap = map[string]interface{}{
i := sort.SearchStrings(tags, t)
return i < len(tags) && tags[i] == t
},
// lite/offline versions; multiple step files
"inc": func(n int) int {
return n + 1
},
"dec": func(n int) int {
return n - 1
},
"tocItemClass": func(curr, n int) string {
a := "toc-item"
if n < curr {
a += " toc-item--complete"
} else if curr == n {
a += " toc-item--current"
}
return a
},
"stepLink": func(n int) string {
if n <= 1 {
return "index.html"
}
return fmt.Sprintf("step-%d.html", n)
},
}

//go:generate go run gen-tmpldata.go
Expand Down
Loading

0 comments on commit 58cc95b

Please sign in to comment.