Skip to content

Commit

Permalink
Integrate Goldmark Markdown parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitar-kostadinov committed Dec 20, 2021
1 parent c3a04a0 commit f211b06
Show file tree
Hide file tree
Showing 100 changed files with 23,671 additions and 4,980 deletions.
13 changes: 6 additions & 7 deletions cmd/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/gardener/docforge/cmd/configuration"
"github.com/gardener/docforge/pkg/api"
"github.com/gardener/docforge/pkg/hugo"
"github.com/gardener/docforge/pkg/resourcehandlers"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -310,27 +309,27 @@ func cacheHomeDir(f *cmdFlags, config *configuration.Config) string {
return filepath.Join(userHomeDir, configuration.DocforgeHomeDir)
}

func hugoOptions(f *cmdFlags, config *configuration.Config) *hugo.Options {
func hugoOptions(f *cmdFlags, config *configuration.Config) *Hugo {
if !f.hugo && (config == nil || config.Hugo == nil) {
return nil
}

hugoOptions := &hugo.Options{
ho := &Hugo{
PrettyUrls: prettyURLs(f.hugoPrettyUrls, config.Hugo),
IndexFileNames: combineSectionFiles(f.hugoSectionFiles, config.Hugo),
}

if f.hugoBaseURL != "" {
hugoOptions.BaseURL = f.hugoBaseURL
return hugoOptions
ho.BaseURL = f.hugoBaseURL
return ho
}

if config.Hugo != nil {
if config.Hugo.BaseURL != nil {
hugoOptions.BaseURL = *config.Hugo.BaseURL
ho.BaseURL = *config.Hugo.BaseURL
}
}
return hugoOptions
return ho
}

func combineSectionFiles(sectionFilesFromFlags []string, hugoConfig *configuration.Hugo) []string {
Expand Down
110 changes: 0 additions & 110 deletions cmd/app/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,123 +5,13 @@
package app

import (
"reflect"
"testing"

"github.com/gardener/docforge/cmd/configuration"
"github.com/gardener/docforge/pkg/hugo"
"github.com/stretchr/testify/assert"
"k8s.io/utils/pointer"
)

func Test_hugoOptions(t *testing.T) {
type args struct {
f *cmdFlags
config *configuration.Config
}
tests := []struct {
name string
args args
want *hugo.Options
}{
{
name: "return_nil_when_no_config_or_flag_provided",
args: args{
f: &cmdFlags{
hugo: false,
},
config: &configuration.Config{
Hugo: nil,
},
},
want: nil,
},
{
name: "return_default_hugo_options",
args: args{
f: &cmdFlags{
hugo: true,
hugoPrettyUrls: true,
},
config: &configuration.Config{
Hugo: nil,
},
},
want: &hugo.Options{
PrettyUrls: true,
IndexFileNames: []string{},
BaseURL: "",
},
},
{
name: "use_base_url_from_config_when_not_specified_in_flags",
args: args{
f: &cmdFlags{
hugo: true,
hugoPrettyUrls: true,
},
config: &configuration.Config{
Hugo: &configuration.Hugo{
BaseURL: pointer.StringPtr("/new/baseURL"),
},
},
},
want: &hugo.Options{
PrettyUrls: true,
IndexFileNames: []string{},
BaseURL: "/new/baseURL",
},
},
{
name: "use_base_url_from_flags_with_priority",
args: args{
f: &cmdFlags{
hugo: true,
hugoPrettyUrls: true,
hugoBaseURL: "/override",
},
config: &configuration.Config{
Hugo: &configuration.Hugo{
BaseURL: pointer.StringPtr("/new/baseURL"),
},
},
},
want: &hugo.Options{
PrettyUrls: true,
IndexFileNames: []string{},
BaseURL: "/override",
},
},
{
name: "set_hugo_base_url_from_flags",
args: args{
f: &cmdFlags{
hugo: true,
hugoPrettyUrls: true,
hugoBaseURL: "/fromFlag",
},
config: &configuration.Config{
Hugo: &configuration.Hugo{
BaseURL: nil,
},
},
},
want: &hugo.Options{
PrettyUrls: true,
IndexFileNames: []string{},
BaseURL: "/fromFlag",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hugoOptions(tt.args.f, tt.args.config); !reflect.DeepEqual(got, tt.want) {
t.Errorf("hugoOptions() = %v, want %v", got, tt.want)
}
})
}
}

func Test_prettyURLs(t *testing.T) {
type args struct {
fromFlag bool
Expand Down
58 changes: 26 additions & 32 deletions cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ import (
"k8s.io/klog/v2"

"github.com/gardener/docforge/pkg/api"
"github.com/gardener/docforge/pkg/hugo"
"github.com/gardener/docforge/pkg/resourcehandlers"
"github.com/gardener/docforge/pkg/writers"
"github.com/hashicorp/go-multierror"

"github.com/gardener/docforge/pkg/processors"
"github.com/gardener/docforge/pkg/reactor"
"github.com/gardener/docforge/pkg/resourcehandlers"
"github.com/gardener/docforge/pkg/resourcehandlers/git"
ghrs "github.com/gardener/docforge/pkg/resourcehandlers/github"
"github.com/gardener/docforge/pkg/writers"
"github.com/hashicorp/go-multierror"

"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -56,14 +53,30 @@ type Options struct {
GitHubInfoPath string
DryRunWriter io.Writer
Resolve bool
Hugo *hugo.Options
Hugo *Hugo
UseGit bool
HomeDir string
LocalMappings map[string]string
DefaultBranches map[string]string
LastNVersions map[string]int
}

// Hugo is the configuration options for creating HUGO implementations
// docforge interfaces
type Hugo struct {
// PrettyUrls indicates if links will rewritten for Hugo will be
// formatted for pretty url support or not. Pretty urls in Hugo
// place built source content in index.html, which resides in a path segment with
// the name of the file, making request URLs more resource-oriented.
// Example: (source) sample.md -> (build) sample/index.html -> (runtime) ./sample
PrettyUrls bool
// IndexFileNames defines a list of file names that indicate
// their content can be used as Hugo section files (_index.md).
IndexFileNames []string
// BaseURL is used from the Hugo processor to rewrite relative links to root-relative
BaseURL string
}

// Credentials holds repositories access credentials
type Credentials struct {
Host string
Expand All @@ -89,7 +102,6 @@ func NewReactor(ctx context.Context, options *Options, rhs []resourcehandlers.Re
ResourcesPath: options.ResourcesPath,
ResourceDownloadWorkersCount: options.ResourceDownloadWorkersCount,
RewriteEmbedded: options.RewriteEmbedded,
Processor: nil,
ResourceHandlers: rhs,
DryRunWriter: dryRunWriters,
Resolve: options.Resolve,
Expand All @@ -103,6 +115,7 @@ func NewReactor(ctx context.Context, options *Options, rhs []resourcehandlers.Re
} else {
o.Writer = &writers.FSWriter{
Root: options.DestinationPath,
Hugo: options.Hugo != nil,
}
o.ResourceDownloadWriter = &writers.FSWriter{
Root: filepath.Join(options.DestinationPath, options.ResourcesPath),
Expand All @@ -117,32 +130,13 @@ func NewReactor(ctx context.Context, options *Options, rhs []resourcehandlers.Re
}

if options.Hugo != nil {
WithHugo(o, options)
o.Hugo = true
o.PrettyUrls = options.Hugo.PrettyUrls
o.IndexFileNames = options.Hugo.IndexFileNames
o.BaseURL = options.Hugo.BaseURL
}
return reactor.NewReactor(o)
}

// WithHugo adapts the reactor.Options object with Hugo-specific
// settings for writer and processor
func WithHugo(reactorOptions *reactor.Options, o *Options) {
hugoOptions := o.Hugo
reactorOptions.Processor = &processors.ProcessorChain{
Processors: []processors.Processor{
&processors.FrontMatter{
IndexFileNames: hugoOptions.IndexFileNames,
},
hugo.NewProcessor(hugoOptions),
},
}
if o.DryRunWriter != nil {
hugoOptions.Writer = reactorOptions.Writer
} else {
hugoOptions.Writer = &writers.FSWriter{
Root: filepath.Join(o.DestinationPath),
}
}
reactorOptions.IndexFileNames = hugoOptions.IndexFileNames
reactorOptions.Writer = hugo.NewWriter(hugoOptions)
return reactor.NewReactor(o)
}

func initResourceHandlers(ctx context.Context, o *Options) ([]resourcehandlers.ResourceHandler, error) {
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ require (
k8s.io/utils v0.0.0-20201027101359-01387209bb0d
)

require (
github.com/yuin/goldmark v1.4.4
github.com/yuin/goldmark-meta v1.0.0
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -59,7 +65,6 @@ require (
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6e
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.4 h1:zNWRjYUW32G9KirMXYHQHVNFkXvMI7LpgNW2AgYAoIs=
github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg=
github.com/yuin/goldmark-meta v1.0.0 h1:ScsatUIT2gFS6azqzLGUjgOnELsBOxMXerM3ogdJhAM=
github.com/yuin/goldmark-meta v1.0.0/go.mod h1:zsNNOrZ4nLuyHAJeLQEZcQat8dm70SmB2kHbls092Gc=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
Expand Down
80 changes: 0 additions & 80 deletions pkg/hugo/fswriter.go

This file was deleted.

Loading

0 comments on commit f211b06

Please sign in to comment.