Skip to content

Commit

Permalink
[FLINK-9013] Allow formatting text as code in option description
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidwys committed Aug 15, 2018
1 parent e91b2d5 commit 30e8edb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public DescriptionBuilder text(String text) {
return this;
}

/**
* Block of description add.
*
* @param block block of description to add
* @return block of description
*/
public DescriptionBuilder add(BlockElement block) {
blocks.add(block);
return this;
}

/**
* Creates a line break in the description.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.apache.flink.configuration.description;

import java.util.EnumSet;

/**
* Allows providing multiple formatters for the description. E.g. Html formatter, Markdown formatter etc.
*/
Expand Down Expand Up @@ -49,7 +51,7 @@ public void format(TextElement element) {
return formatter.finalizeFormatting();
}
).toArray(String[]::new);
formatText(state, escapeFormatPlaceholder(element.getFormat()), inlineElements);
formatText(state, escapeFormatPlaceholder(element.getFormat()), inlineElements, element.getStyles());
}

public void format(LineBreakElement element) {
Expand All @@ -76,7 +78,11 @@ private String finalizeFormatting() {

protected abstract void formatLineBreak(StringBuilder state);

protected abstract void formatText(StringBuilder state, String format, String[] elements);
protected abstract void formatText(
StringBuilder state,
String format,
String[] elements,
EnumSet<TextElement.TextStyle> styles);

protected abstract void formatList(StringBuilder state, String[] entries);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.apache.flink.configuration.description;

import java.util.EnumSet;

/**
* Formatter that transforms {@link Description} into Html representation.
*/
Expand All @@ -34,9 +36,22 @@ protected void formatLineBreak(StringBuilder state) {
}

@Override
protected void formatText(StringBuilder state, String format, String[] elements) {
protected void formatText(
StringBuilder state,
String format,
String[] elements,
EnumSet<TextElement.TextStyle> styles) {
String escapedFormat = escapeCharacters(format);

String prefix = "";
String suffix = "";
if (styles.contains(TextElement.TextStyle.CODE)) {
prefix = "<span markdown=\"span\">`";
suffix = "`</span>";
}
state.append(prefix);
state.append(String.format(escapedFormat, elements));
state.append(suffix);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;

/**
Expand All @@ -28,14 +29,15 @@
public class TextElement implements BlockElement, InlineElement {
private final String format;
private final List<InlineElement> elements;
private final EnumSet<TextStyle> textStyles = EnumSet.noneOf(TextStyle.class);

/**
* Creates a block of text with placeholders ("%s") that will be replaced with proper string representation of
* given {@link InlineElement}. For example:
*
* <p>{@code text("This is a text with a link %s", link("https://somepage", "to here"))}
*
* @param format text with placeholders for elements
* @param format text with placeholders for elements
* @param elements elements to be put in the text
* @return block of text
*/
Expand All @@ -53,6 +55,18 @@ public static TextElement text(String text) {
return new TextElement(text, Collections.emptyList());
}

/**
* Creates a block of text formatted as code.
*
* @param text a block of text that will be formatted as code
* @return block of text formatted as code
*/
public static TextElement code(String text) {
TextElement element = text(text);
element.textStyles.add(TextStyle.CODE);
return element;
}

public String getFormat() {
return format;
}
Expand All @@ -61,6 +75,10 @@ public List<InlineElement> getElements() {
return elements;
}

public EnumSet<TextStyle> getStyles() {
return textStyles;
}

private TextElement(String format, List<InlineElement> elements) {
this.format = format;
this.elements = elements;
Expand All @@ -70,4 +88,11 @@ private TextElement(String format, List<InlineElement> elements) {
public void format(Formatter formatter) {
formatter.format(this);
}

/**
* Styles that can be applied to {@link TextElement} e.g. code, bold etc.
*/
public enum TextStyle {
CODE
}
}

0 comments on commit 30e8edb

Please sign in to comment.