diff --git a/claat/cmd/export.go b/claat/cmd/export.go index 7c638c450..209cc8f56 100644 --- a/claat/cmd/export.go +++ b/claat/cmd/export.go @@ -27,7 +27,6 @@ import ( "time" "github.com/googlecodelabs/tools/claat/fetch" - "github.com/googlecodelabs/tools/claat/parser" "github.com/googlecodelabs/tools/claat/render" "github.com/googlecodelabs/tools/claat/types" "github.com/googlecodelabs/tools/claat/util" @@ -43,8 +42,6 @@ type CmdExportOptions struct { ExtraVars map[string]string // GlobalGA is the global Google Analytics account to use. GlobalGA string - // MDParser is the underlying Markdown parser to use. - MDParser parser.MarkdownParser // Output is the output directory, or "-" for stdout. Output string // PassMetadata are the extra metadata fields to pass along. @@ -101,7 +98,7 @@ func CmdExport(opts CmdExportOptions) int { // // An alternate http.RoundTripper may be specified if desired. Leave null for default. func ExportCodelab(src string, rt http.RoundTripper, opts CmdExportOptions) (*types.Meta, error) { - f, err := fetch.NewFetcher(opts.AuthToken, opts.PassMetadata, rt, opts.MDParser) + f, err := fetch.NewFetcher(opts.AuthToken, opts.PassMetadata, rt) if err != nil { return nil, err } @@ -131,7 +128,7 @@ func ExportCodelab(src string, rt http.RoundTripper, opts CmdExportOptions) (*ty } func ExportCodelabMemory(src io.ReadCloser, w io.Writer, opts CmdExportOptions) (*types.Meta, error) { - m := fetch.NewMemoryFetcher(opts.PassMetadata, opts.MDParser) + m := fetch.NewMemoryFetcher(opts.PassMetadata) clab, err := m.SlurpCodelab(src) if err != nil { return nil, err diff --git a/claat/cmd/update.go b/claat/cmd/update.go index 1d71d949f..76725b839 100644 --- a/claat/cmd/update.go +++ b/claat/cmd/update.go @@ -27,7 +27,6 @@ import ( "time" "github.com/googlecodelabs/tools/claat/fetch" - "github.com/googlecodelabs/tools/claat/parser" "github.com/googlecodelabs/tools/claat/types" "github.com/googlecodelabs/tools/claat/util" ) @@ -40,8 +39,6 @@ type CmdUpdateOptions struct { ExtraVars map[string]string // GlobalGA is the global Google Analytics account to use. GlobalGA string - // MDParser is the underlying Markdown parser to use. - MDParser parser.MarkdownParser // PassMetadata are the extra metadata fields to pass along. PassMetadata map[string]bool // Prefix is a URL prefix to prepend when using HTML format. @@ -110,7 +107,7 @@ func updateCodelab(dir string, opts CmdUpdateOptions) (*types.Meta, error) { } // fetch and parse codelab source - f, err := fetch.NewFetcher(opts.AuthToken, opts.PassMetadata, nil, opts.MDParser) + f, err := fetch.NewFetcher(opts.AuthToken, opts.PassMetadata, nil) if err != nil { return nil, err } diff --git a/claat/fetch/fetch.go b/claat/fetch/fetch.go index 62ad3204c..d0823a0a0 100644 --- a/claat/fetch/fetch.go +++ b/claat/fetch/fetch.go @@ -71,13 +71,11 @@ type codelab struct { type MemoryFetcher struct { passMetadata map[string]bool - mdParser parser.MarkdownParser } -func NewMemoryFetcher(pm map[string]bool, mdp parser.MarkdownParser) *MemoryFetcher { +func NewMemoryFetcher(pm map[string]bool) *MemoryFetcher { return &MemoryFetcher{ passMetadata: pm, - mdParser: mdp, } } @@ -89,7 +87,7 @@ func (m *MemoryFetcher) SlurpCodelab(rc io.ReadCloser) (*codelab, error) { } defer r.body.Close() - opts := *parser.NewOptions(m.mdParser) + opts := *parser.NewOptions() opts.PassMetadata = m.passMetadata clab, err := parser.Parse(string(r.typ), r.body, opts) @@ -108,17 +106,15 @@ type Fetcher struct { authHelper *auth.Helper authToken string crcTable *crc64.Table - mdParser parser.MarkdownParser passMetadata map[string]bool roundTripper http.RoundTripper } -func NewFetcher(at string, pm map[string]bool, rt http.RoundTripper, mdp parser.MarkdownParser) (*Fetcher, error) { +func NewFetcher(at string, pm map[string]bool, rt http.RoundTripper) (*Fetcher, error) { return &Fetcher{ authHelper: nil, authToken: at, crcTable: crc64.MakeTable(crc64.ECMA), - mdParser: mdp, passMetadata: pm, roundTripper: rt, }, nil @@ -147,7 +143,7 @@ func (f *Fetcher) SlurpCodelab(src string, output string) (*codelab, error) { } defer res.body.Close() - opts := *parser.NewOptions(f.mdParser) + opts := *parser.NewOptions() opts.PassMetadata = f.passMetadata clab, err := parser.Parse(string(res.typ), res.body, opts) @@ -300,7 +296,7 @@ func (f *Fetcher) slurpFragment(url string) ([]types.Node, error) { } defer res.body.Close() - opts := *parser.NewOptions(f.mdParser) + opts := *parser.NewOptions() opts.PassMetadata = f.passMetadata return parser.ParseFragment(string(res.typ), res.body, opts) diff --git a/claat/main.go b/claat/main.go index 5211a0c38..72fb2c9b7 100644 --- a/claat/main.go +++ b/claat/main.go @@ -30,7 +30,6 @@ import ( "time" "github.com/googlecodelabs/tools/claat/cmd" - "github.com/googlecodelabs/tools/claat/parser" // allow parsers to register themselves _ "github.com/googlecodelabs/tools/claat/parser/gdoc" @@ -46,7 +45,6 @@ var ( expenv = flag.String("e", "web", "codelab environment") extra = flag.String("extra", "", "Additional arguments to pass to format templates. JSON object of string,string key values.") globalGA = flag.String("ga", "UA-49880327-14", "global Google Analytics account") - mdParser = flag.String("md_parser", "blackfriday", "Markdown parser to use. Accepted values: \"blackfriday\", \"goldmark\"") output = flag.String("o", ".", "output directory or '-' for stdout") passMetadata = flag.String("pass_metadata", "", "Metadata fields to pass through to the output. Comma-delimited list of field names.") prefix = flag.String("prefix", "https://storage.googleapis.com", "URL prefix for html format") @@ -74,16 +72,6 @@ func main() { pm := parsePassMetadata(*passMetadata) - var mdp parser.MarkdownParser - switch *mdParser { - case "blackfriday": - mdp = parser.Blackfriday - case "goldmark": - mdp = parser.Goldmark - default: - log.Fatalf("Unrecognized md_parser value %q", *mdParser) - } - exitCode := 0 switch os.Args[1] { case "export": @@ -92,7 +80,6 @@ func main() { Expenv: *expenv, ExtraVars: extraVars, GlobalGA: *globalGA, - MDParser: mdp, Output: *output, PassMetadata: pm, Prefix: *prefix, @@ -106,7 +93,6 @@ func main() { AuthToken: *authToken, ExtraVars: extraVars, GlobalGA: *globalGA, - MDParser: mdp, PassMetadata: pm, Prefix: *prefix, }) diff --git a/claat/parser/gdoc/parse_test.go b/claat/parser/gdoc/parse_test.go index 6cb555cd0..a5a633c25 100644 --- a/claat/parser/gdoc/parse_test.go +++ b/claat/parser/gdoc/parse_test.go @@ -176,7 +176,7 @@ func TestMetaTable(t *testing.T) { ` p := &Parser{} - clab, err := p.Parse(markupReader(markup), *parser.NewOptions(parser.Goldmark)) + clab, err := p.Parse(markupReader(markup), *parser.NewOptions()) if err != nil { t.Fatal(err) } @@ -252,7 +252,7 @@ func TestMetaTablePassMetadata(t *testing.T) { ` p := &Parser{} - opts := *parser.NewOptions(parser.Goldmark) + opts := *parser.NewOptions() opts.PassMetadata = map[string]bool{ "extrafieldone": true, } @@ -379,7 +379,7 @@ func TestParseDoc(t *testing.T) { ` p := &Parser{} - c, err := p.Parse(markupReader(markup), *parser.NewOptions(parser.Goldmark)) + c, err := p.Parse(markupReader(markup), *parser.NewOptions()) if err != nil { t.Fatal(err) } @@ -556,7 +556,7 @@ func TestParseFragment(t *testing.T) { ` p := &Parser{} - opts := *parser.NewOptions(parser.Goldmark) + opts := *parser.NewOptions() nodes, err := p.ParseFragment(markupReader(markup), opts) if err != nil { t.Fatal(err) diff --git a/claat/parser/md/parse.go b/claat/parser/md/parse.go index d4c9a2cbd..565124d31 100644 --- a/claat/parser/md/parse.go +++ b/claat/parser/md/parse.go @@ -115,7 +115,7 @@ func (p *Parser) Parse(r io.Reader, opts parser.Options) (*types.Codelab, error) if err != nil { return nil, err } - b, err = renderToHTML(b, opts.MDParser) + b, err = renderToHTML(b) if err != nil { return nil, err } @@ -134,7 +134,7 @@ func (p *Parser) ParseFragment(r io.Reader, opts parser.Options) ([]types.Node, if err != nil { return nil, err } - b, err = renderToHTML(b, opts.MDParser) + b, err = renderToHTML(b) if err != nil { return nil, err } @@ -227,8 +227,8 @@ func (ds *docState) appendNodes(nn ...types.Node) { } // renderToHTML preprocesses Markdown bytes and then calls a Markdown parser on the Markdown. -// It takes a raw markdown bytes and output parsed xhtml in bytes. -func renderToHTML(b []byte, mdp parser.MarkdownParser) ([]byte, error) { +// It takes a raw markdown bytes and outputs parsed xhtml in bytes. +func renderToHTML(b []byte) ([]byte, error) { b = convertImports(b) gmParser := goldmark.New(goldmark.WithRendererOptions(gmhtml.WithUnsafe()), goldmark.WithExtensions(extension.Typographer, extension.Table)) var out bytes.Buffer diff --git a/claat/parser/md/parse_test.go b/claat/parser/md/parse_test.go index 51041ac8f..2068548f7 100644 --- a/claat/parser/md/parse_test.go +++ b/claat/parser/md/parse_test.go @@ -56,7 +56,7 @@ func parseFragment(markup string) ([]types.Node, error) { r := strings.NewReader(markup) p := &Parser{} - opts := *parser.NewOptions(parser.Goldmark) + opts := *parser.NewOptions() return p.ParseFragment(r, opts) } @@ -86,7 +86,7 @@ func stringify(nodes []types.Node, level string) string { func TestHandleCodelabTitle(t *testing.T) { // Set up. title := "Egret" - c := mustParseCodelab(fmt.Sprintf("# %s", title), *parser.NewOptions(parser.Goldmark)) + c := mustParseCodelab(fmt.Sprintf("# %s", title), *parser.NewOptions()) if c.Title != title { t.Errorf("[%q] got %v, want %v", title, c.Title, title) @@ -122,7 +122,7 @@ func TestProcessDuration(t *testing.T) { for i, tc := range tests { content := fmt.Sprintf(stdHeader+"\n## Step Title\nDuration: %v\n", tc.in) - c := mustParseCodelab(content, *parser.NewOptions(parser.Goldmark)) + c := mustParseCodelab(content, *parser.NewOptions()) out := time.Duration(c.Duration) * time.Minute if out != tc.out { @@ -152,7 +152,7 @@ Duration: %v content += fmt.Sprintf(tmp, dur) } - c := mustParseCodelab(content, *parser.NewOptions(parser.Goldmark)) + c := mustParseCodelab(content, *parser.NewOptions()) if c.Duration != tc.out { t.Errorf("%d: wanted duration %d but got %d", i, c.Duration, tc.out) } @@ -186,7 +186,7 @@ feedback link: https://www.google.com ` content += ("# " + title) - c := mustParseCodelab(content, *parser.NewOptions(parser.Goldmark)) + c := mustParseCodelab(content, *parser.NewOptions()) if !reflect.DeepEqual(c.Meta, wantMeta) { t.Errorf("\ngot:\n%+v\nwant:\n%+v", c.Meta, wantMeta) } @@ -223,7 +223,7 @@ extrafieldtwo: bbbbb ` content += ("# " + title) - opts := *parser.NewOptions(parser.Goldmark) + opts := *parser.NewOptions() opts.PassMetadata = map[string]bool{ "extrafieldtwo": true, } @@ -393,7 +393,7 @@ I'm going to inject some HTML test := test t.Run(test.name, func(t *testing.T) { t.Parallel() - lab := mustParseCodelab(test.input, *parser.NewOptions(parser.Goldmark)) + lab := mustParseCodelab(test.input, *parser.NewOptions()) var got []string for _, s := range lab.Steps { for _, n := range types.ImportNodes(s.Content.Nodes) { diff --git a/claat/parser/parse.go b/claat/parser/parse.go index 70f513798..1de885fbe 100644 --- a/claat/parser/parse.go +++ b/claat/parser/parse.go @@ -32,23 +32,14 @@ type Parser interface { ParseFragment(r io.Reader, opts Options) ([]types.Node, error) } -type MarkdownParser int - -const ( - Blackfriday MarkdownParser = iota - Goldmark MarkdownParser = iota -) - // Container for parsing options. type Options struct { PassMetadata map[string]bool - MDParser MarkdownParser } -func NewOptions(mdp MarkdownParser) *Options { +func NewOptions() *Options { return &Options{ PassMetadata: map[string]bool{}, - MDParser: mdp, } }