Added arn to the main repository

This commit is contained in:
2019-06-03 18:32:43 +09:00
parent cf258573a8
commit 29a48d94a5
465 changed files with 15968 additions and 288 deletions

80
arn/autodocs/Type.go Normal file
View File

@ -0,0 +1,80 @@
package autodocs
import (
"bufio"
"fmt"
"os"
"strings"
)
// Type represents a type in a Go source file.
type Type struct {
Name string
Comment string
LineNumber int
}
// Endpoint returns the REST endpoint for that type.
func (typ *Type) Endpoint() string {
return "/api/" + strings.ToLower(typ.Name) + "/"
}
// GitHubLink returns link to display the type in GitHub.
func (typ *Type) GitHubLink() string {
return fmt.Sprintf("https://github.com/animenotifier/notify.moe/tree/go/arn/blob/go/%s.go#L%d", typ.Name, typ.LineNumber)
}
// GetTypeDocumentation tries to gather documentation about the given type.
func GetTypeDocumentation(typeName string, filePath string) (*Type, error) {
typ := &Type{
Name: typeName,
}
file, err := os.Open(filePath)
if err != nil {
return typ, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineNumber := 0
var comments []string
for scanner.Scan() {
lineNumber++
line := scanner.Text()
line = strings.TrimSpace(line)
isComment := strings.HasPrefix(line, "// ")
if isComment {
comment := strings.TrimPrefix(line, "// ")
comments = append(comments, comment)
continue
}
if strings.HasPrefix(line, "type ") {
definedTypeName := strings.TrimPrefix(line, "type ")
space := strings.Index(definedTypeName, " ")
definedTypeName = definedTypeName[:space]
if definedTypeName == typeName {
typ.Comment = strings.Join(comments, " ")
typ.LineNumber = lineNumber
}
}
if !isComment {
comments = nil
}
}
if err := scanner.Err(); err != nil {
return typ, err
}
return typ, nil
}