forked from alshedivat/al-folio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
pseudocode.js
support (alshedivat#2344)
Signed-off-by: George Araújo <george.gcac@gmail.com>
- Loading branch information
1 parent
e04586b
commit 397658b
Showing
7 changed files
with
166 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
{% if site.enable_math %} | ||
<!-- MathJax --> | ||
<script type="text/javascript"> | ||
window.MathJax = { | ||
tex: { | ||
tags: 'ams', | ||
}, | ||
}; | ||
</script> | ||
<script | ||
defer | ||
type="text/javascript" | ||
id="MathJax-script" | ||
src="{{ site.third_party_libraries.mathjax.url.js }}" | ||
></script> | ||
<script defer src="{{ site.third_party_libraries.polyfill.url.js }}"></script> | ||
{% unless page.pseudocode %} | ||
<!-- MathJax --> | ||
<script type="text/javascript"> | ||
window.MathJax = { | ||
tex: { | ||
tags: 'ams', | ||
}, | ||
}; | ||
</script> | ||
<script | ||
defer | ||
type="text/javascript" | ||
id="MathJax-script" | ||
src="{{ site.third_party_libraries.mathjax.url.js }}" | ||
></script> | ||
<script defer src="{{ site.third_party_libraries.polyfill.url.js }}"></script> | ||
{% endunless %} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{% if site.enable_math and page.pseudocode %} | ||
<!-- MathJax --> | ||
<script type="text/javascript"> | ||
window.MathJax = { | ||
tex: { | ||
inlineMath: [ | ||
['$', '$'], | ||
['\\(', '\\)'], | ||
], | ||
displayMath: [ | ||
['$$', '$$'], | ||
['\\[', '\\]'], | ||
], | ||
processEscapes: true, | ||
processEnvironments: true, | ||
}, | ||
}; | ||
</script> | ||
<script | ||
type="text/javascript" | ||
id="MathJax-script" | ||
src="{{ site.third_party_libraries.mathjax.url.js }}" | ||
></script> | ||
<script | ||
type="text/javascript" | ||
src="{{ site.third_party_libraries.pseudocode.url.js }}" | ||
></script> | ||
<script> | ||
document.onreadystatechange = () => { | ||
if (document.readyState === 'complete') { | ||
document.querySelectorAll('pre>code.language-pseudocode').forEach((elem) => { | ||
const texData = elem.textContent; | ||
const parent = elem.parentElement.parentElement; | ||
/* create pseudocode node */ | ||
let pseudoCodeElement = document.createElement('pre'); | ||
pseudoCodeElement.classList.add('pseudocode'); | ||
const text = document.createTextNode(texData); | ||
pseudoCodeElement.appendChild(text); | ||
/* add pseudocode node and remove the original code block */ | ||
parent.appendChild(pseudoCodeElement); | ||
parent.removeChild(elem.parentElement); | ||
/* embed the visualization in the container */ | ||
pseudocode.renderElement(pseudoCodeElement); | ||
}); | ||
} | ||
}; | ||
</script> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters