Skip to content

Commit

Permalink
[maven] Add line continuation option in the documentation generator.
Browse files Browse the repository at this point in the history
close #666

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed May 11, 2017
1 parent 15b439b commit 83d5d39
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
Expand Up @@ -148,6 +148,12 @@ public abstract class AbstractDocumentationMojo extends AbstractMojo {
@Parameter(defaultValue = "false", required = false)
protected boolean githubExtension;

/**
* Indicates if the line continuation syntax is enabled or not.
*/
@Parameter(defaultValue = "true", required = false)
protected boolean isLineContinuationEnable;

/**
* Java version number to support.
*/
Expand Down Expand Up @@ -374,6 +380,12 @@ protected AbstractMarkerLanguageParser createLanguageParser(File inputFile) thro

final SarlDocumentationParser internalParser = parser.getDocumentParser();

if (this.isLineContinuationEnable) {
internalParser.setLineContinuation(SarlDocumentationParser.DEFAULT_LINE_CONTINUATION);
} else {
internalParser.addPropertyProvider(createProjectProperties());
}

final ScriptExecutor scriptExecutor = internalParser.getScriptExecutor();
final StringBuilder cp = new StringBuilder();
for (final File cpElement : getClassPath()) {
Expand Down
Expand Up @@ -79,9 +79,17 @@
*/
public class SarlDocumentationParser {

private static final String DEFAULT_INLINE_FORMAT = "`{0}`"; //$NON-NLS-1$
/** Default pattern for formatting inline code.
*/
public static final String DEFAULT_INLINE_FORMAT = "`{0}`"; //$NON-NLS-1$

/** Default string to put for the outline location.
*/
public static final String DEFAULT_OUTLINE_OUTPUT_TAG = "[::Outline::]"; //$NON-NLS-1$

private static final String DEFAULT_OUTLINE_OUTPUT_TAG = "[::Outline::]"; //$NON-NLS-1$
/** Default text for line continuation.
*/
public static final String DEFAULT_LINE_CONTINUATION = " "; //$NON-NLS-1$

private static final String DEFAULT_TAG_NAME_PATTERN = "\\[:(.*?)[:!]?\\]"; //$NON-NLS-1$

Expand Down Expand Up @@ -111,6 +119,8 @@ public class SarlDocumentationParser {

private ScriptExecutor scriptExecutor;

private String lineContinuation;

/** Constructor.
*/
public SarlDocumentationParser() {
Expand Down Expand Up @@ -163,6 +173,22 @@ public ScriptExecutor getScriptExecutor() {
return this.scriptExecutor;
}

/** Replies the string of character to put in the text when line continuation is detected.
*
* @return the line continuation string of characters, or {@code null} to ignore line continuations.
*/
public String getLineContinuation() {
return this.lineContinuation;
}

/** Change the string of character to put in the text when line continuation is detected.
*
* @param lineContinuationText the line continuation string of characters, or {@code null} to ignore line continuations.
*/
public void setLineContinuation(String lineContinuationText) {
this.lineContinuation = lineContinuationText;
}

/** Replies the fenced code block formatter.
*
* <p>This code block formatter is usually used by Github.
Expand All @@ -172,7 +198,7 @@ public ScriptExecutor getScriptExecutor() {
public static Function2<String, String, String> getFencedCodeBlockFormatter() {
return (languageName, content) -> {
return "```" + Strings.nullToEmpty(languageName).toLowerCase() + "\n" //$NON-NLS-1$ //$NON-NLS-2$
+ Pattern.compile("^", Pattern.MULTILINE).matcher(content).replaceAll("\t") //$NON-NLS-1$ //$NON-NLS-2$
+ content
+ "```\n"; //$NON-NLS-1$
};
}
Expand Down Expand Up @@ -223,6 +249,7 @@ public void reset() {
this.blockFormat = null;
this.outlineOutputTag = DEFAULT_OUTLINE_OUTPUT_TAG;
this.dynamicNameExtractionPattern = DEFAULT_TAG_NAME_PATTERN;
this.lineContinuation = DEFAULT_LINE_CONTINUATION;
}

/** Add a provider of properties that could be used for finding replacement values.
Expand Down Expand Up @@ -643,7 +670,7 @@ public String transform(Reader reader, File inputFile) {
public String transform(CharSequence content, File inputFile) {
final ParsingContext rootContextForReplacements = new ParsingContext();
initializeContext(rootContextForReplacements);
CharSequence rawContent = content;
CharSequence rawContent = preProcessing(content);
Stage stage = Stage.first();
do {
final ContentParserInterceptor interceptor = new ContentParserInterceptor();
Expand All @@ -655,8 +682,34 @@ public String transform(CharSequence content, File inputFile) {
}
stage = stage.next();
} while (stage != null);
return postProcessing(rawContent);
}

/** Do a pre processing of the text.
*
* @param text the text to pre process.
* @return the pre-processed text.
*/
@SuppressWarnings("static-method")
protected CharSequence preProcessing(CharSequence text) {
return text;
}

return rawContent.toString().trim();
/** Do a post processing of the text.
*
* @param text the text to post process.
* @return the post-processed text.
*/
protected String postProcessing(CharSequence text) {
final String lineContinuation = getLineContinuation();
if (lineContinuation != null) {
final Pattern pattern = Pattern.compile(
"\\s*\\\\[\\n\\r]+\\s*", //$NON-NLS-1$
Pattern.DOTALL);
final Matcher matcher = pattern.matcher(text.toString().trim());
return matcher.replaceAll(lineContinuation);
}
return text.toString().trim();
}

/** Read the given input content and extract validation components.
Expand Down

0 comments on commit 83d5d39

Please sign in to comment.