-
Notifications
You must be signed in to change notification settings - Fork 30
allow markdown and html files for documentation of library steps #117
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
Open
mawinter69
wants to merge
2
commits into
jenkinsci:master
Choose a base branch
from
mawinter69:allow-markdown-for-step-doc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,7 +118,15 @@ | |
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>script-security</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.jenkins.plugins</groupId> | ||
<artifactId>markdown-formatter</artifactId> | ||
<version>95.v17a_965e696ee</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>antisamy-markup-formatter</artifactId> | ||
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. Needs to be |
||
</dependency> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>variant</artifactId> | ||
|
104 changes: 104 additions & 0 deletions
104
src/main/java/org/jenkinsci/plugins/workflow/cps/global/HelpFormatter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.jenkinsci.plugins.workflow.cps.global; | ||
|
||
import hudson.Extension; | ||
import hudson.ExtensionPoint; | ||
import hudson.markup.MarkupFormatter; | ||
import hudson.markup.RawHtmlMarkupFormatter; | ||
import io.jenkins.plugins.MarkdownFormatter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import jenkins.model.Jenkins; | ||
import org.apache.commons.io.FileUtils; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
||
/** | ||
* Formatter for the help of a library step. | ||
* Allows to use markdown and html for the help. Avoids using the globally configured formatter | ||
* that might not fit. | ||
*/ | ||
@Restricted(NoExternalUse.class) | ||
public abstract class HelpFormatter implements ExtensionPoint { | ||
|
||
public boolean isApplicable(String file) { | ||
return getFile(file).exists(); | ||
} | ||
|
||
private File getFile(String file) { | ||
return new File(file + getExtension()); | ||
} | ||
protected abstract String getExtension(); | ||
|
||
protected abstract MarkupFormatter getFormatter(); | ||
|
||
public final String translate(String file) throws IOException { | ||
return "<div class=\"library-step-help\">\n" + | ||
getFormatter().translate(readFile(file)) + | ||
"</div>"; | ||
} | ||
|
||
private String readFile(String file) throws IOException { | ||
// Util.escape translates \n but not \r, and we do not know what platform the library will | ||
// be checked out on: | ||
return FileUtils.readFileToString(getFile(file), StandardCharsets.UTF_8).replace("\r\n", "\n"); | ||
} | ||
|
||
public static List<HelpFormatter> all() { | ||
return Jenkins.get().getExtensionList(HelpFormatter.class); | ||
} | ||
|
||
@Extension(ordinal = 99999) | ||
public static class MarkDown extends HelpFormatter { | ||
|
||
private static final String EXTENSION = ".md"; | ||
|
||
@Override | ||
protected String getExtension() { | ||
return EXTENSION; | ||
} | ||
|
||
@Override | ||
protected MarkupFormatter getFormatter() { | ||
return new MarkdownFormatter(); | ||
} | ||
} | ||
|
||
/** | ||
* formatter for html files based on the OWASP formatter | ||
*/ | ||
@Extension(ordinal = 50000) | ||
public static class HtmlFormatter extends HelpFormatter { | ||
|
||
private static final String EXTENSION = ".html"; | ||
|
||
@Override | ||
protected String getExtension() { | ||
return EXTENSION; | ||
} | ||
|
||
protected MarkupFormatter getFormatter() { | ||
return new RawHtmlMarkupFormatter(true); | ||
} | ||
} | ||
|
||
/** | ||
* Formatter for txt files, uses the globally configured formatter | ||
*/ | ||
@Extension(ordinal = -99999) | ||
public static class Default extends HelpFormatter { | ||
|
||
private static final String EXTENSION = ".txt"; | ||
|
||
@Override | ||
protected String getExtension() { | ||
return EXTENSION; | ||
} | ||
|
||
@Override | ||
protected MarkupFormatter getFormatter() { | ||
return Jenkins.get().getMarkupFormatter(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not in
bom
? I would omit this one and stick to the more standardantisamy-markup-formatter
.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.
The antisamy-markup-formatter can only sanitize html and is not able to convert markdown to html. So without this dependency it is not possible to use markdown files for documentation (and I think we should avoid directly setting a dependency to the commonmark java library when we have a plugin that provides it). Maybe we can add it to the bom
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.
Right, I was proposing to just drop that feature.
You could. I have no idea how well supported this plugin is.
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.
Mark Waite is maintaining the plugin, so should be well supported
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.
I really like the markdown formatter plugin and plan to continue maintaining it. I have it installed on my local Jenkins instances and use it regularly
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.
I'm happy to submit the plugin to the BOM. It has less than 1000 installations but it is very helpful and has worked well in all the cases where I've used it.
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.
Submitted for possible inclusion in the plugin bill of materials:
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.
The markdown formatter plugin is included in plugin bill of materials 2961.v1f472390972e (April 8, 2024).