overpass-doc 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +1 -0
- data/Rakefile +27 -0
- data/bin/overpass-doc +12 -0
- data/lib/overpass-doc.rb +9 -0
- data/lib/overpass-doc/assets/bootstrap.min.css +7 -0
- data/lib/overpass-doc/assets/bootstrap.min.js +7 -0
- data/lib/overpass-doc/assets/codemirror.css +176 -0
- data/lib/overpass-doc/assets/codemirror.js +3190 -0
- data/lib/overpass-doc/assets/jquery.js +2 -0
- data/lib/overpass-doc/assets/mode/clike/clike.js +303 -0
- data/lib/overpass-doc/assets/mode/clike/index.html +102 -0
- data/lib/overpass-doc/assets/mode/clike/scala.html +766 -0
- data/lib/overpass-doc/assets/mode/clike/test.js +165 -0
- data/lib/overpass-doc/assets/util/closetag.js +165 -0
- data/lib/overpass-doc/assets/util/continuecomment.js +36 -0
- data/lib/overpass-doc/assets/util/continuelist.js +29 -0
- data/lib/overpass-doc/assets/util/dialog.css +32 -0
- data/lib/overpass-doc/assets/util/dialog.js +76 -0
- data/lib/overpass-doc/assets/util/foldcode.js +196 -0
- data/lib/overpass-doc/assets/util/formatting.js +108 -0
- data/lib/overpass-doc/assets/util/javascript-hint.js +138 -0
- data/lib/overpass-doc/assets/util/loadmode.js +51 -0
- data/lib/overpass-doc/assets/util/match-highlighter.js +44 -0
- data/lib/overpass-doc/assets/util/multiplex.js +95 -0
- data/lib/overpass-doc/assets/util/overlay.js +59 -0
- data/lib/overpass-doc/assets/util/pig-hint.js +123 -0
- data/lib/overpass-doc/assets/util/runmode-standalone.js +90 -0
- data/lib/overpass-doc/assets/util/runmode.js +53 -0
- data/lib/overpass-doc/assets/util/search.js +118 -0
- data/lib/overpass-doc/assets/util/searchcursor.js +119 -0
- data/lib/overpass-doc/assets/util/simple-hint.css +16 -0
- data/lib/overpass-doc/assets/util/simple-hint.js +102 -0
- data/lib/overpass-doc/assets/util/xml-hint.js +131 -0
- data/lib/overpass-doc/generator.rb +122 -0
- data/lib/overpass-doc/query.rb +119 -0
- data/lib/overpass-doc/views/extra.erb +4 -0
- data/lib/overpass-doc/views/index.erb +37 -0
- data/lib/overpass-doc/views/layout.erb +80 -0
- data/lib/overpass-doc/views/query.erb +115 -0
- metadata +144 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
// Define match-highlighter commands. Depends on searchcursor.js
|
2
|
+
// Use by attaching the following function call to the onCursorActivity event:
|
3
|
+
//myCodeMirror.matchHighlight(minChars);
|
4
|
+
// And including a special span.CodeMirror-matchhighlight css class (also optionally a separate one for .CodeMirror-focused -- see demo matchhighlighter.html)
|
5
|
+
|
6
|
+
(function() {
|
7
|
+
var DEFAULT_MIN_CHARS = 2;
|
8
|
+
|
9
|
+
function MatchHighlightState() {
|
10
|
+
this.marked = [];
|
11
|
+
}
|
12
|
+
function getMatchHighlightState(cm) {
|
13
|
+
return cm._matchHighlightState || (cm._matchHighlightState = new MatchHighlightState());
|
14
|
+
}
|
15
|
+
|
16
|
+
function clearMarks(cm) {
|
17
|
+
var state = getMatchHighlightState(cm);
|
18
|
+
for (var i = 0; i < state.marked.length; ++i)
|
19
|
+
state.marked[i].clear();
|
20
|
+
state.marked = [];
|
21
|
+
}
|
22
|
+
|
23
|
+
function markDocument(cm, className, minChars) {
|
24
|
+
clearMarks(cm);
|
25
|
+
minChars = (typeof minChars !== 'undefined' ? minChars : DEFAULT_MIN_CHARS);
|
26
|
+
if (cm.somethingSelected() && cm.getSelection().replace(/^\s+|\s+$/g, "").length >= minChars) {
|
27
|
+
var state = getMatchHighlightState(cm);
|
28
|
+
var query = cm.getSelection();
|
29
|
+
cm.operation(function() {
|
30
|
+
if (cm.lineCount() < 2000) { // This is too expensive on big documents.
|
31
|
+
for (var cursor = cm.getSearchCursor(query); cursor.findNext();) {
|
32
|
+
//Only apply matchhighlight to the matches other than the one actually selected
|
33
|
+
if (!(cursor.from().line === cm.getCursor(true).line && cursor.from().ch === cm.getCursor(true).ch))
|
34
|
+
state.marked.push(cm.markText(cursor.from(), cursor.to(), className));
|
35
|
+
}
|
36
|
+
}
|
37
|
+
});
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
CodeMirror.defineExtension("matchHighlight", function(className, minChars) {
|
42
|
+
markDocument(this, className, minChars);
|
43
|
+
});
|
44
|
+
})();
|
@@ -0,0 +1,95 @@
|
|
1
|
+
CodeMirror.multiplexingMode = function(outer /*, others */) {
|
2
|
+
// Others should be {open, close, mode [, delimStyle]} objects
|
3
|
+
var others = Array.prototype.slice.call(arguments, 1);
|
4
|
+
var n_others = others.length;
|
5
|
+
|
6
|
+
function indexOf(string, pattern, from) {
|
7
|
+
if (typeof pattern == "string") return string.indexOf(pattern, from);
|
8
|
+
var m = pattern.exec(from ? string.slice(from) : string);
|
9
|
+
return m ? m.index + from : -1;
|
10
|
+
}
|
11
|
+
|
12
|
+
return {
|
13
|
+
startState: function() {
|
14
|
+
return {
|
15
|
+
outer: CodeMirror.startState(outer),
|
16
|
+
innerActive: null,
|
17
|
+
inner: null
|
18
|
+
};
|
19
|
+
},
|
20
|
+
|
21
|
+
copyState: function(state) {
|
22
|
+
return {
|
23
|
+
outer: CodeMirror.copyState(outer, state.outer),
|
24
|
+
innerActive: state.innerActive,
|
25
|
+
inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner)
|
26
|
+
};
|
27
|
+
},
|
28
|
+
|
29
|
+
token: function(stream, state) {
|
30
|
+
if (!state.innerActive) {
|
31
|
+
var cutOff = Infinity, oldContent = stream.string;
|
32
|
+
for (var i = 0; i < n_others; ++i) {
|
33
|
+
var other = others[i];
|
34
|
+
var found = indexOf(oldContent, other.open, stream.pos);
|
35
|
+
if (found == stream.pos) {
|
36
|
+
stream.match(other.open);
|
37
|
+
state.innerActive = other;
|
38
|
+
state.inner = CodeMirror.startState(other.mode, outer.indent ? outer.indent(state.outer, "") : 0);
|
39
|
+
return other.delimStyle;
|
40
|
+
} else if (found != -1 && found < cutOff) {
|
41
|
+
cutOff = found;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff);
|
45
|
+
var outerToken = outer.token(stream, state.outer);
|
46
|
+
if (cutOff != Infinity) stream.string = oldContent;
|
47
|
+
return outerToken;
|
48
|
+
} else {
|
49
|
+
var curInner = state.innerActive, oldContent = stream.string;
|
50
|
+
var found = indexOf(oldContent, curInner.close, stream.pos);
|
51
|
+
if (found == stream.pos) {
|
52
|
+
stream.match(curInner.close);
|
53
|
+
state.innerActive = state.inner = null;
|
54
|
+
return curInner.delimStyle;
|
55
|
+
}
|
56
|
+
if (found > -1) stream.string = oldContent.slice(0, found);
|
57
|
+
var innerToken = curInner.mode.token(stream, state.inner);
|
58
|
+
if (found > -1) stream.string = oldContent;
|
59
|
+
var cur = stream.current(), found = cur.indexOf(curInner.close);
|
60
|
+
if (found > -1) stream.backUp(cur.length - found);
|
61
|
+
return innerToken;
|
62
|
+
}
|
63
|
+
},
|
64
|
+
|
65
|
+
indent: function(state, textAfter) {
|
66
|
+
var mode = state.innerActive ? state.innerActive.mode : outer;
|
67
|
+
if (!mode.indent) return CodeMirror.Pass;
|
68
|
+
return mode.indent(state.innerActive ? state.inner : state.outer, textAfter);
|
69
|
+
},
|
70
|
+
|
71
|
+
blankLine: function(state) {
|
72
|
+
var mode = state.innerActive ? state.innerActive.mode : outer;
|
73
|
+
if (mode.blankLine) {
|
74
|
+
mode.blankLine(state.innerActive ? state.inner : state.outer);
|
75
|
+
}
|
76
|
+
if (!state.innerActive) {
|
77
|
+
for (var i = 0; i < n_others; ++i) {
|
78
|
+
var other = others[i];
|
79
|
+
if (other.open === "\n") {
|
80
|
+
state.innerActive = other;
|
81
|
+
state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "") : 0);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
} else if (mode.close === "\n") {
|
85
|
+
state.innerActive = state.inner = null;
|
86
|
+
}
|
87
|
+
},
|
88
|
+
|
89
|
+
electricChars: outer.electricChars,
|
90
|
+
|
91
|
+
innerMode: function(state) {
|
92
|
+
return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer};
|
93
|
+
}
|
94
|
+
};
|
95
|
+
};
|
@@ -0,0 +1,59 @@
|
|
1
|
+
// Utility function that allows modes to be combined. The mode given
|
2
|
+
// as the base argument takes care of most of the normal mode
|
3
|
+
// functionality, but a second (typically simple) mode is used, which
|
4
|
+
// can override the style of text. Both modes get to parse all of the
|
5
|
+
// text, but when both assign a non-null style to a piece of code, the
|
6
|
+
// overlay wins, unless the combine argument was true, in which case
|
7
|
+
// the styles are combined.
|
8
|
+
|
9
|
+
// overlayParser is the old, deprecated name
|
10
|
+
CodeMirror.overlayMode = CodeMirror.overlayParser = function(base, overlay, combine) {
|
11
|
+
return {
|
12
|
+
startState: function() {
|
13
|
+
return {
|
14
|
+
base: CodeMirror.startState(base),
|
15
|
+
overlay: CodeMirror.startState(overlay),
|
16
|
+
basePos: 0, baseCur: null,
|
17
|
+
overlayPos: 0, overlayCur: null
|
18
|
+
};
|
19
|
+
},
|
20
|
+
copyState: function(state) {
|
21
|
+
return {
|
22
|
+
base: CodeMirror.copyState(base, state.base),
|
23
|
+
overlay: CodeMirror.copyState(overlay, state.overlay),
|
24
|
+
basePos: state.basePos, baseCur: null,
|
25
|
+
overlayPos: state.overlayPos, overlayCur: null
|
26
|
+
};
|
27
|
+
},
|
28
|
+
|
29
|
+
token: function(stream, state) {
|
30
|
+
if (stream.start == state.basePos) {
|
31
|
+
state.baseCur = base.token(stream, state.base);
|
32
|
+
state.basePos = stream.pos;
|
33
|
+
}
|
34
|
+
if (stream.start == state.overlayPos) {
|
35
|
+
stream.pos = stream.start;
|
36
|
+
state.overlayCur = overlay.token(stream, state.overlay);
|
37
|
+
state.overlayPos = stream.pos;
|
38
|
+
}
|
39
|
+
stream.pos = Math.min(state.basePos, state.overlayPos);
|
40
|
+
if (stream.eol()) state.basePos = state.overlayPos = 0;
|
41
|
+
|
42
|
+
if (state.overlayCur == null) return state.baseCur;
|
43
|
+
if (state.baseCur != null && combine) return state.baseCur + " " + state.overlayCur;
|
44
|
+
else return state.overlayCur;
|
45
|
+
},
|
46
|
+
|
47
|
+
indent: base.indent && function(state, textAfter) {
|
48
|
+
return base.indent(state.base, textAfter);
|
49
|
+
},
|
50
|
+
electricChars: base.electricChars,
|
51
|
+
|
52
|
+
innerMode: function(state) { return {state: state.base, mode: base}; },
|
53
|
+
|
54
|
+
blankLine: function(state) {
|
55
|
+
if (base.blankLine) base.blankLine(state.base);
|
56
|
+
if (overlay.blankLine) overlay.blankLine(state.overlay);
|
57
|
+
}
|
58
|
+
};
|
59
|
+
};
|
@@ -0,0 +1,123 @@
|
|
1
|
+
(function () {
|
2
|
+
function forEach(arr, f) {
|
3
|
+
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
|
4
|
+
}
|
5
|
+
|
6
|
+
function arrayContains(arr, item) {
|
7
|
+
if (!Array.prototype.indexOf) {
|
8
|
+
var i = arr.length;
|
9
|
+
while (i--) {
|
10
|
+
if (arr[i] === item) {
|
11
|
+
return true;
|
12
|
+
}
|
13
|
+
}
|
14
|
+
return false;
|
15
|
+
}
|
16
|
+
return arr.indexOf(item) != -1;
|
17
|
+
}
|
18
|
+
|
19
|
+
function scriptHint(editor, keywords, getToken) {
|
20
|
+
// Find the token at the cursor
|
21
|
+
var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token;
|
22
|
+
// If it's not a 'word-style' token, ignore the token.
|
23
|
+
|
24
|
+
if (!/^[\w$_]*$/.test(token.string)) {
|
25
|
+
token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
|
26
|
+
className: token.string == ":" ? "pig-type" : null};
|
27
|
+
}
|
28
|
+
|
29
|
+
if (!context) var context = [];
|
30
|
+
context.push(tprop);
|
31
|
+
|
32
|
+
var completionList = getCompletions(token, context);
|
33
|
+
completionList = completionList.sort();
|
34
|
+
//prevent autocomplete for last word, instead show dropdown with one word
|
35
|
+
if(completionList.length == 1) {
|
36
|
+
completionList.push(" ");
|
37
|
+
}
|
38
|
+
|
39
|
+
return {list: completionList,
|
40
|
+
from: {line: cur.line, ch: token.start},
|
41
|
+
to: {line: cur.line, ch: token.end}};
|
42
|
+
}
|
43
|
+
|
44
|
+
CodeMirror.pigHint = function(editor) {
|
45
|
+
return scriptHint(editor, pigKeywordsU, function (e, cur) {return e.getTokenAt(cur);});
|
46
|
+
};
|
47
|
+
|
48
|
+
function toTitleCase(str) {
|
49
|
+
return str.replace(/(?:^|\s)\w/g, function(match) {
|
50
|
+
return match.toUpperCase();
|
51
|
+
});
|
52
|
+
}
|
53
|
+
|
54
|
+
var pigKeywords = "VOID IMPORT RETURNS DEFINE LOAD FILTER FOREACH ORDER CUBE DISTINCT COGROUP "
|
55
|
+
+ "JOIN CROSS UNION SPLIT INTO IF OTHERWISE ALL AS BY USING INNER OUTER ONSCHEMA PARALLEL "
|
56
|
+
+ "PARTITION GROUP AND OR NOT GENERATE FLATTEN ASC DESC IS STREAM THROUGH STORE MAPREDUCE "
|
57
|
+
+ "SHIP CACHE INPUT OUTPUT STDERROR STDIN STDOUT LIMIT SAMPLE LEFT RIGHT FULL EQ GT LT GTE LTE "
|
58
|
+
+ "NEQ MATCHES TRUE FALSE";
|
59
|
+
var pigKeywordsU = pigKeywords.split(" ");
|
60
|
+
var pigKeywordsL = pigKeywords.toLowerCase().split(" ");
|
61
|
+
|
62
|
+
var pigTypes = "BOOLEAN INT LONG FLOAT DOUBLE CHARARRAY BYTEARRAY BAG TUPLE MAP";
|
63
|
+
var pigTypesU = pigTypes.split(" ");
|
64
|
+
var pigTypesL = pigTypes.toLowerCase().split(" ");
|
65
|
+
|
66
|
+
var pigBuiltins = "ABS ACOS ARITY ASIN ATAN AVG BAGSIZE BINSTORAGE BLOOM BUILDBLOOM CBRT CEIL "
|
67
|
+
+ "CONCAT COR COS COSH COUNT COUNT_STAR COV CONSTANTSIZE CUBEDIMENSIONS DIFF DISTINCT DOUBLEABS "
|
68
|
+
+ "DOUBLEAVG DOUBLEBASE DOUBLEMAX DOUBLEMIN DOUBLEROUND DOUBLESUM EXP FLOOR FLOATABS FLOATAVG "
|
69
|
+
+ "FLOATMAX FLOATMIN FLOATROUND FLOATSUM GENERICINVOKER INDEXOF INTABS INTAVG INTMAX INTMIN "
|
70
|
+
+ "INTSUM INVOKEFORDOUBLE INVOKEFORFLOAT INVOKEFORINT INVOKEFORLONG INVOKEFORSTRING INVOKER "
|
71
|
+
+ "ISEMPTY JSONLOADER JSONMETADATA JSONSTORAGE LAST_INDEX_OF LCFIRST LOG LOG10 LOWER LONGABS "
|
72
|
+
+ "LONGAVG LONGMAX LONGMIN LONGSUM MAX MIN MAPSIZE MONITOREDUDF NONDETERMINISTIC OUTPUTSCHEMA "
|
73
|
+
+ "PIGSTORAGE PIGSTREAMING RANDOM REGEX_EXTRACT REGEX_EXTRACT_ALL REPLACE ROUND SIN SINH SIZE "
|
74
|
+
+ "SQRT STRSPLIT SUBSTRING SUM STRINGCONCAT STRINGMAX STRINGMIN STRINGSIZE TAN TANH TOBAG "
|
75
|
+
+ "TOKENIZE TOMAP TOP TOTUPLE TRIM TEXTLOADER TUPLESIZE UCFIRST UPPER UTF8STORAGECONVERTER";
|
76
|
+
var pigBuiltinsU = pigBuiltins.split(" ").join("() ").split(" ");
|
77
|
+
var pigBuiltinsL = pigBuiltins.toLowerCase().split(" ").join("() ").split(" ");
|
78
|
+
var pigBuiltinsC = ("BagSize BinStorage Bloom BuildBloom ConstantSize CubeDimensions DoubleAbs "
|
79
|
+
+ "DoubleAvg DoubleBase DoubleMax DoubleMin DoubleRound DoubleSum FloatAbs FloatAvg FloatMax "
|
80
|
+
+ "FloatMin FloatRound FloatSum GenericInvoker IntAbs IntAvg IntMax IntMin IntSum "
|
81
|
+
+ "InvokeForDouble InvokeForFloat InvokeForInt InvokeForLong InvokeForString Invoker "
|
82
|
+
+ "IsEmpty JsonLoader JsonMetadata JsonStorage LongAbs LongAvg LongMax LongMin LongSum MapSize "
|
83
|
+
+ "MonitoredUDF Nondeterministic OutputSchema PigStorage PigStreaming StringConcat StringMax "
|
84
|
+
+ "StringMin StringSize TextLoader TupleSize Utf8StorageConverter").split(" ").join("() ").split(" ");
|
85
|
+
|
86
|
+
function getCompletions(token, context) {
|
87
|
+
var found = [], start = token.string;
|
88
|
+
function maybeAdd(str) {
|
89
|
+
if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str);
|
90
|
+
}
|
91
|
+
|
92
|
+
function gatherCompletions(obj) {
|
93
|
+
if(obj == ":") {
|
94
|
+
forEach(pigTypesL, maybeAdd);
|
95
|
+
}
|
96
|
+
else {
|
97
|
+
forEach(pigBuiltinsU, maybeAdd);
|
98
|
+
forEach(pigBuiltinsL, maybeAdd);
|
99
|
+
forEach(pigBuiltinsC, maybeAdd);
|
100
|
+
forEach(pigTypesU, maybeAdd);
|
101
|
+
forEach(pigTypesL, maybeAdd);
|
102
|
+
forEach(pigKeywordsU, maybeAdd);
|
103
|
+
forEach(pigKeywordsL, maybeAdd);
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
if (context) {
|
108
|
+
// If this is a property, see if it belongs to some object we can
|
109
|
+
// find in the current environment.
|
110
|
+
var obj = context.pop(), base;
|
111
|
+
|
112
|
+
if (obj.className == "pig-word")
|
113
|
+
base = obj.string;
|
114
|
+
else if(obj.className == "pig-type")
|
115
|
+
base = ":" + obj.string;
|
116
|
+
|
117
|
+
while (base != null && context.length)
|
118
|
+
base = base[context.pop().string];
|
119
|
+
if (base != null) gatherCompletions(base);
|
120
|
+
}
|
121
|
+
return found;
|
122
|
+
}
|
123
|
+
})();
|
@@ -0,0 +1,90 @@
|
|
1
|
+
/* Just enough of CodeMirror to run runMode under node.js */
|
2
|
+
|
3
|
+
function splitLines(string){ return string.split(/\r?\n|\r/); };
|
4
|
+
|
5
|
+
function StringStream(string) {
|
6
|
+
this.pos = this.start = 0;
|
7
|
+
this.string = string;
|
8
|
+
}
|
9
|
+
StringStream.prototype = {
|
10
|
+
eol: function() {return this.pos >= this.string.length;},
|
11
|
+
sol: function() {return this.pos == 0;},
|
12
|
+
peek: function() {return this.string.charAt(this.pos) || null;},
|
13
|
+
next: function() {
|
14
|
+
if (this.pos < this.string.length)
|
15
|
+
return this.string.charAt(this.pos++);
|
16
|
+
},
|
17
|
+
eat: function(match) {
|
18
|
+
var ch = this.string.charAt(this.pos);
|
19
|
+
if (typeof match == "string") var ok = ch == match;
|
20
|
+
else var ok = ch && (match.test ? match.test(ch) : match(ch));
|
21
|
+
if (ok) {++this.pos; return ch;}
|
22
|
+
},
|
23
|
+
eatWhile: function(match) {
|
24
|
+
var start = this.pos;
|
25
|
+
while (this.eat(match)){}
|
26
|
+
return this.pos > start;
|
27
|
+
},
|
28
|
+
eatSpace: function() {
|
29
|
+
var start = this.pos;
|
30
|
+
while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
|
31
|
+
return this.pos > start;
|
32
|
+
},
|
33
|
+
skipToEnd: function() {this.pos = this.string.length;},
|
34
|
+
skipTo: function(ch) {
|
35
|
+
var found = this.string.indexOf(ch, this.pos);
|
36
|
+
if (found > -1) {this.pos = found; return true;}
|
37
|
+
},
|
38
|
+
backUp: function(n) {this.pos -= n;},
|
39
|
+
column: function() {return this.start;},
|
40
|
+
indentation: function() {return 0;},
|
41
|
+
match: function(pattern, consume, caseInsensitive) {
|
42
|
+
if (typeof pattern == "string") {
|
43
|
+
function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
|
44
|
+
if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
|
45
|
+
if (consume !== false) this.pos += pattern.length;
|
46
|
+
return true;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
else {
|
50
|
+
var match = this.string.slice(this.pos).match(pattern);
|
51
|
+
if (match && consume !== false) this.pos += match[0].length;
|
52
|
+
return match;
|
53
|
+
}
|
54
|
+
},
|
55
|
+
current: function(){return this.string.slice(this.start, this.pos);}
|
56
|
+
};
|
57
|
+
exports.StringStream = StringStream;
|
58
|
+
|
59
|
+
exports.startState = function(mode, a1, a2) {
|
60
|
+
return mode.startState ? mode.startState(a1, a2) : true;
|
61
|
+
};
|
62
|
+
|
63
|
+
var modes = exports.modes = {}, mimeModes = exports.mimeModes = {};
|
64
|
+
exports.defineMode = function(name, mode) { modes[name] = mode; };
|
65
|
+
exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };
|
66
|
+
exports.getMode = function(options, spec) {
|
67
|
+
if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
|
68
|
+
spec = mimeModes[spec];
|
69
|
+
if (typeof spec == "string")
|
70
|
+
var mname = spec, config = {};
|
71
|
+
else if (spec != null)
|
72
|
+
var mname = spec.name, config = spec;
|
73
|
+
var mfactory = modes[mname];
|
74
|
+
if (!mfactory) throw new Error("Unknown mode: " + spec);
|
75
|
+
return mfactory(options, config || {});
|
76
|
+
};
|
77
|
+
|
78
|
+
exports.runMode = function(string, modespec, callback) {
|
79
|
+
var mode = exports.getMode({indentUnit: 2}, modespec);
|
80
|
+
var lines = splitLines(string), state = exports.startState(mode);
|
81
|
+
for (var i = 0, e = lines.length; i < e; ++i) {
|
82
|
+
if (i) callback("\n");
|
83
|
+
var stream = new exports.StringStream(lines[i]);
|
84
|
+
while (!stream.eol()) {
|
85
|
+
var style = mode.token(stream, state);
|
86
|
+
callback(stream.current(), style, i, stream.start);
|
87
|
+
stream.start = stream.pos;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
};
|
@@ -0,0 +1,53 @@
|
|
1
|
+
CodeMirror.runMode = function(string, modespec, callback, options) {
|
2
|
+
function esc(str) {
|
3
|
+
return str.replace(/[<&]/g, function(ch) { return ch == "<" ? "<" : "&"; });
|
4
|
+
}
|
5
|
+
|
6
|
+
var mode = CodeMirror.getMode(CodeMirror.defaults, modespec);
|
7
|
+
var isNode = callback.nodeType == 1;
|
8
|
+
var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize;
|
9
|
+
if (isNode) {
|
10
|
+
var node = callback, accum = [], col = 0;
|
11
|
+
callback = function(text, style) {
|
12
|
+
if (text == "\n") {
|
13
|
+
accum.push("<br>");
|
14
|
+
col = 0;
|
15
|
+
return;
|
16
|
+
}
|
17
|
+
var escaped = "";
|
18
|
+
// HTML-escape and replace tabs
|
19
|
+
for (var pos = 0;;) {
|
20
|
+
var idx = text.indexOf("\t", pos);
|
21
|
+
if (idx == -1) {
|
22
|
+
escaped += esc(text.slice(pos));
|
23
|
+
col += text.length - pos;
|
24
|
+
break;
|
25
|
+
} else {
|
26
|
+
col += idx - pos;
|
27
|
+
escaped += esc(text.slice(pos, idx));
|
28
|
+
var size = tabSize - col % tabSize;
|
29
|
+
col += size;
|
30
|
+
for (var i = 0; i < size; ++i) escaped += " ";
|
31
|
+
pos = idx + 1;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
if (style)
|
36
|
+
accum.push("<span class=\"cm-" + esc(style) + "\">" + escaped + "</span>");
|
37
|
+
else
|
38
|
+
accum.push(escaped);
|
39
|
+
};
|
40
|
+
}
|
41
|
+
var lines = CodeMirror.splitLines(string), state = CodeMirror.startState(mode);
|
42
|
+
for (var i = 0, e = lines.length; i < e; ++i) {
|
43
|
+
if (i) callback("\n");
|
44
|
+
var stream = new CodeMirror.StringStream(lines[i]);
|
45
|
+
while (!stream.eol()) {
|
46
|
+
var style = mode.token(stream, state);
|
47
|
+
callback(stream.current(), style, i, stream.start);
|
48
|
+
stream.start = stream.pos;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
if (isNode)
|
52
|
+
node.innerHTML = accum.join("");
|
53
|
+
};
|