-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
# ### | ||
# Copyright (c) 2015, Rice University | ||
# This software is subject to the provisions of the GNU Affero General | ||
# Public License version 3 (AGPLv3). | ||
# See LICENCE.txt for details. | ||
# ### | ||
import requests | ||
from lxml import etree | ||
from pyramid.settings import asbool | ||
from pyramid.threadlocal import get_current_registry | ||
|
||
|
||
MATHML_NAMESPACE = "http://www.w3.org/1998/Math/MathML" | ||
NSMAP = {'m': MATHML_NAMESPACE} | ||
|
||
|
||
def _find_or_create_semantics_block(math_block): | ||
"""Finds or creates the MathML semantics tag.""" | ||
try: | ||
semantics_block = math_block.xpath( | ||
'/m:samantics', | ||
namespaces=NSMAP)[0] | ||
except IndexError: | ||
# Move all the contents of the math_block into a semantics wrapper. | ||
children = [] | ||
for child in math_block.getchildren(): | ||
children.append(child) | ||
math_block.remove(child) # why no pop? | ||
semantics_block = etree.SubElement( | ||
math_block, | ||
'{{{}}}semantics'.format(MATHML_NAMESPACE)) | ||
for child in children: | ||
semantics_block.append(child) | ||
return semantics_block | ||
|
||
|
||
def inject_mathml_svgs(content): | ||
"""Inject MathML SVG annotations into HTML content.""" | ||
settings = get_current_registry().settings | ||
is_enabled = asbool(settings.get('mathml2svg.enabled?', False)) | ||
url = settings.get('mathml2svg.url') | ||
|
||
# Bailout when svg generation is disabled. | ||
if not is_enabled: | ||
return content | ||
|
||
xml = etree.fromstring(content) | ||
math_blocks = xml.xpath( | ||
'//m:math[not(/m:annotation-xml[@encoding="image/svg+xml"])]', | ||
namespaces=NSMAP) | ||
for math_block in math_blocks: | ||
# Submit the MathML block to the SVG generation service. | ||
payload = {'MathML': etree.tostring(math_block)} | ||
response = requests.post(url, data=payload) | ||
# Inject the SVG into the MathML as an annotation | ||
# only if the resposne was good, otherwise skip over it. | ||
if response.status_code == 200: | ||
semantics_wrapper = _find_or_create_semantics_block(math_block) | ||
svg = response.text | ||
content_type = response.headers['content-type'] | ||
# Insert the svg into the content | ||
annotation = etree.SubElement( | ||
semantics_wrapper, | ||
'{{{}}}annotation-xml'.format(MATHML_NAMESPACE)) | ||
annotation.set('encoding', content_type) | ||
annotation.append(etree.fromstring(svg)) | ||
modified_content = etree.tostring(xml) | ||
return modified_content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
# ### | ||
# Copyright (c) 2015, Rice University | ||
# This software is subject to the provisions of the GNU Affero General | ||
# Public License version 3 (AGPLv3). | ||
# See LICENCE.txt for details. | ||
# ### | ||
import unittest | ||
try: | ||
from unittest import mock | ||
except ImportError: | ||
import mock | ||
|
||
from lxml import etree | ||
|
||
|
||
class SVGInjectionUtilTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
# Mock the registry, since this is a utility test. | ||
self.registry_patcher = mock.patch( | ||
'cnxpublishing.models.get_current_registry') | ||
self.mock_registry = self.registry_patcher.start() | ||
self.mock_registry.return_value.settings = { | ||
'mathml2svg.enabled?': 'on', | ||
} | ||
|
||
# Mock the external communication with the cnx-mathml2svg service. | ||
self.requests_patcher = mock.patch( | ||
'requests.post') | ||
self.mocked_post = self.requests_patcher.start() | ||
self.mocked_post.return_value.status_code = 200 | ||
self.mocked_post.return_value.headers = { | ||
'content-type': 'image/svg+xml', | ||
} | ||
self.mocked_post.return_value.text = '<svg>mocked</svg>' | ||
|
||
def tearDown(self): | ||
self.registry_patcher.stop() | ||
self.requests_patcher.stop() | ||
|
||
@property | ||
def target(self): | ||
from ..models import inject_mathml_svgs | ||
return inject_mathml_svgs | ||
|
||
def test_mathml2svg(self): | ||
content = """\ | ||
<div class="equation"> | ||
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow> <mi>x</mi> <mo>=</mo> <mfrac> <mrow> <mo>−<!-- − --></mo> <mi>b</mi> <mo>±<!-- ± --></mo> <msqrt> <msup> <mi>b</mi> <mn>2</mn> </msup> <mo>−<!-- − --></mo> <mn>4</mn> <mi>a</mi> <mi>c</mi> </msqrt> </mrow> <mrow> <mn>2</mn> <mi>a</mi> </mrow> </mfrac> </mrow></semantics></math> | ||
</div>""" | ||
|
||
# Call the target function. | ||
result = self.target(content) | ||
|
||
elms = etree.fromstring(result) | ||
annotation = elms.xpath( | ||
'/div/m:math//m:annotation-xml[@encoding="image/svg+xml"]', | ||
namespaces={'m': "http://www.w3.org/1998/Math/MathML"})[0] | ||
expected = """<annotation-xml xmlns="http://www.w3.org/1998/Math/MathML" encoding="image/svg+xml"><svg>mocked</svg></annotation-xml>""" | ||
|
||
self.assertEqual(etree.tostring(annotation), expected) | ||
|
||
def test_missing_semantics_wrapper(self): | ||
content = """\ | ||
<div class="equation"> | ||
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow> <mi>x</mi> <mo>=</mo> <mfrac> <mrow> <mo>−<!-- − --></mo> <mi>b</mi> <mo>±<!-- ± --></mo> <msqrt> <msup> <mi>b</mi> <mn>2</mn> </msup> <mo>−<!-- − --></mo> <mn>4</mn> <mi>a</mi> <mi>c</mi> </msqrt> </mrow> <mrow> <mn>2</mn> <mi>a</mi> </mrow> </mfrac> </mrow></math> | ||
</div>""" | ||
|
||
# Call the target function. | ||
result = self.target(content) | ||
|
||
elms = etree.fromstring(result) | ||
annotation = elms.xpath( | ||
'/div/m:math/m:semantics/m:annotation-xml[@encoding="image/svg+xml"]', | ||
namespaces={'m': "http://www.w3.org/1998/Math/MathML"})[0] | ||
expected = """<annotation-xml xmlns="http://www.w3.org/1998/Math/MathML" encoding="image/svg+xml"><svg>mocked</svg></annotation-xml>""" | ||
|
||
self.assertEqual(etree.tostring(annotation), expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
'pyramid>=1.5', | ||
'pyramid_jinja2', | ||
'pyramid_multiauth', | ||
'requests', | ||
) | ||
tests_require = [ | ||
'webtest', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters