mirror of
https://github.com/McShelby/hugo-theme-relearn.git
synced 2024-11-23 07:47:54 +00:00
highlight: add option for wrapping vs. scrolling #169
This commit is contained in:
parent
73fdfb5ea6
commit
83e403ea2e
17 changed files with 435 additions and 95 deletions
|
@ -25,7 +25,7 @@ The Relearn theme is a fork of the great [Learn theme](https://github.com/matcor
|
|||
- Predefined light, dark and color variants
|
||||
- [User selectable variants](https://mcshelby.github.io/hugo-theme-relearn/basics/customization#multiple-variants)
|
||||
- [Stylesheet generator](https://mcshelby.github.io/hugo-theme-relearn/basics/generator)
|
||||
- [Configurable syntax highlighting](https://mcshelby.github.io/hugo-theme-relearn/cont/syntaxhighlight)
|
||||
- [Configurable syntax highlighting](https://mcshelby.github.io/hugo-theme-relearn/shortcodes/highlight)
|
||||
- **Unique theme features**
|
||||
- [Print whole chapters or even the complete site](https://mcshelby.github.io/hugo-theme-relearn/basics/configuration#activate-print-support)
|
||||
- In page search
|
||||
|
|
|
@ -47,18 +47,20 @@ title = "Hugo Relearn Documentation"
|
|||
|
||||
[markup]
|
||||
[markup.highlight]
|
||||
# line numbers in a table layout will shift if code is wrapping, so better
|
||||
# not use it; visually both layouts have the same look and behavior
|
||||
lineNumbersInTable = false
|
||||
|
||||
# if `guessSyntax = true`, there will be no unstyled code even if no language
|
||||
# was given BUT Mermaid and Math codefences will not work anymore! So this is a
|
||||
# mandatory setting for your site if you want to use Mermaid or Math codefences
|
||||
guessSyntax = false
|
||||
|
||||
# here in this showcase we use our own modified chroma syntax highlightning style
|
||||
# which is imported in theme-relearn-light.css / theme-relearn-dark.css;
|
||||
# the shipped variants come with their own modified chroma syntax highlightning
|
||||
# style which is imported in theme-relearn-light.css, theme-relearn-dark.css, etc.;
|
||||
# if you want to use a predefined style instead:
|
||||
# - remove the following `noClasses`
|
||||
# - set the following `style` to a predefined style name
|
||||
# - remove the `@import` of the self-defined chroma stylesheet from your CSS files
|
||||
# (here eg.: theme-relearn-light.css / theme-relearn-dark.css)
|
||||
# - remove `noClasses` or set `noClasses = true`
|
||||
# - set `style` to a predefined style name
|
||||
noClasses = false
|
||||
# style = "tango"
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ The theme is a fork of the great [Learn theme](https://github.com/matcornic/hugo
|
|||
- Predefined light, dark and color variants
|
||||
- [User selectable variants]({{%relref "basics/customization#multiple-variants" %}})
|
||||
- [Stylesheet generator]({{%relref "basics/generator" %}})
|
||||
- [Configurable syntax highlighting]({{%relref "cont/syntaxhighlight" %}})
|
||||
- [Configurable syntax highlighting]({{%relref "shortcodes/highlight" %}})
|
||||
- **Unique theme features**
|
||||
- [Print whole chapters or even the complete site]({{%relref "basics/configuration#activate-print-support" %}})
|
||||
- In page search
|
||||
|
|
|
@ -18,6 +18,14 @@ This document shows you what's new in the latest release. For a detailed list of
|
|||
|
||||
---
|
||||
|
||||
## 5.17.0 (2023-06-20)
|
||||
|
||||
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The new [`highlight` shortcode]({{% relref "shortcodes/highlight" %}}) replaces Hugo's default implementation and is fully compatible. So you don't need to change anything.
|
||||
|
||||
In addition it offers some extensions. Currently only the `wrap` extension option is provided to control whether a code block should be wrapped or scrolled if to long to fit.
|
||||
|
||||
---
|
||||
|
||||
## 5.16.0 (2023-06-10)
|
||||
|
||||
- {{% badge style="note" title=" " %}}Change{{% /badge %}} The theme now provides warnings for deprecated or now unsupported features. The warnings include hints how to fix them and an additional link to the documenation.
|
||||
|
|
|
@ -323,7 +323,7 @@ If you want to gain more control of your code block you can enclose your code by
|
|||
|
||||
In GFM (GitHub Flavored Markdown) you can also add a language specifier directly after the opening fence, ` ```js `, and syntax highlighting will automatically be applied according to the selected language in the rendered HTML.
|
||||
|
||||
See [Code Highlighting]({{% relref "syntaxhighlight" %}}) for additional documentation.
|
||||
See [Code Highlighting]({{% relref "shortcodes/highlight" %}}) for additional documentation.
|
||||
|
||||
````plaintext
|
||||
```js
|
||||
|
|
|
@ -1,66 +0,0 @@
|
|||
+++
|
||||
title = "Code highlighting"
|
||||
weight = 4
|
||||
+++
|
||||
|
||||
The Relearn theme uses [Hugo's built-in syntax highlighting](https://gohugo.io/content-management/syntax-highlighting/) for code.
|
||||
|
||||
## Markdown syntax
|
||||
|
||||
Wrap the code block with three backticks and the name of the language. Highlight will try to auto detect the language if one is not provided.
|
||||
|
||||
<!-- markdownlint-disable MD046 -->
|
||||
````plaintext
|
||||
```json
|
||||
[
|
||||
{
|
||||
"title": "apples",
|
||||
"count": [12000, 20000],
|
||||
"description": {"text": "...", "sensitive": false}
|
||||
},
|
||||
{
|
||||
"title": "oranges",
|
||||
"count": [17500, null],
|
||||
"description": {"text": "...", "sensitive": false}
|
||||
}
|
||||
]
|
||||
```
|
||||
````
|
||||
<!-- markdownlint-disable MD046 -->
|
||||
|
||||
Renders to:
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"title": "apples",
|
||||
"count": [12000, 20000],
|
||||
"description": {"text": "...", "sensitive": false}
|
||||
},
|
||||
{
|
||||
"title": "oranges",
|
||||
"count": [17500, null],
|
||||
"description": {"text": "...", "sensitive": false}
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
## Supported languages
|
||||
|
||||
Hugo comes with a [remarkable list](https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages) of supported languages.
|
||||
|
||||
## Recommended configuration
|
||||
|
||||
You can choose a color theme from the [list of supported themes](https://xyproto.github.io/splash/docs/all.html) and add it in your `config.toml`
|
||||
|
||||
````toml
|
||||
[markup]
|
||||
[markup.highlight]
|
||||
# if `guessSyntax = true`, there will be no unstyled code even if no language
|
||||
# was given BUT Mermaid and Math codefences will not work anymore! So this is a
|
||||
# mandatory setting for your site if you want to use Mermaid or Math codefences
|
||||
guessSyntax = false
|
||||
|
||||
# choose a color theme or create your own
|
||||
style = "base16-snazzy"
|
||||
````
|
|
@ -1,5 +0,0 @@
|
|||
+++
|
||||
title = "Code highlight'n"
|
||||
weight = 4
|
||||
+++
|
||||
{{< piratify >}}
|
186
exampleSite/content/shortcodes/highlight.en.md
Normal file
186
exampleSite/content/shortcodes/highlight.en.md
Normal file
|
@ -0,0 +1,186 @@
|
|||
+++
|
||||
description = "Generate diagrams and flowcharts from text"
|
||||
title = "Highlight"
|
||||
+++
|
||||
|
||||
The `highlight` shortcode renders your code with a syntax highlighter.
|
||||
|
||||
{{< highlight lineNos="table" type="py" wrap="true" >}}
|
||||
print("Hello World!")
|
||||
{{< /highlight >}}
|
||||
|
||||
## Usage
|
||||
|
||||
This shortcode is compatible with Hugo's [`highlight` shortcode](https://gohugo.io/content-management/syntax-highlighting/#highlight-shortcode) but offers some extensions.
|
||||
|
||||
You can call it interchangeably in the same way as Hugo's own shortcode using positional parameter or by simply using codefences.
|
||||
|
||||
You are free to also call this shortcode from your own partials. In this case it somewhat resemble Hugo's [`highlight` function](https://gohugo.io/functions/highlight/) syntax if you call this shortcode as a partial using compatiblity syntax.
|
||||
|
||||
While the examples are using shortcodes with named parameter it is recommended to use codefences instead. This is because more and more other software supports codefences (eg. GitHub) and so your markdown becomes more portable.
|
||||
|
||||
|
||||
{{< tabs groupId="shortcode-parameter">}}
|
||||
{{% tab title="codefence" %}}
|
||||
|
||||
````md
|
||||
```py { lineNos="table" wrap="true" }
|
||||
print("Hello World!")
|
||||
```
|
||||
````
|
||||
|
||||
{{% /tab %}}
|
||||
{{% tab title="shortcode" %}}
|
||||
|
||||
````go
|
||||
{{</* highlight lineNos="table" type="py" wrap="true" */>}}
|
||||
print("Hello World!")
|
||||
{{</* /highlight */>}}
|
||||
````
|
||||
|
||||
{{% /tab %}}
|
||||
{{% tab title="shortcode (positional)" %}}
|
||||
|
||||
````go
|
||||
{{</* highlight py "lineNos=table,wrap=true" */>}}
|
||||
print("Hello World!")
|
||||
{{</* /highlight */>}}
|
||||
````
|
||||
|
||||
{{% /tab %}}
|
||||
{{% tab title="partial" %}}
|
||||
|
||||
````go
|
||||
{{ partial "shortcodes/highlight.html" (dict
|
||||
"context" .
|
||||
"content" "print(\"Hello World!\")"
|
||||
"lineNos" "table"
|
||||
"type" "py"
|
||||
"wrap" "true"
|
||||
)}}
|
||||
|
||||
````
|
||||
|
||||
{{% /tab %}}
|
||||
{{% tab title="partial (compat)" %}}
|
||||
|
||||
````go
|
||||
{{ partial "shortcodes/highlight.html" (dict
|
||||
"context" .
|
||||
"content" "print(\"Hello World!\")"
|
||||
"options" "lineNos=table,wrap=true"
|
||||
"type" "py"
|
||||
)}}
|
||||
|
||||
````
|
||||
|
||||
{{% /tab %}}
|
||||
{{< /tabs >}}
|
||||
|
||||
### Parameter
|
||||
|
||||
| Name | Default | Notes |
|
||||
|:----------------------|:-----------------|:------------|
|
||||
| **type** | _<empty>_ | The language of the code to highlight. Choose from one of the [supported languages](https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages). Case-insensitive. |
|
||||
| **wrap** | see notes | _Extension_. When `true` the content may wrap on long lines otherwise it will be scrollable.<br><br>The default value can be set in your `config.toml` and overwritten via frontmatter. [See below](#configuration). |
|
||||
| **options** | _<empty>_ | An optional, comma-separated list of zero or more [Hugo supported options](https://gohugo.io/functions/highlight/#options) as well as extension parameter from this table. |
|
||||
| _**<option>**_ | _<empty>_ | Any of [Hugo's supported options](https://gohugo.io/functions/highlight/#options). |
|
||||
| _**<content>**_ | _<empty>_ | Your code to highlight. |
|
||||
|
||||
## Configuration
|
||||
|
||||
Default values for [Hugo's supported options](https://gohugo.io/functions/highlight/#options) can be set via [goldmark settings](https://gohugo.io/getting-started/configuration-markup/#highlight) in your `config.toml`
|
||||
|
||||
Default values for extension options can be set via params settings in your `config.toml` or be overwritten by frontmatter for each individual page.
|
||||
|
||||
### Global Configuration File
|
||||
|
||||
#### Recommended Settings
|
||||
|
||||
````toml
|
||||
[markup]
|
||||
[markup.highlight]
|
||||
# line numbers in a table layout will shift if code is wrapping, so better
|
||||
# use inline; besides that visually both layouts have the same look and behavior
|
||||
lineNumbersInTable = false
|
||||
|
||||
# if `guessSyntax = true`, there will be no unstyled code even if no language
|
||||
# was given BUT Mermaid and Math codefences will not work anymore! So this is a
|
||||
# mandatory setting for your site if you want to use Mermaid or Math codefences
|
||||
guessSyntax = false
|
||||
|
||||
# the shipped variants come with their own modified chroma syntax highlightning
|
||||
# style which is imported in theme-relearn-light.css, theme-relearn-dark.css, etc.;
|
||||
# if you want to use a predefined style instead:
|
||||
# - remove `noClasses` or set `noClasses = true`
|
||||
# - set `style` to a predefined style name
|
||||
noClasses = false
|
||||
# style = "tango"
|
||||
````
|
||||
|
||||
#### Optional Settings
|
||||
|
||||
````toml
|
||||
[params]
|
||||
highlightWrap = true
|
||||
````
|
||||
|
||||
### Page's Frontmatter
|
||||
|
||||
````toml
|
||||
+++
|
||||
highlightWrap = true
|
||||
+++
|
||||
````
|
||||
|
||||
## Examples
|
||||
|
||||
### Line Numbers
|
||||
|
||||
As mentioned above, line numbers in a `table` layout will shift if code is wrapping, so better use `inline`. To make things easier for you, set `lineNumbersInTable = false` in your `config.toml` and add `lineNos = true` when calling the shortcode instead of the specific values `table` or `inline`.
|
||||
|
||||
````go
|
||||
{{</* highlight lineNos="true" type="py" */>}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{</* /highlight */>}}
|
||||
````
|
||||
|
||||
{{< highlight lineNos="true" type="py" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
### With Wrap
|
||||
|
||||
````go
|
||||
{{</* highlight type="py" wrap="true" hl_lines="2" */>}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{</* /highlight */>}}
|
||||
````
|
||||
|
||||
{{< highlight type="py" wrap="true" hl_lines="2" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
### Without Wrap
|
||||
|
||||
````go
|
||||
{{</* highlight type="py" wrap="false" hl_lines="2" */>}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{</* /highlight */>}}
|
||||
````
|
||||
|
||||
{{< highlight type="py" wrap="false" hl_lines="2">}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
5
exampleSite/content/shortcodes/highlight.pir.md
Normal file
5
exampleSite/content/shortcodes/highlight.pir.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
+++
|
||||
descrption = "Beaut'fl math and chemical formulae"
|
||||
title = "Highlight"
|
||||
+++
|
||||
{{< piratify true >}}
|
|
@ -64,7 +64,7 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
#### <>
|
||||
|
||||
{{< highlight json "linenos=table,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{{< highlight json "linenos=true,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ Some preformatted stuff in HTML elements
|
|||
#### <> %%
|
||||
|
||||
{{< tab title="json" >}}
|
||||
{{% highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{{% highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ Some preformatted stuff in HTML elements
|
|||
#### <> <>
|
||||
|
||||
{{< tab title="json" >}}
|
||||
{{< highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{{< highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
{{% tabs %}}
|
||||
{{% tab title="json" %}}
|
||||
{{% highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{{% highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
{{% tabs %}}
|
||||
{{% tab title="json" %}}
|
||||
{{< highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{{< highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
{{% tabs %}}
|
||||
{{< tab title="json" >}}
|
||||
{{% highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{{% highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
{{% tabs %}}
|
||||
{{< tab title="json" >}}
|
||||
{{< highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{{< highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -166,7 +166,7 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
{{< tabs >}}
|
||||
{{% tab title="json" %}}
|
||||
{{% highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{{% highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
{{< tabs >}}
|
||||
{{% tab title="json" %}}
|
||||
{{< highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{{< highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
{{< tabs >}}
|
||||
{{< tab title="json" >}}
|
||||
{{% highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{{% highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" %}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
|
@ -202,10 +202,78 @@ Some preformatted stuff in HTML elements
|
|||
|
||||
{{< tabs >}}
|
||||
{{< tab title="json" >}}
|
||||
{{< highlight html "linenos=table,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{{< highlight html "linenos=true,hl_lines=1 2,anchorlinenos=true" >}}
|
||||
{
|
||||
"Hello": "World" "Hello": "World" "Hello": "World"
|
||||
}
|
||||
{{< /highlight >}}
|
||||
{{< /tab >}}
|
||||
{{< /tabs >}}
|
||||
|
||||
## With Wrap
|
||||
|
||||
### LineNo Table
|
||||
|
||||
{{< highlight lineNos="table" type="py" wrap="true" hl_lines="2" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
### LineNo Inline
|
||||
|
||||
{{< highlight lineNos="inline" type="py" wrap="true" hl_lines="2" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
### No LineNo
|
||||
|
||||
{{< highlight type="py" wrap="true" hl_lines="2" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
### No Type
|
||||
|
||||
{{< highlight wrap="true" hl_lines="2" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
## Without Wrap
|
||||
|
||||
### LineNo Table
|
||||
|
||||
{{< highlight lineNos="table" type="py" wrap="false" hl_lines="2">}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
### LineNo Inline
|
||||
|
||||
{{< highlight lineNos="inline" type="py" wrap="false" hl_lines="2" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
### No LineNo
|
||||
|
||||
{{< highlight type="py" wrap="false" hl_lines="2" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
||||
### No Type
|
||||
|
||||
{{< highlight wrap="false" hl_lines="2" >}}
|
||||
# Quicksort Python One-liner
|
||||
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
|
||||
# Some more stuff
|
||||
{{< /highlight >}}
|
||||
|
|
7
layouts/_default/_markup/render-codeblock.html
Normal file
7
layouts/_default/_markup/render-codeblock.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
{{- partial "shortcodes/highlight.html" (dict
|
||||
"context" .Page
|
||||
"attributes" .Attributes
|
||||
"content" .Inner
|
||||
"options" .Options
|
||||
"type" .Type
|
||||
) }}
|
60
layouts/partials/shortcodes/highlight.html
Normal file
60
layouts/partials/shortcodes/highlight.html
Normal file
|
@ -0,0 +1,60 @@
|
|||
{{- $context := .context }}
|
||||
{{- $attributes := dict }}
|
||||
{{- $otherAttributes := dict }}
|
||||
{{- $content := "" }}
|
||||
{{- $options := dict }}
|
||||
{{- $otherOptions := dict }}
|
||||
{{- $type := "" }}
|
||||
{{- range $k, $v := . }}
|
||||
{{- if eq $k "context" }}
|
||||
{{- else if eq $k "attributes" }}
|
||||
{{- $attributes = $v }}
|
||||
{{- else if eq $k "content" }}
|
||||
{{- $content = trim $v "\n\r" }}
|
||||
{{- else if eq $k "options" }}
|
||||
{{- $options = $v }}
|
||||
{{- else if eq $k "type" }}
|
||||
{{- $type = $v }}
|
||||
{{- else if eq $k "wrap" }}
|
||||
{{- $otherAttributes = $otherAttributes | merge (dict $k $v) }}
|
||||
{{- else }}
|
||||
{{- $otherOptions = $otherOptions | merge (dict $k $v) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- $options = $options | merge $otherOptions }}
|
||||
{{- $otherOptions := dict }}
|
||||
{{- range $k, $v := $options }}
|
||||
{{- if eq $k "wrap" }}
|
||||
{{- $otherAttributes = (dict $k $v) | merge $otherAttributes }}
|
||||
{{- else }}
|
||||
{{- $otherOptions = $otherOptions | merge (dict $k $v) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- $options = $otherOptions }}
|
||||
{{- $attributes = $attributes | merge $otherAttributes }}
|
||||
{{- $params := slice }}
|
||||
{{- range $k, $v := $options }}
|
||||
{{- $params = $params | append (printf "%s=%s" $k $v) }}
|
||||
{{- end }}
|
||||
{{- $params = delimit $params ", " }}
|
||||
{{- $wrap := true }}
|
||||
{{- if isset $context.Site.Params "highlightwrap" }}
|
||||
{{- $wrap = $context.Site.Params.highlightWrap }}
|
||||
{{- end }}
|
||||
{{- if isset $context.Params "highlightwrap" }}
|
||||
{{- $wrap = $context.Params.highlightWrap }}
|
||||
{{- end }}
|
||||
{{- range $k, $v := $attributes }}
|
||||
{{- if eq $k "wrap" }}
|
||||
{{- $wrap = $v }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- if eq (printf "%T" $wrap) "string" }}
|
||||
{{- $wrap = (eq $wrap "true") }}
|
||||
{{- end }}
|
||||
{{- $content = highlight $content $type $params }}
|
||||
{{- $content = replaceRE "^([\\s\\n\\r]*)(<pre\\s+?[\\s\\S]*)$" "${1}<div class=\"highlight\">${2}</div>" $content }}
|
||||
{{- if $wrap }}
|
||||
{{- $content = replaceRE "^([\\s\\n\\r]*<div\\s+class=\")" "${1}wrap-code " $content 1 }}
|
||||
{{- end }}
|
||||
{{- $content | safeHTML }}
|
39
layouts/shortcodes/highlight.html
Normal file
39
layouts/shortcodes/highlight.html
Normal file
|
@ -0,0 +1,39 @@
|
|||
{{- $version := split hugo.Version "." }}
|
||||
{{- $major := int (index $version 0) }}
|
||||
{{- $minor := int (index $version 1) }}
|
||||
{{- $content := "" }}
|
||||
{{- if or (and (eq $major 0) (ge $minor 110)) (gt $major 0) }}
|
||||
{{- $content = .InnerDeindent }}
|
||||
{{- else }}
|
||||
{{- $content = .Inner }}
|
||||
{{- end }}
|
||||
{{- $attributes := dict }}
|
||||
{{- $options := dict }}
|
||||
{{- $stringOptions := "" }}
|
||||
{{- $otherOptions := dict }}
|
||||
{{- $type := "" }}
|
||||
{{- range $k, $v := .Params }}
|
||||
{{- if eq $k 0 }}
|
||||
{{- $type = $v }}
|
||||
{{- else if eq $k 1 }}
|
||||
{{- $stringOptions = trim $v "\n\r\t " }}
|
||||
{{- else if eq $k "type" }}
|
||||
{{- $type = $v }}
|
||||
{{- else if eq $k "wrap" }}
|
||||
{{- $attributes = $attributes | merge (dict $k $v) }}
|
||||
{{- else }}
|
||||
{{- $otherOptions = $otherOptions | merge (dict $k $v) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- range (split $stringOptions ",") }}
|
||||
{{- $pair := split . "=" }}
|
||||
{{- $options = $options | merge (dict (index $pair 0) (index $pair 1)) }}
|
||||
{{- end }}
|
||||
{{- $options = $options | merge $otherOptions }}
|
||||
{{- partial "shortcodes/highlight.html" (dict
|
||||
"context" .Page
|
||||
"attributes" $attributes
|
||||
"content" $content
|
||||
"options" $options
|
||||
"type" $type
|
||||
) }}
|
|
@ -524,12 +524,14 @@
|
|||
}
|
||||
|
||||
div.highlight > div table + .copy-to-clipboard-button,
|
||||
div.highlight pre + .copy-to-clipboard-button,
|
||||
pre .copy-to-clipboard-button {
|
||||
border-color: rgba( 216, 216, 216, 1 ); /* var(--CODE-BLOCK-BORDER-color) */
|
||||
color: rgba( 72, 106, 201, 1 ); /* var(--MAIN-LINK-color) */
|
||||
}
|
||||
|
||||
div.highlight > div table + .copy-to-clipboard-button:hover,
|
||||
div.highlight pre + .copy-to-clipboard-button:hover,
|
||||
pre .copy-to-clipboard-button:hover {
|
||||
background-color: rgba( 72, 106, 201, 1 ); /* var(--MAIN-LINK-color) */
|
||||
border-color: rgba( 72, 106, 201, 1 ); /* var(--MAIN-LINK-color) */
|
||||
|
|
|
@ -544,14 +544,19 @@ div.box > .box-content {
|
|||
|
||||
#body table th > :last-child,
|
||||
#body table th > :last-child :last-child,
|
||||
#body table th > div.highlight:last-child pre,
|
||||
#body table td > :last-child,
|
||||
#body table td > :last-child :last-child,
|
||||
#body table td > div:last-child pre,
|
||||
#body div.box > .box-content > :last-child,
|
||||
#body div.box > .box-content > :last-child :last-child,
|
||||
#body div.box > .box-content > div:last-child pre,
|
||||
#body div.expand > .expand-content-text > :last-child,
|
||||
#body div.expand > .expand-content-text > :last-child :last-child,
|
||||
#body div.expand > .expand-content-text > div:last-child pre,
|
||||
#body div.tab-content > .tab-content-text > :last-child,
|
||||
#body div.tab-content > .tab-content-text > :last-child :last-child {
|
||||
#body div.tab-content > .tab-content-text > :last-child :last-child,
|
||||
#body div.tab-content > .tab-content-text > div:last-child pre {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
|
@ -663,14 +668,15 @@ pre code {
|
|||
font-size: .9375rem;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
white-space: inherit;
|
||||
}
|
||||
|
||||
div.highlight{
|
||||
position: relative;
|
||||
}
|
||||
/* we may have special treatment if highlight shortcode was used in table lineno mode */
|
||||
div.highlight > div{
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
position: relative;
|
||||
}
|
||||
/* remove default style for usual markdown tables */
|
||||
div.highlight > div table{
|
||||
|
@ -700,9 +706,15 @@ div.highlight > div table{
|
|||
display: block;
|
||||
overflow: auto;
|
||||
}
|
||||
div.highlight > div td:not(:first-child):last-child pre code{
|
||||
div.highlight:not(.wrap-code) pre{
|
||||
overflow: auto;
|
||||
}
|
||||
div.highlight:not(.wrap-code) pre code{
|
||||
white-space: pre;
|
||||
}
|
||||
div.highlight.wrap-code pre code{
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
/* remove border from row cells if highlight shortcode was used in table lineno mode */
|
||||
div.highlight > div td > pre {
|
||||
border-radius: 0;
|
||||
|
@ -1101,12 +1113,14 @@ span > .copy-to-clipboard-button {
|
|||
}
|
||||
|
||||
div.highlight > div table + .copy-to-clipboard-button > i,
|
||||
div.highlight pre + .copy-to-clipboard-button > i,
|
||||
.copy-to-clipboard-code + .copy-to-clipboard-button > i {
|
||||
padding-left: 5px;
|
||||
padding-right: 5px;
|
||||
}
|
||||
|
||||
div.highlight > div table + .copy-to-clipboard-button,
|
||||
div.highlight pre + .copy-to-clipboard-button,
|
||||
pre > .copy-to-clipboard-button {
|
||||
background-color: rgba( 160, 160, 160, .2 );
|
||||
border-radius: 2px;
|
||||
|
|
|
@ -273,12 +273,14 @@ table {
|
|||
}
|
||||
|
||||
div.highlight > div table + .copy-to-clipboard-button,
|
||||
div.highlight pre + .copy-to-clipboard-button,
|
||||
pre .copy-to-clipboard-button {
|
||||
border-color: var(--INTERNAL-CODE-BLOCK-BORDER-color);
|
||||
color: var(--INTERNAL-MAIN-LINK-color);
|
||||
}
|
||||
|
||||
div.highlight > div table + .copy-to-clipboard-button:hover,
|
||||
div.highlight pre + .copy-to-clipboard-button:hover,
|
||||
pre .copy-to-clipboard-button:hover {
|
||||
background-color: var(--INTERNAL-MAIN-LINK-color);
|
||||
border-color: var(--INTERNAL-MAIN-LINK-color);
|
||||
|
|
|
@ -577,6 +577,24 @@ function initCodeClipboard(){
|
|||
table.dataset[ 'code' ] = text;
|
||||
table.parentNode.insertBefore( button, table.nextSibling );
|
||||
}
|
||||
else if( inPre ){
|
||||
var pre = code.parentNode;
|
||||
pre.dataset[ 'code' ] = text;
|
||||
var p = pre.parentNode;
|
||||
// indented code blocks are missing the div
|
||||
while( p != document && ( p.tagName.toLowerCase() != 'div' || !p.classList.contains( 'highlight' ) ) ){
|
||||
p = p.parentNode;
|
||||
}
|
||||
if( p == document ){
|
||||
var clone = pre.cloneNode( true );
|
||||
var div = document.createElement( 'div' );
|
||||
div.classList.add( 'highlight' );
|
||||
div.appendChild( clone );
|
||||
pre.parentNode.replaceChild( div, pre );
|
||||
pre = clone;
|
||||
}
|
||||
pre.parentNode.insertBefore( button, pre.nextSibling );
|
||||
}
|
||||
else{
|
||||
code.dataset[ 'code' ] = text;
|
||||
code.parentNode.insertBefore( button, code.nextSibling );
|
||||
|
|
Loading…
Reference in a new issue