aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: e84538c5ef35ca733ed59279dce73e3e933ac422 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main

import (
	"fmt"
	"os"

	"git.sr.ht/~mango/opts/v2"
	"git.thomasvoss.com/gsp/formatter"
	"git.thomasvoss.com/gsp/parser"
)

var dflag bool

func main() {
	flags, rest, err := opts.GetLong(os.Args, []opts.LongOpt{
		{Short: 'd', Long: "no-doctype", Arg: opts.None},
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err)
		fmt.Fprintf(os.Stderr, "Usage: %s [-d] [file ...]\n", os.Args[0])
		os.Exit(1)
	}

	for _, f := range flags {
		switch f.Key {
		case 'd':
			dflag = true
		}
	}

	if len(rest) == 0 {
		process("-")
	}

	for _, a := range rest {
		process(a)
	}
}

func process(filename string) {
	var file *os.File
	var err error

	if filename == "-" {
		file = os.Stdin
	} else {
		file, err = os.Open(filename)
		if err != nil {
			die(err)
		}
		defer file.Close()
	}

	ast, err := parser.ParseFile(file)
	if err != nil {
		die(err)
	}

	if !dflag {
		fmt.Print("<!DOCTYPE html>")
	}
	formatter.PrintAst(ast)
	fmt.Print("\n")
}

func die(e error) {
	fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], e)
	os.Exit(1)
}