Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
d29ffce4
Unverified
Commit
d29ffce4
authored
6 years ago
by
Feanil Patel
Committed by
GitHub
6 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #18639 from edx/private_to_public_02d5477
Mergeback PR from private to public.
parents
c158a31b
edf1c892
Loading
Loading
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
common/lib/chem/chem/chemcalc.py
+24
-12
24 additions, 12 deletions
common/lib/chem/chem/chemcalc.py
common/lib/chem/chem/tests.py
+26
-2
26 additions, 2 deletions
common/lib/chem/chem/tests.py
common/lib/chem/setup.py
+2
-1
2 additions, 1 deletion
common/lib/chem/setup.py
with
52 additions
and
15 deletions
common/lib/chem/chem/chemcalc.py
+
24
−
12
View file @
d29ffce4
...
...
@@ -2,10 +2,12 @@ from __future__ import division
from
fractions
import
Fraction
import
markupsafe
import
nltk
from
nltk.tree
import
Tree
from
pyparsing
import
Literal
,
OneOrMore
,
ParseException
,
StringEnd
ARROWS
=
(
'
<->
'
,
'
->
'
)
# Defines a simple pyparsing tokenizer for chemical equations
...
...
@@ -26,6 +28,16 @@ phases = ["(s)", "(l)", "(g)", "(aq)"]
tokens
=
reduce
(
lambda
a
,
b
:
a
^
b
,
map
(
Literal
,
elements
+
digits
+
symbols
+
phases
))
tokenizer
=
OneOrMore
(
tokens
)
+
StringEnd
()
# HTML, Text are temporarily copied from openedx.core.djangolib.markup
# These libraries need to be moved out of edx-platform to be used by
# other applications.
# See LEARNER-5853 for more details.
Text
=
markupsafe
.
escape
# pylint: disable=invalid-name
def
HTML
(
html
):
# pylint: disable=invalid-name
return
markupsafe
.
Markup
(
html
)
def
_orjoin
(
l
):
return
"'"
+
"'
|
'"
.
join
(
l
)
+
"'"
...
...
@@ -166,20 +178,20 @@ def _render_to_html(tree):
return
tree
[
0
][
0
]
# If a fraction, return the fraction
if
len
(
tree
)
==
3
:
return
"
<sup>{num}</sup>⁄<sub>{den}</sub>
"
.
format
(
num
=
tree
[
0
][
0
],
den
=
tree
[
2
][
0
])
return
HTML
(
"
<sup>{num}</sup>⁄<sub>{den}</sub>
"
)
.
format
(
num
=
tree
[
0
][
0
],
den
=
tree
[
2
][
0
])
return
"
Error
"
def
subscript
(
tree
,
children
):
return
"
<sub>{sub}</sub>
"
.
format
(
sub
=
children
)
return
HTML
(
"
<sub>{sub}</sub>
"
)
.
format
(
sub
=
children
)
def
superscript
(
tree
,
children
):
return
"
<sup>{sup}</sup>
"
.
format
(
sup
=
children
)
return
HTML
(
"
<sup>{sup}</sup>
"
)
.
format
(
sup
=
children
)
def
round_brackets
(
tree
,
children
):
return
"
({insider})
"
.
format
(
insider
=
children
)
return
HTML
(
"
({insider})
"
)
.
format
(
insider
=
children
)
def
square_brackets
(
tree
,
children
):
return
"
[{insider}]
"
.
format
(
insider
=
children
)
return
HTML
(
"
[{insider}]
"
)
.
format
(
insider
=
children
)
dispatch
=
{
'
count
'
:
molecule_count
,
'
number_suffix
'
:
subscript
,
...
...
@@ -190,7 +202,7 @@ def _render_to_html(tree):
if
isinstance
(
tree
,
str
):
return
tree
else
:
children
=
""
.
join
(
map
(
_render_to_html
,
tree
))
children
=
HTML
(
""
)
.
join
(
map
(
_render_to_html
,
tree
))
if
tree
.
label
()
in
dispatch
:
return
dispatch
[
tree
.
label
()](
tree
,
children
)
else
:
...
...
@@ -207,20 +219,20 @@ def render_to_html(eq):
"""
Render as an error span
"""
return
'
<span class=
"
inline-error inline
"
>{0}</span>
'
.
format
(
s
)
return
HTML
(
'
<span class=
"
inline-error inline
"
>{0}</span>
'
)
.
format
(
s
)
def
render_arrow
(
arrow
):
"""
Turn text arrows into pretty ones
"""
if
arrow
==
'
->
'
:
return
u
'
\u2192
'
return
HTML
(
u
'
\u2192
'
)
if
arrow
==
'
<->
'
:
return
u
'
\u2194
'
return
HTML
(
u
'
\u2194
'
)
# this won't be reached unless we add more arrow types, but keep it to avoid explosions when
# that happens.
return
arrow
# that happens.
HTML-escape this unknown arrow just in case.
return
Text
(
arrow
)
def
render_expression
(
ex
):
"""
...
...
@@ -232,7 +244,7 @@ def render_to_html(eq):
return
err
(
ex
)
def
spanify
(
s
):
return
u
'
<span class=
"
math
"
>{0}</span>
'
.
format
(
s
)
return
HTML
(
u
'
<span class=
"
math
"
>{0}</span>
'
)
.
format
(
s
)
left
,
arrow
,
right
=
split_on_arrow
(
eq
)
if
arrow
==
''
:
...
...
This diff is collapsed.
Click to expand it.
common/lib/chem/chem/tests.py
+
26
−
2
View file @
d29ffce4
...
...
@@ -296,13 +296,20 @@ class Test_Render_Equations(unittest.TestCase):
log
(
out
+
'
-------
'
+
correct
,
'
html
'
)
self
.
assertEqual
(
out
,
correct
)
def
test_render_simple_brackets
(
self
):
def
test_render_simple_
round_
brackets
(
self
):
test_string
=
"
(Ar)
"
out
=
render_to_html
(
test_string
)
correct
=
u
'
<span class=
"
math
"
>(Ar)</span>
'
log
(
out
+
'
-------
'
+
correct
,
'
html
'
)
self
.
assertEqual
(
out
,
correct
)
def
test_render_simple_square_brackets
(
self
):
test_string
=
"
[Ar]
"
out
=
render_to_html
(
test_string
)
correct
=
u
'
<span class=
"
math
"
>[Ar]</span>
'
log
(
out
+
'
-------
'
+
correct
,
'
html
'
)
self
.
assertEqual
(
out
,
correct
)
def
test_render_eq1
(
self
):
test_string
=
"
H^+ + OH^- -> H2O
"
out
=
render_to_html
(
test_string
)
...
...
@@ -320,7 +327,24 @@ class Test_Render_Equations(unittest.TestCase):
def
test_render_eq3
(
self
):
test_string
=
"
H^+ + OH^- <= H2O
"
# unsupported arrow
out
=
render_to_html
(
test_string
)
correct
=
u
'
<span class=
"
math
"
><span class=
"
inline-error inline
"
>H^+ + OH^- <= H2O</span></span>
'
correct
=
u
'
<span class=
"
math
"
><span class=
"
inline-error inline
"
>H^+ + OH^- <= H2O</span></span>
'
log
(
out
+
'
-------
'
+
correct
,
'
html
'
)
self
.
assertEqual
(
out
,
correct
)
def
test_render_eq4
(
self
):
test_string
=
"
[H^+] + OH^- <-> (H2O)
"
# with brackets
out
=
render_to_html
(
test_string
)
correct
=
u
'
<span class=
"
math
"
>[H<sup>+</sup>]+OH<sup>-</sup>
\u2194
(H<sub>2</sub>O)</span>
'
log
(
out
+
'
-------
'
+
correct
,
'
html
'
)
self
.
assertEqual
(
out
,
correct
)
def
test_escaping
(
self
):
"""
Tests that invalid input is escaped.
"""
test_string
=
"
<script>f()</script>
"
out
=
render_to_html
(
test_string
)
correct
=
u
'
<span class=
"
math
"
><span class=
"
inline-error inline
"
><script>f()</script></span></span>
'
log
(
out
+
'
-------
'
+
correct
,
'
html
'
)
self
.
assertEqual
(
out
,
correct
)
...
...
This diff is collapsed.
Click to expand it.
common/lib/chem/setup.py
+
2
−
1
View file @
d29ffce4
...
...
@@ -2,12 +2,13 @@ from setuptools import setup
setup
(
name
=
"
chem
"
,
version
=
"
0.
1.2
"
,
version
=
"
0.
2.0
"
,
packages
=
[
"
chem
"
],
install_requires
=
[
"
pyparsing==2.2.0
"
,
"
numpy==1.6.2
"
,
"
scipy==0.14.0
"
,
"
nltk
"
,
"
markupsafe
"
,
# Should be replaced by other utilities. See LEARNER-5853 for more details.
],
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment