Skip to content

Commit

Permalink
more work
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Feb 21, 2024
1 parent 3354c59 commit 926aa51
Show file tree
Hide file tree
Showing 84 changed files with 95,007 additions and 109,765 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
dev: build
npm run test:dev

build: clean
gotypes:
go run ./cmd/gotypes

gentestdata:
go run ./cmd/gentestdata

build: clean gotypes
npm run build

clean:
rm -rf dist

test: build
test: build gentestdata
npm run test

publish: test
Expand Down
71 changes: 71 additions & 0 deletions cmd/gentestdata/main.go
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()
}
93 changes: 93 additions & 0 deletions cmd/gotypes/generate.go
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
}
99 changes: 99 additions & 0 deletions cmd/gotypes/generate_test.go
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)

}
26 changes: 26 additions & 0 deletions cmd/gotypes/main.go
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)
}
}
2 changes: 1 addition & 1 deletion dist/cmd_error.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Parser } from "./parser";
export declare class CmdError {
args: string[];
env: string[];
err: any;
error: any;
exit: number;
filename: string;
output: string;
Expand Down
2 changes: 1 addition & 1 deletion dist/cmd_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CmdError {
this.output = data.output;
this.root = data.root;
parser = parser || new Parser();
this.err = parser.parseError(data.err);
this.error = parser.parseError(data.err);
}
}
// args: [ 'ech', 'Hello World' ],
Expand Down
2 changes: 1 addition & 1 deletion dist/execute_error.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { Parser } from "./parser";
export declare class ExecuteError {
filename: string;
root: string;
err: any;
error: any;
constructor(data: any, parser?: Parser);
}
2 changes: 1 addition & 1 deletion dist/execute_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export class ExecuteError {
this.filename = data.filename;
this.root = data.root;
parser = parser || new Parser();
this.err = parser.parseError(data.err);
this.error = parser.parseError(data.err);
}
}
Loading

0 comments on commit 926aa51

Please sign in to comment.