Skip to content

Commit e87fa4c

Browse files
committed
更新至 v5.2.1
favicon 和 logo 固定为 SVG 格式,Firefox 支持 SVG 图片作为 favicon, 而 chrome 将在 80 版本支持 SVG,safari 也支持使用 mask-icon 作为图片。 修正 static.Pack 可能的错误,不再允许打包目标文件夹中的二进制文件, 将 favicon 允许的格式改为 SVG,也将唯一的进制文件 icon.png 去掉了。
1 parent 6bab7ad commit e87fa4c

File tree

18 files changed

+139
-95
lines changed

18 files changed

+139
-95
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ dist
1919
# project
2020
.apidoc.yaml
2121
.apidoc.yml
22-
/.vscode
2322
apidoc.json
2423
apidoc.yaml
2524
apidoc.xml
2625

2726
.vscode
2827
.idea
28+
29+
.testdata

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# CHANGELOG
22

3-
## [Unreleased]
3+
## [v5.2.1]
4+
5+
### Changed
6+
7+
- favicon 现在只支持 SVG 格式的图片;
8+
9+
### Fixed
10+
11+
- 修正 Pack 可能将二进制等文件进行打包的错误;
412

513
## [v5.2.0]
614

apidoc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func Pack(h *message.Handler, url string, contentType, pkgName, varName, path st
106106
contentType = http.DetectContentType(data)
107107
}
108108

109-
return static.Pack("./docs", pkgName, varName, path, t, nil, &static.FileInfo{
109+
return static.Pack("./docs", pkgName, varName, path, t, &static.FileInfo{
110110
Name: url,
111111
Content: data,
112112
ContentType: contentType,

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Config struct {
4848

4949
// LoadConfig 加载指定目录下的配置文件
5050
//
51-
// 所有的错误信息会输出到 h
51+
// 所有的错误信息会输出到 h,在出错时,会返回 nil
5252
func LoadConfig(h *message.Handler, wd string) *Config {
5353
for _, filename := range vars.AllowConfigFilenames {
5454
p := filepath.Join(wd, filename)

config_test.go

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package apidoc
55
import (
66
"bytes"
77
"path/filepath"
8-
"strconv"
98
"testing"
109
"time"
1110

@@ -14,15 +13,40 @@ import (
1413
"github.com/caixw/apidoc/v5/input"
1514
"github.com/caixw/apidoc/v5/internal/vars"
1615
"github.com/caixw/apidoc/v5/message"
16+
"github.com/caixw/apidoc/v5/static"
1717
)
1818

19-
func buildMessageHandle() (*bytes.Buffer, message.HandlerFunc) {
20-
buf := new(bytes.Buffer)
21-
22-
return buf, func(msg *message.Message) {
23-
buf.WriteString(strconv.Itoa(int(msg.Type)))
24-
buf.WriteString(msg.Message)
19+
func buildMessageHandle() (erro, succ *bytes.Buffer, h *message.Handler) {
20+
erro = new(bytes.Buffer)
21+
succ = new(bytes.Buffer)
22+
23+
f := func(msg *message.Message) {
24+
switch msg.Type {
25+
case message.Erro:
26+
erro.WriteString(msg.Message)
27+
default:
28+
succ.WriteString(msg.Message)
29+
}
2530
}
31+
32+
return erro, succ, message.NewHandler(f)
33+
}
34+
35+
func TestLoadConfig(t *testing.T) {
36+
a := assert.New(t)
37+
38+
erro, succ, h := buildMessageHandle()
39+
cfg := LoadConfig(h, "./")
40+
a.NotNil(cfg).
41+
Empty(erro.String())
42+
43+
erro, succ, h = buildMessageHandle()
44+
cfg = LoadConfig(h, "./docs") // 不存在 apidoc 的配置文件
45+
46+
h.Stop()
47+
a.Nil(cfg).
48+
NotEmpty(erro.String()).
49+
Empty(succ.String())
2650
}
2751

2852
func TestLoadFile(t *testing.T) {
@@ -42,9 +66,9 @@ func TestDetect_Load(t *testing.T) {
4266
a.NotError(err).NotEmpty(wd)
4367
a.NotError(Detect(wd, true))
4468

45-
out, f := buildMessageHandle()
46-
cfg := LoadConfig(message.NewHandler(f), wd)
47-
a.Empty(out.String()).NotNil(cfg)
69+
erro, _, h := buildMessageHandle()
70+
cfg := LoadConfig(h, wd)
71+
a.Empty(erro.String()).NotNil(cfg)
4872

4973
a.Equal(cfg.Version, vars.Version()).
5074
Equal(cfg.Inputs[0].Lang, "go")
@@ -78,21 +102,55 @@ func TestConfig_sanitize(t *testing.T) {
78102
Equal(err.Field, "output")
79103
}
80104

105+
func TestConfig_Test(t *testing.T) {
106+
a := assert.New(t)
107+
108+
erro, succ, h := buildMessageHandle()
109+
cfg := LoadConfig(h, "./docs/example")
110+
a.NotNil(cfg)
111+
cfg.Test()
112+
113+
h.Stop()
114+
a.Empty(erro.String()).
115+
NotEmpty(succ.String()) // 有成功提示
116+
}
117+
118+
func TestConfig_Pack(t *testing.T) {
119+
a := assert.New(t)
120+
121+
erro, succ, h := buildMessageHandle()
122+
cfg := LoadConfig(h, "./docs/example")
123+
a.NotNil(cfg)
124+
cfg.Pack("testdata", "Data", "./.testdata", "apidoc.xml", "application/xml", static.TypeAll)
125+
126+
h.Stop()
127+
a.Empty(erro.String()).
128+
Empty(succ.String())
129+
}
130+
81131
func TestConfig_Do(t *testing.T) {
82132
a := assert.New(t)
83133

84-
out, f := buildMessageHandle()
85-
h := message.NewHandler(f)
86-
LoadConfig(h, "./docs/example").Do(time.Now())
87-
a.Empty(out.String())
134+
erro, succ, h := buildMessageHandle()
135+
cfg := LoadConfig(h, "./docs/example")
136+
a.NotNil(cfg)
137+
cfg.Do(time.Now())
138+
139+
h.Stop()
140+
a.NotEmpty(succ.String()). // 有成功提示
141+
Empty(erro.String())
88142
}
89143

90144
func TestConfig_Buffer(t *testing.T) {
91145
a := assert.New(t)
92146

93-
out, f := buildMessageHandle()
94-
h := message.NewHandler(f)
95-
buf := LoadConfig(h, "./docs/example").Buffer()
96-
a.Empty(out.String()).
147+
erro, succ, h := buildMessageHandle()
148+
cfg := LoadConfig(h, "./docs/example")
149+
a.NotNil(cfg)
150+
151+
buf := cfg.Buffer()
152+
h.Stop()
153+
a.Empty(erro.String()).
154+
Empty(succ.String()).
97155
True(buf.Len() > 0)
98156
}

docs/example/.apidoc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 5.0.0
1+
version: 5.2.0
22
inputs:
33
- lang: c++
44
dir: .

docs/example/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<?xml-stylesheet type="text/xsl" href="../v5/apidoc.xsl"?>
4-
<apidoc apidoc="5.2.0" created="2019-12-17T21:57:24+08:00" version="1.1.1">
4+
<apidoc apidoc="5.2.1" created="2019-12-18T00:16:56+08:00" version="1.1.1">
55
<title>示例文档</title>
66
<description type="html"><![CDATA[
77

docs/icon.png

-2.88 KB
Binary file not shown.

docs/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<description><p>用于描述整个文档的相关内容,只能出现一次。</p></description>
122122
<item name="@version">文档的版本</item>
123123
<item name="@lang">内容的本地化 ID,比如 <samp><var>zh-hans</var></samp> 等。</item>
124-
<item name="@logo">图标</item>
124+
<item name="@logo">图标,默认采用官网的 <var>https://apidoc.tools/icon.svg</var>,同时作用于 favicon 和 logo,只支持 SVG 格式。</item>
125125
<item name="@created">文档的生成时间</item>
126126
<item name="title">文档的标题</item>
127127
<item name="description">文档的整体介绍,可以是使用 HTML 内容。</item>

docs/index.xsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
<meta charset="UTF-8" />
3333
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"/>
3434
<meta name="keywords" content="{$keywords}RESTful API,document,apidoc" />
35-
<link rel="icon" type="image/png" href="./icon.png" />
35+
<link rel="icon" type="image/svg+xml" href="./icon.svg" />
36+
<link rel="mask-icon" type="image/svg+xml" href="./icon.svg" color="black" />
3637
<link rel="canonical" href="{document('config.xml')/config/url}" />
3738
<link rel="stylesheet" type="text/css" href="./index.css" />
3839
<link rel="license" href="{/docs/liense/@url}" />

docs/index.zh-hant.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<description><p>用於描述整個文檔的相關內容,只能出現壹次。</p></description>
122122
<item name="@version">文檔的版本</item>
123123
<item name="@lang">內容的本地化 ID,比如 <samp><var>zh-hans</var></samp> 等。</item>
124-
<item name="@logo">圖標</item>
124+
<item name="@logo">圖標,默認采用官網的 <var>https://apidoc.tools/icon.svg</var>,同時作用於 favicon 和 logo,只支持 SVG 格式。</item>
125125
<item name="@created">文檔的生成時間</item>
126126
<item name="title">文檔的標題</item>
127127
<item name="description">文檔的整體介紹,可以是使用 HTML 內容。</item>

docs/v5/apidoc.xsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
<meta charset="UTF-8" />
1818
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
1919
<meta name="generator" content="apidoc" />
20-
<link rel="icon" type="image/png" href="{$icon}" />
20+
<link rel="icon" type="image/svg+xml" href="{$icon}" />
21+
<link rel="mask-icon" type="image/svg+xml" href="{$icon}" color="black" />
2122
<xsl:if test="apidoc/license"><link rel="license" href="{apidoc/license/@url}" /></xsl:if>
2223
<link rel="stylesheet" type="text/css" href="{$base-url}apidoc.css" />
2324
<script src="{$base-url}apidoc.js"></script>
@@ -509,7 +510,7 @@
509510
<xsl:value-of select="/apidoc/@logo" />
510511
</xsl:when>
511512
<xsl:otherwise>
512-
<xsl:value-of select="concat($base-url, '../icon.png')" />
513+
<xsl:value-of select="concat($base-url, '../icon.svg')" />
513514
</xsl:otherwise>
514515
</xsl:choose>
515516
</xsl:variable>

internal/vars/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "strings"
88
//
99
// 遵守 https://semver.org/lang/zh-CN/ 规则。
1010
// 程序不兼容或是文档格式不兼容时,需要提升主版本号。
11-
const version = "5.2.0"
11+
const version = "5.2.1"
1212

1313
var (
1414
fullVersion = version

static/pack.go

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33
package static
44

55
import (
6-
"bufio"
76
"bytes"
87
"io/ioutil"
98
"net/http"
109
"os"
1110
"path/filepath"
12-
"strings"
13-
"unicode"
1411

1512
"github.com/issue9/utils"
16-
17-
"github.com/caixw/apidoc/v5/internal/locale"
18-
"github.com/caixw/apidoc/v5/message"
1913
)
2014

21-
const goModPath = "../go.mod"
15+
const modulePath = "github.com/caixw/apidoc/v5/static"
2216

2317
const header = "// 当前文件由工具自动生成,请勿手动修改!\n\n"
2418

19+
var allowPackExts = []string{
20+
".xml", ".xsl", ".svg",
21+
".css", ".js", ".html", ".htm",
22+
}
23+
2524
// FileInfo 被打包文件的信息
2625
type FileInfo struct {
2726
// 相对于打包根目录的地址,同时也会被作为路由地址
@@ -39,6 +38,8 @@ type FileInfo struct {
3938
// path 内容保存的文件名;
4039
// t 打包的文件类型,如果为 TypeNone,则只打包 addTo 的内容;
4140
// addTo 追加的打包内容;
41+
//
42+
// NOTE: 隐藏文件不会被打包
4243
func Pack(root, pkgName, varName, path string, t Type, addTo ...*FileInfo) error {
4344
fis, err := getFileInfos(root, t)
4445
if err != nil {
@@ -58,11 +59,7 @@ func Pack(root, pkgName, varName, path string, t Type, addTo ...*FileInfo) error
5859

5960
ws("package ", pkgName, "\n\n")
6061

61-
goMod, err := getPkgPath(goModPath)
62-
if err != nil {
63-
return err
64-
}
65-
ws("import \"", goMod+"/static", "\"\n\n")
62+
ws("import \"", modulePath, "\"\n\n")
6663

6764
ws("var ", varName, "= []*static.FileInfo{")
6865
for _, info := range fis {
@@ -82,24 +79,25 @@ func getFileInfos(root string, t Type) ([]*FileInfo, error) {
8279
return nil, nil
8380
}
8481

85-
paths := []string{}
82+
var paths []string
8683

8784
walk := func(path string, info os.FileInfo, err error) error {
8885
if err != nil {
8986
return err
9087
}
9188

92-
if info.IsDir() {
89+
// 过滤各类未知的隐藏文件
90+
if info.IsDir() || !isAllowPackFile(filepath.Ext(info.Name())) {
9391
return nil
9492
}
9593

96-
relpath, err := filepath.Rel(root, path)
94+
relPath, err := filepath.Rel(root, path)
9795
if err != nil {
9896
return err
9997
}
10098

101-
if t != TypeStylesheet || isStylesheetFile(relpath) {
102-
paths = append(paths, relpath)
99+
if t != TypeStylesheet || isStylesheetFile(relPath) {
100+
paths = append(paths, relPath)
103101
}
104102

105103
return nil
@@ -144,30 +142,11 @@ func dump(buf *bytes.Buffer, file *FileInfo) (err error) {
144142
return err
145143
}
146144

147-
const modulePrefix = "module"
148-
149-
// 分析 go.mod 文件,获取其中的 module 值
150-
func getPkgPath(path string) (string, error) {
151-
file, err := os.Open(path)
152-
if err != nil {
153-
return "", err
154-
}
155-
156-
s := bufio.NewScanner(bufio.NewReader(file))
157-
s.Split(bufio.ScanLines)
158-
for s.Scan() {
159-
line := strings.TrimSpace(s.Text())
160-
if !strings.HasPrefix(line, modulePrefix) {
161-
continue
145+
func isAllowPackFile(ext string) bool {
146+
for _, e := range allowPackExts {
147+
if e == ext {
148+
return true
162149
}
163-
164-
line = line[len(modulePrefix):]
165-
if line == "" || !unicode.IsSpace(rune(line[0])) {
166-
continue
167-
}
168-
169-
return strings.TrimSpace(line), nil
170150
}
171-
172-
return "", message.NewLocaleError(goModPath, "", 0, locale.ErrInvalidFormat)
151+
return false
173152
}

0 commit comments

Comments
 (0)