Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
fc4d4c82
Commit
fc4d4c82
authored
May 14, 2012
by
Prem Sichanugrist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix section convention and code highlight
parent
5eac6ffa
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
377 additions
and
32 deletions
+377
-32
static/js/CodeMirror/python.js
+341
-0
templates/jstextline.html
+10
-10
templates/textbox.html
+19
-15
templates/textinput.html
+7
-7
No files found.
static/js/CodeMirror/python.js
0 → 100644
View file @
fc4d4c82
CodeMirror
.
defineMode
(
"python"
,
function
(
conf
,
parserConf
)
{
var
ERRORCLASS
=
'error'
;
function
wordRegexp
(
words
)
{
return
new
RegExp
(
"^(("
+
words
.
join
(
")|("
)
+
"))
\\
b"
);
}
var
singleOperators
=
new
RegExp
(
"^[
\\
+
\\
-
\\
*/%&|
\\
^~<>!]"
);
var
singleDelimiters
=
new
RegExp
(
'^[
\\
(
\\
)
\\
[
\\
]
\\
{
\\
}@,:`=;
\\
.]'
);
var
doubleOperators
=
new
RegExp
(
"^((==)|(!=)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(
\\
*
\\
*))"
);
var
doubleDelimiters
=
new
RegExp
(
"^((
\\
+=)|(
\\
-=)|(
\\
*=)|(%=)|(/=)|(&=)|(
\\
|=)|(
\\
^=))"
);
var
tripleDelimiters
=
new
RegExp
(
"^((//=)|(>>=)|(<<=)|(
\\
*
\\
*=))"
);
var
identifiers
=
new
RegExp
(
"^[_A-Za-z][_A-Za-z0-9]*"
);
var
wordOperators
=
wordRegexp
([
'and'
,
'or'
,
'not'
,
'is'
,
'in'
]);
var
commonkeywords
=
[
'as'
,
'assert'
,
'break'
,
'class'
,
'continue'
,
'def'
,
'del'
,
'elif'
,
'else'
,
'except'
,
'finally'
,
'for'
,
'from'
,
'global'
,
'if'
,
'import'
,
'lambda'
,
'pass'
,
'raise'
,
'return'
,
'try'
,
'while'
,
'with'
,
'yield'
];
var
commonBuiltins
=
[
'abs'
,
'all'
,
'any'
,
'bin'
,
'bool'
,
'bytearray'
,
'callable'
,
'chr'
,
'classmethod'
,
'compile'
,
'complex'
,
'delattr'
,
'dict'
,
'dir'
,
'divmod'
,
'enumerate'
,
'eval'
,
'filter'
,
'float'
,
'format'
,
'frozenset'
,
'getattr'
,
'globals'
,
'hasattr'
,
'hash'
,
'help'
,
'hex'
,
'id'
,
'input'
,
'int'
,
'isinstance'
,
'issubclass'
,
'iter'
,
'len'
,
'list'
,
'locals'
,
'map'
,
'max'
,
'memoryview'
,
'min'
,
'next'
,
'object'
,
'oct'
,
'open'
,
'ord'
,
'pow'
,
'property'
,
'range'
,
'repr'
,
'reversed'
,
'round'
,
'set'
,
'setattr'
,
'slice'
,
'sorted'
,
'staticmethod'
,
'str'
,
'sum'
,
'super'
,
'tuple'
,
'type'
,
'vars'
,
'zip'
,
'__import__'
,
'NotImplemented'
,
'Ellipsis'
,
'__debug__'
];
var
py2
=
{
'builtins'
:
[
'apply'
,
'basestring'
,
'buffer'
,
'cmp'
,
'coerce'
,
'execfile'
,
'file'
,
'intern'
,
'long'
,
'raw_input'
,
'reduce'
,
'reload'
,
'unichr'
,
'unicode'
,
'xrange'
,
'False'
,
'True'
,
'None'
],
'keywords'
:
[
'exec'
,
'print'
]};
var
py3
=
{
'builtins'
:
[
'ascii'
,
'bytes'
,
'exec'
,
'print'
],
'keywords'
:
[
'nonlocal'
,
'False'
,
'True'
,
'None'
]};
if
(
!!
parserConf
.
version
&&
parseInt
(
parserConf
.
version
,
10
)
===
3
)
{
commonkeywords
=
commonkeywords
.
concat
(
py3
.
keywords
);
commonBuiltins
=
commonBuiltins
.
concat
(
py3
.
builtins
);
var
stringPrefixes
=
new
RegExp
(
"^(([rb]|(br))?('{3}|
\"
{3}|['
\"
]))"
,
"i"
);
}
else
{
commonkeywords
=
commonkeywords
.
concat
(
py2
.
keywords
);
commonBuiltins
=
commonBuiltins
.
concat
(
py2
.
builtins
);
var
stringPrefixes
=
new
RegExp
(
"^(([rub]|(ur)|(br))?('{3}|
\"
{3}|['
\"
]))"
,
"i"
);
}
var
keywords
=
wordRegexp
(
commonkeywords
);
var
builtins
=
wordRegexp
(
commonBuiltins
);
var
indentInfo
=
null
;
// tokenizers
function
tokenBase
(
stream
,
state
)
{
// Handle scope changes
if
(
stream
.
sol
())
{
var
scopeOffset
=
state
.
scopes
[
0
].
offset
;
if
(
stream
.
eatSpace
())
{
var
lineOffset
=
stream
.
indentation
();
if
(
lineOffset
>
scopeOffset
)
{
indentInfo
=
'indent'
;
}
else
if
(
lineOffset
<
scopeOffset
)
{
indentInfo
=
'dedent'
;
}
return
null
;
}
else
{
if
(
scopeOffset
>
0
)
{
dedent
(
stream
,
state
);
}
}
}
if
(
stream
.
eatSpace
())
{
return
null
;
}
var
ch
=
stream
.
peek
();
// Handle Comments
if
(
ch
===
'#'
)
{
stream
.
skipToEnd
();
return
'comment'
;
}
// Handle Number Literals
if
(
stream
.
match
(
/^
[
0-9
\.]
/
,
false
))
{
var
floatLiteral
=
false
;
// Floats
if
(
stream
.
match
(
/^
\d
*
\.\d
+
(
e
[\+\-]?\d
+
)?
/i
))
{
floatLiteral
=
true
;
}
if
(
stream
.
match
(
/^
\d
+
\.\d
*/
))
{
floatLiteral
=
true
;
}
if
(
stream
.
match
(
/^
\.\d
+/
))
{
floatLiteral
=
true
;
}
if
(
floatLiteral
)
{
// Float literals may be "imaginary"
stream
.
eat
(
/J/i
);
return
'number'
;
}
// Integers
var
intLiteral
=
false
;
// Hex
if
(
stream
.
match
(
/^0x
[
0-9a-f
]
+/i
))
{
intLiteral
=
true
;
}
// Binary
if
(
stream
.
match
(
/^0b
[
01
]
+/i
))
{
intLiteral
=
true
;
}
// Octal
if
(
stream
.
match
(
/^0o
[
0-7
]
+/i
))
{
intLiteral
=
true
;
}
// Decimal
if
(
stream
.
match
(
/^
[
1-9
]\d
*
(
e
[\+\-]?\d
+
)?
/
))
{
// Decimal literals may be "imaginary"
stream
.
eat
(
/J/i
);
// TODO - Can you have imaginary longs?
intLiteral
=
true
;
}
// Zero by itself with no other piece of number.
if
(
stream
.
match
(
/^0
(?![\d
x
])
/i
))
{
intLiteral
=
true
;
}
if
(
intLiteral
)
{
// Integer literals may be "long"
stream
.
eat
(
/L/i
);
return
'number'
;
}
}
// Handle Strings
if
(
stream
.
match
(
stringPrefixes
))
{
state
.
tokenize
=
tokenStringFactory
(
stream
.
current
());
return
state
.
tokenize
(
stream
,
state
);
}
// Handle operators and Delimiters
if
(
stream
.
match
(
tripleDelimiters
)
||
stream
.
match
(
doubleDelimiters
))
{
return
null
;
}
if
(
stream
.
match
(
doubleOperators
)
||
stream
.
match
(
singleOperators
)
||
stream
.
match
(
wordOperators
))
{
return
'operator'
;
}
if
(
stream
.
match
(
singleDelimiters
))
{
return
null
;
}
if
(
stream
.
match
(
keywords
))
{
return
'keyword'
;
}
if
(
stream
.
match
(
builtins
))
{
return
'builtin'
;
}
if
(
stream
.
match
(
identifiers
))
{
return
'variable'
;
}
// Handle non-detected items
stream
.
next
();
return
ERRORCLASS
;
}
function
tokenStringFactory
(
delimiter
)
{
while
(
'rub'
.
indexOf
(
delimiter
.
charAt
(
0
).
toLowerCase
())
>=
0
)
{
delimiter
=
delimiter
.
substr
(
1
);
}
var
singleline
=
delimiter
.
length
==
1
;
var
OUTCLASS
=
'string'
;
return
function
tokenString
(
stream
,
state
)
{
while
(
!
stream
.
eol
())
{
stream
.
eatWhile
(
/
[^
'"
\\]
/
);
if
(
stream
.
eat
(
'
\
\'
)) {
stream.next();
if (singleline && stream.eol()) {
return OUTCLASS;
}
} else if (stream.match(delimiter)) {
state.tokenize = tokenBase;
return OUTCLASS;
} else {
stream.eat(/['
"]/);
}
}
if (singleline) {
if (parserConf.singleLineStringErrors) {
return ERRORCLASS;
} else {
state.tokenize = tokenBase;
}
}
return OUTCLASS;
};
}
function indent(stream, state, type) {
type = type || 'py';
var indentUnit = 0;
if (type === 'py') {
if (state.scopes[0].type !== 'py') {
state.scopes[0].offset = stream.indentation();
return;
}
for (var i = 0; i < state.scopes.length; ++i) {
if (state.scopes[i].type === 'py') {
indentUnit = state.scopes[i].offset + conf.indentUnit;
break;
}
}
} else {
indentUnit = stream.column() + stream.current().length;
}
state.scopes.unshift({
offset: indentUnit,
type: type
});
}
function dedent(stream, state, type) {
type = type || 'py';
if (state.scopes.length == 1) return;
if (state.scopes[0].type === 'py') {
var _indent = stream.indentation();
var _indent_index = -1;
for (var i = 0; i < state.scopes.length; ++i) {
if (_indent === state.scopes[i].offset) {
_indent_index = i;
break;
}
}
if (_indent_index === -1) {
return true;
}
while (state.scopes[0].offset !== _indent) {
state.scopes.shift();
}
return false
} else {
if (type === 'py') {
state.scopes[0].offset = stream.indentation();
return false;
} else {
if (state.scopes[0].type != type) {
return true;
}
state.scopes.shift();
return false;
}
}
}
function tokenLexer(stream, state) {
indentInfo = null;
var style = state.tokenize(stream, state);
var current = stream.current();
// Handle '.' connected identifiers
if (current === '.') {
style = state.tokenize(stream, state);
current = stream.current();
if (style === 'variable' || style === 'builtin') {
return 'variable';
} else {
return ERRORCLASS;
}
}
// Handle decorators
if (current === '@') {
style = state.tokenize(stream, state);
current = stream.current();
if (style === 'variable'
|| current === '@staticmethod'
|| current === '@classmethod') {
return 'meta';
} else {
return ERRORCLASS;
}
}
// Handle scope changes.
if (current === 'pass' || current === 'return') {
state.dedent += 1;
}
if (current === 'lambda') state.lambda = true;
if ((current === ':' && !state.lambda && state.scopes[0].type == 'py')
|| indentInfo === 'indent') {
indent(stream, state);
}
var delimiter_index = '[({'.indexOf(current);
if (delimiter_index !== -1) {
indent(stream, state, '])}'.slice(delimiter_index, delimiter_index+1));
}
if (indentInfo === 'dedent') {
if (dedent(stream, state)) {
return ERRORCLASS;
}
}
delimiter_index = '])}'.indexOf(current);
if (delimiter_index !== -1) {
if (dedent(stream, state, current)) {
return ERRORCLASS;
}
}
if (state.dedent > 0 && stream.eol() && state.scopes[0].type == 'py') {
if (state.scopes.length > 1) state.scopes.shift();
state.dedent -= 1;
}
return style;
}
var external = {
startState: function(basecolumn) {
return {
tokenize: tokenBase,
scopes: [{offset:basecolumn || 0, type:'py'}],
lastToken: null,
lambda: false,
dedent: 0
};
},
token: function(stream, state) {
var style = tokenLexer(stream, state);
state.lastToken = {style:style, content: stream.current()};
if (stream.eol() && stream.lambda) {
state.lambda = false;
}
return style;
},
indent: function(state, textAfter) {
if (state.tokenize != tokenBase) {
return 0;
}
return state.scopes[0].offset;
}
};
return external;
});
CodeMirror.defineMIME("
text
/
x
-
python
", "
python
");
templates/jstextline.html
View file @
fc4d4c82
<section
class=
"text-input
"
>
<input
type=
"text"
name=
"input_${id}"
id=
"input_${id}"
value=
"${value}"
%
if
size:
size=
"${size}"
%
endif
%
if
dojs =
=
'
math
'
:
onkeyup=
"DoUpdateMath('${id}')"
%
endif
/>
<section
id=
"jstextline_${id}"
class=
"jstextline
"
>
<input
type=
"text"
name=
"input_${id}"
id=
"input_${id}"
value=
"${value}"
%
if
size:
size=
"${size}"
%
endif
%
if
dojs =
=
'
math
'
:
onkeyup=
"DoUpdateMath('${id}')"
%
endif
/>
% if dojs == 'math':
<span
id=
"display_${id}"
>
`{::}`
</span>
...
...
@@ -20,7 +20,7 @@
% if state == 'unsubmitted':
<span
class=
"unanswered"
style=
"display:inline-block;"
id=
"status_${id}"
></span>
% elif state == 'correct':
% elif state == 'correct':
<span
class=
"correct"
id=
"status_${id}"
></span>
% elif state == 'incorrect':
<span
class=
"incorrect"
id=
"status_${id}"
></span>
...
...
templates/textbox.html
View file @
fc4d4c82
<section
class=
"text-input
"
>
<section
id=
"textbox_${id}"
class=
"textbox
"
>
<textarea
rows=
"30"
cols=
"80"
name=
"input_${id}"
id=
"input_${id}"
>
${value|h}
</textarea>
<span
id=
"answer_${id}"
></span>
% if state == 'unsubmitted':
<span
class=
"unanswered"
style=
"display:inline-block;"
id=
"status_${id}"
></span>
% elif state == 'correct':
% elif state == 'correct':
<span
class=
"correct"
id=
"status_${id}"
></span>
% elif state == 'incorrect':
<span
class=
"incorrect"
id=
"status_${id}"
></span>
...
...
@@ -18,17 +18,21 @@
<span
class=
"debug"
>
${msg|n}
</span>
<br/>
<br/>
<script>
// Note: We need to make the area follow the CodeMirror for this to
// work.
$
(
function
(){
var
cm
=
CodeMirror
.
fromTextArea
(
document
.
getElementById
(
"input_${id}"
),
{
'mode'
:
"python"
});
});
</script>
<style
type=
"text/css"
>
.CodeMirror
{
border-style
:
solid
;
border-width
:
1px
;}
</style>
<br/>
<script
type=
"text/javascript"
src=
"${ settings.LIB_URL }CodeMirror/codemirror.js"
></script>
<script
type=
"text/javascript"
src=
"${ settings.LIB_URL }CodeMirror/python.js"
></script>
<link
rel=
"stylesheet"
href=
"${ settings.LIB_URL }CodeMirror/codemirror.css"
/>
<script>
// Note: We need to make the area follow the CodeMirror for this to work.
$
(
function
(){
var
cm
=
CodeMirror
.
fromTextArea
(
document
.
getElementById
(
"input_${id}"
),
{
mode
:
"python"
});
});
</script>
<style
type=
"text/css"
>
.CodeMirror
{
border-style
:
solid
;
border-width
:
1px
;}
</style>
</section>
templates/textinput.html
View file @
fc4d4c82
<section
class=
"text-
input"
>
<input
type=
"text"
name=
"input_${id}"
id=
"input_${id}"
value=
"${value}"
%
if
size:
size=
"${size}"
%
endif
/>
<section
id=
"textinput_${id}"
class=
"text
input"
>
<input
type=
"text"
name=
"input_${id}"
id=
"input_${id}"
value=
"${value}"
%
if
size:
size=
"${size}"
%
endif
/>
<span
id=
"answer_${id}"
></span>
% if state == 'unsubmitted':
<span
class=
"unanswered"
style=
"display:inline-block;"
id=
"status_${id}"
></span>
% elif state == 'correct':
% elif state == 'correct':
<span
class=
"correct"
id=
"status_${id}"
></span>
% elif state == 'incorrect':
<span
class=
"incorrect"
id=
"status_${id}"
></span>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment