ebiten/_docs/gen.go

290 lines
7.2 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"
"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"
2018-10-15 17:47:29 +02:00
"sort"
2014-12-29 15:16:02 +01:00
"strings"
"sync"
2014-12-27 16:26:33 +01:00
)
2017-08-18 16:43:08 +02:00
const (
2018-07-28 12:00:05 +02:00
url = "https://hajimehoshi.github.io/ebiten/"
licenseYear = 2013
2017-08-18 16:43:08 +02:00
)
var (
2018-10-15 17:47:29 +02:00
examplesDir = filepath.Join("public", "examples")
copyright = fmt.Sprintf("© %d Hajime Hoshi", licenseYear)
2016-08-27 18:30:06 +02:00
stableVersion = ""
2018-10-15 17:47:29 +02:00
rcVersion = ""
2016-08-27 18:30:06 +02:00
devVersion = ""
)
2014-12-27 16:26:33 +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)
}
2018-10-15 17:47:29 +02:00
vers := strings.Split(strings.TrimSpace(string(b)), "\n")
// TODO: Sort by a semantic version lib
sort.Strings(vers)
devVers := []string{}
rcVers := []string{}
stableVers := []string{}
for _, ver := range vers {
if strings.Index(ver, "-rc") != -1 {
rcVers = append(rcVers, ver)
continue
}
2018-10-15 17:47:29 +02:00
if strings.Index(ver, "-") != -1 {
devVers = append(devVers, ver)
continue
}
2018-10-15 17:47:29 +02:00
stableVers = append(stableVers, ver)
}
2014-12-29 15:52:37 +01:00
2018-10-15 17:47:29 +02:00
stableVersion = stableVers[len(stableVers)-1]
rcVersion = rcVers[len(rcVers)-1]
if rcVersion[:strings.Index(rcVersion, "-")] == stableVersion {
rcVersion = ""
2014-12-29 15:52:37 +01:00
}
2018-10-15 17:47:29 +02:00
devVersion = devVers[len(devVers)-1]
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
}
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-04-08 13:54:39 +02:00
{Name: "flappy", ThumbWidth: 320, ThumbHeight: 240},
2018-01-31 20:34:38 +01:00
}
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-10-10 18:39:47 +02:00
{Name: "animation", ThumbWidth: 320, ThumbHeight: 240},
2018-09-30 16:21:25 +02:00
{Name: "blur", ThumbWidth: 320, ThumbHeight: 240},
2018-05-12 10:26:37 +02:00
{Name: "drag", 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},
2018-09-25 19:32:24 +02:00
{Name: "polygons", ThumbWidth: 320, ThumbHeight: 240},
2018-01-31 20:34:38 +01:00
{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-09-30 16:21:25 +02:00
{Name: "wheel", 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
}
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,
2018-10-15 17:47:29 +02:00
"RCVersion": rcVersion,
2018-01-30 17:18:33 +01:00
"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
}
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)
}
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 := 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
}