Sfoglia il codice sorgente

Support displaying READMEs

Lucas Stadler 8 anni fa
parent
commit
2645dca8af
1 ha cambiato i file con 64 aggiunte e 8 eliminazioni
  1. 64 8
      quit.go

+ 64 - 8
quit.go

@ -57,14 +57,22 @@ func (fr *FancyRepo) Commit() (*FancyCommit, error) {
57 57
			return nil, err
58 58
		}
59 59
60
		fr.commit = &FancyCommit{commit: commit}
60
		fr.commit = NewFancyCommit(commit)
61 61
	}
62 62
	return fr.commit, nil
63 63
}
64 64
65 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 78
func (fc *FancyCommit) Author() string {
@ -98,10 +106,7 @@ func (fc *FancyCommit) Files() ([]*FancyFile, error) {
98 106
		}
99 107
100 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 110
			return 1
106 111
		})
107 112
		if err != nil {
@ -111,12 +116,44 @@ func (fc *FancyCommit) Files() ([]*FancyFile, error) {
111 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 147
type FancyFile struct {
115 148
	repo   *git.Repository
116 149
	entry  *git.TreeEntry
117 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 157
func (fc *FancyFile) Name() string {
121 158
	return fc.entry.Name
122 159
}
@ -128,7 +165,7 @@ func (fc *FancyFile) Commit() (*FancyCommit, error) {
128 165
			return nil, err
129 166
		}
130 167
131
		fc.commit = &FancyCommit{commit: commit}
168
		fc.commit = NewFancyCommit(commit)
132 169
	}
133 170
	return fc.commit, nil
134 171
}
@ -137,6 +174,15 @@ func (fc *FancyFile) Type() string {
137 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 186
var repoTmpl = template.Must(template.New("").Parse(`<!doctype html>
141 187
<html>
142 188
	<head>
@ -159,6 +205,16 @@ var repoTmpl = template.Must(template.New("").Parse(`<!doctype html>
159 205
				<td class="file-name file-type-{{ $file.Type }}">{{ $file.Name }}</td>
160 206
			</tr>
161 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 218
	</body>
163 219
</html>
164 220
`))