diff --git a/_config.yml b/_config.yml index cc1c4b55606c..94b9cc9849f9 100644 --- a/_config.yml +++ b/_config.yml @@ -499,7 +499,7 @@ third_party_libraries: fonts: "output/chtml/fonts/woff-v2/" url: fonts: "https://cdn.jsdelivr.net/npm/mathjax@{{version}}/es5/output/chtml/fonts/woff-v2/" - js: "https://cdn.jsdelivr.net/npm/mathjax@{{version}}/es5/tex-mml-chtml.js" + js: "https://cdn.jsdelivr.net/npm/mathjax@{{version}}/es5/tex-mml-chtml.min.js" version: "3.2.0" masonry: integrity: @@ -535,6 +535,11 @@ third_party_libraries: url: js: "https://cdnjs.cloudflare.com/polyfill/v{{version}}/polyfill.min.js?features=es6" version: "3" + pseudocode: + url: + css: "https://cdn.jsdelivr.net/npm/pseudocode@{{version}}/build/pseudocode.min.css" + js: "https://cdn.jsdelivr.net/npm/pseudocode@{{version}}/build/pseudocode.min.js" + version: "2.4.1" swiper: integrity: css: "sha256-yUoNxsvX+Vo8Trj3lZ/Y5ZBf8HlBFsB6Xwm7rH75/9E=" diff --git a/_includes/head.liquid b/_includes/head.liquid index da9e98ede746..fc316baec30d 100644 --- a/_includes/head.liquid +++ b/_includes/head.liquid @@ -45,6 +45,17 @@ {% endif %} + + +{% if page.pseudocode %} + +{% endif %} + {% if site.icon.size <= 4 %} - - - + {% unless page.pseudocode %} + + + + + {% endunless %} {% endif %} diff --git a/_includes/scripts/pseudocode.liquid b/_includes/scripts/pseudocode.liquid new file mode 100644 index 000000000000..0db4ca897fde --- /dev/null +++ b/_includes/scripts/pseudocode.liquid @@ -0,0 +1,48 @@ +{% if site.enable_math and page.pseudocode %} + + + + + +{% endif %} diff --git a/_layouts/default.liquid b/_layouts/default.liquid index 1c3584e01082..ea8870eb8c40 100644 --- a/_layouts/default.liquid +++ b/_layouts/default.liquid @@ -64,6 +64,7 @@ {% include scripts/misc.liquid %} {% include scripts/badges.liquid %} {% include scripts/mathjax.liquid %} + {% include scripts/pseudocode.liquid %} {% include scripts/analytics.liquid %} {% include scripts/progressBar.liquid %} {% include scripts/wechatModal.liquid %} diff --git a/_posts/2024-04-15-pseudocode.md b/_posts/2024-04-15-pseudocode.md new file mode 100644 index 000000000000..d7ff18b86334 --- /dev/null +++ b/_posts/2024-04-15-pseudocode.md @@ -0,0 +1,71 @@ +--- +layout: post +title: a post with pseudo code +date: 2024-04-15 00:01:00 +description: this is what included pseudo code could look like +tags: formatting code +categories: sample-posts +pseudocode: true +--- + +This is an example post with some pseudo code rendered by [pseudocode](https://github.com/SaswatPadhi/pseudocode.js). The example presented here is the same as the one in the [pseudocode.js](https://saswat.padhi.me/pseudocode.js/) documentation, with only one simple but important change: everytime you would use `$`, you should use `$$` instead. Also, note that the `pseudocode` key in the front matter is set to `true` to enable the rendering of pseudo code. As an example, using this code: + +````markdown +```pseudocode +% This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition) +\begin{algorithm} +\caption{Quicksort} +\begin{algorithmic} +\PROCEDURE{Quicksort}{$$A, p, r$$} + \IF{$$p < r$$} + \STATE $$q = $$ \CALL{Partition}{$$A, p, r$$} + \STATE \CALL{Quicksort}{$$A, p, q - 1$$} + \STATE \CALL{Quicksort}{$$A, q + 1, r$$} + \ENDIF +\ENDPROCEDURE +\PROCEDURE{Partition}{$$A, p, r$$} + \STATE $$x = A[r]$$ + \STATE $$i = p - 1$$ + \FOR{$$j = p$$ \TO $$r - 1$$} + \IF{$$A[j] < x$$} + \STATE $$i = i + 1$$ + \STATE exchange + $$A[i]$$ with $$A[j]$$ + \ENDIF + \STATE exchange $$A[i]$$ with $$A[r]$$ + \ENDFOR +\ENDPROCEDURE +\end{algorithmic} +\end{algorithm} +``` +```` + +Generates: + +```pseudocode +% This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition) +\begin{algorithm} +\caption{Quicksort} +\begin{algorithmic} +\PROCEDURE{Quicksort}{$$A, p, r$$} + \IF{$$p < r$$} + \STATE $$q = $$ \CALL{Partition}{$$A, p, r$$} + \STATE \CALL{Quicksort}{$$A, p, q - 1$$} + \STATE \CALL{Quicksort}{$$A, q + 1, r$$} + \ENDIF +\ENDPROCEDURE +\PROCEDURE{Partition}{$$A, p, r$$} + \STATE $$x = A[r]$$ + \STATE $$i = p - 1$$ + \FOR{$$j = p$$ \TO $$r - 1$$} + \IF{$$A[j] < x$$} + \STATE $$i = i + 1$$ + \STATE exchange + $$A[i]$$ with $$A[j]$$ + \ENDIF + \STATE exchange $$A[i]$$ with $$A[r]$$ + \ENDFOR +\ENDPROCEDURE +\end{algorithmic} +\end{algorithm} +``` diff --git a/_sass/_base.scss b/_sass/_base.scss index f36290a14871..554aa82d1f03 100644 --- a/_sass/_base.scss +++ b/_sass/_base.scss @@ -1127,3 +1127,15 @@ swiper-container { --swiper-pagination-color: var(--global-theme-color); --swiper-pagination-bullet-inactive-color: var(--global-text-color); } + +.ps-root { + .ps-algorithm { + margin: 0.8em 0; + border-top: 3px solid var(--global-text-color); + border-bottom: 2px solid var(--global-text-color); + } + + .ps-algorithm.with-caption > .ps-line:first-child { + border-bottom: 2px solid var(--global-text-color); + } +}