-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add reStructedText support #2564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package rst | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/markup" | ||
|
||
gorst "github.com/hhatto/gorst" | ||
) | ||
|
||
func init() { | ||
markup.RegisterParser(Parser{}) | ||
} | ||
|
||
// Parser implements markup.Parser for reStructuredText | ||
type Parser struct { | ||
} | ||
|
||
// Name return the parser's name | ||
func (Parser) Name() string { | ||
return "reStructuredText" | ||
} | ||
|
||
// Extensions return the parser supported extensions | ||
func (Parser) Extensions() []string { | ||
return []string{".rst"} | ||
} | ||
|
||
// Render renders reStructuredText bytes to HTML | ||
func Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte { | ||
p := gorst.NewParser(nil) | ||
var b bytes.Buffer | ||
w := bufio.NewWriter(&b) | ||
p.ReStructuredText(bytes.NewReader(rawBytes), gorst.ToHTML(w)) | ||
if err := w.Flush(); err != nil { | ||
log.Error(4, "Render ReStructuredText failed: %v", err) | ||
return []byte("") | ||
} | ||
|
||
return b.Bytes() | ||
} | ||
|
||
// RenderString renders reStructuredText string to HTML | ||
func RenderString(rawContent string, urlPrefix string, metas map[string]string, isWiki bool) string { | ||
return string(Render([]byte(rawContent), urlPrefix, metas, isWiki)) | ||
} | ||
|
||
// Render renders reStructuredText bytes to HTML, for implementations of markup.Parser | ||
func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte { | ||
return Render(rawBytes, urlPrefix, metas, isWiki) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package rst | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const AppURL = "http://localhost:3000/" | ||
const Repo = "go-gitea/gitea" | ||
const AppSubURL = AppURL + Repo + "/" | ||
|
||
func test(t *testing.T, input, expected string) { | ||
buffer := RenderString(input, setting.AppSubURL, nil, false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) | ||
} | ||
|
||
func TestRender_StandardLinks(t *testing.T) { | ||
var appURL = setting.AppURL | ||
var appSubURL = setting.AppSubURL | ||
setting.AppURL = AppURL | ||
setting.AppSubURL = AppSubURL | ||
defer func() { | ||
setting.AppURL = appURL | ||
setting.AppSubURL = appSubURL | ||
}() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can simplify it like this:
or to use same deffer for both tests:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, when I look at whole change at once... Why you need to change settings? I see only |
||
|
||
googleRendered := `<p><a href="https://google.com/">reStructuredText</a></p>` | ||
test(t, "reStructuredText_\n\n.. _reStructuredText: https://google.com/\n", googleRendered) | ||
|
||
// TODO: gorst didn't support relative link. | ||
/*lnk := markup.URLJoin(AppSubURL, "WikiPage") | ||
test("WikiPage_\n\n.. _WikiPage: WikiPage\n", | ||
`<p><a href="`+lnk+`">WikiPage</a></p>`)*/ | ||
} | ||
|
||
func TestRender_Images(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don' need to change settings (look at |
||
var appURL = setting.AppURL | ||
var appSubURL = setting.AppSubURL | ||
setting.AppURL = AppURL | ||
setting.AppSubURL = AppSubURL | ||
defer func() { | ||
setting.AppURL = appURL | ||
setting.AppSubURL = appSubURL | ||
}() | ||
|
||
// TODO: relative image link is not supported by gorst | ||
//url := "../../.images/src/02/train.jpg" | ||
//result := markup.URLJoin(AppSubURL, url) | ||
url := "https://help.github.com/assets/images/site/favicon.png" | ||
result := url | ||
|
||
test(t, | ||
".. image:: "+url, | ||
`<img src="`+result+`" alt="`+result+`" />`) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't need all constants (look at comment for
defer
), merge them together. Also I think you can use unexported version. E.g.const appSubURL = "http://localhost:3000/go-gitea/gitea/"
.