pseudocode 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/pseudocode/version.rb +8 -0
- data/lib/pseudocode.rb +73 -0
- data/vendor/pseudocode/pseudocode.min.css +1 -0
- data/vendor/pseudocode/pseudocode.min.js +1 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e9943f73afe2ef28fd1fd4c9b3a7f826855b386004802f2392366ac5ed423856
|
4
|
+
data.tar.gz: b449d7ec1408ea9bc77be7ab364525b1f43ea152d51b960449c83ab9195148ad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e8e8fd631641d87bd109761239143f2446ba4e6b86b7421c7aa97705593243aed4bcf77d8222cc4b1829117b0878687075377e0721208de346ad8fbc95c7dca
|
7
|
+
data.tar.gz: 3e1bc3f11253918974b60db14f2581309a2d144385239a49e5de8c7b45bf746945bc196d97532ce1fb15fbd7ea49a07af1b0ddd2971a968eddab67e5a9b7d282
|
data/lib/pseudocode.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "pseudocode/version"
|
4
|
+
|
5
|
+
require 'execjs'
|
6
|
+
require 'katex'
|
7
|
+
|
8
|
+
# Provides a Ruby wrapper for pseudocode server-side rendering.
|
9
|
+
module Pseudocode
|
10
|
+
# JavaScript runtime and context
|
11
|
+
@load_context_mutex = Mutex.new
|
12
|
+
@context = nil
|
13
|
+
@execjs_runtime = -> { ExecJS.runtime }
|
14
|
+
|
15
|
+
# Autoincrementing caption counter
|
16
|
+
@caption_count = 0
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# Renders the given pseudocode algorithm to HTML via pseudocode.renderToString.
|
20
|
+
#
|
21
|
+
# @param algorithm [String] The algorithm expression
|
22
|
+
# @param line_number [Bool] Whether to include line numbers
|
23
|
+
# @param options [Hash] Additional options for pseudocode.renderToString.
|
24
|
+
#
|
25
|
+
# @return [String, Int] HTML render and caption number
|
26
|
+
#
|
27
|
+
# @note This method is thread-safe as long as your ExecJS runtime is
|
28
|
+
# thread-safe. MiniRacer is the recommended runtime.
|
29
|
+
def render(algorithm, line_number: false, **options)
|
30
|
+
return pseudocode_context.call('pseudocode.renderToString', algorithm,
|
31
|
+
lineNumber: line_number,
|
32
|
+
captionCount: @caption_count,
|
33
|
+
**options), @caption_count += 1
|
34
|
+
rescue ExecJS::ProgramError => e
|
35
|
+
raise e
|
36
|
+
end
|
37
|
+
|
38
|
+
# The ExecJS runtime factory, default: `-> { ExecJS.runtime }`.
|
39
|
+
# Set this before calling any other methods to use a different runtime.
|
40
|
+
#
|
41
|
+
# This proc is guaranteed to be called at most once.
|
42
|
+
attr_accessor :execjs_runtime
|
43
|
+
|
44
|
+
# Emulated caption counter to allow manipulation from ruby
|
45
|
+
attr_accessor :caption_count
|
46
|
+
|
47
|
+
# Return JS context, preloaded with KaTeX and Pseudocode.js
|
48
|
+
def pseudocode_context
|
49
|
+
@load_context_mutex.synchronize do
|
50
|
+
source = (File.read Katex.katex_js_path) + (File.read pseudocode_js_path)
|
51
|
+
@context ||= @execjs_runtime.call.compile source
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Return path to included Pseudocode.js script file
|
56
|
+
def pseudocode_js_path
|
57
|
+
File.expand_path File.join('vendor', 'pseudocode', 'pseudocode.min.js'),
|
58
|
+
gem_path
|
59
|
+
end
|
60
|
+
|
61
|
+
# Return path to included Pseudocode.js style file
|
62
|
+
def pseudocode_css_path
|
63
|
+
File.expand_path File.join('vendor', 'pseudocode', 'pseudocode.min.css'),
|
64
|
+
gem_path
|
65
|
+
end
|
66
|
+
|
67
|
+
# Return gem base path
|
68
|
+
def gem_path
|
69
|
+
@gem_path ||=
|
70
|
+
File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
@import url(https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css);.ps-root{font-family:KaTeX_Main,'Times New Roman',Times,serif;font-size:1em;font-weight:100;-webkit-font-smoothing:antialiased!important}.ps-root .ps-algorithm{margin:.8em 0;border-top:3px solid #000;border-bottom:2px solid #000}.ps-root .ps-algorithm.with-caption>.ps-line:first-child{border-bottom:2px solid #000}.ps-root .katex{text-indent:0;font-size:1em}.ps-root .MathJax,.ps-root .MathJax_CHTML{text-indent:0;font-size:1em!important}.ps-root .ps-line{margin:0;padding:0;line-height:1.2}.ps-root .ps-funcname{font-family:KaTeX_Main,'Times New Roman',Times,serif;font-weight:400;font-variant:small-caps;font-style:normal;text-transform:none}.ps-root .ps-keyword{font-family:KaTeX_Main,'Times New Roman',Times,serif;font-weight:700;font-variant:normal;font-style:normal;text-transform:none}.ps-root .ps-comment{font-family:KaTeX_Main,'Times New Roman',Times,serif;font-weight:400;font-variant:normal;font-style:normal;text-transform:none}.ps-root .ps-linenum{font-size:.8em;line-height:1em;width:1.6em;text-align:right;display:inline-block;position:relative;padding-right:.3em}.ps-root .ps-algorithmic.with-linenum .ps-line.ps-code{text-indent:-1.6em}.ps-root .ps-algorithmic.with-linenum .ps-line.ps-code>span{text-indent:0}
|
@@ -0,0 +1 @@
|
|
1
|
+
(function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var t;if(typeof window!=="undefined"){t=window}else if(typeof global!=="undefined"){t=global}else if(typeof self!=="undefined"){t=self}else{t=this}t.pseudocode=e()}})(function(){var e,t,n;return function(){function p(o,s,a){function h(n,e){if(!s[n]){if(!o[n]){var t="function"==typeof require&&require;if(!e&&t)return t(n,!0);if(l)return l(n,!0);var i=new Error("Cannot find module '"+n+"'");throw i.code="MODULE_NOT_FOUND",i}var r=s[n]={exports:{}};o[n][0].call(r.exports,function(e){var t=o[n][1][e];return h(t||e)},r,r.exports,p,o,s,a)}return s[n].exports}for(var l="function"==typeof require&&require,e=0;e<a.length;e++)h(a[e]);return h}return p}()({1:[function(e,t,n){var i=e("./src/ParseError");var r=e("./src/Lexer");var o=e("./src/Parser");var s=e("./src/Renderer");function a(e,t){var n=new r(e);var i=new o(n);return new s(i,t)}function h(t){try{MathJax.typeset([t])}catch(e){MathJax.Hub.Queue(["Typeset",MathJax.Hub,t])}}t.exports={ParseError:i,render:function(e,t,n){if(e===null||e===undefined)throw"input cannot be empty";var i=a(e,n);var r=i.toDOM();if(t)t.appendChild(r);if(i.backend.name==="mathjax"){h(r)}return r},renderToString:function(e,t){if(e===null||e===undefined)throw"input cannot be empty";var n=a(e,t);if(n.backend.name==="mathjax"){console.warn("Using MathJax backend -- math may not be rendered.")}return n.toMarkup()},renderElement:function(e,t){if(!(e instanceof Element))throw"a DOM element is required";e.style.display="none";var n=a(e.textContent,t);var i=n.toDOM();e.replaceWith(i);if(n.backend.name==="mathjax"){h(i)}}}},{"./src/Lexer":2,"./src/ParseError":3,"./src/Parser":4,"./src/Renderer":5}],2:[function(e,t,n){var i=e("./utils");var u=e("./ParseError");var r=function(e){this._input=e;this._remain=e;this._pos=0;this._nextAtom=this._currentAtom=null;this._next()};r.prototype.accept=function(e,t){if(this._nextAtom.type===e&&this._matchText(t)){this._next();return this._currentAtom.text}return null};r.prototype.expect=function(e,t){var n=this._nextAtom;if(n.type!==e)throw new u("Expect an atom of "+e+" but received "+n.type,this._pos,this._input);if(!this._matchText(t))throw new u("Expect `"+t+"` but received `"+n.text+"`",this._pos,this._input);this._next();return this._currentAtom.text};r.prototype.get=function(){return this._currentAtom};var o={exec:function(e){var t=[{start:"$",end:"$"},{start:"\\(",end:"\\)"}];var n=e.length;for(var i=0;i<t.length;i++){var r=t[i].start;if(e.indexOf(r)!==0)continue;var o=t[i].end;var s=r.length;var a=e.slice(s);while(s<n){var h=a.indexOf(o);if(h<0)throw new u("Math environment is not closed",this._pos,this._input);if(h>0&&a[h-1]==="\\"){var l=h+o.length;a=a.slice(l);s+=l;continue}var p=[e.slice(0,s+h+o.length),e.slice(r.length,s+h)];return p}}return null}};var p={special:/^(\\\\|\\{|\\}|\\\$|\\&|\\#|\\%|\\_)/,math:o,func:/^\\([a-zA-Z]+)/,open:/^\{/,close:/^\}/,quote:/^(`|``|'|'')/,ordinary:/^[^\\{}$&#%_\s]+/};var c=/^%.*/;var f=/^\s+/;r.prototype._skip=function(e){this._pos+=e;this._remain=this._remain.slice(e)};r.prototype._next=function(){var e=false;while(1){var t=f.exec(this._remain);if(t){e=true;var n=t[0].length;this._skip(n)}var i=c.exec(this._remain);if(!i)break;var r=i[0].length;this._skip(r)}this._currentAtom=this._nextAtom;if(this._remain===""){this._nextAtom={type:"EOF",text:null,whitespace:false};return false}for(var o in p){var s=p[o];var a=s.exec(this._remain);if(!a)continue;var h=a[0];var l=a[1]?a[1]:h;this._nextAtom={type:o,text:l,whitespace:e};this._pos+=h.length;this._remain=this._remain.slice(a[0].length);return true}throw new u("Unrecoganizable atom",this._pos,this._input)};r.prototype._matchText=function(e){if(e===null||e===undefined)return true;if(i.isString(e))return e.toLowerCase()===this._nextAtom.text.toLowerCase();else{e=e.map(function(e){return e.toLowerCase()});return e.indexOf(this._nextAtom.text.toLowerCase())>=0}};t.exports=r},{"./ParseError":3,"./utils":6}],3:[function(e,t,n){function i(e,t,n){var i="Error: "+e;if(t!==undefined&&n!==undefined){i+=" at position "+t+": `";n=n.slice(0,t)+"\u21b1"+n.slice(t);var r=Math.max(0,t-15);var o=t+15;i+=n.slice(r,o)+"`"}this.message=i}i.prototype=Object.create(Error.prototype);i.prototype.constructor=i;t.exports=i},{}],4:[function(e,t,n){var s=e("./utils");var r=e("./ParseError");var a=function(e,t){this.type=e;this.value=t;this.children=[]};a.prototype.toString=function(e){if(!e)e=0;var t="";for(var n=0;n<e;n++)t+=" ";var i=t+"<"+this.type+">";if(this.value)i+=" ("+s.toString(this.value)+")";i+="\n";if(this.children){for(var r=0;r<this.children.length;r++){var o=this.children[r];i+=o.toString(e+1)}}return i};a.prototype.addChild=function(e){if(!e)throw"argument cannot be null";this.children.push(e)};var o=function(e,t,n){this.type=e;this.value=t;this.children=null;this.whitespace=!!n};o.prototype=a.prototype;var i=function(e){this._lexer=e};i.prototype.parse=function(){var e=new a("root");while(true){var t=this._acceptEnvironment();if(t===null)break;var n;if(t==="algorithm")n=this._parseAlgorithmInner();else if(t==="algorithmic")n=this._parseAlgorithmicInner();else throw new r("Unexpected environment "+t);this._closeEnvironment(t);e.addChild(n)}this._lexer.expect("EOF");return e};i.prototype._acceptEnvironment=function(){var e=this._lexer;if(!e.accept("func","begin"))return null;e.expect("open");var t=e.expect("ordinary");e.expect("close");return t};i.prototype._closeEnvironment=function(e){var t=this._lexer;t.expect("func","end");t.expect("open");t.expect("ordinary",e);t.expect("close")};i.prototype._parseAlgorithmInner=function(){var e=new a("algorithm");while(true){var t=this._acceptEnvironment();if(t!==null){if(t!=="algorithmic")throw new r("Unexpected environment "+t);var n=this._parseAlgorithmicInner();this._closeEnvironment();e.addChild(n);continue}var i=this._parseCaption();if(i){e.addChild(i);continue}break}return e};i.prototype._parseAlgorithmicInner=function(){var e=new a("algorithmic");var t;while(true){t=this._parseStatement(h);if(t){e.addChild(t);continue}t=this._parseBlock();if(t.children.length>0){e.addChild(t);continue}break}return e};i.prototype._parseCaption=function(){var e=this._lexer;if(!e.accept("func","caption"))return null;var t=new a("caption");e.expect("open");t.addChild(this._parseCloseText());e.expect("close");return t};i.prototype._parseBlock=function(){var e=new a("block");while(true){var t=this._parseControl();if(t){e.addChild(t);continue}var n=this._parseFunction();if(n){e.addChild(n);continue}var i=this._parseStatement(l);if(i){e.addChild(i);continue}var r=this._parseCommand(p);if(r){e.addChild(r);continue}var o=this._parseComment();if(o){e.addChild(o);continue}break}return e};i.prototype._parseControl=function(){var e;if(e=this._parseIf())return e;if(e=this._parseLoop())return e;if(e=this._parseRepeat())return e};i.prototype._parseFunction=function(){var e=this._lexer;if(!e.accept("func",["function","procedure"]))return null;var t=this._lexer.get().text;e.expect("open");var n=e.expect("ordinary");e.expect("close");e.expect("open");var i=this._parseCloseText();e.expect("close");var r=this._parseBlock();e.expect("func","end"+t);var o=new a("function",{type:t,name:n});o.addChild(i);o.addChild(r);return o};i.prototype._parseIf=function(){if(!this._lexer.accept("func","if"))return null;var e=new a("if");this._lexer.expect("open");e.addChild(this._parseCond());this._lexer.expect("close");e.addChild(this._parseBlock());var t=0;while(this._lexer.accept("func",["elif","elsif","elseif"])){this._lexer.expect("open");e.addChild(this._parseCond());this._lexer.expect("close");e.addChild(this._parseBlock());t++}var n=false;if(this._lexer.accept("func","else")){n=true;e.addChild(this._parseBlock())}this._lexer.expect("func","endif");e.value={numElif:t,hasElse:n};return e};i.prototype._parseLoop=function(){if(!this._lexer.accept("func",["FOR","FORALL","WHILE"]))return null;var e=this._lexer.get().text.toLowerCase();var t=new a("loop",e);this._lexer.expect("open");t.addChild(this._parseCond());this._lexer.expect("close");t.addChild(this._parseBlock());var n=e!=="forall"?"end"+e:"endfor";this._lexer.expect("func",n);return t};i.prototype._parseRepeat=function(){if(!this._lexer.accept("func",["REPEAT"]))return null;var e=this._lexer.get().text.toLowerCase();var t=new a("repeat",e);t.addChild(this._parseBlock());this._lexer.expect("func","until");this._lexer.expect("open");t.addChild(this._parseCond());this._lexer.expect("close");return t};var h=["ensure","require","input","output"];var l=["state","print","return"];i.prototype._parseStatement=function(e){if(!this._lexer.accept("func",e))return null;var t=this._lexer.get().text.toLowerCase();var n=new a("statement",t);n.addChild(this._parseOpenText());return n};var p=["break","continue"];i.prototype._parseCommand=function(e){if(!this._lexer.accept("func",e))return null;var t=this._lexer.get().text.toLowerCase();var n=new a("command",t);return n};i.prototype._parseComment=function(){if(!this._lexer.accept("func","comment"))return null;var e=new a("comment");this._lexer.expect("open");e.addChild(this._parseCloseText());this._lexer.expect("close");return e};i.prototype._parseCall=function(){var e=this._lexer;if(!e.accept("func","call"))return null;var t=e.get().whitespace;e.expect("open");var n=e.expect("ordinary");e.expect("close");var i=new a("call");i.whitespace=t;i.value=n;e.expect("open");var r=this._parseCloseText();i.addChild(r);e.expect("close");return i};i.prototype._parseCond=i.prototype._parseCloseText=function(){return this._parseText("close")};i.prototype._parseOpenText=function(){return this._parseText("open")};i.prototype._parseText=function(e){var t=new a(e+"-text");var n=false;var i;while(true){i=this._parseAtom()||this._parseCall();if(i){if(n)i.whitespace|=n;t.addChild(i);continue}if(this._lexer.accept("open")){i=this._parseCloseText();n=this._lexer.get().whitespace;i.whitespace=n;t.addChild(i);this._lexer.expect("close");n=this._lexer.get().whitespace;continue}break}return t};var u={ordinary:{tokenType:"ordinary"},math:{tokenType:"math"},special:{tokenType:"special"},"cond-symbol":{tokenType:"func",tokenValues:["and","or","not","true","false","to","downto"]},"quote-symbol":{tokenType:"quote"},"sizing-dclr":{tokenType:"func",tokenValues:["tiny","scriptsize","footnotesize","small","normalsize","large","Large","LARGE","huge","Huge"]},"font-dclr":{tokenType:"func",tokenValues:["normalfont","rmfamily","sffamily","ttfamily","upshape","itshape","slshape","scshape","bfseries","mdseries","lfseries"]},"font-cmd":{tokenType:"func",tokenValues:["textnormal","textrm","textsf","texttt","textup","textit","textsl","textsc","uppercase","lowercase","textbf","textmd","textlf"]},"text-symbol":{tokenType:"func",tokenValues:["textbackslash"]}};i.prototype._parseAtom=function(){for(var e in u){var t=u[e];var n=this._lexer.accept(t.tokenType,t.tokenValues);if(n===null)continue;var i=this._lexer.get().whitespace;if(e!=="ordinary"&&e!=="math")n=n.toLowerCase();return new o(e,n,i)}return null};t.exports=i},{"./ParseError":3,"./utils":6}],5:[function(n,e,t){var a=n("./utils");function z(e){this._css={};this._fontSize=this._outerFontSize=e!==undefined?e:1}z.prototype.outerFontSize=function(e){if(e!==undefined)this._outerFontSize=e;return this._outerFontSize};z.prototype.fontSize=function(){return this._fontSize};z.prototype._fontCommandTable={normalfont:{"font-family":"KaTeX_Main"},rmfamily:{"font-family":"KaTeX_Main"},sffamily:{"font-family":"KaTeX_SansSerif"},ttfamily:{"font-family":"KaTeX_Typewriter"},bfseries:{"font-weight":"bold"},mdseries:{"font-weight":"medium"},lfseries:{"font-weight":"lighter"},upshape:{"font-style":"normal","font-variant":"normal"},itshape:{"font-style":"italic","font-variant":"normal"},scshape:{"font-style":"normal","font-variant":"small-caps"},slshape:{"font-style":"oblique","font-variant":"normal"},textnormal:{"font-family":"KaTeX_Main"},textrm:{"font-family":"KaTeX_Main"},textsf:{"font-family":"KaTeX_SansSerif"},texttt:{"font-family":"KaTeX_Typewriter"},textbf:{"font-weight":"bold"},textmd:{"font-weight":"medium"},textlf:{"font-weight":"lighter"},textup:{"font-style":"normal","font-variant":"normal"},textit:{"font-style":"italic","font-variant":"normal"},textsc:{"font-style":"normal","font-variant":"small-caps"},textsl:{"font-style":"oblique","font-variant":"normal"},uppercase:{"text-transform":"uppercase"},lowercase:{"text-transform":"lowercase"}};z.prototype._sizingScalesTable={tiny:.68,scriptsize:.8,footnotesize:.85,small:.92,normalsize:1,large:1.17,Large:1.41,LARGE:1.58,huge:1.9,Huge:2.28};z.prototype.updateByCommand=function(e){var t=this._fontCommandTable[e];if(t!==undefined){for(var n in t)this._css[n]=t[n];return}var i=this._sizingScalesTable[e];if(i!==undefined){this._outerFontSize=this._fontSize;this._fontSize=i;return}throw new ParserError("unrecogniazed text-style command")};z.prototype.toCSS=function(){var e="";for(var t in this._css){var n=this._css[t];if(n===undefined)continue;e+=t+":"+n+";"}if(this._fontSize!==this._outerFontSize){e+="font-size:"+this._fontSize/this._outerFontSize+"em;"}return e};function A(e,t){this._nodes=e;this._textStyle=t}A.prototype._renderCloseText=function(e,t){var n=new z(this._textStyle.fontSize());var i=new A(e.children,n);if(e.whitespace)this._html.putText(" ");this._html.putHTML(i.renderToHTML(t))};A.prototype.renderToHTML=function(e){this._html=new _;var t;while((t=this._nodes.shift())!==undefined){var n=t.type;var i=t.value;if(t.whitespace)this._html.putText(" ");switch(n){case"ordinary":this._html.putText(i);break;case"math":if(typeof e==="undefined"){throw"No math backend found. Please setup KaTeX or MathJax."}else if(e.name==="katex"){this._html.putHTML(e.driver.renderToString(i))}else if(e.name==="mathjax"){this._html.putText("$"+i+"$")}else{throw"Unknown math backend "+e}break;case"cond-symbol":this._html.beginSpan("ps-keyword").putText(i.toLowerCase()).endSpan();break;case"special":if(i==="\\\\"){this._html.putHTML("<br/>");break}var r={"\\{":"{","\\}":"}","\\$":"$","\\&":"&","\\#":"#","\\%":"%","\\_":"_"};var o=r[i];this._html.putText(o);break;case"text-symbol":var s={textbackslash:"\\"};var a=s[i];this._html.putText(a);break;case"quote-symbol":var h={"`":"\u2018","``":"\u201c","'":"\u2019","''":"\u201d"};var l=h[i];this._html.putText(l);break;case"call":this._html.beginSpan("ps-funcname").putText(i).endSpan();this._html.write("(");var p=t.children[0];this._renderCloseText(p,e);this._html.write(")");break;case"close-text":this._renderCloseText(t,e);break;case"font-dclr":case"sizing-dclr":this._textStyle.updateByCommand(i);this._html.beginSpan(null,this._textStyle.toCSS());var u=new A(this._nodes,this._textStyle);this._html.putHTML(u.renderToHTML(e));this._html.endSpan();break;case"font-cmd":var c=this._nodes[0];if(c.type!=="close-text")continue;var f=new z(this._textStyle.fontSize());f.updateByCommand(i);this._html.beginSpan(null,f.toCSS());var d=new A(c.children,f);this._html.putHTML(d.renderToHTML(e));this._html.endSpan();break;default:throw new ParseError("Unexpected ParseNode of type "+t.type)}}return this._html.toMarkup()};function _(){this._body=[];this._textBuf=[]}_.prototype.beginDiv=function(e,t,n){this._beginTag("div",e,t,n);this._body.push("\n");return this};_.prototype.endDiv=function(){this._endTag("div");this._body.push("\n");return this};_.prototype.beginP=function(e,t,n){this._beginTag("p",e,t,n);this._body.push("\n");return this};_.prototype.endP=function(){this._flushText();this._endTag("p");this._body.push("\n");return this};_.prototype.beginSpan=function(e,t,n){this._flushText();return this._beginTag("span",e,t,n)};_.prototype.endSpan=function(){this._flushText();return this._endTag("span")};_.prototype.putHTML=function(e){this._flushText();this._body.push(e);return this};_.prototype.putText=function(e){this._textBuf.push(e);return this};_.prototype.write=function(e){this._body.push(e)};_.prototype.toMarkup=function(){this._flushText();var e=this._body.join("");return e.trim()};_.prototype.toDOM=function(){var e=this.toMarkup();var t=document.createElement("div");t.innerHTML=e;return t.firstChild};_.prototype._flushText=function(){if(this._textBuf.length===0)return;var e=this._textBuf.join("");this._body.push(this._escapeHtml(e));this._textBuf=[]};_.prototype._beginTag=function(e,t,n,i){var r="<"+e;if(t)r+=' class="'+t+'"';if(n){var o;if(a.isString(n))o=n;else{o="";for(var s in n){attrVal=n[s];o+=s+":"+attrVal+";"}}if(i)o+=i;r+=' style="'+o+'"'}r+=">";this._body.push(r);return this};_.prototype._endTag=function(e){this._body.push("</"+e+">");return this};var i={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/"};_.prototype._escapeHtml=function(e){return String(e).replace(/[&<>"'/]/g,function(e){return i[e]})};function r(e){e=e||{};this.indentSize=e.indentSize?this._parseEmVal(e.indentSize):1.2;this.commentDelimiter=e.commentDelimiter||" // ";this.lineNumberPunc=e.lineNumberPunc||":";this.lineNumber=e.lineNumber!==undefined?e.lineNumber:false;this.noEnd=e.noEnd!==undefined?e.noEnd:false;if(e.captionCount!==undefined)F.captionCount=e.captionCount;this.titlePrefix=e.titlePrefix||"Algorithm"}r.prototype._parseEmVal=function(e){e=e.trim();if(e.indexOf("em")!==e.length-2)throw"option unit error; no `em` found";return Number(e.substring(0,e.length-2))};function F(e,t){this._root=e.parse();this._options=new r(t);this._openLine=false;this._blockLevel=0;this._textLevel=-1;this._globalTextStyle=new z;this.backend=undefined;try{if(typeof katex==="undefined")katex=n("katex")}catch(e){}try{if(typeof MathJax==="undefined")MathJax=n("mathjax")}catch(e){}if(typeof katex!=="undefined"){this.backend={name:"katex",driver:katex}}else if(typeof MathJax!=="undefined"){this.backend={name:"mathjax",driver:MathJax}}}F.captionCount=0;F.prototype.toMarkup=function(){var e=this._html=new _;this._buildTree(this._root);delete this._html;return e.toMarkup()};F.prototype.toDOM=function(){var e=this.toMarkup();var t=document.createElement("div");t.innerHTML=e;return t.firstChild};F.prototype._beginGroup=function(e,t,n){this._closeLineIfAny();this._html.beginDiv("ps-"+e+(t?" "+t:""),n)};F.prototype._endGroup=function(e){this._closeLineIfAny();this._html.endDiv()};F.prototype._beginBlock=function(){var e=this._options.lineNumber&&this._blockLevel===0?.6:0;var t=this._options.indentSize+e;this._beginGroup("block",null,{"margin-left":t+"em"});this._blockLevel++};F.prototype._endBlock=function(){this._closeLineIfAny();this._endGroup();this._blockLevel--};F.prototype._newLine=function(){this._closeLineIfAny();this._openLine=true;this._globalTextStyle.outerFontSize(1);var e=this._options.indentSize;if(this._blockLevel>0){this._numLOC++;this._html.beginP("ps-line ps-code",this._globalTextStyle.toCSS());if(this._options.lineNumber){this._html.beginSpan("ps-linenum",{left:-((this._blockLevel-1)*(e*1.25))+"em"}).putText(this._numLOC+this._options.lineNumberPunc).endSpan()}}else{this._html.beginP("ps-line",{"text-indent":-e+"em","padding-left":e+"em"},this._globalTextStyle.toCSS())}};F.prototype._closeLineIfAny=function(){if(!this._openLine)return;this._html.endP();this._openLine=false};F.prototype._typeKeyword=function(e){this._html.beginSpan("ps-keyword").putText(e).endSpan()};F.prototype._typeFuncName=function(e){this._html.beginSpan("ps-funcname").putText(e).endSpan()};F.prototype._typeText=function(e){this._html.write(e)};F.prototype._buildTreeForAllChildren=function(e){var t=e.children;for(var n=0;n<t.length;n++)this._buildTree(t[n])};F.prototype._buildCommentsFromBlock=function(e){var t=e.children;while(t.length>0&&t[0].type==="comment"){var n=t.shift();this._buildTree(n)}};F.prototype._buildTree=function(e){var t;var n;var i;switch(e.type){case"root":this._beginGroup("root");this._buildTreeForAllChildren(e);this._endGroup();break;case"algorithm":var r;for(t=0;t<e.children.length;t++){n=e.children[t];if(n.type!=="caption")continue;r=n;F.captionCount++}if(r){this._beginGroup("algorithm","with-caption");this._buildTree(r)}else{this._beginGroup("algorithm")}for(t=0;t<e.children.length;t++){n=e.children[t];if(n.type==="caption")continue;this._buildTree(n)}this._endGroup();break;case"algorithmic":if(this._options.lineNumber){this._beginGroup("algorithmic","with-linenum");this._numLOC=0}else{this._beginGroup("algorithmic")}this._buildTreeForAllChildren(e);this._endGroup();break;case"block":this._beginBlock();this._buildTreeForAllChildren(e);this._endBlock();break;case"function":var o=e.value.type.toLowerCase();var s=e.value.name;i=e.children[0];var a=e.children[1];this._newLine();this._typeKeyword(o+" ");this._typeFuncName(s);this._typeText("(");this._buildTree(i);this._typeText(")");this._buildCommentsFromBlock(a);this._buildTree(a);if(!this._options.noEnd){this._newLine();this._typeKeyword("end "+o)}break;case"if":this._newLine();this._typeKeyword("if ");ifCond=e.children[0];this._buildTree(ifCond);this._typeKeyword(" then");var h=e.children[1];this._buildCommentsFromBlock(h);this._buildTree(h);var l=e.value.numElif;for(var p=0;p<l;p++){this._newLine();this._typeKeyword("else if ");var u=e.children[2+2*p];this._buildTree(u);this._typeKeyword(" then");var c=e.children[2+2*p+1];this._buildCommentsFromBlock(c);this._buildTree(c)}var f=e.value.hasElse;if(f){this._newLine();this._typeKeyword("else");var d=e.children[e.children.length-1];this._buildCommentsFromBlock(d);this._buildTree(d)}if(!this._options.noEnd){this._newLine();this._typeKeyword("end if")}break;case"loop":this._newLine();var _=e.value;var m={for:"for",forall:"for all",while:"while"};this._typeKeyword(m[_]+" ");var x=e.children[0];this._buildTree(x);this._typeKeyword(" do");var y=e.children[1];this._buildCommentsFromBlock(y);this._buildTree(y);if(!this._options.noEnd){this._newLine();var v=_==="while"?"end while":"end for";this._typeKeyword(v)}break;case"repeat":this._newLine();this._typeKeyword("repeat");var b=e.children[0];this._buildCommentsFromBlock(b);this._buildTree(b);if(!this._options.noEnd){this._newLine();this._typeKeyword("until ");var w=e.children[1];this._buildTree(w)}break;case"command":var g=e.value;var T={break:"break",continue:"continue"}[g];this._newLine();if(T)this._typeKeyword(T);break;case"caption":this._newLine();this._typeKeyword(this._options.titlePrefix+" "+F.captionCount+" ");i=e.children[0];this._buildTree(i);break;case"comment":i=e.children[0];this._html.beginSpan("ps-comment");this._html.putText(this._options.commentDelimiter);this._buildTree(i);this._html.endSpan();break;case"statement":var k=e.value;var C={state:"",ensure:"Ensure: ",require:"Require: ",input:"Input: ",output:"Output: ",print:"print ",return:"return "}[k];this._newLine();if(C)this._typeKeyword(C);i=e.children[0];this._buildTree(i);break;case"open-text":var S=new A(e.children,this._globalTextStyle);this._html.putHTML(S.renderToHTML(this.backend));break;case"close-text":var L=this._globalTextStyle.fontSize();var E=new z(L);var M=new A(e.children,E);this._html.putHTML(M.renderToHTML(this.backend));break;default:throw new ParseError("Unexpected ParseNode of type "+e.type)}};e.exports=F},{"./utils":6,katex:undefined,mathjax:undefined}],6:[function(e,t,n){function i(e){return typeof e==="string"||e instanceof String}function r(e){return typeof e==="object"&&e instanceof Object}function o(e){if(!r(e))return e+"";var t=[];for(var n in e)t.push(n+": "+o(e[n]));return t.join(", ")}t.exports={isString:i,isObject:r,toString:o}},{}]},{},[1])(1)});
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pseudocode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Florian Franzen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: execjs
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: katex
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.8'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.8'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- Florian.Franzen@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/pseudocode.rb
|
49
|
+
- lib/pseudocode/version.rb
|
50
|
+
- vendor/pseudocode/pseudocode.min.css
|
51
|
+
- vendor/pseudocode/pseudocode.min.js
|
52
|
+
homepage: https://github.com/FlorianFranzen/pseudocode-ruby
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata:
|
56
|
+
homepage_uri: https://github.com/FlorianFranzen/pseudocode-ruby
|
57
|
+
source_code_uri: https://github.com/FlorianFranzen/pseudocode-ruby
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 2.6.0
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.2.26
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Render Pseudocode.js from Ruby
|
77
|
+
test_files: []
|