コード例 #1
0
ファイル: test1.go プロジェクト: pschlump/css
func main() {

	_, err := flags.ParseArgs(&opts, os.Args)

	if err != nil {
		fmt.Printf("Invalid Command Line: %s\n", err)
		os.Exit(1)
	}

	if opts.CssFileName == "" {
		fmt.Printf("Usage test1 -s File.css\n")
		os.Exit(1)
	}

	myCSS, err := ioutil.ReadFile(opts.CssFileName)
	if err != nil {
		fmt.Printf("Errror: %s\n", err)
		os.Exit(1)
	}

	s := scanner.New(string(myCSS))
	for {
		token := s.Next()
		if token.Type == scanner.TokenEOF || token.Type == scanner.TokenError {
			break
		}
		// Do something with the token...
		fmt.Printf("%+v\n", token)
	}

}
コード例 #2
0
ファイル: main.go プロジェクト: pschlump/css-pack
func main() {

	_, err := flags.ParseArgs(&opts, os.Args)

	if err != nil {
		fmt.Printf("Invalid Command Line: %s\n", err)
		os.Exit(1)
	}

	if opts.CssFileName == "" {
		fmt.Printf("Usage test1 -s File.css\n")
		os.Exit(1)
	}

	myCSS, err := ioutil.ReadFile(opts.CssFileName)
	if err != nil {
		fmt.Printf("Errror: %s\n", err)
		os.Exit(1)
	}

	o := ""
	d := ""
	atKeyword := 0
	atImport := 0

	s := scanner.New(string(myCSS))
	for {
		token := s.Next()
		if token.Type == scanner.TokenError {
			fmt.Printf("Syntax Error: %s\n", token)
			os.Exit(2)
		} else if token.Type == scanner.TokenEOF {
			break
		}

		if opts.Debug {
			fmt.Printf("%+v\n", token)
		}

		switch token.Type {

		case scanner.TokenComment:
		case scanner.TokenS:

		case scanner.TokenAtKeyword:
			o += token.Value
			atKeyword = 1
			if token.Value == "@import" {
				atImport = 1
			}
		case scanner.TokenString:
			o += token.Value
			if atImport == 1 {
				token.Type = scanner.TokenImportURI
				if opts.Deps != "" {
					d += "@import " + token.Value + "\n"
				}
			}
			atKeyword, atImport = 0, 0

		case scanner.TokenIdent:
			fallthrough
		case scanner.TokenHash:
			fallthrough
		case scanner.TokenNumber:
			fallthrough
		case scanner.TokenPercentage:
			fallthrough
		case scanner.TokenDimension:
			fallthrough
		case scanner.TokenUnicodeRange:
			fallthrough
		case scanner.TokenCDO:
			fallthrough
		case scanner.TokenCDC:
			fallthrough
		case scanner.TokenFunction:
			fallthrough
		case scanner.TokenIncludes:
			fallthrough
		case scanner.TokenDashMatch:
			fallthrough
		case scanner.TokenPrefixMatch:
			fallthrough
		case scanner.TokenSuffixMatch:
			fallthrough
		case scanner.TokenSubstringMatch:
			fallthrough
		case scanner.TokenBOM:
			o += token.Value
		case scanner.TokenChar:
			if token.Value == "}" {
				o += "}\n"
			} else {
				o += token.Value
			}

		case scanner.TokenURI:
			o += token.Value
			if opts.Deps != "" {
				d += token.Value + "\n"
			}
		}
	}
	// fmt.Printf("s=%s\n", o)
	ioutil.WriteFile(opts.OutFileName, []byte(o), 0644)
	if opts.Deps != "" {

		ioutil.WriteFile(opts.Deps, []byte(d), 0644)
	}

	_, _ = atKeyword, atImport
}