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
1 change: 1 addition & 0 deletions crates/quarto-markdown-pandoc/src/pandoc/treesitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ fn native_visitor<T: Write>(
}
"pipe_table_delimiter_row" => process_pipe_table_delimiter_row(children, context),
"pipe_table_cell" => process_pipe_table_cell(node, children, context),
"table_caption" => PandocNativeIntermediate::IntermediateInlines(native_inlines(children)),
"pipe_table" => process_pipe_table(node, children, context),
"setext_h1_underline" => PandocNativeIntermediate::IntermediateSetextHeadingLevel(1),
"setext_h2_underline" => PandocNativeIntermediate::IntermediateSetextHeadingLevel(2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub fn process_pipe_table(
let mut header: Option<Row> = None;
let mut colspec: Vec<ColSpec> = Vec::new();
let mut rows: Vec<Row> = Vec::new();
let mut caption_inlines: Option<Inlines> = None;
for (node, child) in children {
if node == "block_continuation" {
continue; // skip block continuation nodes
Expand Down Expand Up @@ -179,6 +180,12 @@ pub fn process_pipe_table(
} else {
panic!("Expected Row in pipe_table_row, got {:?}", child);
}
} else if node == "table_caption" {
if let PandocNativeIntermediate::IntermediateInlines(inlines) = child {
caption_inlines = Some(inlines);
} else {
panic!("Expected Inlines in table_caption, got {:?}", child);
}
} else {
panic!("Unexpected node in pipe_table: {}", node);
}
Expand All @@ -204,12 +211,25 @@ pub fn process_pipe_table(
(vec![header.unwrap()], rows)
};

PandocNativeIntermediate::IntermediateBlock(Block::Table(Table {
attr,
caption: Caption {
// Construct caption from caption_inlines if present
let caption = if let Some(inlines) = caption_inlines {
Caption {
short: None,
long: Some(vec![Block::Plain(Plain {
content: inlines,
source_info: node_source_info_with_context(node, context),
})]),
}
} else {
Caption {
short: None,
long: None,
},
}
};

PandocNativeIntermediate::IntermediateBlock(Block::Table(Table {
attr,
caption,
colspec,
head: TableHead {
attr: empty_attr(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ fn transform_definition_list_div(div: Div) -> Block {
})
}


/// Apply post-processing transformations to the Pandoc AST
pub fn postprocess(doc: Pandoc) -> Result<Pandoc, Vec<String>> {
let mut errors = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
| Name | Age |
|-------|-----|
| Alice | 30 |
| Bob | 25 |

: Sample table caption
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ Table ( "" , [] , [] ) (Caption Nothing [ Plain [Str "Sample", Space, Str "table", Space, Str "caption"] ]) [(AlignDefault, ColWidthDefault), (AlignDefault, ColWidthDefault)] (TableHead ( "" , [] , [] ) [Row ( "" , [] , [] ) [Cell ( "" , [] , [] ) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Name"]] , Cell ( "" , [] , [] ) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Age"]] ] ]) [TableBody ( "" , [] , [] ) (RowHeadColumns 0) [] [Row ( "" , [] , [] ) [Cell ( "" , [] , [] ) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Alice"]] , Cell ( "" , [] , [] ) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "30"]] ] , Row ( "" , [] , [] ) [Cell ( "" , [] , [] ) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Bob"]] , Cell ( "" , [] , [] ) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "25"]] ] ]] (TableFoot ( "" , [] , [] ) [] ) ]
18 changes: 17 additions & 1 deletion crates/tree-sitter-qmd/tree-sitter-markdown/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ module.exports = grammar({
),
// Some symbols get parsed as single tokens so that html blocks get detected properly
_code_line: $ => prec.right(repeat1(choice($._word, $._display_math_state_track_marker, $._inline_math_state_track_marker, $._whitespace, common.punctuation_without($, [])))),

// the gymnastics around `:` in _line exist to make the parser reject paragraphs that start with a colon.
// Those are technically valid in Markdown, but disallowing them here makes it possible to detect an
// accidentally-continued paragraph with a colon that should have been a fenced div marker.
Expand All @@ -375,8 +375,24 @@ module.exports = grammar({
$.pipe_table_delimiter_row,
repeat(seq($._pipe_table_newline, optional($.pipe_table_row))),
choice($._newline, $._eof),
optional($.table_caption),
)),

// Table caption: blank line followed by ": caption text"
// This is a Pandoc extension for table captions
table_caption: $ => prec(1, seq(
$._blank_line,
':',
optional(seq(
optional($._whitespace),
alias($._table_caption_line, $.inline)
)),
choice($._newline, $._eof),
)),

// Caption line content - similar to _line but only used in table_caption context
_table_caption_line: $ => prec.right(repeat1(choice($._word, $._display_math_state_track_marker, $._inline_math_state_track_marker, $._whitespace, common.punctuation_without($, [])))),

_pipe_table_newline: $ => seq(
$._pipe_table_line_ending,
optional($.block_continuation)
Expand Down
254 changes: 254 additions & 0 deletions crates/tree-sitter-qmd/tree-sitter-markdown/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -4722,10 +4722,264 @@
"name": "_eof"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "table_caption"
},
{
"type": "BLANK"
}
]
}
]
}
},
"table_caption": {
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_blank_line"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_whitespace"
},
{
"type": "BLANK"
}
]
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_table_caption_line"
},
"named": true,
"value": "inline"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_newline"
},
{
"type": "SYMBOL",
"name": "_eof"
}
]
}
]
}
},
"_table_caption_line": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_word"
},
{
"type": "SYMBOL",
"name": "_display_math_state_track_marker"
},
{
"type": "SYMBOL",
"name": "_inline_math_state_track_marker"
},
{
"type": "SYMBOL",
"name": "_whitespace"
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "!"
},
{
"type": "STRING",
"value": "\""
},
{
"type": "STRING",
"value": "#"
},
{
"type": "STRING",
"value": "$"
},
{
"type": "STRING",
"value": "%"
},
{
"type": "STRING",
"value": "&"
},
{
"type": "STRING",
"value": "'"
},
{
"type": "STRING",
"value": "("
},
{
"type": "STRING",
"value": ")"
},
{
"type": "STRING",
"value": "*"
},
{
"type": "STRING",
"value": "+"
},
{
"type": "STRING",
"value": ","
},
{
"type": "STRING",
"value": "-"
},
{
"type": "STRING",
"value": "."
},
{
"type": "STRING",
"value": "/"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "STRING",
"value": ";"
},
{
"type": "STRING",
"value": "<"
},
{
"type": "STRING",
"value": "="
},
{
"type": "STRING",
"value": ">"
},
{
"type": "STRING",
"value": "?"
},
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "["
},
{
"type": "STRING",
"value": "\\"
},
{
"type": "STRING",
"value": "]"
},
{
"type": "STRING",
"value": "^"
},
{
"type": "STRING",
"value": "_"
},
{
"type": "STRING",
"value": "`"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "STRING",
"value": "|"
},
{
"type": "STRING",
"value": "}"
},
{
"type": "STRING",
"value": "~"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_last_token_punctuation"
},
{
"type": "BLANK"
}
]
}
]
}
]
}
}
},
"_pipe_table_newline": {
"type": "SEQ",
"members": [
Expand Down
Loading