ebiten/_docs/gen.go

283 lines
5.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"
2015-01-08 15:45:30 +01:00
"time"
2014-12-27 16:26:33 +01:00
)
2014-12-29 15:16:02 +01:00
var license = ""
2014-12-27 16:26:33 +01:00
2014-12-29 15:16:02 +01:00
func init() {
b, err := ioutil.ReadFile("../license.txt")
if err != nil {
panic(err)
}
2015-01-08 17:00:30 +01:00
license = strings.TrimSpace(string(b))
2014-12-29 15:16:02 +01:00
2015-01-08 17:50:11 +01:00
year, err := strconv.Atoi(regexp.MustCompile(`^Copyright (\d+)`).FindStringSubmatch(license)[1])
if err != nil {
panic(err)
}
if year != time.Now().Year() {
panic("the license's year is not this year")
}
2014-12-29 15:16:02 +01:00
}
2014-12-27 16:26:33 +01:00
2015-01-08 15:45:30 +01:00
var copyright = ""
func init() {
copyright = fmt.Sprintf("© %d Hajime Hoshi", time.Now().Year())
}
2014-12-29 15:16:02 +01:00
var stableVersion = ""
2014-12-27 16:26:33 +01:00
2014-12-29 15:16:02 +01:00
var devVersion = ""
func init() {
b, err := ioutil.ReadFile("../version.txt")
if err != nil {
panic(err)
}
2014-12-29 15:52:37 +01:00
stableVersion = strings.TrimSpace(string(b))
}
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 {
// TODO: text should be escaped
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 {
Name string
}
2015-01-08 15:45:30 +01:00
func (e *example) ThumbWidth() int {
2014-12-29 10:43:35 +01:00
if e.Name == "blocks" {
return 256
}
return 320
}
2015-01-08 15:45:30 +01:00
func (e *example) ThumbHeight() int {
2014-12-29 10:43:35 +01:00
if e.Name == "blocks" {
return 240
}
return 240
}
2015-01-08 15:45:30 +01:00
func (e *example) Width() int {
return e.ThumbWidth() * 2
}
func (e *example) Height() int {
return e.ThumbHeight() * 2
}
2014-12-27 22:18:23 +01:00
func (e *example) Source() string {
2014-12-29 10:43:35 +01:00
if e.Name == "blocks" {
return "// Please read example/blocks/main.go and example/blocks/blocks/*.go"
}
2014-12-27 22:18:23 +01:00
path := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "hajimehoshi", "ebiten", "example", e.Name, "main.go")
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), "")
2015-01-06 15:04:57 +01: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
}
2015-01-05 16:44:39 +01:00
var examples = []example{
{Name: "hue"},
2015-01-08 15:45:30 +01:00
{Name: "keyboard"},
2015-01-05 16:44:39 +01:00
{Name: "mosaic"},
2015-01-08 18:39:20 +01:00
{Name: "paint"},
2015-01-05 16:44:39 +01:00
{Name: "perspective"},
{Name: "rotate"},
2015-01-08 15:45:30 +01:00
{Name: "blocks"},
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
}
// Remove auto-generated html files.
m, err := regexp.MatchString(".html$", path)
if err != nil {
return err
}
if !m {
return nil
}
// Remove example resources that are copied.
m, err = regexp.MatchString("public/example/images", path)
if err != nil {
return err
}
if !m {
return nil
}
return os.Remove(path)
}); 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{}{
2014-12-29 15:52:37 +01:00
"License": license,
2015-01-08 15:45:30 +01:00
"Copyright": copyright,
2014-12-29 15:52:37 +01:00
"StableVersion": stableVersion,
"DevVersion": devVersion,
"Examples": examples,
2014-12-27 16:26:33 +01:00
}
2015-01-05 16:44:39 +01:00
return t.Funcs(funcs).Execute(f, data)
}
2015-01-08 15:45:30 +01:00
func outputExampleContent(e *example) error {
2015-01-05 16:44:39 +01:00
const dir = "public/example"
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
2015-01-08 15:45:30 +01:00
f, err := os.Create(filepath.Join(dir, 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{}{
2015-01-08 15:45:30 +01:00
"License": license,
"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(dir, e.Name+".js")
path := "github.com/hajimehoshi/ebiten/example/" + e.Name
if err := exec.Command("gopherjs", "build", "-m", "-o", out, path).Run(); err != nil {
return err
}
return nil
}
2015-01-08 15:45:30 +01:00
func outputExample(e *example) error {
const dir = "public/example"
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
f, err := os.Create(filepath.Join(dir, e.Name+".html"))
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{}{
"License": license,
"Copyright": copyright,
"Example": e,
}
return t.Funcs(funcs).Execute(f, data)
}
2015-01-05 16:44:39 +01:00
func main() {
if err := clear(); err != nil {
log.Fatal(err)
}
if err := outputMain(); err != nil {
2014-12-27 16:26:33 +01:00
log.Fatal(err)
}
2015-01-05 16:44:39 +01:00
for _, e := range examples {
2015-01-08 15:45:30 +01:00
if err := outputExampleContent(&e); err != nil {
log.Fatal(err)
}
2015-01-05 16:44:39 +01:00
if err := outputExample(&e); err != nil {
log.Fatal(err)
}
}
2014-12-27 16:26:33 +01:00
}