Skip to content
Merged
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
@@ -0,0 +1 @@
* Fix: [Jupyter](snippets/jupyter-notebook) `includeSection` to handle section delimiter that has other text below it
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public static List<JupyterCell> fromSection(List<JupyterCell> cells, String sect
JupyterCell cell = cells.get(i);
if (cell.getType().equals(JupyterCell.MARKDOWN_TYPE) &&
cell.getInput() != null) {
String input = cell.getInput().trim();
String firstLine = input.contains("\n") ? input.substring(0, input.indexOf("\n")) : input;
if (firstLine.matches("^#+\\s+" + java.util.regex.Pattern.quote(sectionName))) {
if (firstLineTrimmed(cell.getInput()).matches("^#+\\s+" + java.util.regex.Pattern.quote(sectionName))) {
startIdx = i;
break;
}
Expand All @@ -34,10 +32,11 @@ public static List<JupyterCell> fromSection(List<JupyterCell> cells, String sect
for (int i = startIdx + 1; i < cells.size(); i++) {
JupyterCell cell = cells.get(i);
if (cell.getType().equals(JupyterCell.MARKDOWN_TYPE) &&
cell.getInput() != null &&
cell.getInput().trim().matches("^#+ .*")) {
endIdx = i;
break;
cell.getInput() != null) {
if (firstLineTrimmed(cell.getInput()).matches("^#+ .*")) {
endIdx = i;
break;
}
}
}

Expand All @@ -51,6 +50,11 @@ public static List<JupyterCell> fromSection(List<JupyterCell> cells, String sect
sectionCells;
}

private static String firstLineTrimmed(String input) {
input = input.trim();
return input.contains("\n") ? input.substring(0, input.indexOf("\n")) : input;
}

private static String stripFirstSectionHeader(String content) {
String[] lines = content.split("\\n");
int firstMatchIdx = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ class JupyterCellFilterTest {
def cells = [
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "# Top Level", []),
new JupyterCell(JupyterCell.CODE_TYPE, "code1()", []),
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "## Second Level Section", []),
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "## Second Level Section\nsome text goes here", []),
new JupyterCell(JupyterCell.CODE_TYPE, "code2()", []),
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "### Third Level", []),
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "### Third Level\nmore text goes here", []),
new JupyterCell(JupyterCell.CODE_TYPE, "code3()", []),
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "#### Fourth Level Section", []),
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "#### Fourth Level Section\nmore text", []),
new JupyterCell(JupyterCell.CODE_TYPE, "code4()", []),
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "# Another Top", [])
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "#### Another Top", [])
]

def result2 = JupyterCellFilter.fromSection(cells, "Second Level Section")
result2.size().should == 2
result2[0].input.should == "## Second Level Section"
result2[0].input.should == "## Second Level Section\nsome text goes here"
result2[1].input.should == "code2()"

def result4 = JupyterCellFilter.fromSection(cells, "Fourth Level Section")
result4.size().should == 2
result4[0].input.should == "#### Fourth Level Section"
result4[0].input.should == "#### Fourth Level Section\nmore text"
result4[1].input.should == "code4()"
}

Expand All @@ -113,7 +113,7 @@ class JupyterCellFilterTest {
def cells = [
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "# Section One\nSome content after header", []),
new JupyterCell(JupyterCell.CODE_TYPE, "code1()", []),
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "# Section Two", [])
new JupyterCell(JupyterCell.MARKDOWN_TYPE, "# Section Two\nWith other content", [])
]

def result = JupyterCellFilter.fromSection(cells, "Section One", true)
Expand Down