File: main.go

package info (click to toggle)
golang-github-yosssi-ace 0.0.4%2Bgit20160102.51.71afeb7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 468 kB
  • ctags: 249
  • sloc: makefile: 3; sh: 1
file content (90 lines) | stat: -rw-r--r-- 1,861 bytes parent folder | download | duplicates (3)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/yosssi/ace"
	"github.com/yosssi/gohtml"
)

var (
	noFormat bool
	lineNo   bool
)

func compileResultFromStdin() (string, error) {
	b, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return "", err
	}
	name, baseFile := "stdin", "stdin.ace"
	base := ace.NewFile(baseFile, b)
	inner := ace.NewFile("", []byte{})

	src := ace.NewSource(base, inner, []*ace.File{})
	rslt, err := ace.ParseSource(src, nil)
	if err != nil {
		return "", err
	}

	tpl, err := ace.CompileResult(name, rslt, nil)
	if err != nil {
		return "", err
	}
	return tpl.Lookup(name).Tree.Root.String(), nil
}

func compileResultFromFile(baseFile, innerFile string) (string, error) {
	base := baseFile[:len(baseFile)-len(filepath.Ext(baseFile))]

	var inner string
	if len(innerFile) > 0 {
		inner = innerFile[:len(innerFile)-len(filepath.Ext(innerFile))]
	}
	name := base + ":" + inner

	tpl, err := ace.Load(base, inner, nil)
	if err != nil {
		return "", err
	}
	return tpl.Lookup(name).Tree.Root.String(), nil
}

func main() {
	flag.BoolVar(&noFormat, "no-format", false, "output HTML without format")
	flag.BoolVar(&lineNo, "lineno", false, "output formatted HTML with line numbers")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage:\n  %s [options] [base.ace] [inner.ace]\n\nOptions:\n", os.Args[0])
		flag.PrintDefaults()
	}
	flag.Parse()

	var (
		compiled string
		err      error
	)
	baseFile := flag.Arg(0)
	if len(baseFile) == 0 {
		compiled, err = compileResultFromStdin()
	} else {
		compiled, err = compileResultFromFile(baseFile, flag.Arg(1))
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	if noFormat {
		fmt.Println(compiled)
	} else {
		if lineNo {
			fmt.Println(gohtml.FormatWithLineNo(compiled))
		} else {
			fmt.Println(gohtml.Format(compiled))
		}
	}
}