From ccd1637523a4c9193747a34084ae0a5631ebc6ac Mon Sep 17 00:00:00 2001 From: Chai Varier Date: Wed, 3 Nov 2021 15:13:01 -0400 Subject: [PATCH] add support for rendering HTML in markdown table cells --- .../build/skydoc/rendering/MarkdownUtil.java | 44 +++++++++++++++---- .../devtools/build/skydoc/SkydocTest.java | 41 +++++++++++++++++ 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownUtil.java b/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownUtil.java index 331c80db798042..d2040546376ef9 100644 --- a/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownUtil.java +++ b/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownUtil.java @@ -35,21 +35,47 @@ public final class MarkdownUtil { private static final int MAX_LINE_LENGTH = 100; /** - * Return a string that formats the input string so it is displayable in a markdown table cell. - * This performs the following operations: + * Wrapper around {@link #markdownCellFormatWithRenderedHtml}, calling it with HTML rendering disabled. * + * @return The formatted string, upon which the following operations have been performed: * */ public String markdownCellFormat(String docString) { - String resultString = htmlEscape(docString.trim()); + return markdownCellFormatWithRenderedHtml(docString, false); + } + + /** + * Return a string that formats the input string, possibly containing HTML, so it is displayable in a + * markdown table cell. + * + * @param docString The input string + * @param renderHtml If set to true, rendering HTML in markdown table cells will be enabled. + * Otherwise, the string will be transformed using {@link #htmlEscape}. + * + * @return The formatted string, upon which the following operations have been performed: + * + */ + public String markdownCellFormatWithRenderedHtml(String docString, Boolean renderHtml) { + String resultString = docString.trim(); + if (!renderHtml) { + resultString = htmlEscape(resultString); + } resultString = replaceWithTag(resultString, "```", "
", "
"); resultString = replaceWithTag(resultString, "`", "", ""); diff --git a/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java b/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java index d5f849e0d9e0de..22179f981f0adf 100644 --- a/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java +++ b/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java @@ -30,6 +30,7 @@ import com.google.devtools.build.skydoc.rendering.DocstringParseException; import com.google.devtools.build.skydoc.rendering.FunctionUtil; import com.google.devtools.build.skydoc.rendering.ProtoRenderer; +import com.google.devtools.build.skydoc.rendering.MarkdownUtil; import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AspectInfo; import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeType; import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ModuleInfo; @@ -53,6 +54,7 @@ public final class SkydocTest extends BuildViewTestCase { private SkydocMain skydocMain; + private MarkdownUtil util; @Before public void setUp() throws IOException { @@ -83,6 +85,7 @@ public boolean fileExists(String pathString) { }, "io_bazel", ImmutableList.of("/other_root", ".")); + util = new MarkdownUtil(); } @Test @@ -829,4 +832,42 @@ public void testModuleDocAcrossFiles() throws Exception { String moduleDoc = moduleInfo.getModuleDocstring(); assertThat(moduleDoc).isEqualTo("Should be displayed."); } + + @Test + public void testMarkdownCellFormat() throws Exception { + // Exercises all the operations of markdownCellFormat() + String testData = " test_start :\n" + + " Test
\n" + + " `test`\n" + + " ``` \n" + + " test\n" + + " ```\n\n" + + " test\n \n" + + " test_end "; + + String expected = "test_start <a></a>: " + + "Test<br> test
   "
+                        + "test  


test

test_end"; + + assertThat(util.markdownCellFormat(testData)).isEqualTo(expected); + } + + @Test + public void testMarkdownCellFormatWithRenderedHtml() throws Exception { + // Exercises markdownCellFormatWithRenderedHtml() with renderHtml set to true + String testData = " test_start :\n" + + " Test
\n" + + " `test`\n" + + " ``` \n" + + " test\n" + + " ```\n\n" + + " test\n \n" + + " test_end "; + + String expected = "test_start : " + + "Test
test
   "
+                        + "test  


test

test_end"; + + assertThat(util.markdownCellFormatWithRenderedHtml(testData, true)).isEqualTo(expected); + } }