Skip to content

Commit

Permalink
Remove Blackfriday parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
cassierecher committed Jun 24, 2021
1 parent c2b0fcb commit 06739b2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 57 deletions.
7 changes: 2 additions & 5 deletions claat/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions claat/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 5 additions & 9 deletions claat/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 0 additions & 14 deletions claat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down Expand Up @@ -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":
Expand All @@ -92,7 +80,6 @@ func main() {
Expenv: *expenv,
ExtraVars: extraVars,
GlobalGA: *globalGA,
MDParser: mdp,
Output: *output,
PassMetadata: pm,
Prefix: *prefix,
Expand All @@ -106,7 +93,6 @@ func main() {
AuthToken: *authToken,
ExtraVars: extraVars,
GlobalGA: *globalGA,
MDParser: mdp,
PassMetadata: pm,
Prefix: *prefix,
})
Expand Down
8 changes: 4 additions & 4 deletions claat/parser/gdoc/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions claat/parser/md/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions claat/parser/md/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -223,7 +223,7 @@ extrafieldtwo: bbbbb
`
content += ("# " + title)

opts := *parser.NewOptions(parser.Goldmark)
opts := *parser.NewOptions()
opts.PassMetadata = map[string]bool{
"extrafieldtwo": true,
}
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 1 addition & 10 deletions claat/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down

0 comments on commit 06739b2

Please sign in to comment.