Browse Source

Merge pull request #250 from lunny/lunny/golint_modules_markdown

Golint fixed for modules/markdown
tags/v1.0.0
Thomas Boerger 7 years ago
parent
commit
7c5de1e393
4 changed files with 23 additions and 21 deletions
  1. 4
    4
      models/repo.go
  2. 6
    6
      models/repo_test.go
  3. 11
    9
      modules/markdown/markdown.go
  4. 2
    2
      modules/markdown/markdown_test.go

+ 4
- 4
models/repo.go View File

repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
case "external_tracker_style": case "external_tracker_style":
if len(repo.ExternalTrackerStyle) == 0 { if len(repo.ExternalTrackerStyle) == 0 {
repo.ExternalTrackerStyle = markdown.ISSUE_NAME_STYLE_NUMERIC
repo.ExternalTrackerStyle = markdown.IssueNameStyleNumeric
} }
case "created_unix": case "created_unix":
repo.Created = time.Unix(repo.CreatedUnix, 0).Local() repo.Created = time.Unix(repo.CreatedUnix, 0).Local()
"repo": repo.Name, "repo": repo.Name,
} }
switch repo.ExternalTrackerStyle { switch repo.ExternalTrackerStyle {
case markdown.ISSUE_NAME_STYLE_ALPHANUMERIC:
repo.ExternalMetas["style"] = markdown.ISSUE_NAME_STYLE_ALPHANUMERIC
case markdown.IssueNameStyleAlphanumeric:
repo.ExternalMetas["style"] = markdown.IssueNameStyleAlphanumeric
default: default:
repo.ExternalMetas["style"] = markdown.ISSUE_NAME_STYLE_NUMERIC
repo.ExternalMetas["style"] = markdown.IssueNameStyleNumeric
} }


} }

+ 6
- 6
models/repo_test.go View File

Convey("It should be nil even if other settings are present", func() { Convey("It should be nil even if other settings are present", func() {
repo.EnableExternalTracker = false repo.EnableExternalTracker = false
repo.ExternalTrackerFormat = "http://someurl.com/{user}/{repo}/{issue}" repo.ExternalTrackerFormat = "http://someurl.com/{user}/{repo}/{issue}"
repo.ExternalTrackerStyle = markdown.ISSUE_NAME_STYLE_NUMERIC
repo.ExternalTrackerStyle = markdown.IssueNameStyleNumeric
So(repo.ComposeMetas(), ShouldEqual, map[string]string(nil)) So(repo.ComposeMetas(), ShouldEqual, map[string]string(nil))
}) })
}) })
repo.EnableExternalTracker = true repo.EnableExternalTracker = true
Convey("It should default to numeric issue style", func() { Convey("It should default to numeric issue style", func() {
metas := repo.ComposeMetas() metas := repo.ComposeMetas()
So(metas["style"], ShouldEqual, markdown.ISSUE_NAME_STYLE_NUMERIC)
So(metas["style"], ShouldEqual, markdown.IssueNameStyleNumeric)
}) })
Convey("It should pass through numeric issue style setting", func() { Convey("It should pass through numeric issue style setting", func() {
repo.ExternalTrackerStyle = markdown.ISSUE_NAME_STYLE_NUMERIC
repo.ExternalTrackerStyle = markdown.IssueNameStyleNumeric
metas := repo.ComposeMetas() metas := repo.ComposeMetas()
So(metas["style"], ShouldEqual, markdown.ISSUE_NAME_STYLE_NUMERIC)
So(metas["style"], ShouldEqual, markdown.IssueNameStyleNumeric)
}) })
Convey("It should pass through alphanumeric issue style setting", func() { Convey("It should pass through alphanumeric issue style setting", func() {
repo.ExternalTrackerStyle = markdown.ISSUE_NAME_STYLE_ALPHANUMERIC
repo.ExternalTrackerStyle = markdown.IssueNameStyleAlphanumeric
metas := repo.ComposeMetas() metas := repo.ComposeMetas()
So(metas["style"], ShouldEqual, markdown.ISSUE_NAME_STYLE_ALPHANUMERIC)
So(metas["style"], ShouldEqual, markdown.IssueNameStyleAlphanumeric)
}) })
Convey("It should contain the user name", func() { Convey("It should contain the user name", func() {
metas := repo.ComposeMetas() metas := repo.ComposeMetas()

+ 11
- 9
modules/markdown/markdown.go View File

"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
) )


// Issue name styles
const ( const (
ISSUE_NAME_STYLE_NUMERIC = "numeric"
ISSUE_NAME_STYLE_ALPHANUMERIC = "alphanumeric"
IssueNameStyleNumeric = "numeric"
IssueNameStyleAlphanumeric = "alphanumeric"
) )


// Sanitizer markdown sanitizer
var Sanitizer = bluemonday.UGCPolicy() var Sanitizer = bluemonday.UGCPolicy()


// BuildSanitizer initializes sanitizer with allowed attributes based on settings. // BuildSanitizer initializes sanitizer with allowed attributes based on settings.
} }


