1- # Pseudocode .js - Beautiful pseudocode for the Web
1+ # pseudocode .js - Beautiful pseudocode for the Web
22
3- Pseudocode.js is a JavaScript library that typesets pseudocode beautifully to
3+ <img align =" right " width =" 42% " src =" docs/screenshot.png " >
4+
5+ ** pseudocode.js** is a JavaScript library that typesets pseudocode beautifully to
46HTML.
57
6- * ** Intuitive grammar** : Pseudocode.js takes a LaTeX-style input that supports
8+ * _ Intuitive grammar: _ Pseudocode.js takes a LaTeX-style input that supports
79 the algorithmic constructs from LaTeX's algorithm packages. With or without
810 LaTeX experience, a user should find the grammar fairly intuitive.
9- * ** Print quality:** The HTML output produced by pseudocode.js is (almost)
11+ * _ Print quality:_ The HTML output produced by pseudocode.js is (almost)
1012 identical with the pretty algorithms printed on publications that are
1113 typeset by LaTeX.
12- * ** Math formula support:** Inserting math formulas in pseudocode.js is as easy
14+ * _ Math formula support:_ Inserting math formulas in pseudocode.js is as easy
1315 as LaTeX. Just enclose math expression in ` $...$ ` or ` \(...\) ` .
1416
1517It supports all modern browsers, including Chrome, Safari, Firefox, Edge, and
1618IE 9 - IE 11.
1719
20+
1821## Demo
1922Visit the [ project website] ( https://saswatpadhi.github.io/pseudocode.js ) for demo.
2023
24+
2125## Usage
2226
2327### Quick Start
2428
25- Pseudocode .js can render math formulas using either
29+ pseudocode .js can render math formulas using either
2630[ KaTeX] ( https://github.com/Khan/KaTeX ) , or [ MathJax] ( https://www.mathjax.org/ ) .
2731
2832#### Step 1A · ; For KaTeX users
2933Include the following in the ` <head> ` of your page:
3034
3135``` html
32- <link rel =" stylesheet" href =" https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css"
33- integrity =" sha256-V8SV2MO1FUb63Bwht5Wx9x6PVHNa02gv8BgH/uH3ung=" crossorigin =" anonymous" />
3436<script src =" https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.js"
3537 integrity =" sha256-F/Xda58SPdcUCr+xhSGz9MA2zQBPb0ASEYKohl8UCHc=" crossorigin =" anonymous" >
3638 </script >
@@ -83,68 +85,51 @@ Then include the following in the `<head>` of your page:
8385<script src =" //path/to/pseudocode/pseudocode.min.js" ></script >
8486```
8587
86-
8788#### Step 3 · ; Write your pseudocode inside a ` <pre> `
8889We assume the pseudocode to be rendered is in a ` <pre> ` DOM element.
8990Here is an example that illustrates a quicksort algorithm:
9091
9192``` tex
92- % This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition)
93- \begin{algorithm}
94- \caption{Quicksort}
95- \begin{algorithmic}
96- \PROCEDURE{Quicksort}{$A, p, r$}
97- \IF{$p < r$}
98- \STATE $q = $ \CALL{Partition}{$A, p, r$}
99- \STATE \CALL{Quicksort}{$A, p, q - 1$}
100- \STATE \CALL{Quicksort}{$A, q + 1, r$}
101- \ENDIF
102- \ENDPROCEDURE
103- \PROCEDURE{Partition}{$A, p, r$}
104- \STATE $x = A[r]$
105- \STATE $i = p - 1$
106- \FOR{$j = p$ \TO $r - 1$}
107- \IF{$A[j] < x$}
108- \STATE $i = i + 1$
109- \STATE exchange
110- $A[i]$ with $A[j]$
93+ <pre id="quicksort" style="display:hidden;">
94+ % This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition)
95+ \begin{algorithm}
96+ \caption{Quicksort}
97+ \begin{algorithmic}
98+ \PROCEDURE{Quicksort}{$A, p, r$}
99+ \IF{$p < r$}
100+ \STATE $q = $ \CALL{Partition}{$A, p, r$}
101+ \STATE \CALL{Quicksort}{$A, p, q - 1$}
102+ \STATE \CALL{Quicksort}{$A, q + 1, r$}
111103 \ENDIF
112- \STATE exchange $A[i]$ with $A[r]$
113- \ENDFOR
114- \ENDPROCEDURE
115- \end{algorithmic}
116- \end{algorithm}</textarea>
117- ```
118-
119- ### API
120- Assume the pseudocode to be rendered is in a ` <pre> ` DOM element:
121- ``` html
122- <pre id =" hello-world-code" style =" display :hidden ;" >
123- \begin{algorithmc}
124- \PRINT \texttt{'hello world'}
125- \end{algorithmc}
104+ \ENDPROCEDURE
105+ \PROCEDURE{Partition}{$A, p, r$}
106+ \STATE $x = A[r]$
107+ \STATE $i = p - 1$
108+ \FOR{$j = p$ \TO $r - 1$}
109+ \IF{$A[j] < x$}
110+ \STATE $i = i + 1$
111+ \STATE exchange
112+ $A[i]$ with $A[j]$
113+ \ENDIF
114+ \STATE exchange $A[i]$ with $A[r]$
115+ \ENDFOR
116+ \ENDPROCEDURE
117+ \end{algorithmic}
118+ \end{algorithm}
126119</pre>
127120```
128121
129- To render the above code as a HTML element and append to a parent DOM element,
130- call ` pseudocode.render ` :
131- ``` js
132- var code = document .getElementById (" hello-world-code" ).textContent ;
133- var parentEl = document .body ;
134- var options = {
135- lineNumber: true
136- };
137- pseudocode .render (code, parentEl, options);
138- ```
122+ #### Step 4 · ; Render the element using pseudocode.js
123+ Insert the following Javascript snippet at the end of your document:
139124
140- To generate a string of rendered HTML, call ` pseudocode.renderToString ` :
141- ``` js
142- var code = document .getElementById (" hello-world-code" ). textContent ;
143- var options = {
144- lineNumber: true
145- };
146- var htmlStr = pseudocode .renderToString (code , options);
147- console . log (htmlStr);
125+ ``` html
126+ < script >
127+ var elem = document .getElementById (" hello-world-code" );
128+ var options = {
129+ lineNumber: true
130+ };
131+ pseudocode .render ( elem . textContent , elem . parentNode , options);
132+ </ script >
148133```
149134
150135
@@ -279,7 +264,7 @@ var DEFAULT_OPTIONS = {
279264```
280265
281266## Build and Test
282- Pseudocode .js is written in JavaScript and built with [ Node.js] ( https://nodejs.org ) .
267+ pseudocode .js is written in JavaScript and built with [ Node.js] ( https://nodejs.org ) .
283268So, make sure you have Node.js installed before building pseudocode.js.
284269
285270To compile the project on Ubuntu Linux, run the following commands in terminal:
@@ -294,7 +279,7 @@ Then, open one of the sample documents:
294279- ` build/katex-samples.html ` , or
295280- ` build/mathjax-v2-samples.html ` , or
296281- ` build/mathjax-v3-samples.html `
297- in your favourite browser to check if the algorithms are typeset correctly.
282+ in your favorite browser to check if the algorithms are typeset correctly.
298283
299284
300285## Author
@@ -305,6 +290,6 @@ and I am the current maintainer of this project.
305290Suggestions, bug reports and pull requests are most welcome.
306291
307292## Acknowledgement
308- Pseudocode .js is partially inspired by [ KaTeX] ( http://khan.github.io/KaTeX/ ) .
293+ pseudocode .js is partially inspired by [ KaTeX] ( http://khan.github.io/KaTeX/ ) .
309294Thanks Emily Eisenberg([ @xymostech ] ( https://github.com/xymostech ) )
310- and other contributers for building such a wonderful project.
295+ and other contributors for building such a wonderful project.
0 commit comments