Skip to content

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// register supported doc types
_ "code.gitea.io/gitea/modules/markup/markdown"
_ "code.gitea.io/gitea/modules/markup/orgmode"
_ "code.gitea.io/gitea/modules/markup/reStructuredText"

"github.com/urfave/cli"
)
Expand Down
2 changes: 1 addition & 1 deletion modules/markup/orgmode/orgmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package markup
package orgmode

import (
"code.gitea.io/gitea/modules/markup"
Expand Down
2 changes: 1 addition & 1 deletion modules/markup/orgmode/orgmode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package markup
package orgmode

import (
"strings"
Expand Down
57 changes: 57 additions & 0 deletions modules/markup/reStructuredText/reStructuredText.go
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)
}
63 changes: 63 additions & 0 deletions modules/markup/reStructuredText/reStructuredText_test.go
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 + "/"
Copy link
Member

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/".


func test(t *testing.T, input, expected string) {
buffer := RenderString(input, setting.AppSubURL, nil, false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use setting.AppSubURL as another test() argument. E.g. test(t *testing.T, input, expected, appSubURL string)

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
}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simplify it like this:

 	defer func(appURL, appSubURL string) {
 		setting.AppURL = appURL
 		setting.AppSubURL = appSubURL
 	}(setting.AppURL, setting.AppSubURL)

or to use same deffer for both tests:

func resetSetings(appURL, appSubURL string) {
 	setting.AppURL = appURL
 	setting.AppSubURL = appSubURL
}

...

defer resetSetings(setting.AppURL, setting.AppSubURL)

Copy link
Member

Choose a reason for hiding this comment

The 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 setting.AppSubURL is used in test().


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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don' need to change settings (look at defer comment above), you can merge both tests together, remove setting variable changes and defer and use t.Run() (just create array of test cases) to run tests in parallel.

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+`" />`)
}
85 changes: 85 additions & 0 deletions vendor/github.com/hhatto/gorst/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions vendor/github.com/hhatto/gorst/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions vendor/github.com/hhatto/gorst/README.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/hhatto/gorst/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading