Code
yaml-file:
name: "example.yml"
another-random-field: "Hello!"DISCLAIMER: The main focus of this extension is to enable
code-foldingfor code chunk with added content from external files.Show all code,Hide all codefromcode-toolsstill would not work.
Note: View the source code of this document by clicking
</> Codeon top-right corner.
You can add content from external file using either Pandoc Divs ::: (easier) or using code chunk.
add-fromTo 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).
if (#arg < 1 or arg[1] == '')
then
print('Usage: please provide a string')
else
str = {...}
s = ""
for i,v in pairs(str) do
s = s .. v .. " "
end
s, _ = s:gsub("^%l", string.upper)
print(s)
endYou 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-lineNote 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-filenameUsing filename does not align properly with code-folding. Instead use code-filename when using code-fold: true.
code-filename only works with code-folding. For othercases, use filename
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
</> Codeon top-right corner to see how code chunk is used.
add-fromyaml-file:
name: "example.yml"
another-random-field: "Hello!"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-lineYou can specify from which line to which line you want to add,
import scala.io.StdIn.readLine
object Capitalize {
// Adding a method for Brevity
def Capitalize_String(str: String): String = str.length match {
case 0 => ""
case _ => str.capitalize
}
def main(args: Array[String]) {
val inputStr: Option[String] = args.length match {
case 0 => None
case _ => Some(args(0))
}
inputStr.map(Capitalize_String).map(println)
}
}Note that, we have used code-filename: capitalize.scala to add the filename.
code-filenamefunction capitalize(stringToCapitalize) {
return stringToCapitalize[0].toUpperCase() + stringToCapitalize.slice(1);
}
function main() {
if (process.argv.length == 3 && process.argv[2].length > 0) {
let input = process.argv[2];
console.log(capitalize(input));
} else {
console.log("Usage: please provide a string");
}
}
main();