docs/layouts/_shortcodes/ebnf.html
{{/* Get the inner content of the shortcode */}} {{ $statements := .Inner }} {{/* type has to be one of [tabs, grammar, diagram, reference] (default:tabs) */}} {{ $type := default "tabs" (.Get "type") }} {{/* process inner content to remove whitespaces, newlines, commas, hypens etc */}} {{/* statements will finally be rule1,rule2,rule3 ... */}} {{ $statements = replaceRE "\s*-\s*" "," $statements }} {{ $statements = replaceRE "\s+" "," $statements }} {{ $statements = replaceRE ",+" "," $statements }} {{ $statements = trim $statements "," }} {{/* localrefs are the rules to be anchored as #rule instead of grammar_diagrams#rule */}} {{ $local := default "" (.Get "localrefs") }} {{ $local = replaceRE "\s+" "" $local }} {{ $local = replaceRE ",+" "," $local }} {{/* get the page path so determine depth and version */}} {{ $path := split $.Page.File.Dir "/" }} {{/* version is the first path of the path. eg: preview/stable/v2.12 ...*/}} {{ $version := index $path 0 }} {{ $depth := add 1 (len $path) }} {{/* determine the api (ysql/ycql) from the page path */}} {{ $api := "ysql" }} {{ if strings.Contains $.Page.File.Dir "/ycql/" }}{{ $api = "ycql" }}{{ end }} {{/* form the full service url with parameters */}} {{ $url := "http://localhost:1314/ebnf" }} {{ $url = printf "%s?depth=%d&api=%s&version=%s&rules=%s" $url $depth $api $version $statements}} {{ if $local }}{{ $url = printf "%s&local=%s" $url $local }}{{ end }} {{/* add a timestamp value to skip the hugo cache and force the request to be fetched */}} {{/* if the regenerateDiagrams is set to true in config.toml, forcefully skip the cache by */}} {{/* setting the ts value to current timestamp */}} {{ if $.Site.Params.regenerateDiagrams }} {{ $url = printf "%s&ts=%d" $url now.Unix }} {{ else }} {{/* set the ts value to the lastmod ts of the base ebnf file to use cache unless the file is changed */}} {{ $f := os.Stat (printf "content/%s/api/%s/syntax_resources/%s_grammar.ebnf" $version $api $api) }} {{ $url = printf "%s&ts=%d" $url $f.ModTime.Unix }} {{ end }} {{/* Check if diagrams is enabled/disabled*/}} {{ $diagrams_enabled := true }} {{ if eq $.Site.Params.ebnfDiagrams "disabled" }} {{ $diagrams_enabled = false }} {{ end }} {{$bnfResponse := ""}} {{/*!!! NOTE !!! The output generated by this shortcode will be processed as Markdown (for ToC generation). Hence, the boiler plate HTML should be Markdown safe - so no indentation */}} {{ if eq $type "tabs" }} {{/* counter for the no.of diagrams on the page, to name tabs differently for each tab pair */}} {{ $suffix := "" }} {{ .Page.Scratch.Add "numdiagrams" 1 }} {{ $suffix = printf "-%d" (.Page.Scratch.Get "numdiagrams") }}
{{ if $diagrams_enabled }} {{ $bnfResponse = resources.GetRemote (printf "%s&mode=grammar" $url) }} output.ebnf {{$bnfResponse.Content | safeHTML}} {{ else }} EBNF Diagrams have been disabled {{ end }}
{{ if $diagrams_enabled }} {{ $bnfResponse = resources.GetRemote (printf "%s&mode=diagram" $url) }} {{$bnfResponse.Content | safeHTML}} {{ else }} EBNF Diagrams have been disabled {{ end }}
{{ else if eq $type "grammar" }} {{ if $diagrams_enabled }} {{/* set the mode to be grammar to fetch only the grammar */}} {{ $bnfResponse = resources.GetRemote (printf "%s&mode=grammar" $url) }} output.ebnf {{$bnfResponse.Content | safeHTML}} {{ else }} EBNF Diagrams have been disabled {{ end }} {{ else if eq $type "diagram" }} {{ if $diagrams_enabled }} {{/* set the mode to be diagram to fetch only the diagram */}} {{ $bnfResponse = resources.GetRemote (printf "%s&mode=diagram" $url) }} {{ $bnfResponse.Content | safeHTML }} {{ else }} EBNF Diagrams have been disabled {{ end }} {{ else if eq $type "reference" }} {{ if $diagrams_enabled }} {{/* set the mode to be reference to generate the reference file */}} {{ $bnfResponse = resources.GetRemote (printf "%s&mode=reference" $url) }} {{ $bnfResponse.Content | safeHTML }} {{ else }} EBNF Diagrams have been disabled {{ end }} {{ else }} {{ errorf "invalid type for ebnf: %s" $type }} {{ end }}