ebiten/_docs/gen.go

407 lines
9.7 KiB
Go
Raw Normal View History

2014-12-27 16:26:33 +01:00
// Copyright 2014 Hajime Hoshi
//
// 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
//
2015-01-08 15:45:30 +01:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-12-27 16:26:33 +01:00
//
// 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.
// +build ignore
package main
import (
2014-12-29 15:52:37 +01:00
"fmt"
2014-12-27 16:26:33 +01:00
"html/template"
"io/ioutil"
"log"
"os"
2014-12-29 15:52:37 +01:00
"os/exec"
2014-12-27 16:26:33 +01:00
"path/filepath"
2014-12-27 22:18:23 +01:00
"regexp"
2015-01-08 17:50:11 +01:00
"strconv"
2014-12-29 15:16:02 +01:00
"strings"
"sync"
2016-10-20 17:12:31 +02:00
"github.com/hajimehoshi/ebiten/internal"
2014-12-27 16:26:33 +01:00
)
2017-08-18 16:43:08 +02:00
const (
url = "https://hajimehoshi.github.io/ebiten/"
)
var (
examplesDir = filepath.Join("public", "examples")
)
2016-02-10 19:07:14 +01:00
func execute(command string, args ...string) error {
cmd := exec.Command(command, args...)
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
msg, err := ioutil.ReadAll(stderr)
if err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("%v: %s", err, string(msg))
}
return nil
}
2016-08-27 18:30:06 +02:00
var (
copyright = ""
stableVersion = ""
devVersion = ""
)
2014-12-27 16:26:33 +01:00
2014-12-29 15:16:02 +01:00
func init() {
2016-10-20 17:12:31 +02:00
year, err := internal.LicenseYear()
2016-02-05 15:31:35 +01:00
if err != nil {
panic(err)
}
copyright = fmt.Sprintf("© %d Hajime Hoshi", year)
2015-01-08 15:45:30 +01:00
}
2014-12-29 15:16:02 +01:00
func init() {
b, err := exec.Command("git", "tag").Output()
2014-12-29 15:16:02 +01:00
if err != nil {
panic(err)
}
lastStableVersion := ""
lastCommitTime := 0
for _, tag := range strings.Split(string(b), "\n") {
m := regexp.MustCompile(`^v(\d.+)$`).FindStringSubmatch(tag)
if m == nil {
continue
}
t, err := exec.Command("git", "log", tag, "-1", "--format=%ct").Output()
if err != nil {
panic(err)
}
tt, err := strconv.Atoi(strings.TrimSpace(string(t)))
if err != nil {
panic(err)
}
if lastCommitTime >= tt {
continue
}
lastCommitTime = tt
lastStableVersion = m[1]
}
// See the HEAD commit time
stableVersion = lastStableVersion
2014-12-29 15:52:37 +01:00
}
func init() {
b, err := exec.Command("git", "show", "master:version.txt").Output()
if err != nil {
panic(err)
}
devVersion = strings.TrimSpace(string(b))
2014-12-29 15:16:02 +01:00
}
2014-12-27 16:26:33 +01:00
func comment(text string) template.HTML {
2015-02-01 18:34:01 +01:00
// http://www.w3.org/TR/html-markup/syntax.html#comments
// The text part of comments has the following restrictions:
// * must not start with a ">" character
// * must not start with the string "->"
// * must not contain the string "--"
// * must not end with a "-" character
for strings.HasPrefix(text, ">") {
text = text[1:]
}
for strings.HasPrefix(text, "->") {
text = text[2:]
}
text = strings.Replace(text, "--", "", -1)
for strings.HasSuffix(text, "-") {
text = text[:len(text)-1]
}
2014-12-29 15:52:37 +01:00
return template.HTML("<!--\n" + text + "\n-->")
2014-12-27 16:26:33 +01:00
}
func safeHTML(text string) template.HTML {
return template.HTML(text)
}
2014-12-27 22:18:23 +01:00
type example struct {
2018-01-31 20:34:38 +01:00
Name string
ThumbWidth int
ThumbHeight int
ScreenWidth int
ScreenHeight int
2014-12-29 10:43:35 +01:00
}
2015-01-08 15:45:30 +01:00
func (e *example) Width() int {
2018-01-31 20:34:38 +01:00
if e.ScreenWidth == 0 {
return e.ThumbWidth * 2
}
return e.ScreenWidth
2015-01-08 15:45:30 +01:00
}
func (e *example) Height() int {
2018-01-31 20:34:38 +01:00
if e.ScreenHeight == 0 {
return e.ThumbHeight * 2
}
return e.ScreenHeight
2015-01-08 15:45:30 +01:00
}
2014-12-27 22:18:23 +01:00
func (e *example) Source() string {
2017-06-24 17:48:11 +02:00
const (
commentFor2048 = `// Please read examples/2048/main.go and examples/2048/2048/*.go`
commentForBlocks = `// Please read examples/blocks/main.go and examples/blocks/blocks/*.go
// NOTE: If Gamepad API is available in your browswer, you can use gamepads. Try it out!`
)
if e.Name == "2048" {
return commentFor2048
}
2014-12-29 10:43:35 +01:00
if e.Name == "blocks" {
2016-02-15 19:04:44 +01:00
return commentForBlocks
2014-12-29 10:43:35 +01:00
}
2016-02-10 19:07:14 +01:00
path := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "hajimehoshi", "ebiten", "examples", e.Name, "main.go")
2014-12-27 22:18:23 +01:00
b, err := ioutil.ReadFile(path)
2014-12-27 16:26:33 +01:00
if err != nil {
2014-12-27 22:18:23 +01:00
panic(err)
2014-12-27 16:26:33 +01:00
}
2014-12-27 22:18:23 +01:00
str := regexp.MustCompile("(?s)^.*?\n\n").ReplaceAllString(string(b), "")
2016-08-26 22:36:52 +02:00
str = strings.Replace(str, "\t", " ", -1)
2014-12-27 22:18:23 +01:00
return str
}
2014-12-29 15:16:02 +01:00
func versions() string {
2014-12-29 15:52:37 +01:00
return fmt.Sprintf("v%s (dev: v%s)", stableVersion, devVersion)
2014-12-29 15:16:02 +01:00
}
2018-01-30 17:18:33 +01:00
var (
2018-01-31 20:34:38 +01:00
gamesExamples = []example{
{Name: "2048", ThumbWidth: 210, ThumbHeight: 300},
{Name: "blocks", ThumbWidth: 256, ThumbHeight: 240},
}
2018-01-30 17:18:33 +01:00
graphicsExamples = []example{
2018-03-11 17:33:52 +01:00
{Name: "airship", ThumbWidth: 320, ThumbHeight: 240},
2018-01-31 20:34:38 +01:00
{Name: "alphablending", ThumbWidth: 320, ThumbHeight: 240},
2018-02-14 02:38:26 +01:00
{Name: "filter", ThumbWidth: 320, ThumbHeight: 240},
2018-01-31 20:34:38 +01:00
{Name: "flood", ThumbWidth: 320, ThumbHeight: 240},
{Name: "font", ThumbWidth: 320, ThumbHeight: 240},
{Name: "highdpi", ThumbWidth: 320, ThumbHeight: 240},
{Name: "hsv", ThumbWidth: 320, ThumbHeight: 240},
{Name: "hue", ThumbWidth: 320, ThumbHeight: 240},
{Name: "infinitescroll", ThumbWidth: 320, ThumbHeight: 240},
{Name: "life", ThumbWidth: 320, ThumbHeight: 240},
{Name: "mandelbrot", ThumbWidth: 320, ThumbHeight: 320, ScreenWidth: 640, ScreenHeight: 640},
{Name: "masking", ThumbWidth: 320, ThumbHeight: 240},
{Name: "mosaic", ThumbWidth: 320, ThumbHeight: 240},
{Name: "noise", ThumbWidth: 320, ThumbHeight: 240},
{Name: "paint", ThumbWidth: 320, ThumbHeight: 240},
{Name: "perspective", ThumbWidth: 320, ThumbHeight: 240},
{Name: "rotate", ThumbWidth: 320, ThumbHeight: 240},
{Name: "sprites", ThumbWidth: 320, ThumbHeight: 240},
2018-02-05 19:59:10 +01:00
{Name: "tiles", ThumbWidth: 240, ThumbHeight: 240},
2018-01-30 17:18:33 +01:00
}
inputExamples = []example{
2018-01-31 20:34:38 +01:00
{Name: "gamepad", ThumbWidth: 320, ThumbHeight: 240},
{Name: "keyboard", ThumbWidth: 320, ThumbHeight: 240},
{Name: "typewriter", ThumbWidth: 320, ThumbHeight: 240},
2018-01-30 17:18:33 +01:00
}
audioExamples = []example{
2018-01-31 20:34:38 +01:00
{Name: "audio", ThumbWidth: 320, ThumbHeight: 240},
{Name: "piano", ThumbWidth: 320, ThumbHeight: 240},
{Name: "sinewave", ThumbWidth: 320, ThumbHeight: 240},
2018-01-30 17:18:33 +01:00
}
)
2015-01-05 16:44:39 +01:00
func clear() error {
2015-01-08 15:45:30 +01:00
if err := filepath.Walk("public", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if m, _ := regexp.MatchString("~$", path); m {
return nil
}
2015-01-08 15:45:30 +01:00
// Remove auto-generated html files.
m, err := regexp.MatchString(".html$", path)
if err != nil {
return err
}
if m {
return os.Remove(path)
2015-01-08 15:45:30 +01:00
}
// Remove example resources that are copied.
2016-02-20 21:40:56 +01:00
m, err = regexp.MatchString("^public/examples/_resources/images$", path)
2015-01-08 15:45:30 +01:00
if err != nil {
return err
}
if m {
if err := os.RemoveAll(path); err != nil {
return err
}
return filepath.SkipDir
2015-01-08 15:45:30 +01:00
}
return nil
2015-01-08 15:45:30 +01:00
}); err != nil {
return err
}
2015-01-05 16:44:39 +01:00
return nil
}
func outputMain() error {
f, err := os.Create("public/index.html")
2014-12-27 16:26:33 +01:00
if err != nil {
2015-01-05 16:44:39 +01:00
return err
2014-12-27 16:26:33 +01:00
}
2014-12-27 22:18:23 +01:00
defer f.Close()
2014-12-27 16:26:33 +01:00
funcs := template.FuncMap{
"comment": comment,
"safeHTML": safeHTML,
}
2015-01-05 16:44:39 +01:00
const templatePath = "index.tmpl.html"
2014-12-27 16:26:33 +01:00
name := filepath.Base(templatePath)
t, err := template.New(name).Funcs(funcs).ParseFiles(templatePath)
if err != nil {
2015-01-05 16:44:39 +01:00
return err
2014-12-27 16:26:33 +01:00
}
2014-12-29 15:16:02 +01:00
2014-12-27 22:18:23 +01:00
data := map[string]interface{}{
2018-01-30 17:18:33 +01:00
"URL": url,
"Copyright": copyright,
"StableVersion": stableVersion,
"DevVersion": devVersion,
"GraphicsExamples": graphicsExamples,
"InputExamples": inputExamples,
"AudioExamples": audioExamples,
2018-01-31 20:34:38 +01:00
"GamesExamples": gamesExamples,
2014-12-27 16:26:33 +01:00
}
2015-01-05 16:44:39 +01:00
return t.Funcs(funcs).Execute(f, data)
}
func createExamplesDir() error {
if err := os.RemoveAll(examplesDir); err != nil {
return err
}
if err := os.MkdirAll(examplesDir, 0755); err != nil {
return err
}
return nil
}
func outputExampleResources() error {
2016-02-06 21:50:41 +01:00
// TODO: Using cp command might not be portable.
// Use io.Copy instead.
if err := execute("cp", "-R", filepath.Join("..", "examples", "_resources"), filepath.Join(examplesDir, "_resources")); err != nil {
2016-02-10 19:07:14 +01:00
return err
}
return nil
2016-02-06 21:50:41 +01:00
}
2015-01-08 15:45:30 +01:00
func outputExampleContent(e *example) error {
f, err := os.Create(filepath.Join(examplesDir, e.Name+".content.html"))
2015-01-05 16:44:39 +01:00
if err != nil {
return err
}
defer f.Close()
funcs := template.FuncMap{
"comment": comment,
"safeHTML": safeHTML,
}
2015-01-08 15:45:30 +01:00
const templatePath = "examplecontent.tmpl.html"
2015-01-05 16:44:39 +01:00
name := filepath.Base(templatePath)
t, err := template.New(name).Funcs(funcs).ParseFiles(templatePath)
if err != nil {
return err
}
data := map[string]interface{}{
2016-08-27 19:17:00 +02:00
"Copyright": copyright,
"Example": e,
2015-01-05 16:44:39 +01:00
}
2014-12-27 16:26:33 +01:00
if err := t.Funcs(funcs).Execute(f, data); err != nil {
2015-01-05 16:44:39 +01:00
return err
}
out := filepath.Join(examplesDir, e.Name+".js")
2016-02-10 19:07:14 +01:00
path := "github.com/hajimehoshi/ebiten/examples/" + e.Name
2016-08-27 20:52:00 +02:00
if err := execute("gopherjs", "build", "--tags", "example", "-m", "-o", out, path); err != nil {
2015-01-05 16:44:39 +01:00
return err
}
return nil
}
2015-01-08 15:45:30 +01:00
func outputExample(e *example) error {
f, err := os.Create(filepath.Join(examplesDir, e.Name+".html"))
2015-01-08 15:45:30 +01:00
if err != nil {
return err
}
defer f.Close()
funcs := template.FuncMap{
"comment": comment,
"safeHTML": safeHTML,
}
const templatePath = "example.tmpl.html"
name := filepath.Base(templatePath)
t, err := template.New(name).Funcs(funcs).ParseFiles(templatePath)
if err != nil {
return err
}
data := map[string]interface{}{
2017-08-18 16:43:08 +02:00
"URL": url,
2015-01-08 15:45:30 +01:00
"Copyright": copyright,
"Example": e,
}
return t.Funcs(funcs).Execute(f, data)
}
2015-01-05 16:44:39 +01:00
func main() {
2017-06-24 17:48:11 +02:00
if err := clear(); err != nil {
2015-01-05 16:44:39 +01:00
log.Fatal(err)
2017-06-24 17:48:11 +02:00
}
2015-01-05 16:44:39 +01:00
if err := outputMain(); err != nil {
2014-12-27 16:26:33 +01:00
log.Fatal(err)
}
if err := createExamplesDir(); err != nil {
log.Fatal(err)
}
if err := outputExampleResources(); err != nil {
2016-02-06 21:50:41 +01:00
log.Fatal(err)
}
2018-01-30 17:18:33 +01:00
examples := []example{}
examples = append(examples, graphicsExamples...)
examples = append(examples, inputExamples...)
examples = append(examples, audioExamples...)
2018-01-31 20:34:38 +01:00
examples = append(examples, gamesExamples...)
wg := sync.WaitGroup{}
2015-01-05 16:44:39 +01:00
for _, e := range examples {
e := e
wg.Add(1)
go func() {
defer wg.Done()
if err := outputExampleContent(&e); err != nil {
log.Fatal(err)
}
if err := outputExample(&e); err != nil {
log.Fatal(err)
}
}()
2015-01-05 16:44:39 +01:00
}
wg.Wait()
2014-12-27 16:26:33 +01:00
}