File: douceur.go

package info (click to toggle)
golang-github-aymerick-douceur 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, sid, trixie
  • size: 196 kB
  • sloc: makefile: 2
file content (96 lines) | stat: -rw-r--r-- 1,494 bytes parent folder | download
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
91
92
93
94
95
96
package main

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

	"github.com/aymerick/douceur/inliner"
	"github.com/aymerick/douceur/parser"
)

const (
	// Version is package version
	Version = "0.2.0"
)

var (
	flagVersion bool
)

func init() {
	flag.BoolVar(&flagVersion, "version", false, "Display version")
}

func main() {
	flag.Parse()

	if flagVersion {
		fmt.Println(Version)
		os.Exit(0)
	}

	args := flag.Args()

	if len(args) == 0 {
		fmt.Println("No command supplied")
		os.Exit(1)
	}

	switch args[0] {
	case "parse":
		if len(args) < 2 {
			fmt.Println("Missing file path")
			os.Exit(1)
		}

		parseCSS(args[1])
	case "inline":
		if len(args) < 2 {
			fmt.Println("Missing file path")
			os.Exit(1)
		}

		inlineCSS(args[1])
	default:
		fmt.Println("Unexpected command: ", args[0])
		os.Exit(1)
	}
}

// parse and display CSS file
func parseCSS(filePath string) {
	input := readFile(filePath)

	stylesheet, err := parser.Parse(string(input))
	if err != nil {
		fmt.Println("Parsing error: ", err)
		os.Exit(1)
	}

	fmt.Println(stylesheet.String())
}

// inlines CSS into HTML and display result
func inlineCSS(filePath string) {
	input := readFile(filePath)

	output, err := inliner.Inline(string(input))
	if err != nil {
		fmt.Println("Inlining error: ", err)
		os.Exit(1)
	}

	fmt.Println(output)
}

func readFile(filePath string) []byte {
	file, err := ioutil.ReadFile(filePath)
	if err != nil {
		fmt.Println("Failed to open file: ", filePath, err)
		os.Exit(1)
	}

	return file
}