From 5066d54b9fa3685371d360d0a3a1c086757e2fec Mon Sep 17 00:00:00 2001 From: Chai Varier Date: Wed, 3 Nov 2021 15:13:01 -0400 Subject: [PATCH 1/2] 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); + } } From dc223a8067bd647d36e94b41d566bc9bff342f12 Mon Sep 17 00:00:00 2001 From: Chai Varier Date: Fri, 5 Nov 2021 15:39:49 -0400 Subject: [PATCH 2/2] minor refactor --- .../devtools/build/skydoc/rendering/MarkdownUtil.java | 6 +++--- .../java/com/google/devtools/build/skydoc/SkydocTest.java | 6 +++--- 2 files changed, 6 insertions(+), 6 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 d2040546376ef9..e9cd11089c45aa 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,7 +35,7 @@ public final class MarkdownUtil { private static final int MAX_LINE_LENGTH = 100; /** - * Wrapper around {@link #markdownCellFormatWithRenderedHtml}, calling it with HTML rendering disabled. + * Wrapper around {@link #markdownCellFormatWithHtml}, calling it with HTML rendering disabled. * * @return The formatted string, upon which the following operations have been performed: * */ public String markdownCellFormat(String docString) { - return markdownCellFormatWithRenderedHtml(docString, false); + return markdownCellFormatWithHtml(docString, false); } /** @@ -71,7 +71,7 @@ public String markdownCellFormat(String docString) { *
  • Turn lingering new line tags into spaces (as they generally indicate intended line wrap. * */ - public String markdownCellFormatWithRenderedHtml(String docString, Boolean renderHtml) { + public String markdownCellFormatWithHtml(String docString, Boolean renderHtml) { String resultString = docString.trim(); if (!renderHtml) { resultString = htmlEscape(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 22179f981f0adf..a0d50b2a0aae01 100644 --- a/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java +++ b/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java @@ -853,8 +853,8 @@ public void testMarkdownCellFormat() throws Exception { } @Test - public void testMarkdownCellFormatWithRenderedHtml() throws Exception { - // Exercises markdownCellFormatWithRenderedHtml() with renderHtml set to true + public void testMarkdownCellFormatWithHtml() throws Exception { + // Exercises markdownCellFormatWithHtml() with renderHtml set to true String testData = " test_start :\n" + " Test
    \n" + " `test`\n" @@ -868,6 +868,6 @@ public void testMarkdownCellFormatWithRenderedHtml() throws Exception { + "Test
    test
       "
                             + "test  


    test

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