Skip to content

Commit 706411f

Browse files
committed
split renderWikiPage into 3 functions
Signed-off-by: Michael Gnehr <[email protected]>
1 parent 9208fd3 commit 706411f

File tree

1 file changed

+131
-65
lines changed

1 file changed

+131
-65
lines changed

routers/repo/wiki.go

+131-65
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName strin
121121
return wikiContentsByEntry(ctx, entry), entry, pageFilename, false
122122
}
123123

124-
func renderWikiPage(ctx *context.Context, isViewPage bool, isFileHistory bool) (*git.Repository, *git.TreeEntry) {
124+
func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
125125
wikiRepo, commit, err := findWikiRepoCommit(ctx)
126126
if err != nil {
127127
if !git.IsErrNotExist(err) {
@@ -131,34 +131,32 @@ func renderWikiPage(ctx *context.Context, isViewPage bool, isFileHistory bool) (
131131
}
132132

133133
// Get page list.
134-
if isViewPage {
135-
entries, err := commit.ListEntries()
136-
if err != nil {
137-
ctx.ServerError("ListEntries", err)
138-
return nil, nil
134+
entries, err := commit.ListEntries()
135+
if err != nil {
136+
ctx.ServerError("ListEntries", err)
137+
return nil, nil
138+
}
139+
pages := make([]PageMeta, 0, len(entries))
140+
for _, entry := range entries {
141+
if !entry.IsRegular() {
142+
continue
139143
}
140-
pages := make([]PageMeta, 0, len(entries))
141-
for _, entry := range entries {
142-
if !entry.IsRegular() {
143-
continue
144-
}
145-
wikiName, err := models.WikiFilenameToName(entry.Name())
146-
if err != nil {
147-
if models.IsErrWikiInvalidFileName(err) {
148-
continue
149-
}
150-
ctx.ServerError("WikiFilenameToName", err)
151-
return nil, nil
152-
} else if wikiName == "_Sidebar" || wikiName == "_Footer" {
144+
wikiName, err := models.WikiFilenameToName(entry.Name())
145+
if err != nil {
146+
if models.IsErrWikiInvalidFileName(err) {
153147
continue
154148
}
155-
pages = append(pages, PageMeta{
156-
Name: wikiName,
157-
SubURL: models.WikiNameToSubURL(wikiName),
158-
})
149+
ctx.ServerError("WikiFilenameToName", err)
150+
return nil, nil
151+
} else if wikiName == "_Sidebar" || wikiName == "_Footer" {
152+
continue
159153
}
160-
ctx.Data["Pages"] = pages
154+
pages = append(pages, PageMeta{
155+
Name: wikiName,
156+
SubURL: models.WikiNameToSubURL(wikiName),
157+
})
161158
}
159+
ctx.Data["Pages"] = pages
162160

163161
// get requested pagename
164162
pageName := models.NormalizeWikiName(ctx.Params(":page"))
@@ -180,59 +178,127 @@ func renderWikiPage(ctx *context.Context, isViewPage bool, isFileHistory bool) (
180178
return nil, nil
181179
}
182180

183-
if isViewPage {
184-
sidebarContent, _, _, _ := wikiContentsByName(ctx, commit, "_Sidebar")
185-
if ctx.Written() {
186-
return nil, nil
187-
}
181+
sidebarContent, _, _, _ := wikiContentsByName(ctx, commit, "_Sidebar")
182+
if ctx.Written() {
183+
return nil, nil
184+
}
188185

189-
footerContent, _, _, _ := wikiContentsByName(ctx, commit, "_Footer")
190-
if ctx.Written() {
191-
return nil, nil
186+
footerContent, _, _, _ := wikiContentsByName(ctx, commit, "_Footer")
187+
if ctx.Written() {
188+
return nil, nil
189+
}
190+
191+
metas := ctx.Repo.Repository.ComposeMetas()
192+
ctx.Data["content"] = markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
193+
ctx.Data["sidebarPresent"] = sidebarContent != nil
194+
ctx.Data["sidebarContent"] = markdown.RenderWiki(sidebarContent, ctx.Repo.RepoLink, metas)
195+
ctx.Data["footerPresent"] = footerContent != nil
196+
ctx.Data["footerContent"] = markdown.RenderWiki(footerContent, ctx.Repo.RepoLink, metas)
197+
198+
// get commit count - wiki revisions
199+
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
200+
ctx.Data["CommitCount"] = commitsCount
201+
202+
return wikiRepo, entry
203+
}
204+
205+
func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
206+
wikiRepo, commit, err := findWikiRepoCommit(ctx)
207+
if err != nil {
208+
if !git.IsErrNotExist(err) {
209+
ctx.ServerError("GetBranchCommit", err)
192210
}
211+
return nil, nil
212+
}
193213

194-
metas := ctx.Repo.Repository.ComposeMetas()
195-
ctx.Data["content"] = markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
196-
ctx.Data["sidebarPresent"] = sidebarContent != nil
197-
ctx.Data["sidebarContent"] = markdown.RenderWiki(sidebarContent, ctx.Repo.RepoLink, metas)
198-
ctx.Data["footerPresent"] = footerContent != nil
199-
ctx.Data["footerContent"] = markdown.RenderWiki(footerContent, ctx.Repo.RepoLink, metas)
200-
} else {
201-
ctx.Data["content"] = string(data)
202-
ctx.Data["sidebarPresent"] = false
203-
ctx.Data["sidebarContent"] = ""
204-
ctx.Data["footerPresent"] = false
205-
ctx.Data["footerContent"] = ""
214+
// get requested pagename
215+
pageName := models.NormalizeWikiName(ctx.Params(":page"))
216+
if len(pageName) == 0 {
217+
pageName = "Home"
206218
}
219+
ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
220+
ctx.Data["old_title"] = pageName
221+
ctx.Data["Title"] = pageName
222+
ctx.Data["title"] = pageName
223+
ctx.Data["RequireHighlightJS"] = true
224+
225+
//lookup filename in wiki - get filecontent, gitTree entry , real filename
226+
data, entry, pageFilename, noEntry := wikiContentsByName(ctx, commit, pageName)
227+
if noEntry {
228+
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
229+
}
230+
if entry == nil || ctx.Written() {
231+
return nil, nil
232+
}
233+
234+
ctx.Data["content"] = string(data)
235+
ctx.Data["sidebarPresent"] = false
236+
ctx.Data["sidebarContent"] = ""
237+
ctx.Data["footerPresent"] = false
238+
ctx.Data["footerContent"] = ""
207239

208240
// get commit count - wiki revisions
209241
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
210242
ctx.Data["CommitCount"] = commitsCount
211243

212-
if isFileHistory {
213-
// get page
214-
page := ctx.QueryInt("page")
215-
if page <= 1 {
216-
page = 1
217-
}
244+
// get page
245+
page := ctx.QueryInt("page")
246+
if page <= 1 {
247+
page = 1
248+
}
218249

219-
// get Commit Count
220-
commitsHistory, err := wikiRepo.CommitsByFileAndRange("master", pageFilename, page)
221-
if err != nil {
222-
ctx.ServerError("CommitsByFileAndRange", err)
223-
return nil, nil
250+
// get Commit Count
251+
commitsHistory, err := wikiRepo.CommitsByFileAndRange("master", pageFilename, page)
252+
if err != nil {
253+
ctx.ServerError("CommitsByFileAndRange", err)
254+
return nil, nil
255+
}
256+
commitsHistory = models.ValidateCommitsWithEmails(commitsHistory)
257+
commitsHistory = models.ParseCommitsWithSignature(commitsHistory)
258+
259+
ctx.Data["Commits"] = commitsHistory
260+
261+
pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
262+
pager.SetDefaultParams(ctx)
263+
ctx.Data["Page"] = pager
264+
265+
return wikiRepo, entry
266+
}
267+
268+
func renderEditPage(ctx *context.Context) {
269+
_, commit, err := findWikiRepoCommit(ctx)
270+
if err != nil {
271+
if !git.IsErrNotExist(err) {
272+
ctx.ServerError("GetBranchCommit", err)
224273
}
225-
commitsHistory = models.ValidateCommitsWithEmails(commitsHistory)
226-
commitsHistory = models.ParseCommitsWithSignature(commitsHistory)
274+
return
275+
}
227276

228-
ctx.Data["Commits"] = commitsHistory
277+
// get requested pagename
278+
pageName := models.NormalizeWikiName(ctx.Params(":page"))
279+
if len(pageName) == 0 {
280+
pageName = "Home"
281+
}
282+
ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
283+
ctx.Data["old_title"] = pageName
284+
ctx.Data["Title"] = pageName
285+
ctx.Data["title"] = pageName
286+
ctx.Data["RequireHighlightJS"] = true
229287

230-
pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
231-
pager.SetDefaultParams(ctx)
232-
ctx.Data["Page"] = pager
288+
//lookup filename in wiki - get filecontent, gitTree entry , real filename
289+
data, entry, _, noEntry := wikiContentsByName(ctx, commit, pageName)
290+
if noEntry {
291+
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
292+
}
293+
if entry == nil || ctx.Written() {
294+
return
233295
}
234296

235-
return wikiRepo, entry
297+
ctx.Data["content"] = string(data)
298+
ctx.Data["sidebarPresent"] = false
299+
ctx.Data["sidebarContent"] = ""
300+
ctx.Data["footerPresent"] = false
301+
ctx.Data["footerContent"] = ""
236302
}
237303

238304
// Wiki renders single wiki page
@@ -246,7 +312,7 @@ func Wiki(ctx *context.Context) {
246312
return
247313
}
248314

249-
wikiRepo, entry := renderWikiPage(ctx, true, false)
315+
wikiRepo, entry := renderViewPage(ctx)
250316
if ctx.Written() {
251317
return
252318
}
@@ -283,7 +349,7 @@ func WikiRevision(ctx *context.Context) {
283349
return
284350
}
285351

286-
wikiRepo, entry := renderWikiPage(ctx, false, true)
352+
wikiRepo, entry := renderRevisionPage(ctx)
287353
if ctx.Written() {
288354
return
289355
}
@@ -457,7 +523,7 @@ func EditWiki(ctx *context.Context) {
457523
return
458524
}
459525

460-
renderWikiPage(ctx, false, false)
526+
renderEditPage(ctx)
461527
if ctx.Written() {
462528
return
463529
}

0 commit comments

Comments
 (0)