Skip to content
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

Add external markup render support #2570

Merged
merged 10 commits into from
Nov 7, 2017

Conversation

lunny
Copy link
Member

@lunny lunny commented Sep 22, 2017

Will close #374.

Usage:
Add the below to your custom/conf/app.ini file. You can add one or more sections. The section name should markup.xxxxx, which you can name it as you like. Follow the below config items then restart your gitea, it's done. For example:

On MacOS,

brew install asciidoc

Then add below config to your custom/conf/app.ini file and restart. Migration https://github.com/maxandersen/gdoc2adoc.git and then view the .adoc file.

[markup.asciidoc]
ENABLED = true
; List of file extensions that should be rendered by an external command
FILE_EXTENSIONS = .adoc,.asciidoc
; External command to render all matching extensions
RENDER_COMMAND = "asciidoc --out-file=- -"
; Input is not a standard input but a file
IS_INPUT_FILE = false

@lunny lunny added the type/feature Completely new functionality. Can only be merged if feature freeze is not active. label Sep 22, 2017
@lunny lunny added this to the 1.3.0 milestone Sep 22, 2017
@codecov-io
Copy link

codecov-io commented Sep 22, 2017

Codecov Report

Merging #2570 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2570   +/-   ##
=======================================
  Coverage   26.85%   26.85%           
=======================================
  Files          89       89           
  Lines       17607    17607           
=======================================
  Hits         4728     4728           
  Misses      12193    12193           
  Partials      686      686

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ddb7519...640eaba. Read the comment docs.

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 22, 2017
conf/app.ini Outdated
@@ -551,3 +551,12 @@ SHOW_FOOTER_BRANDING = false
SHOW_FOOTER_VERSION = true
; Show time of template execution in the footer
SHOW_FOOTER_TEMPLATE_LOAD_TIME = true

[markup.asciidoc]
ENABLED = true
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 that this example markup renderer should be disabled by default or in this example because it requires third party tools.

Copy link
Member Author

Choose a reason for hiding this comment

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

@JonasFranzDEV this file is an example file. It will not effect anything if you upgrade.

Copy link
Member

Choose a reason for hiding this comment

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

The example should follow the default values, shouldn't it?

Copy link
Member Author

@lunny lunny Sep 22, 2017

Choose a reason for hiding this comment

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

[markup.xxxx] are dynamic sections. Default values is no any [markup.xxxx] section just like this PR did. I put this section on the example ini file for others know how to add one or more tools. Also I will add these to config cheatsheet but I think put it in example ini file is easier to find for users.

// RegisterParsers registers all supported third part parsers according settings
func RegisterParsers() {
for _, render := range setting.ExternalMarkupRenders {
if render.Enabled && render.Command != "" && len(render.FileExtensions) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

It might be good to validate the file extension to deny illegal file extensions.

Copy link
Member Author

Choose a reason for hiding this comment

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

For example?

Copy link
Member

Choose a reason for hiding this comment

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

.<iframe>

Copy link
Member Author

Choose a reason for hiding this comment

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

But if you can change the ini file. I will trust you.

Copy link
Member

@sapk sapk Sep 25, 2017

Choose a reason for hiding this comment

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

We could only allow the regex \.\w ? This would prevent misconfiguration.

Copy link
Member Author

Choose a reason for hiding this comment

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

@lafriks
Copy link
Member

lafriks commented Sep 22, 2017

Just a random thought... somehow I don't like that external tool is called (and I know there is no go alternatives)

@lunny
Copy link
Member Author

lunny commented Sep 22, 2017

@lafriks any name proposed if you don't like external?

@Morlinest
Copy link
Member

@lunny I think he means calling exec.Command(command, args...) from gitea.

@lunny
Copy link
Member Author

lunny commented Sep 22, 2017

@Morlinest oh, I see.

}

_, err = io.Copy(f, rd)
f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

File.Close() returns an error

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think this is needed. We have handle the err returned above line.

Copy link
Member

Choose a reason for hiding this comment

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

What if the copy is successful, but there's an error closing the file?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ethantkoenig we will ignore that since it's a temp file. It will not effect the rendering.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe good to check to prevent leaks?

Copy link
Member Author

Choose a reason for hiding this comment

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

added.

Copy link
Member

Choose a reason for hiding this comment

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

We still don't check the error

fPath := filepath.Join(os.TempDir(), gouuid.NewV4().String())
f, err := os.Create(fPath)
if err != nil {
log.Error(4, "%s render run command %s failed: %v", p.Name(), p.Command, err)
Copy link
Member

Choose a reason for hiding this comment

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

Incorrect error message

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

var rd = bytes.NewReader(rawBytes)
commands := strings.Fields(p.Command)
var command = commands[0]
var args []string
Copy link
Member

Choose a reason for hiding this comment

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

Can just do args := commands[1:], since we're already assuming commands is not empty

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

return p.MarkupName
}

// Extensions implements markup.Parser
Copy link
Member

Choose a reason for hiding this comment

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

Rewrite comments please, it should tell what is the function doing.

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

}

