Skip to content

Commit

Permalink
Refactor and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjones committed Oct 14, 2018
1 parent ffbe5af commit 5c840d2
Show file tree
Hide file tree
Showing 20 changed files with 507 additions and 156 deletions.
31 changes: 30 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@
[[constraint]]
name = "github.com/BurntSushi/toml"
version = "0.3.1"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.2"
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,4 @@ customMarkdownFile = ""
This tool is based on the <a href="https://github.com/davecheney/godoc2md">https://github.com/davecheney/godoc2md</a> project by Dave
Cheney.




<sub>*generated with [goreadme](https://github.com/dmjones/goreadme)*</sub>
153 changes: 13 additions & 140 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,38 @@
package main

import (
"bytes"
"errors"
"go/ast"
"go/build"
"go/doc"
"go/parser"
"go/token"
"io/ioutil"
"fmt"
"log"
"os"
"path"
"text/template"

"github.com/dmjones/goreadme/parse"

"github.com/BurntSushi/toml"
)

const configFile = ".goreadme.toml"

type config struct {
FencedCodeLanguage string
ShowGeneratedSuffix bool
ShowGodocBadge bool
ShowGoReportBadge bool
CustomMarkdownBadges []string
CustomMarkdownFile string
}

func defaultConfig() *config {
return &config{
FencedCodeLanguage: "go",
ShowGeneratedSuffix: true,
ShowGodocBadge: true,
ShowGoReportBadge: false,
CustomMarkdownBadges: nil,
CustomMarkdownFile: "",
}
}

func main() {
config, err := readConfig()
logFatal(err, "Failed to read config")

err = parse(config)
wd, err := os.Getwd()
logFatal(err, "Failed to get directory info")

output, err := parse.ConvertDocs(wd, config)
logFatal(err, "Failed to parse package docs")

fmt.Println(output)
}

func readConfig() (*config, error) {
// readConfig reads the config file, if present, or returns the default config.
func readConfig() (*parse.Config, error) {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return defaultConfig(), nil
return parse.DefaultConfig(), nil
}

// Begin with default config. User can then overwrite what they want.
c := defaultConfig()
c := parse.DefaultConfig()
_, err := toml.DecodeFile(configFile, c)

if err != nil {
Expand All @@ -77,112 +56,6 @@ func readConfig() (*config, error) {
return c, nil
}

func readExtraMarkdown(file string) (string, error) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}

return string(bytes), nil
}

func getPackageDocs() (docs, name, importPath string, err error) {
var wd string
wd, err = os.Getwd()
if err != nil {
return
}

var buildPkg *build.Package
buildPkg, err = build.ImportDir(wd, build.ImportComment)
if err != nil {
return
}

fs := token.NewFileSet()

filter := func(info os.FileInfo) bool {
for _, name := range buildPkg.GoFiles {
if name == info.Name() {
return true
}
}
return false
}

var pkgs map[string]*ast.Package
pkgs, err = parser.ParseDir(fs, buildPkg.Dir, filter, parser.ParseComments)
if err != nil {
return
}

if len(pkgs) != 1 {
err = errors.New("multiple packages found in directory")
return
}

docPkg := doc.New(pkgs[buildPkg.Name], buildPkg.ImportPath, 0)

docs = docPkg.Doc

if buildPkg.Name == "main" {
// In 99% of cases, this is a tool. We want the name of the containing
// directory instead.
name = path.Base(buildPkg.ImportPath)
} else {
name = buildPkg.Name
}
importPath = buildPkg.ImportPath
return
}

func parse(c *config) error {

// Read additional markdown file, if present in config
var customMarkdown string
if c.CustomMarkdownFile != "" {
var err error
customMarkdown, err = readExtraMarkdown(c.CustomMarkdownFile)
if err != nil {
return err
}
}

packageDocs, packageName, importPath, err := getPackageDocs()
if err != nil {
return err
}

var buf bytes.Buffer
ToMD(&buf, packageDocs, c.FencedCodeLanguage)
markdownDocs := buf.String()

tmpl, err := template.New("").Parse(pkgtemplate)
if err != nil {
return err
}

err = tmpl.Execute(os.Stdout, struct {
PackageName string
ImportPath string
PackageDocs string
ExtraMarkdown string
Config *config
}{
PackageName: packageName,
ImportPath: importPath,
PackageDocs: markdownDocs,
ExtraMarkdown: customMarkdown,
Config: c,
})

if err != nil {
return err
}

return nil
}

func logFatal(err error, msg string) {
if err != nil {
log.Fatal(msg + ": " + err.Error())
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion comment.go → parse/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Minor edits by Duncan Jones and subject to LICENSE file.

package main
package parse

import (
"io"
Expand Down
35 changes: 35 additions & 0 deletions parse/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 Duncan Jones
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package parse

type Config struct {
FencedCodeLanguage string
ShowGeneratedSuffix bool
ShowGodocBadge bool
ShowGoReportBadge bool
CustomMarkdownBadges []string
CustomMarkdownFile string
}

func DefaultConfig() *Config {
return &Config{
FencedCodeLanguage: "go",
ShowGeneratedSuffix: true,
ShowGodocBadge: true,
ShowGoReportBadge: false,
CustomMarkdownBadges: nil,
CustomMarkdownFile: "",
}
}
29 changes: 29 additions & 0 deletions parse/foo/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018 Duncan Jones
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
We don't use the 'testdata' package, as this messes up the import paths reported
by the docs tools.
This is a heading
Here is some text after the heading. Another sentence here too.
func someCode() {
fmt.Println("Hello, world!")
}
The last sentence is here.
*/
package foo

0 comments on commit 5c840d2

Please sign in to comment.