Skip to content

Commit

Permalink
Add option for no diff blocks (#106)
Browse files Browse the repository at this point in the history
* Use goembed for tempaltes

* Add option to skip marking of diff blocks

Fixes #105

* Test only go 1.16
  • Loading branch information
posener authored May 8, 2021
1 parent 20fadea commit a049525
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 197 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ jobs:
strategy:
matrix:
go-version:
- 1.13.x
- 1.14.x
- 1.16.x
platform:
- ubuntu-latest
runs-on: ${{ matrix.platform }}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ require (
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
)

go 1.13
go 1.16
5 changes: 4 additions & 1 deletion goreadme.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import (

"github.com/golang/gddo/doc"
"github.com/pkg/errors"
"github.com/posener/goreadme/internal/markdown"
"github.com/posener/goreadme/internal/template"
)

Expand Down Expand Up @@ -137,6 +138,8 @@ type Config struct {
SkipExamples bool `json:"skip_examples"`
// SkipSubPackages will omit the sub packages section from the README.
SkipSubPackages bool `json:"skip_sub_packages"`
// NoDiffBlocks disables marking code blocks as diffs if they start with minus or plus signes.
NoDiffBlocks bool `json:"no_diff_blocks"`
// RecursiveSubPackages will retrieved subpackages information recursively.
// If false, only one level of subpackages will be retrieved.
RecursiveSubPackages bool `json:"recursive_sub_packages"`
Expand Down Expand Up @@ -170,7 +173,7 @@ func (r *GoReadme) Create(ctx context.Context, name string, w io.Writer) error {
if err != nil {
return err
}
return template.Execute(w, p)
return template.Execute(w, p, markdown.OptNoDiff(r.config.NoDiffBlocks))
}

// pkg contains information about a go package, to be used in the template.
Expand Down
126 changes: 73 additions & 53 deletions internal/markdown/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,77 @@ import (
"unicode/utf8"
)

// ToMarkdown converts comment text to formatted Markdown.
// The comment was prepared by DocReader,
// so it is known not to have leading, trailing blank lines
// nor to have trailing spaces at the end of lines.
// The comment markers have already been removed.
//
// Each span of unindented non-blank lines is converted into
// a single paragraph. There is one exception to the rule: a span that
// consists of a single line, is followed by another paragraph span,
// begins with a capital letter, and contains no punctuation
// other than parentheses and commas is formatted as a heading.
//
// A span of indented lines is converted into a <pre> block,
// with the common indent prefix removed.
//
// URLs in the comment text are converted into links; if the URL also appears
// in the words map, the link is taken from the map (if the corresponding map
// value is the empty string, the URL is not converted into a link).
func ToMarkdown(w io.Writer, text string, opts ...Option) {
var o options
for _, f := range opts {
f(&o)
}

for _, b := range blocks(text, o.noDiffs) {
switch b.op {
case opPara:
// New paragraph
for _, line := range b.lines {
emphasize(w, line, o.words, false)
}
fmt.Fprint(w, "\n")
case opHead:
// Headline
fmt.Fprint(w, "## ")
for _, line := range b.lines {
fmt.Fprint(w, line)
}
fmt.Fprint(w, "\n\n")
case opPre:
// Code block
fmt.Fprintf(w, "```%s\n", b.lang)
for _, line := range b.lines {
emphasize(w, line, nil, false)
}
fmt.Fprint(w, "```\n\n")
}
}
}

// Option is option type for ToMarkdown
type Option func(*options)

// OptWords sets the list of known words.
// Go identifiers that appear in the words map are italicized; if the corresponding
// map value is not the empty string, it is considered a URL and the word is converted
// into a link.
func OptWords(words map[string]string) Option {
return func(o *options) { o.words = words }
}

// OptNoDiff disables automatic marking of code blocks as diffs.
func OptNoDiff(noDiffs bool) Option {
return func(o *options) { o.noDiffs = noDiffs }
}

type options struct {
words map[string]string
noDiffs bool
}

const (
// Regexp for Go identifiers
identRx = `[\pL_][\pL_0-9]*`
Expand Down Expand Up @@ -284,58 +355,7 @@ type block struct {
lang string // for opPre, the language of the code block.
}

var nonAlphaNumRx = regexp.MustCompile(`[^a-zA-Z0-9]`)

// ToMarkdown converts comment text to formatted Markdown.
// The comment was prepared by DocReader,
// so it is known not to have leading, trailing blank lines
// nor to have trailing spaces at the end of lines.
// The comment markers have already been removed.
//
// Each span of unindented non-blank lines is converted into
// a single paragraph. There is one exception to the rule: a span that
// consists of a single line, is followed by another paragraph span,
// begins with a capital letter, and contains no punctuation
// other than parentheses and commas is formatted as a heading.
//
// A span of indented lines is converted into a <pre> block,
// with the common indent prefix removed.
//
// URLs in the comment text are converted into links; if the URL also appears
// in the words map, the link is taken from the map (if the corresponding map
// value is the empty string, the URL is not converted into a link).
//
// Go identifiers that appear in the words map are italicized; if the corresponding
// map value is not the empty string, it is considered a URL and the word is converted
// into a link.
func ToMarkdown(w io.Writer, text string, words map[string]string) {
for _, b := range blocks(text) {
switch b.op {
case opPara:
// New paragraph
for _, line := range b.lines {
emphasize(w, line, words, false)
}
fmt.Fprint(w, "\n")
case opHead:
// Headline
fmt.Fprint(w, "## ")
for _, line := range b.lines {
fmt.Fprint(w, line)
}
fmt.Fprint(w, "\n\n")
case opPre:
// Code block
fmt.Fprintf(w, "```%s\n", b.lang)
for _, line := range b.lines {
emphasize(w, line, nil, false)
}
fmt.Fprint(w, "```\n\n")
}
}
}

func blocks(text string) []block {
func blocks(text string, skipDiffs bool) []block {
var (
out []block
para []string
Expand Down Expand Up @@ -394,7 +414,7 @@ func blocks(text string) []block {

// put those lines in a pre block
lang := "go"
if isValidDiff && anyDiff {
if isValidDiff && anyDiff && !skipDiffs {
lang = "diff"
}
out = append(out, block{op: opPre, lines: pre, lang: lang})
Expand Down
9 changes: 9 additions & 0 deletions internal/template/examples.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{ define "examples" }}
{{ if . }}

## Examples

{{ template "examplesNoHeading" . }}

{{ end }}
{{ end }}
17 changes: 17 additions & 0 deletions internal/template/examples_no_heading.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{ define "examplesNoHeading" }}
{{ if . }}

{{ range . }}

{{ if .Name }}### {{.Name}}{{ end }}

{{ doc .Doc }}

{{ if .Play }}{{gocode .Play}}{{ else }}{{gocode .Code.Text}}{{ end }}
{{ if .Output }} Output:

{{ code .Output }}{{ end }}
{{ end }}

{{ end }}
{{ end }}
18 changes: 18 additions & 0 deletions internal/template/functions.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{ define "functions" }}
{{ if .Funcs }}

## Functions

{{ range .Funcs }}

### func [{{ .Name }}]({{ urlOrName (index $.Files .Pos.File) }}#L{{ .Pos.Line }})

{{ inlineCode .Decl.Text }}

{{ doc .Doc }}

{{ template "examplesNoHeading" .Examples }}
{{ end }}

{{ end }}
{{ end }}
39 changes: 39 additions & 0 deletions internal/template/main.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# {{.Package.Name}}

{{if .Config.Badges.TravisCI -}}
[![Build Status](https://travis-ci.org/{{fullName .Package}}.svg?branch=master)](https://travis-ci.org/{{fullName .Package}})
{{end -}}
{{if .Config.Badges.CodeCov -}}
[![codecov](https://codecov.io/gh/{{fullName .Package}}/branch/master/graph/badge.svg)](https://codecov.io/gh/{{fullName .Package}})
{{end -}}
{{if .Config.Badges.GolangCI -}}
[![golangci](https://golangci.com/badges/{{importPath .Package}}.svg)](https://golangci.com/r/{{importPath .Package}})
{{end -}}
{{if .Config.Badges.GoDoc -}}
[![GoDoc](https://img.shields.io/badge/pkg.go.dev-doc-blue)](http://pkg.go.dev/{{importPath .Package}})
{{end -}}
{{if .Config.Badges.GoReportCard -}}
[![Go Report Card](https://goreportcard.com/badge/{{importPath .Package}})](https://goreportcard.com/report/{{importPath .Package}})
{{ end }}

{{ doc .Package.Doc }}

{{ if .Config.Functions }}
{{ template "functions" .Package }}
{{ end }}

{{ if .Config.Types }}
{{ template "types" .Package }}
{{ end }}

{{ if (not .Config.SkipSubPackages) }}
{{ template "subpackages" . }}
{{ end }}

{{ if (not .Config.SkipExamples) }}
{{ template "examples" .Package.Examples }}
{{ end }}
{{ if .Config.Credit }}
---
Readme created from Go doc with [goreadme](https://github.com/posener/goreadme)
{{ end }}
11 changes: 11 additions & 0 deletions internal/template/subpackages.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{ define "subpackages" }}
{{ if .SubPackages }}

## Sub Packages

{{ range .SubPackages }}
* [{{.Path}}](./{{.Path}}){{if .Package.Synopsis}}: {{.Package.Synopsis}}{{end}}
{{ end }}

{{ end }}
{{ end }}
Loading

0 comments on commit a049525

Please sign in to comment.