Skip to content

Commit 5865622

Browse files
committed
feat(markdown): added proof-of-concept md command
1 parent bdc43d1 commit 5865622

File tree

11 files changed

+184
-12
lines changed

11 files changed

+184
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage.txt
2+
outline

cmd/fmt.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/b5/outline/lib"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// FmtCmd prints present configuration info
12+
var FmtCmd = &cobra.Command{
13+
Use: "fmt",
14+
Short: "format input",
15+
Long: ``,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
for _, fp := range args {
18+
f, err := os.Open(fp)
19+
if err != nil {
20+
fmt.Println(err.Error())
21+
os.Exit(1)
22+
}
23+
24+
doc, err := lib.Parse(f)
25+
if err != nil {
26+
fmt.Println(err.Error())
27+
os.Exit(1)
28+
}
29+
30+
data, err := doc.MarshalIndent(0, " ")
31+
if err != nil {
32+
fmt.Println(err.Error())
33+
os.Exit(1)
34+
}
35+
36+
fmt.Print(string(data))
37+
}
38+
},
39+
}
40+
41+
func init() {
42+
// FmtCmd.Flags().StringP("export", "e", "config.json", "path to configuration json file")
43+
}

cmd/markdown.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"text/template"
7+
8+
"github.com/b5/outline/lib"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
const mdTmpl = `{{ range . }}
13+
# Package {{ .Name }}
14+
15+
{{ .Description }}
16+
17+
{{ if gt (len .Functions) 0}}
18+
## Functions
19+
{{ range .Functions }}
20+
### {{ .Signature }}
21+
{{ .Description }}
22+
{{ end }}
23+
{{ end }}
24+
25+
26+
{{ end }}`
27+
28+
// MarkdownCmd converts an outline to a markdown document
29+
var MarkdownCmd = &cobra.Command{
30+
Use: "markdown",
31+
Aliases: []string{"md"},
32+
Short: "Convert docs to markdown syntax",
33+
Long: ``,
34+
Run: func(cmd *cobra.Command, args []string) {
35+
t := template.Must(template.New("markdown").Parse(mdTmpl))
36+
var docs []*lib.Doc
37+
for _, fp := range args {
38+
f, err := os.Open(fp)
39+
if err != nil {
40+
fmt.Println(err.Error())
41+
os.Exit(1)
42+
}
43+
44+
doc, err := lib.Parse(f)
45+
if err != nil {
46+
fmt.Println(err.Error())
47+
os.Exit(1)
48+
}
49+
docs = append(docs, doc)
50+
}
51+
52+
if err := t.Execute(os.Stdout, docs); err != nil {
53+
fmt.Println(err.Error())
54+
os.Exit(1)
55+
}
56+
},
57+
}
58+
59+
func init() {
60+
// MarkdownCmd.Flags().StringP("export", "e", "config.json", "path to configuration json file")
61+
}

cmd/root.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/qri-io/walk/lib"
8+
"github.com/sirupsen/logrus"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
// logger
13+
var log = logrus.New()
14+
15+
// RootCmd is the walk command
16+
var RootCmd = &cobra.Command{
17+
Short: "outline is a tool for outlining software packages",
18+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
19+
if debug, err := cmd.Flags().GetBool("debug"); err == nil && debug {
20+
lib.SetLogLevel("debug")
21+
}
22+
},
23+
}
24+
25+
// Execute adds all child commands to the root command sets flags appropriately.
26+
// This is called by main.main(). It only needs to happen once to the rootCmd.
27+
func Execute() {
28+
if err := RootCmd.Execute(); err != nil {
29+
fmt.Println(err.Error())
30+
os.Exit(1)
31+
}
32+
}
33+
34+
func init() {
35+
RootCmd.PersistentFlags().Bool("debug", false, "show debug output")
36+
RootCmd.AddCommand(
37+
FmtCmd,
38+
MarkdownCmd,
39+
)
40+
}

cmd/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cmd

outline.go renamed to lib/outline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Package outline generates starlark documentation from go code
2-
package outline
2+
package lib
33

44
import (
55
"bytes"

parser.go renamed to lib/parser.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package outline
1+
package lib
22

33
import (
44
"fmt"
@@ -61,8 +61,6 @@ func (p *parser) scan() (tok Token) {
6161
return
6262
}
6363
}
64-
// fmt.Printf("returning token outside loop: %s %#v\n", tok.Type, tok.Text)
65-
return
6664
}
6765

6866
func (p *parser) unscan() {
@@ -154,7 +152,7 @@ func (p *parser) readType(baseIndent int) (t *Type, err error) {
154152

155153
for {
156154
tok = p.scan()
157-
if p.indent < baseIndent {
155+
if p.indent <= baseIndent {
158156
p.unscan()
159157
return
160158
}
@@ -170,9 +168,15 @@ func (p *parser) readType(baseIndent int) (t *Type, err error) {
170168
if err != nil {
171169
return
172170
}
173-
case TextTok:
174-
p.unscan()
175-
t.Description, err = p.readText(baseIndent + 1)
171+
default:
172+
err = fmt.Errorf("unexpexted token: %s: %s %d %d", tok.Type, tok.Text, p.indent, baseIndent)
173+
return
174+
175+
// TODO (b5): handle field descriptions
176+
// case TextTok:
177+
// fmt.Println("text?")
178+
// p.unscan()
179+
// t.Description, err = p.readText(baseIndent + 1)
176180
}
177181
}
178182
}

outline_test.go renamed to lib/parser_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package outline
1+
package lib
22

33
import (
44
"bytes"
@@ -59,7 +59,12 @@ outline: time
5959
duration - time = duration
6060
duration + time = time
6161
duration == duration = boolean
62-
duration < duration = booleans`
62+
duration < duration = booleans
63+
time
64+
fields:
65+
operators:
66+
time == time = boolean
67+
time < time = boolean`
6368

6469
var time = &Doc{
6570
Name: "time",
@@ -88,6 +93,13 @@ var time = &Doc{
8893
{Opr: "duration < duration = booleans"},
8994
},
9095
},
96+
{Name: "time",
97+
Fields: []*Field{},
98+
Operators: []*Operator{
99+
{Opr: "time == time = boolean"},
100+
{Opr: "time < time = boolean"},
101+
},
102+
},
91103
},
92104
}
93105

scanner.go renamed to lib/scanner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package outline
1+
package lib
22

33
import (
44
"bufio"

token.go renamed to lib/token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package outline
1+
package lib
22

33
type Position struct {
44
Line, Col, Offset int

0 commit comments

Comments
 (0)