github.com/sercand/please@v13.4.0+incompatible/docs/template_lexicon.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	htmltemplate "html/template"
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  	"text/template"
    10  )
    11  
    12  type rules struct {
    13  	Functions map[string]rule `json:"functions"`
    14  }
    15  
    16  // Named returns the rule with given name.
    17  func (r *rules) Named(name string) rule {
    18  	rule, present := r.Functions[name]
    19  	if !present {
    20  		panic("unknown function " + name)
    21  	}
    22  	rule.Name = name // Not actually stored in the JSON, but useful in the template.
    23  	return rule
    24  }
    25  
    26  type rule struct {
    27  	Args []struct {
    28  		Comment    string   `json:"comment"`
    29  		Deprecated bool     `json:"deprecated"`
    30  		Name       string   `json:"name"`
    31  		Required   bool     `json:"required"`
    32  		Types      []string `json:"types"`
    33  	} `json:"args"`
    34  	Aliases   []string `json:"aliases"`
    35  	Docstring string   `json:"docstring"`
    36  	Comment   string   `json:"comment"`
    37  	Language  string   `json:"language"`
    38  	Name      string
    39  }
    40  
    41  func must(err error) {
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  }
    46  
    47  func main() {
    48  	tmpl, err := template.New("lexicon.html").Funcs(template.FuncMap{
    49  		"join": strings.Join,
    50  		"newlines": func(s string) string {
    51  			return strings.Replace(htmltemplate.HTMLEscapeString(s), "\n", "<br/>", -1)
    52  		},
    53  	}).ParseFiles(
    54  		"docs/lexicon.html", "docs/lexicon_entry.html")
    55  	must(err)
    56  	b, err := ioutil.ReadFile("docs/rules.json")
    57  	must(err)
    58  	r := &rules{}
    59  	must(json.Unmarshal(b, r))
    60  	must(tmpl.Execute(os.Stdout, r))
    61  }