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 renders pseudocode beautifully to
4- HTML. The grammar of pseudocode specified by PseudoCode.js
5- resembles that of Tex/LaTeX and its algorithm packages. TeX allows simple
6- construction of math formulas. And LaTeX users who are already familiar with
7- the algorithm packages can easily adopt PseduoCode.js.
3+ Pseudocode.js is a JavaScript library that typesets pseudocode beautifully to
4+ HTML.
5+
6+ Pseudocode.js accepts a TeX-style input that borrows the algorithmic constructs
7+ from LaTeX's algorithm packages and produces a HTML output that looks (almost)
8+ identical to its LaTeX's counterpart. The usage of a TeX-style grammar enables
9+ simple construction of math formulas. And any user who already has some LaTeX
10+ experience should find pseudocode.js very intuitive.
811
912## Demo
13+ Visit the [ project website] ( http://www.tatetian.me/pseudocode.js ) for a live demo.
1014
1115## Usage
12- Download PseudoCodo , and host the files on your server.
16+ Download [ pseudocode.js ] ( ) , and host the files on your server.
1317And then include the ` js ` and ` css ` files in your HTML files:
1418
1519``` html
1620<link rel =" stylesheet" href =" //path/to/pseudocode/pseudocode.min.css" >
1721<script src =" //path/to/pseudocode/pseudocode.min.js" ></script >
1822```
1923
20- Assume your to-be-renderd pseudocode is in an ` <pre> ` DOM element:
24+ Assume your pseudocode is in an ` <pre> ` DOM element:
2125``` html
2226<pre id =" hello-world-code" style =" display :hidden ;" >
2327\begin{algorithmc}
@@ -27,29 +31,170 @@ Assume your to-be-renderd pseudocode is in an `<pre>` DOM element:
2731```
2832
2933To render the above code as a HTML element and append to a parent DOM element,
30- call ` PseudoCode .render` :
34+ call ` pseudocode .render` :
3135``` js
3236var code = document .getElementById (" hello-world-code" ).textContent ;
3337var parentEl = document .body ;
34- var options = {lineNumber: true };
35- PseudoCode .render (code, parentEl, options);
38+ var options = {
39+ lineNumber: true
40+ };
41+ pseudocode .render (code, parentEl, options);
3642```
3743
38- To generate a string of rendered HTML, call ` PseudoCode .renderToString` :
44+ To generate a string of rendered HTML, call ` pseudocode .renderToString` :
3945``` js
4046var code = document .getElementById (" hello-world-code" ).textContent ;
41- var options = {lineNumber: true };
42- var htmlStr = PseudoCode .renderToString (code, options);
47+ var options = {
48+ lineNumber: true
49+ };
50+ var htmlStr = pseudocode .renderToString (code, options);
4351console .log (htmlStr);
4452```
4553
4654## Features
55+ There are several packages for typesetting algorithms in LaTeX, among which
56+ [ ` algorithmic ` ] ( http://mirror.ctan.org/tex-archive/macros/latex/contrib/algorithms/algorithms.pdf )
57+ package is the most simple and intuitive, and is chosen by IEEE in its
58+ [ LaTeX template file] ( http://www.ctan.org/tex-archive/macros/latex/contrib/IEEEtran ) .
59+ The grammar of Pseudocode.js is mostly compatible with ` algorithmic ` package with
60+ a few improvement to make it even more easier to use.
61+
62+ ### Example
63+
64+ ``` tex
65+ % This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition)
66+ \begin{algorithm}
67+ \caption{Quicksort}
68+ \begin{algorithmic}
69+ \PROCEDURE{Quicksort}{$A, p, r$}
70+ \IF{$p < r$}
71+ \STATE $q = $ \CALL{Partition}{$A, p, r$}
72+ \STATE \CALL{Quicksort}{$A, p, q - 1$}
73+ \STATE \CALL{Quicksort}{$A, q + 1, r$}
74+ \ENDIF
75+ \ENDPROCEDURE
76+ \PROCEDURE{Partition}{$A, p, r$}
77+ \STATE $x = A[r]$
78+ \STATE $i = p - 1$
79+ \FOR{$j = p$ \TO $r - 1$}
80+ \IF{$A[j] < x$}
81+ \STATE $i = i + 1$
82+ \STATE exchange
83+ $A[i]$ with $A[j]$
84+ \ENDIF
85+ \STATE exchange $A[i]$ with $A[r]$
86+ \ENDFOR
87+ \ENDPROCEDURE
88+ \end{algorithmic}
89+ \end{algorithm}</textarea>
90+ ```
91+
92+
93+ ### Grammar
94+
95+ Commands for typesetting algorithms must be enclosed in an ` algorithmic ` environment:
96+ ``` tex
97+ \begin{algorithmic}
98+ # A precondition is optional
99+ \REQUIRE <text>
100+ # A postcondition is optional
101+ \ENSURE <text>
102+ # The body of your code is a <block>
103+ \STATE ...
104+ \end{algorithmic}
105+ ```
106+
107+ ` <block> ` can include zero or more ` <statement> ` , ` <control> ` , ` <comment> `
108+ and ` <function> ` :
109+ ``` tex
110+ # A <statement> can be:
111+ \STATE <text>
112+ \RETURN <text>
113+ \PRINT <text>
114+
115+ # A <control> can be:
116+ # A conditional
117+ \IF{<condition>}
118+ <block>
119+ \ELIF{<condition>}
120+ <block>
121+ \ELSE
122+ <block>
123+ \ENDIF
124+ # Or a loop: \WHILE, \FOR or \FORALL
125+ \WHILE{<condition>}
126+ <block>
127+ \ENDWHILE
128+
129+ # A <function> can by defined by either \FUNCTION or \PROCEDURE
130+ # Both are exactly the same
131+ \FUNCTION{<name>}{<params>}
132+ <block>
133+ \ENDFUNCTION
134+
135+ # A <comment> is:
136+ \COMMENT{<text>}
137+ ```
138+
139+ A ` <text> ` (or ` <condition> ` ) can include the following:
140+ ``` tex
141+ # Normal characters
142+ Hello world
143+ # Escaped characters
144+ \\, \{, \}, \$, \&, \#, \% and \_
145+ # Math formula
146+ $i \gets i + 1$
147+ # Function call
148+ \CALL{<func>}{<args>}
149+ # Keywords
150+ \AND, \OR, \XOR, \NOT, \TO, \TRUE, \FALSE
151+ # LaTeX's sizing commands
152+ \tiny, \scriptsize, \footnotesize, \small \normalsize, \large, \Large, \LARGE,
153+ \huge, \HUGE
154+ # LaTeX's font declarations
155+ \rmfamily, \sffamily, \ttfamily
156+ \upshape, \itshape, \slshape, \scshape
157+ \bfseries, \mdseries, \lfseries
158+ # LaTeX's font commands
159+ \textnormal, \textrm, \textsf, \texttt
160+ \textup, \textit, \textsl, \textsc
161+ \uppercase, \lowercase
162+ \textbf, \textmd, \textlf
163+ # And it's possible to group text with braces
164+ normal text {\small the size gets smaller} back to normal again
165+ ```
166+
167+ Note that although Pseudocode.js recognizes some LaTeX commands, it is by no
168+ means a full-featured LaTeX implementation in JavaScript.
169+ It only support a subset of LaTeX commands that are most relevant to
170+ typesetting algorithms.
171+
172+
173+ To display the caption of an algorithm, use ` algorithm ` environtment as a 'float' wrapper :
174+ ``` tex
175+ \begin{algorithm}
176+ \caption{The caption of your algorithm}
177+ \begin{algorithmic}
178+ \STATE ...
179+ \end{algorithmic}
180+ \end{algorithm}
181+ ```
182+
183+ ### Options
184+ Function ` pseudocode.renderToString ` and ` pseudocode.renderToString ` accepts
185+ an option as the last argument.
47186
187+ * ` indentSize ` : The indent size of inside a control block, e.g. if, for,
188+ etc. The unit must be in 'em'. Default value: '1.2em'.
189+ * ` commentSymbol ` : The delimiters used to start and end a comment region.
190+ Note that only line comments are supported. Default value: '//'.
191+ * ` lineNumber ` : Whether line numbering is enabled. Default value: false.
192+ * ` lineNumberPunc ` : The punctuation that follows line number. Default
193+ value: ':'.
194+ * ` noEnd ` : Whether block ending, like ` end if ` , end procedure`, etc., are
195+ showned. Default value: false.
196+ * ` captionCount ` : Set the caption counter to this new value.
48197
49- ## TeX suport
50- Pseudocode.js is by no means a full-featured TeX implementation in JavaScript.
51- It only support a subset of TeX/LaTeX commands that are supposed to be
52- more likely to be used in an algorithm environtment.
53198
54199## Acknowledgement
55- Pseudocode.js is powered by KaTeX to render math expressions .
200+ Pseudocode.js is powered by [ KaTeX] ( http://khan.github.io/KaTeX ) to render math formulas .
0 commit comments