You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by dw...@apache.org on 2018/08/15 13:17:16 UTC

[flink] 01/02: [FLINK-9013] Allow formatting text as code in option description

This is an automated email from the ASF dual-hosted git repository.

dwysakowicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 30e8edb82f4be320e9ef9894874ffe9bddc5e4f7
Author: Dawid Wysakowicz <dw...@apache.org>
AuthorDate: Thu Aug 9 17:10:38 2018 +0200

    [FLINK-9013] Allow formatting text as code in option description
---
 .../configuration/description/Description.java     | 11 +++++++++
 .../flink/configuration/description/Formatter.java | 10 ++++++--
 .../configuration/description/HtmlFormatter.java   | 17 +++++++++++++-
 .../configuration/description/TextElement.java     | 27 +++++++++++++++++++++-
 4 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/flink-core/src/main/java/org/apache/flink/configuration/description/Description.java b/flink-core/src/main/java/org/apache/flink/configuration/description/Description.java
index 25a6a64..c00890d 100644
--- a/flink-core/src/main/java/org/apache/flink/configuration/description/Description.java
+++ b/flink-core/src/main/java/org/apache/flink/configuration/description/Description.java
@@ -79,6 +79,17 @@ public class Description {
 		}
 
 		/**
+		 * 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.
 		 */
 		public DescriptionBuilder linebreak() {
diff --git a/flink-core/src/main/java/org/apache/flink/configuration/description/Formatter.java b/flink-core/src/main/java/org/apache/flink/configuration/description/Formatter.java
index d3fcf40..fdf7db6 100644
--- a/flink-core/src/main/java/org/apache/flink/configuration/description/Formatter.java
+++ b/flink-core/src/main/java/org/apache/flink/configuration/description/Formatter.java
@@ -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.
  */
@@ -49,7 +51,7 @@ public abstract class Formatter {
 				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) {
@@ -76,7 +78,11 @@ public abstract class Formatter {
 
 	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);
 
diff --git a/flink-core/src/main/java/org/apache/flink/configuration/description/HtmlFormatter.java b/flink-core/src/main/java/org/apache/flink/configuration/description/HtmlFormatter.java
index 72531ab..a475303 100644
--- a/flink-core/src/main/java/org/apache/flink/configuration/description/HtmlFormatter.java
+++ b/flink-core/src/main/java/org/apache/flink/configuration/description/HtmlFormatter.java
@@ -18,6 +18,8 @@
 
 package org.apache.flink.configuration.description;
 
+import java.util.EnumSet;
+
 /**
  * Formatter that transforms {@link Description} into Html representation.
  */
@@ -34,9 +36,22 @@ public class HtmlFormatter extends Formatter {
 	}
 
 	@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
diff --git a/flink-core/src/main/java/org/apache/flink/configuration/description/TextElement.java b/flink-core/src/main/java/org/apache/flink/configuration/description/TextElement.java
index aeb1d7a..80bdfcf 100644
--- a/flink-core/src/main/java/org/apache/flink/configuration/description/TextElement.java
+++ b/flink-core/src/main/java/org/apache/flink/configuration/description/TextElement.java
@@ -20,6 +20,7 @@ package org.apache.flink.configuration.description;
 
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.EnumSet;
 import java.util.List;
 
 /**
@@ -28,6 +29,7 @@ import java.util.List;
 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
@@ -35,7 +37,7 @@ public class TextElement implements BlockElement, InlineElement {
 	 *
 	 * <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
 	 */
@@ -53,6 +55,18 @@ public class TextElement implements BlockElement, InlineElement {
 		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;
 	}
@@ -61,6 +75,10 @@ public class TextElement implements BlockElement, InlineElement {
 		return elements;
 	}
 
+	public EnumSet<TextStyle> getStyles() {
+		return textStyles;
+	}
+
 	private TextElement(String format, List<InlineElement> elements) {
 		this.format = format;
 		this.elements = elements;
@@ -70,4 +88,11 @@ public class TextElement implements BlockElement, InlineElement {
 	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
+	}
 }