_, err = io.Copy(f, rd)
f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

Maybe good to check to prevent leaks?

@lunny
Copy link
Member Author

lunny commented Oct 11, 2017

@sapk @JonasFranzDEV @Morlinest all done.

@lunny
Copy link
Member Author

lunny commented Oct 25, 2017

@sapk @JonasFranzDEV @Morlinest rebased.


_, err = io.Copy(f, rd)
f.Close()
os.Remove(fPath)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we delete the file immediately after writing to it?

}

_, err = io.Copy(f, rd)
f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

We still don't check the error

@lunny
Copy link
Member Author

lunny commented Oct 31, 2017

@ethantkoenig done.


if p.IsInputFile {
// write to templ file
fPath := filepath.Join(os.TempDir(), gouuid.NewV4().String())
Copy link
Member

Choose a reason for hiding this comment

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

You should check if the file already exists although it is very unlikely to happen. Or as an alternative you could add this check to your err != nil check at line 60.

@lunny
Copy link
Member Author

lunny commented Nov 2, 2017

@JonasFranzDEV done.

@Morlinest
Copy link
Member

@lunny IMO it would be better to use parser/parsers instead of render/renders (render is verb, don't know if it can be used as noun and if renderer is correct word):
MarkupRender -> MarkupParser, ExternalMarkupRenders -> ExternalMarkupParsers

I don't rly like the way how it is initialized (hard dependency on setting package), I want to look if there is no better way.

@lunny
Copy link
Member Author

lunny commented Nov 5, 2017

@Morlinest did you mean put this note on the setting file?

BTW what about this: // NOTE: do not print any log except error.?

@Morlinest
Copy link
Member

@lunny No, it is already there. As comment on function where we set setting variables.

// NewContext initializes configuration context.
// NOTE: do not print any log except error.
func NewContext() {

@@ -60,6 +60,15 @@ const (
LandingPageExplore LandingPage = "/explore"
)

// MarkupParser defines the external parser configed on ini
Copy link
Member

Choose a reason for hiding this comment

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

nit: configured in

)

if p.IsInputFile {
// write to templ file
Copy link
Member

Choose a reason for hiding this comment

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

nit: temp

@ethantkoenig
Copy link
Member

LGTM just two typos

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Nov 7, 2017
@lunny
Copy link
Member Author

lunny commented Nov 7, 2017

@ethantkoenig done.

@lafriks lafriks merged commit 62d0a4d into go-gitea:master Nov 7, 2017
@lunny lunny deleted the lunny/external_markup branch November 7, 2017 08:01
@kellpossible
Copy link

just wondering, is the expected output of the render command html?

@lunny
Copy link
Member Author

lunny commented Nov 10, 2017

@kellpossible Yes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

External markup renderer [$15]
9 participants