Skip to content

Commit

Permalink
fix(HTML): Fixes to list and table encoding for compatability with ad…
Browse files Browse the repository at this point in the history
…dress resolving algorithm
  • Loading branch information
nokome committed Oct 11, 2021
1 parent b8ffeda commit a57d4b0
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 161 deletions.
32 changes: 32 additions & 0 deletions fixtures/fragments/html/table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<p>Example based on how a Pandas dat a frame is encoded to HTML in a Jupyter notebook:</p>

<table>
<thead>
<tr>
<th></th>
<th>a</th>
<th>b</th>
<th>c</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<th>2</th>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tbody>
</table>
12 changes: 10 additions & 2 deletions rust/src/methods/decode/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use stencila_schema::{
Delete, Emphasis, Heading, ImageObjectSimple, InlineContent, Link, List, ListItem,
ListItemContent, ListOrder, Node, NontextualAnnotation, NumberValidator, Paragraph, Parameter,
Quote, QuoteBlock, StringValidator, Strong, Subscript, Superscript, TableCell,
TableCellContent, TableRow, TableRowRowType, TableSimple, ThematicBreak, ValidatorTypes,
VideoObjectSimple,
TableCellCellType, TableCellContent, TableRow, TableRowRowType, TableSimple, ThematicBreak,
ValidatorTypes, VideoObjectSimple,
};

/// Decode a HTML document to a `Node`
Expand Down Expand Up @@ -535,6 +535,12 @@ fn decode_table_cells(node: &NodeRef, context: &Context) -> Vec<TableCell> {
.filter_map(|child| {
if let Some(element) = child.as_element() {
if matches!(element.name.local, local_name!("td") | local_name!("th")) {
let cell_type = if matches!(element.name.local, local_name!("th")) {
Some(TableCellCellType::Header)
} else {
None
};

let blocks = decode_blocks(&child, context);
let content = if blocks.len() > 1 {
Some(TableCellContent::VecBlockContent(blocks))
Expand All @@ -548,7 +554,9 @@ fn decode_table_cells(node: &NodeRef, context: &Context) -> Vec<TableCell> {
None
}
};

return Some(TableCell {
cell_type,
content,
..Default::default()
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ input_file: fixtures/articles/simple.html
"cells": [
{
"type": "TableCell",
"cellType": "Header",
"content": [
"Group"
]
},
{
"type": "TableCell",
"cellType": "Header",
"content": [
"Value"
]
Expand Down
140 changes: 140 additions & 0 deletions rust/src/methods/decode/snapshots/html_fragments@table.html.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
---
source: rust/src/methods/decode/html.rs
expression: "decode_fragment(&content, false)"
input_file: fixtures/fragments/html/table.html

---
[
{
"type": "Paragraph",
"content": [
"Example based on how a Pandas dat a frame is encoded to HTML in a Jupyter notebook:"
]
},
{
"type": "Table",
"rows": [
{
"type": "TableRow",
"cells": [
{
"type": "TableCell",
"cellType": "Header"
},
{
"type": "TableCell",
"cellType": "Header",
"content": [
"a"
]
},
{
"type": "TableCell",
"cellType": "Header",
"content": [
"b"
]
},
{
"type": "TableCell",
"cellType": "Header",
"content": [
"c"
]
}
],
"rowType": "Header"
},
{
"type": "TableRow",
"cells": [
{
"type": "TableCell",
"cellType": "Header",
"content": [
"0"
]
},
{
"type": "TableCell",
"content": [
"1"
]
},
{
"type": "TableCell",
"content": [
"2"
]
},
{
"type": "TableCell",
"content": [
"3"
]
}
]
},
{
"type": "TableRow",
"cells": [
{
"type": "TableCell",
"cellType": "Header",
"content": [
"1"
]
},
{
"type": "TableCell",
"content": [
"4"
]
},
{
"type": "TableCell",
"content": [
"5"
]
},
{
"type": "TableCell",
"content": [
"6"
]
}
]
},
{
"type": "TableRow",
"cells": [
{
"type": "TableCell",
"cellType": "Header",
"content": [
"2"
]
},
{
"type": "TableCell",
"content": [
"7"
]
},
{
"type": "TableCell",
"content": [
"8"
]
},
{
"type": "TableCell",
"content": [
"9"
]
}
]
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -583,22 +583,26 @@ input_file: fixtures/articles/kitchen-sink.ipynb
"type": "TableRow",
"cells": [
{
"type": "TableCell"
"type": "TableCell",
"cellType": "Header"
},
{
"type": "TableCell",
"cellType": "Header",
"content": [
"a"
]
},
{
"type": "TableCell",
"cellType": "Header",
"content": [
"b"
]
},
{
"type": "TableCell",
"cellType": "Header",
"content": [
"c"
]
Expand All @@ -611,6 +615,7 @@ input_file: fixtures/articles/kitchen-sink.ipynb
"cells": [
{
"type": "TableCell",
"cellType": "Header",
"content": [
"0"
]
Expand Down Expand Up @@ -640,6 +645,7 @@ input_file: fixtures/articles/kitchen-sink.ipynb
"cells": [
{
"type": "TableCell",
"cellType": "Header",
"content": [
"1"
]
Expand Down Expand Up @@ -669,6 +675,7 @@ input_file: fixtures/articles/kitchen-sink.ipynb
"cells": [
{
"type": "TableCell",
"cellType": "Header",
"content": [
"2"
]
Expand Down

0 comments on commit a57d4b0

Please sign in to comment.