-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat.go
More file actions
96 lines (85 loc) · 2.22 KB
/
format.go
File metadata and controls
96 lines (85 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"io/ioutil"
"github.com/chentaihan/phpNote/util"
)
const (
TR_HEADER_FORMAT = "<tr><th colspan=\"3\">%s</th></tr>"
TR_CONTENT_FORMAT = "<tr><td width=\"120px\">%d</td><td>%s</td><td>%s</td></tr>"
FUNNAME_LINK = "<a href=\"%s\">%s</a>"
)
var trIndex int = 0
type NoteClassify struct {
curChar byte
notes []*Note
}
//格式化输出到HTML文件
func FormatOutPut(noteList []*Note) {
var curChar byte = ' '
var noteClassify *NoteClassify
buffer := util.NewBufferWriter(len(noteList) * 4096)
for _, note := range noteList {
if note.funName[0] != curChar {
curChar = note.funName[0]
if noteClassify != nil {
data := noteClassify.GetBytes()
if data != nil {
buffer.Write(data)
}
}
noteClassify = &NoteClassify{
curChar: note.funName[0],
}
} else {
noteClassify.notes = append(noteClassify.notes, note)
}
}
if noteClassify != nil {
data := noteClassify.GetBytes()
if data != nil {
buffer.Write(data)
}
}
outputFile(buffer.GetBuffer())
}
func outputFile(data []byte) {
configFormat := util.GetFormatFile()
content, err := ioutil.ReadFile(configFormat)
if err != nil {
return
}
var index int = -1
for i := 0; i < len(content); i++ {
if content[i] == '$' && content[i+1] == '$' {
index = i
}
}
outputFile := util.GetFormatOutPutFile()
ioutil.WriteFile(outputFile, content[:index], 0777)
util.WriteFileAppend(outputFile, data, 0666)
util.WriteFileAppend(outputFile, content[index+2:], 0666)
}
func getTrHeader(curChar byte) string {
return fmt.Sprintf(TR_HEADER_FORMAT, string(curChar))
}
func getTrContent(noteList []*Note) []byte {
buffer := util.NewBufferWriter(4096)
for _, note := range noteList {
aLink := fmt.Sprintf(FUNNAME_LINK, note.funUrl, note.funName)
buffer.WriteString(fmt.Sprintf(TR_CONTENT_FORMAT, trIndex, aLink, note.funNote))
trIndex++
}
return buffer.GetBuffer()
}
func (noteClassify *NoteClassify) GetBytes() []byte {
if len(noteClassify.notes) > 0 {
buffer := util.NewBufferWriter(4096)
buffer.WriteString("<table>")
buffer.WriteString(getTrHeader(noteClassify.curChar))
buffer.Write(getTrContent(noteClassify.notes))
buffer.WriteString("</table><br/><br/>")
return buffer.GetBuffer()
}
return nil
}