This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Svetlin Ralchev
committed
Dec 13, 2019
1 parent
392bbce
commit dc93046
Showing
11 changed files
with
133 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package markdown | ||
|
||
import ( | ||
"fmt" | ||
"go/build" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/phogolabs/stride/codedom" | ||
"github.com/phogolabs/stride/contract" | ||
"github.com/phogolabs/stride/inflect" | ||
"github.com/phogolabs/stride/syntax" | ||
) | ||
|
||
// Generator builds the main | ||
type Generator struct { | ||
Path string | ||
Reporter contract.Reporter | ||
} | ||
|
||
// Generate generates the source code | ||
func (g *Generator) Generate(spec *codedom.SpecDescriptor) error { | ||
reporter := g.Reporter.With(contract.SeverityVeryHigh) | ||
|
||
if spec.Info == nil { | ||
return nil | ||
} | ||
|
||
reporter.Notice(" Generating markdown documentation...") | ||
|
||
project, err := filepath.Rel(filepath.Join(build.Default.GOPATH, "src"), g.Path) | ||
|
||
if err != nil { | ||
reporter.Error(" Generating markdown documentation fail: ", err) | ||
return err | ||
} | ||
|
||
ctx := map[string]interface{}{ | ||
"command": filepath.Base(g.Path), | ||
"project": project, | ||
"title": strings.TrimSpace(spec.Info.Title), | ||
"description": strings.TrimSpace(spec.Info.Description), | ||
"version": strings.TrimSpace(spec.Info.Version), | ||
} | ||
|
||
// generate README.md | ||
if err := g.sync(filepath.Join(g.Path, "README.md"), ctx); err != nil { | ||
reporter.Error(" Generating markdown documentation fail: ", err) | ||
return err | ||
} | ||
|
||
reporter.Success(" Generating markdown documentation successful!") | ||
return nil | ||
} | ||
|
||
func (g *Generator) sync(path string, ctx map[string]interface{}) error { | ||
reporter := g.Reporter.With(contract.SeverityHigh) | ||
|
||
var ( | ||
name = inflect.LowerCase(filepath.Base(path)) | ||
writer = &syntax.TemplateWriter{ | ||
Path: fmt.Sprintf("syntax/golang/%s.tpl", name), | ||
Context: ctx, | ||
} | ||
) | ||
|
||
file, err := os.Create(path) | ||
if err != nil { | ||
reporter.Error(" Generating markdown file: %s fail: %v", path, err) | ||
return err | ||
} | ||
|
||
defer file.Close() | ||
|
||
if _, err := writer.WriteTo(file); err != nil { | ||
reporter.Error(" Generating markdown file: %s fail: %v", path, err) | ||
return err | ||
} | ||
|
||
reporter.Notice(" Generating markdown file: %s successful", path) | ||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package golang | ||
package syntax | ||
|
||
import ( | ||
"bytes" | ||
|