// ListItem defines how list items should be processed to produce corresponding HTML elements. // ListItem defines how list items should be processed to produce corresponding HTML elements.
func (options *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
func (r *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
// Detect procedures to draw checkboxes. // Detect procedures to draw checkboxes.
switch { switch {
case bytes.HasPrefix(text, []byte("[ ] ")): case bytes.HasPrefix(text, []byte("[ ] ")):
case bytes.HasPrefix(text, []byte("[x] ")): case bytes.HasPrefix(text, []byte("[x] ")):
text = append([]byte(`<input type="checkbox" disabled="" checked="" />`), text[3:]...) text = append([]byte(`<input type="checkbox" disabled="" checked="" />`), text[3:]...)
} }
options.Renderer.ListItem(out, text, flags)
r.Renderer.ListItem(out, text, flags)
} }


// Note: this section is for purpose of increase performance and // Note: this section is for purpose of increase performance and
urlPrefix = cutoutVerbosePrefix(urlPrefix) urlPrefix = cutoutVerbosePrefix(urlPrefix)


pattern := IssueNumericPattern pattern := IssueNumericPattern
if metas["style"] == ISSUE_NAME_STYLE_ALPHANUMERIC {
if metas["style"] == IssueNameStyleAlphanumeric {
pattern = IssueAlphanumericPattern pattern = IssueAlphanumericPattern
} }


link = fmt.Sprintf(`<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m) link = fmt.Sprintf(`<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)
} else { } else {
// Support for external issue tracker // Support for external issue tracker
if metas["style"] == ISSUE_NAME_STYLE_ALPHANUMERIC {
if metas["style"] == IssueNameStyleAlphanumeric {
metas["index"] = string(m) metas["index"] = string(m)
} else { } else {
metas["index"] = string(m[1:]) metas["index"] = string(m[1:])


// PostProcess treats different types of HTML differently, // PostProcess treats different types of HTML differently,
// and only renders special links for plain text blocks. // and only renders special links for plain text blocks.
func PostProcess(rawHtml []byte, urlPrefix string, metas map[string]string) []byte {
func PostProcess(rawHTML []byte, urlPrefix string, metas map[string]string) []byte {
startTags := make([]string, 0, 5) startTags := make([]string, 0, 5)
var buf bytes.Buffer var buf bytes.Buffer
tokenizer := html.NewTokenizer(bytes.NewReader(rawHtml))
tokenizer := html.NewTokenizer(bytes.NewReader(rawHTML))


OUTER_LOOP: OUTER_LOOP:
for html.ErrorToken != tokenizer.Next() { for html.ErrorToken != tokenizer.Next() {


// If we are not at the end of the input, then some other parsing error has occurred, // If we are not at the end of the input, then some other parsing error has occurred,
// so return the input verbatim. // so return the input verbatim.
return rawHtml
return rawHTML
} }


// Render renders Markdown to HTML with special links. // Render renders Markdown to HTML with special links.

+ 2
- 2
modules/markdown/markdown_test.go View File

metas["format"] = "https://someurl.com/{user}/{repo}/{index}" metas["format"] = "https://someurl.com/{user}/{repo}/{index}"
metas["user"] = "someuser" metas["user"] = "someuser"
metas["repo"] = "somerepo" metas["repo"] = "somerepo"
metas["style"] = ISSUE_NAME_STYLE_NUMERIC
metas["style"] = IssueNameStyleNumeric


Convey("should not render anything when there are no mentions", func() { Convey("should not render anything when there are no mentions", func() {
testCases := []string{ testCases := []string{
metas["format"] = "https://someurl.com/{user}/{repo}/?b={index}" metas["format"] = "https://someurl.com/{user}/{repo}/?b={index}"
metas["user"] = "someuser" metas["user"] = "someuser"
metas["repo"] = "somerepo" metas["repo"] = "somerepo"
metas["style"] = ISSUE_NAME_STYLE_ALPHANUMERIC
metas["style"] = IssueNameStyleAlphanumeric
Convey("It should not render anything when there are no mentions", func() { Convey("It should not render anything when there are no mentions", func() {
testCases := []string{ testCases := []string{
"", "",

Loading…
Cancel
Save