Skip to content

Commit

Permalink
Feature - Issue go-gitea#170 - Update YAML Viewer (go-gitea#191)
Browse files Browse the repository at this point in the history
* Changes tabular YAML view to only work with toc.yaml files

* Used switch for YAML module per code review

* Uses a 'type' switch as per code review
  • Loading branch information
richmahn authored and Richard Mahn committed Jun 7, 2017
1 parent 9b0ac3d commit 5fbc52c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
30 changes: 22 additions & 8 deletions modules/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ func renderHorizontalHtmlTable(m yaml.MapSlice) string {
key := mi.Key
value := mi.Value

if key != nil && reflect.TypeOf(key).String() == "yaml.MapSlice" {
switch key.(type) {
case yaml.MapSlice:
key = renderHorizontalHtmlTable(key.(yaml.MapSlice))
}
thead += fmt.Sprintf("<th>%v</th>", key)

if value != nil && reflect.TypeOf(value).String() == "yaml.MapSlice" {
switch value.(type) {
case yaml.MapSlice:
value = renderHorizontalHtmlTable(value.(yaml.MapSlice))
case []interface {}:
value = value.([]interface{})
v := make([]yaml.MapSlice, len(value.([]interface{})))
for i, vs := range value.([]interface{}) {
v[i] = vs.(yaml.MapSlice)
}
value = renderVerticalHtmlTable(v)
}
tbody += fmt.Sprintf("<td>%v</td>", value)
}
Expand All @@ -65,9 +74,10 @@ func renderVerticalHtmlTable(m []yaml.MapSlice) string {
value := mi.Value

table += `<tr>`
if key != nil && reflect.TypeOf(key).String() == "yaml.MapSlice" {
switch key.(type) {
case yaml.MapSlice:
key = renderHorizontalHtmlTable(key.(yaml.MapSlice))
} else if key != nil && reflect.TypeOf(key).String() == "[]interface {}" {
case []interface {}:
var ks string
for _, ki := range key.([]interface{}) {
log.Info("KI: %v", ki)
Expand All @@ -78,21 +88,25 @@ func renderVerticalHtmlTable(m []yaml.MapSlice) string {
}
table += fmt.Sprintf("<td>%v</td>", key)

if value != nil && reflect.TypeOf(value).String() == "yaml.MapSlice" {
switch value.(type) {
case yaml.MapSlice:
value = renderHorizontalHtmlTable(value.(yaml.MapSlice))
} else if value != nil && reflect.TypeOf(value).String() == "[]interface {}" {
case []interface {}:
value = value.([]interface{})
v := make([]yaml.MapSlice, len(value.([]interface{})))
for i, vs := range value.([]interface{}) {
v[i] = vs.(yaml.MapSlice)
}
value = renderVerticalHtmlTable(v)
}
if key == "slug" {

switch key {
case "slug":
value = fmt.Sprintf(`<a href="content/%v.md">%v</a>`, value, value)
case "link":
value = fmt.Sprintf(`<a href="%v/01.md">%v</a>`, value, value)
}
table += fmt.Sprintf("<td>%v</td>", value)

table += `</tr>`
}
table += "</table>"
Expand Down
16 changes: 8 additions & 8 deletions routers/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,20 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st

readmeExist := isSupportedMarkup || markup.IsReadmeFile(blob.Name())
ctx.Data["ReadmeExist"] = readmeExist
isYaml := yaml.IsYamlFile(blob.Name())
ctx.Data["IsYaml"] = isYaml
isTocYaml := blob.Name() == "toc.yaml"
ctx.Data["IsTocYaml"] = isTocYaml
if readmeExist && isSupportedMarkup {
yamlHtml := yaml.RenderMarkdownYaml(buf)
markupBody := markup.Render(blob.Name(), yaml.StripYamlFromText(buf), path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas())
ctx.Data["FileContent"] = string(append(yamlHtml, markupBody...))
} else if isYaml {
rendered, err := yaml.RenderYaml(buf)
if err == nil {
} else if isTocYaml {
if rendered, err := yaml.RenderYaml(buf); err != nil {
ctx.Flash.ErrorMsg = fmt.Sprintf("Unable to parse %v", err)
ctx.Data["Flash"] = ctx.Flash
ctx.Data["FileContent"] = string(buf)
} else {
ctx.Data["FileContent"] = string(rendered)
break
}
ctx.Flash.ErrorMsg = fmt.Sprintf("Unable to parse YAML: %v", err)
ctx.Data["Flash"] = ctx.Flash
} else {
// Building code view blocks with line number on server side.
var fileContent string
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
{{end}}
</h4>
<div class="ui attached table segment">
<div class="file-view {{if .IsMarkdown}}markdown{{else if .IsYaml}}markdown yaml{{else if .IsTextFile}}code-view{{end}} has-emoji">
<div class="file-view {{if .IsMarkdown}}markdown{{else if .IsTocYaml}}markdown yaml{{else if .IsTextFile}}code-view{{end}} has-emoji">
{{if .IsMarkdown}}
<article class="markdown-body" itemprop="text">
{{if .FileContent}}{{.FileContent | Str2html}}{{end}}
</article>
{{else if .IsYaml}}
{{else if .IsTocYaml}}
<article class="markdown-body" itemprop="text">
{{if .FileContent}}{{.FileContent | Str2html}}{{end}}
</article>
Expand Down

0 comments on commit 5fbc52c

Please sign in to comment.