Sfoglia il codice sorgente

generate list of language options automatically

Lucas Stadler 11 anni fa
parent
commit
0eb402c5da
1 ha cambiato i file con 17 aggiunte e 10 eliminazioni
  1. 17 10
      go/linguaevalia/linguaevalia.go

+ 17 - 10
go/linguaevalia/linguaevalia.go

@ -16,23 +16,26 @@ import (
16 16
17 17
type Language interface {
18 18
	RunFile(f *os.File) (result []byte, err error)
19
	Name() string
19 20
	Extension() string
20 21
}
21 22
22 23
type LanguageGeneral struct {
23
	Name    string
24
	Ext     string
25
	Command string
26
	Args    []string
24
	name    string
25
	ext     string
26
	command string
27
	args    []string
27 28
}
28 29
29 30
func (l LanguageGeneral) RunFile(f *os.File) ([]byte, error) {
30
	args := append(l.Args, f.Name())
31
	cmd := exec.Command(l.Command, args...)
31
	args := append(l.args, f.Name())
32
	cmd := exec.Command(l.command, args...)
32 33
	return cmd.CombinedOutput()
33 34
}
34 35
35
func (l LanguageGeneral) Extension() string { return l.Ext }
36
func (l LanguageGeneral) Name() string { return l.name }
37
38
func (l LanguageGeneral) Extension() string { return l.ext }
36 39
37 40
var Go = LanguageGeneral{"Go", "go", "go", []string{"run"}}
38 41
var Python = LanguageGeneral{"Python", "py", "python", []string{}}
@ -107,7 +110,10 @@ func getLanguage(r *http.Request) Language {
107 110
}
108 111
109 112
func homePageHandler(w http.ResponseWriter, r *http.Request) {
110
	homePageTemplate.Execute(w, nil)
113
	bindings := map[string]interface{}{
114
		"languages": languageMappings,
115
	}
116
	homePageTemplate.Execute(w, bindings)
111 117
}
112 118
113 119
var homePageTemplate = template.Must(template.New("homepage").Parse(homePageTemplateStr))
@ -164,8 +170,9 @@ func main() {
164 170
}
165 171
</textarea>
166 172
      <select id="language">
167
        <option value="go">Go</option>
168
        <option value="python">Python</option>
173
        {{ range $short, $name := .languages }}
174
        <option value="{{ $short }}">{{ $name.Name }}</option>
175
        {{ end }}
169 176
      </select>
170 177
    </div>
171 178
    <pre id="result"></pre>