Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtian committed Oct 23, 2022
0 parents commit 93d0d3f
Show file tree
Hide file tree
Showing 18 changed files with 1,074 additions and 0 deletions.
95 changes: 95 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# ---> Go
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
*.DS_Store

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
*.sum

# ---> JetBrains
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

*/log/

bin/
storage/
coverage.out
count.out
*.out
dist/

static/dist

cover.xml
coverage.html
coverage.txt
coverage.xml
report.html
report.xml

vendor/
45 changes: 45 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
project_name: type2md
builds:
- env:
- CGO_ENABLED=0
- GOPROXY="https://goproxy.cn,direct"
flags:
- -trimpath
ldflags:
- -s -w
- -X "main.Version={{ .Version }}"
- -X "main.CommitID={{ .ShortCommit }}"
- -X "main.BuildTime={{ .Date }}"
main: .

checksum:
name_template: 'checksums.txt'

changelog:
sort: asc
use: gitlab
groups:
- title: Features
regexp: "^.*feat[(\\w)]*:+.*$"
order: 100
- title: 'Bug fixes'
regexp: "^.*fix[(\\w)]*:+.*$"
order: 200
- title: 'Documentation updates'
regexp: "^.*docs[(\\w)]*:+.*$"
order: 400
- title: Others
order: 999
filters:
exclude:
- '^test:'
- '^chore'
- 'merge conflict'
- '^ci'
- '^style'
- Merge pull request
- Merge remote-tracking branch
- Merge branch

release:
footer: "Thanks for your support!"
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GO_BIN_PATH := $(if $(GOBIN),$(GOBIN),$(GOPATH)/bin)

VERSION ?= dev
BUILD_TIME ?= `date "+%Y-%m-%d %H:%M:%S"`
COMMIT_ID ?= `git rev-parse --short HEAD`

GO_BUILD := go build --trimpath --ldflags "-w -s \
-X 'main.Version=$(VERSION)' \
-X 'main.CommitID=$(COMMIT_ID)' \
-X 'main.BuildTime=$(BUILD_TIME)'"

all:
$(GO_BUILD) -o type2md ./*.go
mv type2md $(GO_BIN_PATH)/type2md
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# golang type define to markdown


## Usage

```go
package docs

//go:generate type2md github.com/eleztian/test Config

```

```shell
go install github.com/eleztian/type2md
go generate ./...
```
![img.png](docs/img.png)

33 changes: 33 additions & 0 deletions docs/doc_config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Config Doc
Config doc.

| key | 类型 | 必填 | 默认值 | 描述 |
|----------|----------|-----|------------------|--------------|
|Pre|[Hook](#ext.Hook)|false|||
|Post|[Hook](#ext.Hook)|false|||
|servers.{string}.host|string|false|||
|servers.{string}.port|int|true||- `22`<br>- `65522`|

## ext.Mode
**Type:** int

Mode mode define.

| Value | 描述 |
|----------|--------------|
|1|mode q.|
|2|mode a.|

## ext.Hook
Hook hook config.

| key | 类型 | 必填 | 默认值 | 描述 |
|----------|----------|-----|------------------|--------------|
|name|string|true|example|hook name.|
|commands.[].|string|false|||
|envs.{string}.|string|false|||
|mode|[Mode](#ext.Mode)|false|1|run mode.|

---
GENERATED BY THE COMMAND [type2md](https://github.com/eleztian/type2md)
from github.com/eleztian/type2md/test.Config
Binary file added docs/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/eleztian/type2md

go 1.18

require (
github.com/KyleBanks/depth v1.2.1
golang.org/x/tools v0.2.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/sys v0.1.0 // indirect
)
100 changes: 100 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"

"golang.org/x/mod/modfile"
)

var (
dstModPath string
dstTypes []string
)
var (
fileName = flag.String("f", "", "file path")
title = flag.String("t", "", "file title")
showVersion = flag.Bool("v", false, "show version")
)

func main() {
flag.Parse()

if *showVersion {
PrintVersion()
os.Exit(0)
}
dstModPath = os.Args[len(os.Args)-2]
dstTypes = strings.Split(os.Args[len(os.Args)-1], ",")

rootMod, _, _ := getModPath()
log.Println("Current Module:", rootMod)

parser, err := NewParser(dstModPath)
if err != nil {
log.Fatalf(err.Error())
}
for _, tp := range dstTypes {
log.Printf("start generate %s.%s\n", dstModPath, tp)
fs := parser.Parse(dstModPath, tp)

md := Markdown{
Title: *title,
MainStructName: dstModPath + "." + tp,
ObjTitleFunc: func(modPath string, typeName string) string {
modPath = strings.TrimPrefix(modPath, dstModPath)
modPath = strings.TrimPrefix(modPath, rootMod)
modPath = strings.TrimLeft(modPath, "./")
if modPath == "" {
return typeName
} else {
return fmt.Sprintf("%s.%s", modPath, typeName)
}
},
}
if md.Title == "" {
md.Title = tp + " Doc"
}

data := md.Generate(fs)

filename := *fileName
if filename == "" {
filename = tp + "_doc.md"
}
log.Printf("start to save to %s\n", filename)
_ = os.WriteFile(filename, data, 0655)
}
}

func getModPath() (string, string, error) {
current, _ := os.Getwd()
var path, _ = filepath.Abs(current)
for {
_, err := os.Stat(filepath.Join(path, "go.mod"))
if err != nil {
if !os.IsNotExist(err) {
return "", "", err
}
path, _ = filepath.Abs(filepath.Join(path, "../"))
if path == "" {
return "", "", nil
}
continue
}
content, err := os.ReadFile(filepath.Join(path, "go.mod"))
if err != nil {
return "", "", err
}
f, err := modfile.Parse("go.mod", content, nil)
if err != nil {
return "", "", err
}

return f.Module.Mod.Path, strings.Replace(current, path, f.Module.Mod.Path, 1), nil
}
}
26 changes: 26 additions & 0 deletions struct_comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"go/ast"
"strings"
)

func TrimComment(src string) string {
res := strings.Trim(src, "\n ")
if len(res) != 0 && res[len(res)-1] != '.' {
res += "."
}
return res
}

func GetDescribeFromComment(doc *ast.CommentGroup, comment *ast.CommentGroup) string {
res := ""
if doc != nil {
res += TrimComment(doc.Text())
}
if comment != nil {
res += TrimComment(comment.Text())
}

return res
}
33 changes: 33 additions & 0 deletions struct_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

type StructInfo struct {
Describe string `json:"describe"`
Fields []FieldInfo `json:"fields"`
Enums *EnumInfo `json:"enums"`
}

type FieldInfo struct {
Name string `json:"name"`
Type string `json:"type"`
Default string `json:"default"`
Require bool `json:"require"`
Enums EnumInfo `json:"enums"`
Describe string `json:"describe"`
Reference string `json:"reference"`
skipNum int
}

func (fi FieldInfo) Copy() FieldInfo {
n := fi
enums := make([][2]string, 0, len(fi.Enums.Names))
for _, d := range enums {
enums = append(enums, d)
}
n.Enums.Names = enums
return n
}

type EnumInfo struct {
Type string
Names [][2]string // key: desc
}
Loading

0 comments on commit 93d0d3f

Please sign in to comment.