Skip to content

Commit

Permalink
Merge branch 'fix-highlight-shortcode-local'
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Feb 4, 2019
2 parents 1550cfe + 3365265 commit abdceed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
9 changes: 9 additions & 0 deletions hugolib/embedded_shortcodes_test.go
Expand Up @@ -87,6 +87,15 @@ void do();
{{< /highlight >}}`,
`(?s)<div class="highlight"><pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java"`,
},
{` {{< highlight emacs-lisp >}}
(message "hello")
(message "hello again")
{{< /highlight >}}`,
`(?s)<div class="highlight"><pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">\(message <span style="font-style:italic">&#34;hello&#34;</span>\)
\(message <span style="font-style:italic">&#34;hello again&#34;</span>\)</code></pre></div>`,
},
{`{{< highlight java "style=friendly" >}}
void do();
{{< /highlight >}}`,
Expand Down
24 changes: 23 additions & 1 deletion tpl/tplimpl/embedded/templates.autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion tpl/tplimpl/embedded/templates/shortcodes/highlight.html
@@ -1 +1,22 @@
{{ if len .Params | eq 2 }}{{ highlight (trim .Inner "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .Inner "\n\r") (.Get 0) "" }}{{ end }}
{{- $code := (trim .Inner "\n\r") -}}
{{- $lang := .Get 0 -}}
{{- $options := .Get 1 | default "" -}}
{{- $code_ending_in_line_with_spaces := (findRE "[\\r\\n][[:blank:]]+$" $code) -}}
{{/* Ref: https://github.com/gohugoio/hugo/issues/4717#issuecomment-449375012 */}}
{{- with $code_ending_in_line_with_spaces -}}
{{- $code = (replace $code "\r" "") -}} {{/* Windows -> Unix style line endings for ease in further parsing */}}
{{- $offset := (trim (index . 0) "\n") -}}
{{- $lines := (split $code "\n") -}}
{{- $num_lines := (len $lines) -}}
{{- $scratch := newScratch -}}
{{- $scratch.Add "lines_minus_offset" (slice) -}}
{{- range $i, $line := $lines -}}
{{- $line_minus_offset := (strings.TrimPrefix $offset $line) -}}
{{- if (lt $i (sub $num_lines 1)) -}} {{/* Do not add the last blank line */}}
{{- $scratch.Add "lines_minus_offset" (slice $line_minus_offset) -}}
{{- end -}}
{{- end -}}
{{- $code = (delimit ($scratch.Get "lines_minus_offset") "\n") -}}
{{- $scratch.Delete "lines_minus_offset" -}}
{{- end -}}
{{- highlight $code $lang $options -}}

0 comments on commit abdceed

Please sign in to comment.