Преглед на файлове

Support displaying READMEs

Lucas Stadler преди 8 години
родител
ревизия
2645dca8af
променени са 1 файла, в които са добавени 64 реда и са изтрити 8 реда
  1. 64 8
      quit.go

+ 64 - 8
quit.go

57
			return nil, err
57
			return nil, err
58
		}
58
		}
59
59
60
		fr.commit = &FancyCommit{commit: commit}
60
		fr.commit = NewFancyCommit(commit)
61
	}
61
	}
62
	return fr.commit, nil
62
	return fr.commit, nil
63
}
63
}
64
64
65
type FancyCommit struct {
65
type FancyCommit struct {
66
	commit *git.Commit
67
	files  []*FancyFile
66
	commit     *git.Commit
67
	files      []*FancyFile
68
	filesCache map[string]*FancyFile
69
}
70
71
func NewFancyCommit(commit *git.Commit) *FancyCommit {
72
	return &FancyCommit{
73
		commit:     commit,
74
		filesCache: make(map[string]*FancyFile),
75
	}
68
}
76
}
69
77
70
func (fc *FancyCommit) Author() string {
78
func (fc *FancyCommit) Author() string {
98
		}
106
		}
99
107
100
		err = tree.Walk(func(s string, entry *git.TreeEntry) int {
108
		err = tree.Walk(func(s string, entry *git.TreeEntry) int {
101
			fc.files = append(fc.files, &FancyFile{
102
				repo:  fc.commit.Owner(),
103
				entry: entry,
104
			})
109
			fc.files = append(fc.files, NewFancyFile(fc.commit.Owner(), entry))
105
			return 1
110
			return 1
106
		})
111
		})
107
		if err != nil {
112
		if err != nil {
111
	return fc.files, nil
116
	return fc.files, nil
112
}
117
}
113
118
119
var readmeNames = []string{"README.md", "readme.md", "README.rst", "readme.rst", "README.txt", "readme.txt", "README"}
120
121
func (fc *FancyCommit) Readme() (*FancyFile, error) {
122
	readme := fc.filesCache["|||README|||"]
123
	if readme == nil {
124
		tree, err := fc.commit.Tree()
125
		if err != nil {
126
			return nil, err
127
		}
128
129
		var entry *git.TreeEntry
130
		for _, n := range readmeNames {
131
			entry, err = tree.EntryByPath(n)
132
			if err == nil {
133
				break
134
			}
135
		}
136
137
		if entry == nil {
138
			return nil, nil
139
		}
140
141
		readme = NewFancyFile(fc.commit.Owner(), entry)
142
		fc.filesCache["|||README|||"] = readme
143
	}
144
	return readme, nil
145
}
146
114
type FancyFile struct {
147
type FancyFile struct {
115
	repo   *git.Repository
148
	repo   *git.Repository
116
	entry  *git.TreeEntry
149
	entry  *git.TreeEntry
117
	commit *FancyCommit
150
	commit *FancyCommit
118
}
151
}
119
152
153
func NewFancyFile(repo *git.Repository, entry *git.TreeEntry) *FancyFile {
154
	return &FancyFile{repo: repo, entry: entry}
155
}
156
120
func (fc *FancyFile) Name() string {
157
func (fc *FancyFile) Name() string {
121
	return fc.entry.Name
158
	return fc.entry.Name
122
}
159
}
128
			return nil, err
165
			return nil, err
129
		}
166
		}
130
167
131
		fc.commit = &FancyCommit{commit: commit}
168
		fc.commit = NewFancyCommit(commit)
132
	}
169
	}
133
	return fc.commit, nil
170
	return fc.commit, nil
134
}
171
}
137
	return strings.ToLower(fc.entry.Type.String())
174
	return strings.ToLower(fc.entry.Type.String())
138
}
175
}
139
176
177
func (fc *FancyFile) Contents() (string, error) {
178
	blob, err := fc.repo.LookupBlob(fc.entry.Id)
179
	if err != nil {
180
		return "", err
181
	}
182
183
	return string(blob.Contents()), nil
184
}
185
140
var repoTmpl = template.Must(template.New("").Parse(`<!doctype html>
186
var repoTmpl = template.Must(template.New("").Parse(`<!doctype html>
141
<html>
187
<html>
142
	<head>
188
	<head>
159
				<td class="file-name file-type-{{ $file.Type }}">{{ $file.Name }}</td>
205
				<td class="file-name file-type-{{ $file.Type }}">{{ $file.Name }}</td>
160
			</tr>
206
			</tr>
161
			{{ end }}
207
			{{ end }}
208
		</table>
209
210
		{{ if .Repo.Commit.Readme }}
211
		<section id="file" class="readme">
212
			<h1>{{ .Repo.Commit.Readme.Name }}</h1>
213
			<code>
214
				<pre>{{ .Repo.Commit.Readme.Contents }}</pre>
215
			</code>
216
		</section>
217
		{{ end }}
162
	</body>
218
	</body>
163
</html>
219
</html>
164
`))
220
`))