{{/* "Import" source code directly into the output HTML file, with syntax highlighting * * This shortcode expects arguments: * filename: A path _relative to the calling page_ * That is, in a file like content/posts/x/index.md, * referencing this shortcode with {{< import "x.py" "python3" >}} * will insert the file at content/posts/x/x.py. startLnNo: start line to display linesNum: number of lines to display * language: A language supported by the Hugo syntax highlighter * options: (Optional) A list of options to pass to the Hugo syntax highlighter * * Note that you should call this shortcode with angle brackets, not %. * e.g., put something like this in your content: * {{< importcode "filename.py" "2 4" "python3" >}} * If you use the {{% import ... %}} style syntax, * it assumes the output of this shortcode is _markdown_, * which can contain HTML, * but will break for nontrivial code blocks * e.g. comments prefixed with '#' will be interpreted as Markdown titles. * See also */}} {{- $filename := (path.Join (path.Dir .Page.File.Path) (.Get 0)) -}} {{- $fileContents := readFile $filename | safeHTML -}} {{- $startLnNo := .Get 1 -}} {{- $linesNum := .Get 2 -}} {{/* Splitting content by newline */}} {{ $fileContents := split $fileContents "\n" }} {{/* Limit the array/slice, start from startLnNo item then grab all first linesNum item */}} {{ $fileContents := first $linesNum (after $startLnNo $fileContents) }} {{/* Delimit by newline */}} {{ $fileContents := delimit $fileContents "\n" }} {{- $language := .Get 3 -}} {{- $options := .Get 4 -}} {{- highlight $fileContents $language $options -}}