-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
84 changed files
with
95,007 additions
and
109,765 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/gopherguides/hype" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func main() { | ||
if err := run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func run() error { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
op := filepath.Join(pwd, "src", "testdata") | ||
|
||
ctx := context.Background() | ||
ctx, cancel := context.WithTimeout(ctx, 60*time.Second) | ||
defer cancel() | ||
|
||
root := "/Users/markbates/Dropbox/dev/guides/content/book/chapters" | ||
chaps := []string{ | ||
"09-errors", | ||
"10-generics", | ||
"12-context", | ||
} | ||
|
||
var wg errgroup.Group | ||
|
||
for _, c := range chaps { | ||
wg.Go(func() error { | ||
fp := filepath.Join(root, c) | ||
p := hype.NewParser(os.DirFS(fp)) | ||
p.Root = fp | ||
|
||
doc, err := p.ParseExecuteFile(ctx, "module.md") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.Create(filepath.Join(op, c+".json")) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
enc := json.NewEncoder(f) | ||
enc.SetIndent("", " ") | ||
err = enc.Encode(doc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
return wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type GoType struct { | ||
Full string | ||
Name string | ||
Type string | ||
} | ||
|
||
type TypeMap map[string][]GoType | ||
|
||
func generate(w io.Writer) error { | ||
types, err := findTypes() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintln(w, "let gotypes = {") | ||
for _, k := range types["types"] { | ||
printType(w, k) | ||
} | ||
fmt.Fprintln(w, "}") | ||
|
||
fmt.Fprintln(w) | ||
|
||
fmt.Fprintln(w, "let goerrors = {") | ||
for _, k := range types["errors"] { | ||
printType(w, k) | ||
} | ||
fmt.Fprintln(w, "}") | ||
|
||
fmt.Fprintln(w) | ||
|
||
fmt.Fprintln(w, "export { gotypes, goerrors }") | ||
return nil | ||
} | ||
|
||
func printType(w io.Writer, k GoType) { | ||
if k.Type == "func" { | ||
return | ||
} | ||
|
||
v := fmt.Sprintf("hype.%s", k.Name) | ||
fmt.Fprintf(w, "\t%s: \"%s\",\n", k.Name, v) | ||
} | ||
|
||
func findTypes() (TypeMap, error) { | ||
cmd := exec.Command("go", "doc", "github.com/gopherguides/hype") | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
gx, err := regexp.Compile(`type\s([a-zA-Z0-9_]+)\s([^{|(]+)`) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := TypeMap{} | ||
|
||
lines := strings.Split(string(out), "\n") | ||
for _, line := range lines { | ||
mm := gx.FindStringSubmatch(line) | ||
if len(mm) < 1 { | ||
continue | ||
} | ||
|
||
full := mm[0] | ||
key := mm[1] | ||
ty := mm[2] | ||
|
||
tt := GoType{ | ||
Full: full, | ||
Name: key, | ||
Type: ty, | ||
} | ||
|
||
if strings.HasSuffix(key, "Error") || strings.HasPrefix(key, "Err") { | ||
res["errors"] = append(res["errors"], tt) | ||
} else { | ||
res["types"] = append(res["types"], tt) | ||
} | ||
|
||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_generate(t *testing.T) { | ||
t.Parallel() | ||
r := require.New(t) | ||
|
||
bb := &bytes.Buffer{} | ||
|
||
r.NoError(generate(bb)) | ||
|
||
act := bb.String() | ||
act = strings.TrimSpace(act) | ||
|
||
exp := `let gotypes = { | ||
Atom: "hype.Atom", | ||
Atomable: "hype.Atomable", | ||
AtomableNode: "hype.AtomableNode", | ||
AttrNode: "hype.AttrNode", | ||
Attributes: "hype.Attributes", | ||
Body: "hype.Body", | ||
Cmd: "hype.Cmd", | ||
CmdResult: "hype.CmdResult", | ||
Comment: "hype.Comment", | ||
Document: "hype.Document", | ||
Documents: "hype.Documents", | ||
Element: "hype.Element", | ||
EmptyableNode: "hype.EmptyableNode", | ||
ExecutableNode: "hype.ExecutableNode", | ||
FencedCode: "hype.FencedCode", | ||
Figcaption: "hype.Figcaption", | ||
Figure: "hype.Figure", | ||
HTMLNode: "hype.HTMLNode", | ||
Heading: "hype.Heading", | ||
Image: "hype.Image", | ||
Include: "hype.Include", | ||
InlineCode: "hype.InlineCode", | ||
LI: "hype.LI", | ||
Link: "hype.Link", | ||
MDNode: "hype.MDNode", | ||
Metadata: "hype.Metadata", | ||
Node: "hype.Node", | ||
Nodes: "hype.Nodes", | ||
Now: "hype.Now", | ||
OL: "hype.OL", | ||
Page: "hype.Page", | ||
Paragraph: "hype.Paragraph", | ||
Parser: "hype.Parser", | ||
PostExecuter: "hype.PostExecuter", | ||
PostParser: "hype.PostParser", | ||
PreExecuter: "hype.PreExecuter", | ||
PreParser: "hype.PreParser", | ||
PreParsers: "hype.PreParsers", | ||
Ref: "hype.Ref", | ||
RefProcessor: "hype.RefProcessor", | ||
Snippet: "hype.Snippet", | ||
Snippets: "hype.Snippets", | ||
SourceCode: "hype.SourceCode", | ||
TD: "hype.TD", | ||
TH: "hype.TH", | ||
THead: "hype.THead", | ||
TR: "hype.TR", | ||
Table: "hype.Table", | ||
Tag: "hype.Tag", | ||
Tags: "hype.Tags", | ||
Text: "hype.Text", | ||
ToC: "hype.ToC", | ||
UL: "hype.UL", | ||
Var: "hype.Var", | ||
WaitGrouper: "hype.WaitGrouper", | ||
} | ||
let goerrors = { | ||
CmdError: "hype.CmdError", | ||
ErrAttrEmpty: "hype.ErrAttrEmpty", | ||
ErrAttrNotFound: "hype.ErrAttrNotFound", | ||
ErrIsNil: "hype.ErrIsNil", | ||
ExecuteError: "hype.ExecuteError", | ||
ParseError: "hype.ParseError", | ||
PostExecuteError: "hype.PostExecuteError", | ||
PostParseError: "hype.PostParseError", | ||
PreExecuteError: "hype.PreExecuteError", | ||
PreParseError: "hype.PreParseError", | ||
} | ||
export { gotypes, goerrors }` | ||
|
||
exp = strings.TrimSpace(exp) | ||
|
||
r.Equal(exp, act) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func main() { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
pwd = filepath.Join(pwd, "src", "gotypes.ts") | ||
|
||
f, err := os.Create(pwd) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
|
||
if err := generate(f); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.