This function takes an input string containing both text and code blocks, and returns a list of blocks with their respective type, content, and language (if applicable). Each block is either of type 'text' or 'code'.
Value
A list of blocks with type, content, and language (for code blocks). Text without a code block is returned as one text block.
Details
Code blocks are fenced as in Markdown: a line of three or more backticks,
optionally followed by an info string, opens a block, and a line of the same
number of backticks closes it. The language is the first word of the info
string, or the engine of an R Markdown chunk header ({r label} is "r"),
and "plaintext" without one. The lines inside a code block are kept
unchanged; text blocks are trimmed, and blank text between blocks is
dropped.
Examples
parse_response("Hello world!")
#> [[1]]
#> [[1]]$type
#> [1] "text"
#>
#> [[1]]$content
#> [1] "Hello world!"
#>
#>
# [[1]]
# [[1]]$type
# [1] "text"
#
# [[1]]$content
# [1] "Hello world!"
#
parse_response("```python\nprint('Hello world!')\n```")
#> [[1]]
#> [[1]]$type
#> [1] "code"
#>
#> [[1]]$content
#> [1] "print('Hello world!')"
#>
#> [[1]]$language
#> [1] "python"
#>
#>
# [[1]]
# [[1]]$type
# [1] "code"
#
# [[1]]$content
# [1] "print('Hello world!')"
#
# [[1]]$language
# [1] "python"
#
parse_response("Hello world!\n\n```python\nprint('Hello world!')\n```")
#> [[1]]
#> [[1]]$type
#> [1] "text"
#>
#> [[1]]$content
#> [1] "Hello world!"
#>
#>
#> [[2]]
#> [[2]]$type
#> [1] "code"
#>
#> [[2]]$content
#> [1] "print('Hello world!')"
#>
#> [[2]]$language
#> [1] "python"
#>
#>
# [[1]]
# [[1]]$type
# [1] "text"
#
# [[1]]$content
# [1] "Hello world!"
#
# [[2]]
# [[2]]$type
# [1] "code"
#
# [[2]]$content
# [1] "print('Hello world!')"
#
# [[2]]$language
# [1] "python"
#
