Skip to content

Commit c13d42d

Browse files
committed
added flag -i -n -c -v
1 parent c13ea58 commit c13d42d

File tree

8 files changed

+70
-127
lines changed

8 files changed

+70
-127
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313

1414
/cmd/lit/lit*
1515
/cmd/lit/main.go
16+
/cmd/lit/test
1617
/transform.go

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ script:
1212
- make build
1313
- make test
1414
- make release
15-
- cd ../../
16-
- go test -v
15+
- cd ../../ && make test

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
@./cmd/lit/lit -n transform.go.md transform.go
3+
go test -v

cmd/lit/Makefile

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ build: pre
1515

1616
pre:
1717
@echo "pre-processing"
18-
@./lit-bootstrap ../../transform.go.md ../../transform.go
1918
@./lit-bootstrap main.go.md main.go
2019

2120

@@ -29,15 +28,14 @@ bootstrap:
2928
@echo "bootstrap lit"
3029
@./lit-bootstrap version
3130

32-
test: build
33-
@./lit version
34-
@mkdir -p tmp
31+
test:
32+
@./lit -v
33+
@mkdir -p test
3534
@echo "build test sample"
36-
@./lit sample/sample.go.md tmp/main.go
35+
@./lit -n sample/sample.go.md test/main.go
3736
@echo "run test sample\n---"
38-
@go run tmp/main.go
37+
@go run test/main.go
3938
@echo "---"
40-
@rm -rf tmp
4139

4240
release:
4341
GOOS=darwin GOARCH=386 go build -ldflags "-X main.version=${VERSION}" -o lit_darwin_386
@@ -50,6 +48,7 @@ release:
5048

5149
clean:
5250
@rm -f lit
53-
@rm -f lit-bootstrap
51+
@rm -f lit_*
52+
@rm -rf test
5453

5554
.PHONY: build pre bootstrap test release clean

cmd/lit/main.go

Lines changed: 0 additions & 79 deletions
This file was deleted.

cmd/lit/main.go.md

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ at first we check if the first arg is `version`. if it ism then print the versio
88

