kgabryje commented on a change in pull request #11796: URL: https://github.com/apache/beam/pull/11796#discussion_r431327731
########## File path: website/www/site/layouts/shortcodes/code_sample.html ########## @@ -0,0 +1,15 @@ +{{/* + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. See accompanying LICENSE file. +*/}} +{{/* + This shortcode is used to fetch a piece of code with tags from Beam code. + There should be no breaklines here to make sure the string results do not get impacts of newlines. +*/}}{{ $tag := .Get 1 }}{{ $path := printf "code_samples/%s" (replaceRE "/" "_" (.Get 0)) }}{{ $data := readFile $path }}{{ $matchRegex := printf "%s%s%s%s%s" "\\[START " $tag "]\n[\\s\\S]*?\n.*\\[END " $tag "]" }}{{ $match := index (findRE $matchRegex $data) 0 }}{{ $lines := split $match "\n" }}{{ $lineCount := len $lines }}{{ $cleanedLines := $lines | first (sub $lineCount 1) | last (sub $lineCount 2) }}{{ $firstLine := index $cleanedLines 0 }}{{ $numberOfWhitespaces := index (findRE "^\\s*" $firstLine) 0 | len }}{{ $unindentRegex := printf "%s%d%s" "^\\s{" $numberOfWhitespaces "}" }}{{ $unindentedLines := apply $cleanedLines "replaceRE" $unindentRegex "" "." }}{{ $result := delimit $unindentedLines "\n" }}{{ print $result }} Review comment: In `setupDockerContainer` stage of build process, we run `build_code_samples.sh` script, which copies Beam project files from which we take code snippets to `website/www/site/code_samples` directory. We do that because Hugo can't access files which are outside of its project tree (Hugo's root is the directory that contains config.toml). When copying, we name this files according to convention `path_to_file_filename.extension` in order to avoid name conflicts. `.Get 0` references an argument passed to `code_sample` shortcode which is a path to file with code snippet. So if you pass `path/to/file/filename.py` to code_sample, $path variable will have value `code_samples/path_to_file_filename.py` which Hugo can access. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org