Skip to content

Commit 0d80af6

Browse files
authored
Add init support of orgmode document type on file view and readme (#2525)
* add init support of orgmode document type on file view and readme * fix imports * fix imports and readmeExist * fix imports order * fix format * remove unnecessary convert
1 parent 66bc0ac commit 0d80af6

File tree

21 files changed

+1103
-66
lines changed

21 files changed

+1103
-66
lines changed

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313
"code.gitea.io/gitea/cmd"
1414
"code.gitea.io/gitea/modules/log"
1515
"code.gitea.io/gitea/modules/setting"
16+
// register supported doc types
17+
_ "code.gitea.io/gitea/modules/markup/markdown"
18+
_ "code.gitea.io/gitea/modules/markup/orgmode"
19+
1620
"github.com/urfave/cli"
1721
)
1822

models/mail.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"code.gitea.io/gitea/modules/base"
1414
"code.gitea.io/gitea/modules/log"
1515
"code.gitea.io/gitea/modules/mailer"
16-
"code.gitea.io/gitea/modules/markdown"
1716
"code.gitea.io/gitea/modules/markup"
17+
"code.gitea.io/gitea/modules/markup/markdown"
1818
"code.gitea.io/gitea/modules/setting"
1919
"gopkg.in/gomail.v2"
2020
"gopkg.in/macaron.v1"

modules/markup/html_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"strings"
1111
"testing"
1212

13-
_ "code.gitea.io/gitea/modules/markdown"
1413
. "code.gitea.io/gitea/modules/markup"
14+
_ "code.gitea.io/gitea/modules/markup/markdown"
1515
"code.gitea.io/gitea/modules/setting"
1616

1717
"github.com/stretchr/testify/assert"

