DISCLAIMER: The main focus of this extension is to enable code-folding for code chunk with added content from external files. Show all code, Hide all code from code-tools still would not work.
Note: View the source code of this document by clicking </> Code on top-right corner.
You can add content from external file using either Pandoc Divs ::: (easier) or using code chunk.
Using Pandoc Divs
add-from
To add codes using pandoc divs, use the add-from attribute to denote the path of the external file, relative to this qmd file. And within that Div, use a codeblock along with the name of the language of the content to be added to get correct syntax highlighting. (You can run quarto pandoc --list-highlight-languages to know for which languages, syntax highlighting is supported).
Code
class HelloWorld {publicstaticvoidmain(String[] args){System.out.println("Hello, World!");}}
You can also pass options start-line, end-line to add a specific range of lines, code-line-numbers to enable source code line numbering, code-filename or filename to show a name of the file the added code is associated with.
start-line & end-line
Code
importHtmlexposing (text)main=text"Hello, World!"
Note that, we have used code-line-numbers: true to add line numbers in code chunk.
Also We can change the text Code to something nicer using code-filename.
filename and code-filename
Using filename does not align properly with code-folding. Instead use code-filename when using code-fold: true.
Code
with Text_IO;procedure Hello_World isbegin Text_IO.Put_Line("Hello, World!");end Hello_World;
code-filename only works with code-folding. For othercases, use filename
Using Code chunk
Now there is a way to use code chunk instead of pandoc divs to add contents from external file. But there are two things to note,
Since Quarto so far uses two types rendering engine, knitr and jupyter, we need to use code chunk accordingly.If you are rendering the quarto document using jupyter along with python kernel use {python} chunk to add code, similary if you are using julia kernel, you need to use {julia} and on the other hand if you are rendering the quarto document using knitr (i.e. engine: knitr) then use {r} chunk to add code, Or to avoid thinking about rendering engine, jupyter or knitr, you also can follow pandoc Div approach described above.
You also need to use a chunk option source-lang to give the name of the language of the content to be added to get correct syntax highlighting.
Note: View the source code of this document by clicking </> Code on top-right corner to see how code chunk is used.
add-from
Code
functioncapString=capitalize(string)if(nargin==0)fprintf("Usage: please provide a string\n");elseif(strlength(string)==0)fprintf("Usage: please provide a string\n");elsestring=char(string);capString=convertCharsToStrings(strcat(upper(string(1:1)),string(2:end)));endendend
One very important detail to note that, you must put a random comment in code chunk so that the code chunk is not treated as empty, to make this filter work.
start-line & end-line
You can specify from which line to which line you want to add,
Note that, we have used code-line-numbers: true to add line numbers in code chunk.
Some more examples
Code
<?phpif (count($argv) ==2&&strlen($argv[1])) {$inputString=$argv[1];$capitalized=ucfirst($inputString);echo$capitalized;} else {echo"Usage: please provide a string";}
Code
fn main() {for number in1..101{if number %3==0&& number %5==0{println!("FizzBuzz");}elseif number %3==0{println!("Fizz");}elseif number %5==0{println!("Buzz");}else{println!("{}", number);}}}
Code
#!/usr/bin/juliafor i =1:100 str = i %3==0 ? "Fizz":"" str *= i %5==0 ? "Buzz":""ifisempty(str) str = iendprintln(str)end
Source Code
---title: "Add Code Files"author: Shafayet Khan Shafeedate: last-modifiedformat: html: code-fold: true code-tools: trueengine: knitrfilters: - add-code-filesexecute: echo: trueembed-resources: true---> DISCLAIMER: The main focus of this extension is to enable `code-folding` for code chunk with added content from external files. `Show all code`, `Hide all code` from `code-tools` still would not work.> Note: View the source code of this document by clicking `</> Code` on top-right corner.You can add content from external file using either Pandoc Divs `:::` (easier) or using code chunk.## Using Pandoc Divs### `add-from`To add codes using pandoc divs, use the `add-from` attribute to denote the path of the external file, relative to this qmd file. And within that Div, use a codeblock along with the name of the language of the content to be added to get correct syntax highlighting. (You can run `quarto pandoc --list-highlight-languages` to know for which languages, syntax highlighting is supported).::: {add-from=test/snippets/HelloWorld.java}```{.java}```:::You can also pass options `start-line`, `end-line` to add a specific range of lines, `code-line-numbers` to enable source code line numbering, `code-filename` or `filename` to show a name of the file the added code is associated with.### `start-line` & `end-line`::: {add-from=test/snippets/HelloWorld.elm start-line=3 end-line=6 code-line-numbers=true}```{.elm}```:::Note that, we have used `code-line-numbers: true` to add line numbers in code chunk.Also We can change the text `Code` to something nicer using `code-filename`.## `filename` and `code-filename`Using `filename` does not align properly with `code-folding`. Instead use `code-filename` when using `code-fold: true`.::: {add-from=test/snippets/hello-world.adb code-line-numbers="true" code-filename=hello-world.adb}```{.ada}```:::**`code-filename` only works with `code-folding`. For othercases, use `filename`**## Using Code chunkNow there is a way to use code chunk instead of pandoc divs to add contents from external file. But there are two things to **note**,- Since Quarto so far uses two types rendering engine, `knitr` and `jupyter`, we need to use code chunk accordingly.If you are rendering the quarto document using jupyter along with python kernel use {python} chunk to add code, similary if you are using julia kernel, you need to use {julia} and on the other hand if you are rendering the quarto document using knitr (i.e. engine: knitr) then use {r} chunk to add code, Or to avoid thinking about rendering engine, jupyter or knitr, you also can follow pandoc Div approach described above.- You also need to use a chunk option `source-lang` to give the name of the language of the content to be added to get correct syntax highlighting.> Note: View the source code of this document by clicking `</> Code` on top-right corner to see how code chunk is used.## `add-from````{r}#| add-from: test/snippets/capitalize.m#| source-lang: matlab#| code-filename: capitalize.m# comment```**One very important detail to note that, you must put a random comment in code chunk so that the code chunk is not treated as empty, to make this filter work.**## `start-line` & `end-line`You can specify from which line to which line you want to add,```{r}#| add-from: test/snippets/capitalize.ts#| source-lang: typescript#| start-line: 2#| end-line: 6#| code-line-numbers: true#| code-filename: capitalize.ts# comment```Note that, we have used `code-line-numbers: true` to add line numbers in code chunk.## Some more examples```{r}#| add-from: test/snippets/capitalize.php#| source-lang: php#| code-line-numbers: true#| code-filename: capitalize.php# comment``````{r}#| add-from: test/snippets/fizz-buzz.rs#| source-lang: rust#| code-line-numbers: true#| code-filename: fizz-buzz.rs# comment``````{r}#| add-from: test/snippets/fizz-buzz.jl#| source-lang: julia#| code-line-numbers: true#| code-filename: fizz-buzz.jl# comment```