Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rendering HTML in markdown table cells in skydoc #14230

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 #markdownCellFormatWithHtml}, calling it with HTML rendering disabled.
*
* @return The formatted string, upon which the following operations have been performed:
* <ul>
* <li>Trims the string of leading/trailing whitespace.
* <li>Transforms the string using {@link #htmlEscape}.
* <li>Transforms multline code (```) tags into preformatted code HTML tags.
* <li>Transforms single-tick code (`) tags into code HTML tags.
* <li>Transforms 'new paraphgraph' patterns (two or more sequential newline characters) into
* <li>Trim the string of leading/trailing whitespace.
* <li>Transform the string using {@link #htmlEscape}.
* <li>Transform multline code (```) tags into preformatted code HTML tags.
* <li>Transform single-tick code (`) tags into code HTML tags.
* <li>Transform 'new paraphgraph' patterns (two or more sequential newline characters) into
* line break HTML tags.
* <li>Turns lingering new line tags into spaces (as they generally indicate intended line wrap.
* <li>Turn lingering new line tags into spaces (as they generally indicate intended line wrap.
* </ul>
*/
public String markdownCellFormat(String docString) {
String resultString = htmlEscape(docString.trim());
return markdownCellFormatWithHtml(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:
* <ul>
* <li>Trim the string of leading/trailing whitespace.
* <li>If <code>renderHtml</code> is false, transform the string using {@link #htmlEscape}.
* <li>Transform multline code (```) tags into preformatted code HTML tags.
* <li>Transform single-tick code (`) tags into code HTML tags.
* <li>Transform 'new paraphgraph' patterns (two or more sequential newline characters) into
* line break HTML tags.
* <li>Turn lingering new line tags into spaces (as they generally indicate intended line wrap.
* </ul>
*/
public String markdownCellFormatWithHtml(String docString, Boolean renderHtml) {
String resultString = docString.trim();
if (!renderHtml) {
resultString = htmlEscape(resultString);
}

resultString = replaceWithTag(resultString, "```", "<pre><code>", "</code></pre>");
resultString = replaceWithTag(resultString, "`", "<code>", "</code>");
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/google/devtools/build/skydoc/SkydocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,6 +54,7 @@
public final class SkydocTest extends BuildViewTestCase {

private SkydocMain skydocMain;
private MarkdownUtil util;

@Before
public void setUp() throws IOException {
Expand Down Expand Up @@ -83,6 +85,7 @@ public boolean fileExists(String pathString) {
},
"io_bazel",
ImmutableList.of("/other_root", "."));
util = new MarkdownUtil();
}

@Test
Expand Down Expand Up @@ -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 <a></a>:\n"
+ " Test<br>\n"
+ " `test`\n"
+ " ``` \n"
+ " test\n"
+ " ```\n\n"
+ " test\n \n"
+ " test_end ";

String expected = "test_start &lt;a&gt;&lt;/a&gt;: "
+ "Test&lt;br&gt; <code>test</code> <pre><code> "
+ "test </code></pre><br><br> test<br><br> test_end";

assertThat(util.markdownCellFormat(testData)).isEqualTo(expected);
}

@Test
public void testMarkdownCellFormatWithHtml() throws Exception {
// Exercises markdownCellFormatWithHtml() with renderHtml set to true
String testData = " test_start <a></a>:\n"
+ " Test<br>\n"
+ " `test`\n"
+ " ``` \n"
+ " test\n"
+ " ```\n\n"
+ " test\n \n"
+ " test_end ";

String expected = "test_start <a></a>: "
+ "Test<br> <code>test</code> <pre><code> "
+ "test </code></pre><br><br> test<br><br> test_end";

assertThat(util.markdownCellFormatWithHtml(testData, true)).isEqualTo(expected);
}
}