modules/markdown/markdown.go renamed to modules/markup/markdown/markdown.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ import (
1717
// Renderer is a extended version of underlying render object.
1818
type Renderer struct {
1919
blackfriday.Renderer
20-
urlPrefix string
21-
isWikiMarkdown bool
20+
URLPrefix string
21+
IsWiki bool
2222
}
2323

2424
// Link defines how formal links should be processed to produce corresponding HTML elements.
2525
func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
2626
if len(link) > 0 && !markup.IsLink(link) {
2727
if link[0] != '#' {
2828
lnk := string(link)
29-
if r.isWikiMarkdown {
29+
if r.IsWiki {
3030
lnk = markup.URLJoin("wiki", lnk)
3131
}
32-
mLink := markup.URLJoin(r.urlPrefix, lnk)
32+
mLink := markup.URLJoin(r.URLPrefix, lnk)
3333
link = []byte(mLink)
3434
}
3535
}
@@ -95,8 +95,8 @@ var (
9595

9696
// Image defines how images should be processed to produce corresponding HTML elements.
9797
func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
98-
prefix := r.urlPrefix
99-
if r.isWikiMarkdown {
98+
prefix := r.URLPrefix
99+
if r.IsWiki {
100100
prefix = markup.URLJoin(prefix, "wiki", "src")
101101
}
102102
prefix = strings.Replace(prefix, "/src/", "/raw/", 1)
@@ -129,9 +129,9 @@ func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte {
129129
htmlFlags |= blackfriday.HTML_SKIP_STYLE
130130
htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
131131
renderer := &Renderer{
132-
Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
133-
urlPrefix: urlPrefix,
134-
isWikiMarkdown: wikiMarkdown,
132+
Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
133+
URLPrefix: urlPrefix,
134+
IsWiki: wikiMarkdown,
135135
}
136136

137137
// set up the parser

modules/markdown/markdown_test.go renamed to modules/markup/markdown/markdown_test.go

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
package markdown_test
66

77
import (
8-
"fmt"
9-
"strconv"
108
"strings"
119
"testing"
1210

13-
. "code.gitea.io/gitea/modules/markdown"
1411
"code.gitea.io/gitea/modules/markup"
12+
. "code.gitea.io/gitea/modules/markup/markdown"
1513
"code.gitea.io/gitea/modules/setting"
1614

1715
"github.com/stretchr/testify/assert"
@@ -21,45 +19,6 @@ const AppURL = "http://localhost:3000/"
2119
const Repo = "gogits/gogs"
2220
const AppSubURL = AppURL + Repo + "/"
2321

24-
var numericMetas = map[string]string{
25-
"format": "https://someurl.com/{user}/{repo}/{index}",
26-
"user": "someUser",
27-
"repo": "someRepo",
28-
"style": markup.IssueNameStyleNumeric,
29-
}
30-
31-
var alphanumericMetas = map[string]string{
32-
"format": "https://someurl.com/{user}/{repo}/{index}",
33-
"user": "someUser",
34-
"repo": "someRepo",
35-
"style": markup.IssueNameStyleAlphanumeric,
36-
}
37-
38-
// numericLink an HTML to a numeric-style issue
39-
func numericIssueLink(baseURL string, index int) string {
40-
return link(markup.URLJoin(baseURL, strconv.Itoa(index)), fmt.Sprintf("#%d", index))
41-
}
42-
43-
// alphanumLink an HTML link to an alphanumeric-style issue
44-
func alphanumIssueLink(baseURL string, name string) string {
45-
return link(markup.URLJoin(baseURL, name), name)
46-
}
47-
48-
// urlContentsLink an HTML link whose contents is the target URL
49-
func urlContentsLink(href string) string {
50-
return link(href, href)
51-
}
52-
53-
// link an HTML link
54-
func link(href, contents string) string {
55-
return fmt.Sprintf("<a href=\"%s\">%s</a>", href, contents)
56-
}
57-
58-
func testRenderIssueIndexPattern(t *testing.T, input, expected string, metas map[string]string) {
59-
assert.Equal(t, expected,
60-
string(markup.RenderIssueIndexPattern([]byte(input), AppSubURL, metas)))
61-
}
62-
6322
func TestRender_StandardLinks(t *testing.T) {
6423
setting.AppURL = AppURL
6524
setting.AppSubURL = AppSubURL

modules/markup/markup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ package markup_test
77
import (
88
"testing"
99

10-
_ "code.gitea.io/gitea/modules/markdown"
1110
. "code.gitea.io/gitea/modules/markup"
11+
_ "code.gitea.io/gitea/modules/markup/markdown"
1212

1313
"github.com/stretchr/testify/assert"
1414
)

modules/markup/orgmode/orgmode.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package markup
6+
7+
import (
8+
"code.gitea.io/gitea/modules/markup"
9+
"code.gitea.io/gitea/modules/markup/markdown"
10+
11+
"github.com/chaseadamsio/goorgeous"
12+
"github.com/russross/blackfriday"
13+
)
14+
15+
func init() {
16+
markup.RegisterParser(Parser{})
17+
}
18+
19+
// Parser implements markup.Parser for orgmode
20+
type Parser struct {
21+
}
22+
23+
// Name implements markup.Parser
24+
func (Parser) Name() string {
25+
return "orgmode"
26+
}
27+
28+
// Extensions implements markup.Parser
29+
func (Parser) Extensions() []string {
30+
return []string{".org"}
31+
}
32+
33+
// Render renders orgmode rawbytes to HTML
34+
func Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
35+
htmlFlags := blackfriday.HTML_USE_XHTML
36+
htmlFlags |= blackfriday.HTML_SKIP_STYLE
37+
htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
38+
renderer := &markdown.Renderer{
39+
Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
40+
URLPrefix: urlPrefix,
41+
IsWiki: isWiki,
42+
}
43+
44+
result := goorgeous.Org(rawBytes, renderer)
45+
return result
46+
}
47+
48+
// RenderString reners orgmode string to HTML string
49+
func RenderString(rawContent string, urlPrefix string, metas map[string]string, isWiki bool) string {
50+
return string(Render([]byte(rawContent), urlPrefix, metas, isWiki))
51+
}
52+
53+
// Render implements markup.Parser
54+
func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
55+
return Render(rawBytes, urlPrefix, metas, isWiki)
56+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package markup
6+
7+
import (
8+
"strings"
9+
"testing"
10+
11+
"code.gitea.io/gitea/modules/markup"
12+
"code.gitea.io/gitea/modules/setting"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
const AppURL = "http://localhost:3000/"
18+
const Repo = "gogits/gogs"
19+
const AppSubURL = AppURL + Repo + "/"
20+
21+
func TestRender_StandardLinks(t *testing.T) {
22+
setting.AppURL = AppURL
23+
setting.AppSubURL = AppSubURL
24+
25+
test := func(input, expected string) {
26+
buffer := RenderString(input, setting.AppSubURL, nil, false)
27+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
28+
}
29+
30+
googleRendered := `<p><a href="https://google.com/" title="https://google.com/">https://google.com/</a></p>`
31+
test("[[https://google.com/]]", googleRendered)
32+
33+
lnk := markup.URLJoin(AppSubURL, "WikiPage")
34+
test("[[WikiPage][WikiPage]]",
35+
`<p><a href="`+lnk+`" title="WikiPage">WikiPage</a></p>`)
36+
}
37+
38+
func TestRender_Images(t *testing.T) {
39+
setting.AppURL = AppURL
40+
setting.AppSubURL = AppSubURL
41+
42+
test := func(input, expected string) {
43+
buffer := RenderString(input, setting.AppSubURL, nil, false)
44+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
45+
}
46+
47+
url := "../../.images/src/02/train.jpg"
48+
title := "Train"
49+
result := markup.URLJoin(AppSubURL, url)
50+
51+
test(
52+
"[[file:"+url+"]["+title+"]]",
53+
`<p><a href="`+result+`"><img src="`+result+`" alt="`+title+`" title="`+title+`" /></a></p>`)
54+
}

routers/api/v1/misc/markdown.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
api "code.gitea.io/sdk/gitea"
99

1010
"code.gitea.io/gitea/modules/context"
11-
"code.gitea.io/gitea/modules/markdown"
1211
"code.gitea.io/gitea/modules/markup"
12+
"code.gitea.io/gitea/modules/markup/markdown"
1313
"code.gitea.io/gitea/modules/setting"
1414
)
1515

routers/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"code.gitea.io/gitea/modules/context"
2525
"code.gitea.io/gitea/modules/indexer"
2626
"code.gitea.io/gitea/modules/log"
27-
"code.gitea.io/gitea/modules/markdown"
27+
"code.gitea.io/gitea/modules/markup/markdown"
2828
"code.gitea.io/gitea/modules/notification"
2929
"code.gitea.io/gitea/modules/setting"
3030
"code.gitea.io/gitea/modules/util"

routers/repo/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/base"
1313
"code.gitea.io/gitea/modules/context"
1414
"code.gitea.io/gitea/modules/log"
15-
"code.gitea.io/gitea/modules/markdown"
15+
"code.gitea.io/gitea/modules/markup/markdown"
1616
"code.gitea.io/gitea/modules/setting"
1717

1818
"github.com/Unknwon/paginater"

routers/repo/view.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ func renderDirectory(ctx *context.Context, treeLink string) {
9595
buf = append(buf, d...)
9696
newbuf := markup.Render(readmeFile.Name(), buf, treeLink, ctx.Repo.Repository.ComposeMetas())
9797
if newbuf != nil {
98-
ctx.Data["IsMarkdown"] = true
98+
ctx.Data["IsMarkup"] = true
9999
} else {
100100
// FIXME This is the only way to show non-markdown files
101101
// instead of a broken "View Raw" link
102-
ctx.Data["IsMarkdown"] = true
102+
ctx.Data["IsMarkup"] = false
103103
newbuf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
104104
}
105105
ctx.Data["FileContent"] = string(newbuf)
@@ -197,10 +197,8 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
197197

198198
tp := markup.Type(blob.Name())
199199
isSupportedMarkup := tp != ""
200-
// FIXME: currently set IsMarkdown for compatible
201-
ctx.Data["IsMarkdown"] = isSupportedMarkup
202-
203-
readmeExist := isSupportedMarkup || markup.IsReadmeFile(blob.Name())
200+
ctx.Data["IsMarkup"] = isSupportedMarkup
201+
readmeExist := markup.IsReadmeFile(blob.Name())
204202
ctx.Data["ReadmeExist"] = readmeExist
205203
if readmeExist && isSupportedMarkup {
206204
ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))

routers/repo/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"code.gitea.io/gitea/modules/auth"
1919
"code.gitea.io/gitea/modules/base"
2020
"code.gitea.io/gitea/modules/context"
21-
"code.gitea.io/gitea/modules/markdown"
2221
"code.gitea.io/gitea/modules/markup"
22+
"code.gitea.io/gitea/modules/markup/markdown"
2323
)
2424

2525
const (

templates/repo/view_file.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
{{end}}
3737
</h4>
3838
<div class="ui attached table segment">
39-
<div class="file-view {{if .IsMarkdown}}markdown{{else if .IsTextFile}}code-view{{end}} has-emoji">
40-
{{if .IsMarkdown}}
39+
<div class="file-view {{if .IsMarkup}}markdown{{else if .IsTextFile}}code-view{{end}} has-emoji">
40+
{{if .IsMarkup}}
4141
{{if .FileContent}}{{.FileContent | Str2html}}{{end}}
4242
{{else if not .IsTextFile}}
4343
<div class="view-raw ui center">

vendor/github.com/chaseadamsio/goorgeous/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)