-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ rhai - add md::top_block_content_or_raw (rel #11)
- Loading branch information
1 parent
bde232a
commit 9fe8649
Showing
4 changed files
with
270 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// region: --- Modules | ||
|
||
mod md_blocks; | ||
mod top_content; | ||
|
||
pub use md_blocks::*; | ||
pub use top_content::*; | ||
|
||
// endregion: --- Modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
/// Extracts the content between the first and last triple backticks (```) | ||
/// in the given input string. If both opening and closing backticks are found, | ||
/// it returns the enclosed content as a `String`. Otherwise, it returns the | ||
/// raw input content. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `content` - A string slice that holds the input text. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A `String` containing the extracted content between the first and last ``` | ||
pub fn top_block_content_or_raw(content: &str) -> String { | ||
// Split the input content into lines for line-by-line processing. | ||
let lines: Vec<&str> = content.lines().collect(); | ||
|
||
// Find the index of the first line that starts with ``` (ignoring leading whitespace). | ||
let first_backtick = lines.iter().position(|line| line.starts_with("```")); | ||
|
||
// Find the index of the last line that starts with ``` (ignoring leading whitespace). | ||
let last_backtick = lines.iter().rposition(|line| line.starts_with("```")); | ||
|
||
// Check if both opening and closing backticks are found and are distinct. | ||
if let (Some(start), Some(end)) = (first_backtick, last_backtick) { | ||
// Ensure that the first backtick is before the last backtick. | ||
if start < end { | ||
// Extract the lines between the first and last backtick lines. | ||
let extracted_lines = &lines[start + 1..end]; | ||
// Join the extracted lines back into a single string separated by newlines. | ||
return extracted_lines.join("\n"); | ||
} | ||
} | ||
|
||
// If backticks are not properly found, return the original content. | ||
content.to_string() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_md_top_block_content_simple() { | ||
// -- Fixtures | ||
let input = "\ | ||
Here is some text before the code block. | ||
``` | ||
fn main() { | ||
println!(\"Hello, world!\"); | ||
} | ||
``` | ||
Here is some text after the code block."; | ||
let expected = "fn main() {\n println!(\"Hello, world!\");\n}"; | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(input); | ||
|
||
// -- Check | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_md_top_block_content_with_language() { | ||
// -- Fixtures | ||
let input = "\ | ||
Start of the text. | ||
```python | ||
def hello(): | ||
print(\"Hello, Python!\") | ||
``` | ||
End of the text."; | ||
let expected = "def hello():\n print(\"Hello, Python!\")"; | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(input); | ||
|
||
// -- Check | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_md_top_block_content_multiple_code_blocks() { | ||
// -- Fixtures | ||
let fx_content = " | ||
console.log(\"First code block\"); | ||
``` | ||
Some intermediate text. | ||
Second code block: | ||
```rust | ||
fn main() { | ||
println!(\"Second code block\"); | ||
}"; | ||
let input = format!( | ||
" | ||
First code block: | ||
```javascript | ||
{fx_content} | ||
``` | ||
End of the text." | ||
); | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(&input); | ||
|
||
// -- Check | ||
assert_eq!(result, fx_content); | ||
} | ||
|
||
#[test] | ||
fn test_md_top_block_content_no_backticks() { | ||
// -- Fixtures | ||
let input = "This is a regular text without any code blocks."; | ||
let expected = "This is a regular text without any code blocks."; | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(input); | ||
|
||
// -- Check | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_md_top_block_content_only_opening_backticks() { | ||
// -- Fixtures | ||
let input = "\ | ||
Text before the code block. | ||
``` | ||
fn incomplete() { | ||
// Missing closing backticks | ||
}"; | ||
let expected = "\ | ||
Text before the code block. | ||
``` | ||
fn incomplete() { | ||
// Missing closing backticks | ||
}"; | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(input); | ||
|
||
// -- Check | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_md_top_block_content_only_closing_backticks() { | ||
// -- Fixtures | ||
let input = "\ | ||
Missing opening backticks for this code block. | ||
fn incomplete() { | ||
// Missing opening backticks | ||
} | ||
``` | ||
"; | ||
let expected = "\ | ||
Missing opening backticks for this code block. | ||
fn incomplete() { | ||
// Missing opening backticks | ||
} | ||
``` | ||
"; | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(input); | ||
|
||
// -- Check | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_md_top_block_content_adjacent_backticks() { | ||
// -- Fixtures | ||
let input = "\ | ||
Text before. | ||
``` | ||
``` | ||
Text after."; | ||
let expected = ""; | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(input); | ||
|
||
// -- Check | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_md_top_block_content_with_whitespace() { | ||
// -- Fixtures | ||
let fx_input = " | ||
Text before. | ||
``` | ||
Line within code block with leading whitespace. | ||
``` | ||
Text after."; | ||
let expected = fx_input.to_string(); | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(fx_input); | ||
|
||
// -- Check | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_md_top_block_content_with_inner_backticks() { | ||
// -- Fixtures | ||
let fx_content = "Here is some code with backticks: | ||
let s = \"Hello, `world`!\";"; | ||
let input = format!( | ||
"\ | ||
Start text. | ||
``` | ||
{} | ||
``` | ||
End text.", | ||
fx_content | ||
); | ||
let expected = fx_content; | ||
|
||
// -- Exec | ||
let result = top_block_content_or_raw(&input); | ||
|
||
// -- Check | ||
assert_eq!(result, expected); | ||
} | ||
} |