99
import (
1010
"fmt"
11+
"flag"
1112
"io/ioutil"
1213
"os"
1314

@@ -16,35 +17,51 @@ at first we check if the first arg is `version`. if it ism then print the versio
1617

1718
var version = "dev"
1819

20+
func usage() {
21+
fmt.Printf("\n")
22+
fmt.Printf(" +--------+\n")
23+
fmt.Printf(" | --- |\n")
24+
fmt.Printf(" | --- | lit v%s - literate preprocessor\n", version)
25+
fmt.Printf(" | --- | --- ------ - ---------------------\n")
26+
fmt.Printf(" | --- |\n")
27+
fmt.Printf(" | --- |\n")
28+
fmt.Printf(" +--------+\n")
29+
fmt.Printf("\n")
30+
fmt.Printf(" USAGE: lit [flags] <in> <out>\n\n")
31+
fmt.Printf(" FLAGS:\n")
32+
flag.PrintDefaults()
33+
fmt.Printf("\n EXAMPLE:\n")
34+
fmt.Printf(" $ lit foo.go.md foo.go\n\n")
35+
os.Exit(0)
36+
}
37+
1938
func main() {
20-
totalArgs := len(os.Args)
21-
if totalArgs == 1 {
22-
fmt.Printf("\n")
23-
fmt.Printf(" +--------+\n")
24-
fmt.Printf(" | --- |\n")
25-
fmt.Printf(" | --- | lit v%s - literate preprocessor\n", version)
26-
fmt.Printf(" | --- | --- ------ - ---------------------\n")
27-
fmt.Printf(" | --- |\n")
28-
fmt.Printf(" | --- |\n")
29-
fmt.Printf(" +--------+\n")
30-
fmt.Printf("\n")
31-
fmt.Printf(" lit <in> <out>\n")
32-
fmt.Printf(" lit foo.go.lit foo.go\n")
33-
os.Exit(1)
34-
}
39+
flagCommentStyle := flag.String("c", "//", "the comment style")
40+
flagGeneratedInfo := flag.Bool("i", true, "add an information that the code was generated at the top of the file")
41+
flagNoDocs := flag.Bool("n", false, "cut the docs from the final result")
42+
flagVersion := flag.Bool("v", false, "print the version and exit")
3543

36-
var err error
37-
var inputData []byte
44+
flag.Usage = usage
45+
flag.Parse()
3846

39-
if totalArgs >= 1 {
40-
if os.Args[1] == "version" {
41-
fmt.Println("lit v"+version)
47+
flagArgs := flag.Args()
48+
totalArgs := flag.NArg()
49+
50+
if *flagVersion {
51+
fmt.Println("v"+version)
4252
os.Exit(0)
4353
}
54+
if totalArgs == 0 {
55+
usage()
56+
}
57+
58+
var err error
59+
var inputData []byte
4460

4561
read the given filepath and store the data to a variable
4662

47-
inputData, err = ioutil.ReadFile(os.Args[1])
63+
if totalArgs >= 1 {
64+
inputData, err = ioutil.ReadFile(flagArgs[0])
4865
if err != nil {
4966
fmt.Println(err)
5067
os.Exit(1)
@@ -53,24 +70,23 @@ read the given filepath and store the data to a variable
5370

5471
now we can transform the input data
5572

56-
transformed := lit.Transform(inputData, []byte("// "))
57-
73+
transformed := lit.Transform(inputData, []byte(*flagCommentStyle+" "), *flagNoDocs)
5874

5975
last step is to write the file back to the given filepath
6076

61-
if totalArgs == 3 {
62-
outPath := os.Args[2]
63-
outPre := []byte("// GENERATED BY LIT v"+version+"\n// DO NOT EDIT BY HAND\n\n")
64-
err := ioutil.WriteFile(outPath, append(outPre, transformed...), 0777)
77+
if totalArgs == 2 {
78+
outPath := flagArgs[1]
79+
var outInfo []byte
80+
if *flagGeneratedInfo {
81+
outInfo = []byte("// GENERATED BY LIT v"+version+"; DO NOT EDIT\n\n")
82+
}
83+
err := ioutil.WriteFile(outPath, append(outInfo, transformed...), 0777)
6584
if err != nil {
6685
fmt.Println(err)
6786
os.Exit(1)
6887
}
69-
// TODO: verbose mode
70-
// fmt.Println("successful written file to", outPath)
7188
} else {
7289
fmt.Println(string(transformed))
7390
}
7491

75-
7692
}

transform.go.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88

99
The core of the lit tool is the `Transform` function. At this function we process the given source and comment out all non code lines by the given `commentStyle`.
1010

11-
func Transform(code []byte, commentStyle []byte) []byte {
11+
func Transform(code []byte, commentStyle []byte, noDocs bool) []byte {
1212
tokens := Scanner(code)
13-
transformed := make([][]byte, len(tokens))
13+
transformed := make([][]byte, 0)
1414
for i := 0; i < len(tokens); i++ {
1515
switch tokens[i].Type {
1616
case TYPE_DOC, TYPE_CODESIGN:
17-
if len(tokens[i].Value) > 0 {
18-
transformed[i] = append(commentStyle, tokens[i].Value...)
17+
if len(tokens[i].Value) == 0 {
18+
transformed = append(transformed, []byte(""))
19+
} else {
20+
if !noDocs {
21+
transformed = append(transformed, append(commentStyle, tokens[i].Value...))
22+
}
1923
}
2024
break
2125

2226
case TYPE_CODE:
23-
transformed[i] = tokens[i].Value
27+
transformed = append(transformed, tokens[i].Value)
2428
break
2529
}
2630
}

transform_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ this is a test string
1818
fmt.Println("Hello literate programming")
1919
}
2020
`)
21-
result := Transform(code, []byte("// "))
21+
result := Transform(code, []byte("// "), false)
2222
expected := []byte(`// # test code
2323
2424
// this is a test string
@@ -44,7 +44,7 @@ this is a test string
4444
4545
echo hello
4646
`)
47-
result := Transform(code, []byte("# "))
47+
result := Transform(code, []byte("# "), false)
4848
expected := []byte(`# # test code
4949
# this is a test string
5050
@@ -65,7 +65,7 @@ this is a test string
6565
echo "hello"
6666
` + "```" + `
6767
`)
68-
result := Transform(code, []byte("# "))
68+
result := Transform(code, []byte("# "), false)
6969
expected := []byte(`# # test code
7070
# this is a test string
7171

0 commit comments

Comments
 (0)