mirror of https://github.com/gogs/gogs.git
modules/markup: initial support for org-mode (#4373)
parent
a11044f789
commit
1b5a418fd3
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2017 The Gogs 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 markup
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/chaseadamsio/goorgeous"
|
||||
)
|
||||
|
||||
var orgModeExtensions = []string{".org"}
|
||||
|
||||
// IsOrgModeFile reports whether name looks like a Org-mode file based on its extension.
|
||||
func IsOrgModeFile(name string) bool {
|
||||
extension := strings.ToLower(filepath.Ext(name))
|
||||
for _, ext := range orgModeExtensions {
|
||||
if strings.ToLower(ext) == extension {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RawOrgMode renders content in Org-mode syntax to HTML without handling special links.
|
||||
func RawOrgMode(body []byte, urlPrefix string) []byte {
|
||||
return goorgeous.OrgCommon(body)
|
||||
}
|
||||
|
||||
// OrgMode takes a string or []byte and renders to HTML in Org-mode syntax with special links.
|
||||
func OrgMode(input interface{}, urlPrefix string, metas map[string]string) []byte {
|
||||
return Render(ORG_MODE, input, urlPrefix, metas)
|
||||
}
|
|
@ -116,7 +116,7 @@ func (options *MarkdownRenderer) ListItem(out *bytes.Buffer, text []byte, flags
|
|||
options.Renderer.ListItem(out, text, flags)
|
||||
}
|
||||
|
||||
// RawMarkdown renders Markdown to HTML without handling special links.
|
||||
// RawMarkdown renders content in Markdown syntax to HTML without handling special links.
|
||||
func RawMarkdown(body []byte, urlPrefix string) []byte {
|
||||
htmlFlags := 0
|
||||
htmlFlags |= blackfriday.HTML_SKIP_STYLE
|
||||
|
|
|
@ -308,11 +308,26 @@ OUTER_LOOP:
|
|||
type Type string
|
||||
|
||||
const (
|
||||
UNRECOGNIZED Type = "unrecognized"
|
||||
MARKDOWN Type = "markdown"
|
||||
ORG_MODE Type = "orgmode"
|
||||
UNRECOGNIZED Type = "unrecognized"
|
||||
MARKDOWN Type = "markdown"
|
||||
ORG_MODE Type = "orgmode"
|
||||
IPYTHON_NOTEBOOK Type = "ipynb"
|
||||
)
|
||||
|
||||
// Detect returns best guess of a markup type based on file name.
|
||||
func Detect(filename string) Type {
|
||||
switch {
|
||||
case IsMarkdownFile(filename):
|
||||
return MARKDOWN
|
||||
case IsOrgModeFile(filename):
|
||||
return ORG_MODE
|
||||
case IsIPythonNotebook(filename):
|
||||
return IPYTHON_NOTEBOOK
|
||||
default:
|
||||
return UNRECOGNIZED
|
||||
}
|
||||
}
|
||||
|
||||
// Render takes a string or []byte and renders to HTML in given type of syntax with special links.
|
||||
func Render(typ Type, input interface{}, urlPrefix string, metas map[string]string) []byte {
|
||||
var rawBytes []byte
|
||||
|
@ -331,6 +346,7 @@ func Render(typ Type, input interface{}, urlPrefix string, metas map[string]stri
|
|||
case MARKDOWN:
|
||||
rawHTML = RawMarkdown(rawBytes, urlPrefix)
|
||||
case ORG_MODE:
|
||||
rawHTML = RawOrgMode(rawBytes, urlPrefix)
|
||||
default:
|
||||
return rawBytes // Do nothing if syntax type is not recognized
|
||||
}
|
||||
|
|
|
@ -85,11 +85,15 @@ func renderDirectory(c *context.Context, treeLink string) {
|
|||
if isTextFile {
|
||||
d, _ := ioutil.ReadAll(dataRc)
|
||||
buf = append(buf, d...)
|
||||
switch {
|
||||
case markup.IsMarkdownFile(readmeFile.Name()):
|
||||
c.Data["IsMarkdown"] = true
|
||||
buf = markup.Markdown(buf, treeLink, c.Repo.Repository.ComposeMetas())
|
||||
case markup.IsIPythonNotebook(readmeFile.Name()):
|
||||
|
||||
switch markup.Detect(readmeFile.Name()) {
|
||||
case markup.MARKDOWN:
|
||||
ctx.Data["IsMarkdown"] = true
|
||||
buf = markup.Markdown(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
|
||||
case markup.ORG_MODE:
|
||||
ctx.Data["IsMarkdown"] = true
|
||||
buf = markup.OrgMode(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
|
||||
case markup.IPYTHON_NOTEBOOK:
|
||||
c.Data["IsIPythonNotebook"] = true
|
||||
c.Data["RawFileLink"] = c.Repo.RepoLink + "/raw/" + path.Join(c.Repo.BranchName, c.Repo.TreePath, readmeFile.Name())
|
||||
default:
|
||||
|
@ -153,18 +157,21 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
|
|||
break
|
||||
}
|
||||
|
||||
ctx.Data["ReadmeExist"] = markup.IsReadmeFile(blob.Name())
|
||||
|
||||
d, _ := ioutil.ReadAll(dataRc)
|
||||
buf = append(buf, d...)
|
||||
|
||||
isMarkdown := markup.IsMarkdownFile(blob.Name())
|
||||
ctx.Data["IsMarkdown"] = isMarkdown
|
||||
ctx.Data["ReadmeExist"] = isMarkdown && markup.IsReadmeFile(blob.Name())
|
||||
|
||||
ctx.Data["IsIPythonNotebook"] = markup.IsIPythonNotebook(blob.Name())
|
||||
|
||||
if isMarkdown {
|
||||
switch markup.Detect(blob.Name()) {
|
||||
case markup.MARKDOWN:
|
||||
ctx.Data["IsMarkdown"] = true
|
||||
ctx.Data["FileContent"] = string(markup.Markdown(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
|
||||
} else {
|
||||
case markup.ORG_MODE:
|
||||
ctx.Data["IsMarkdown"] = true
|
||||
ctx.Data["FileContent"] = string(markup.OrgMode(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
|
||||
case markup.IPYTHON_NOTEBOOK:
|
||||
ctx.Data["IsIPythonNotebook"] = true
|
||||
default:
|
||||
// Building code view blocks with line number on server side.
|
||||
var fileContent string
|
||||
if err, content := template.ToUTF8WithErr(buf); err != nil {
|
||||
|
|
Loading…
Reference in New Issue