canvas-lms/lib/math_man.rb

62 lines
1.7 KiB
Ruby
Raw Normal View History

#
# Copyright (C) 2016 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
require "addressable/uri"
enable use of MathMan for latex => mathml conversion fixes CNVS-59514 Also adds a MathMan module that wraps some convenience methods for 1- figuring out if it's ok to use mathman; 2- constructing urls for hitting mathman's endpoints. test plan: *With Ritex* - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. *With Mathman* - Navigate to an account's plugin page, and select the MathMan plugin. - On the edit screen, enable the plugin, and provide a working mathman base url and check the 'Use for mml' checkbox. Save the changes. - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. Change-Id: I194d155b339123f7ed1948cf29070c1d17fc7f17 Reviewed-on: https://gerrit.instructure.com/84031 Tested-by: Jenkins Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: Benjamin Christian Nelson <bcnelson@instructure.com> Product-Review: John Corrigan <jcorrigan@instructure.com>
2016-08-02 03:28:43 +08:00
module MathMan
class InvalidConfigurationError < StandardError; end
enable use of MathMan for latex => mathml conversion fixes CNVS-59514 Also adds a MathMan module that wraps some convenience methods for 1- figuring out if it's ok to use mathman; 2- constructing urls for hitting mathman's endpoints. test plan: *With Ritex* - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. *With Mathman* - Navigate to an account's plugin page, and select the MathMan plugin. - On the edit screen, enable the plugin, and provide a working mathman base url and check the 'Use for mml' checkbox. Save the changes. - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. Change-Id: I194d155b339123f7ed1948cf29070c1d17fc7f17 Reviewed-on: https://gerrit.instructure.com/84031 Tested-by: Jenkins Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: Benjamin Christian Nelson <bcnelson@instructure.com> Product-Review: John Corrigan <jcorrigan@instructure.com>
2016-08-02 03:28:43 +08:00
def self.url_for(latex:, target:)
uri = base_url.join(target.to_s)
uri.query = "tex=#{latex}"
uri.to_s
enable use of MathMan for latex => mathml conversion fixes CNVS-59514 Also adds a MathMan module that wraps some convenience methods for 1- figuring out if it's ok to use mathman; 2- constructing urls for hitting mathman's endpoints. test plan: *With Ritex* - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. *With Mathman* - Navigate to an account's plugin page, and select the MathMan plugin. - On the edit screen, enable the plugin, and provide a working mathman base url and check the 'Use for mml' checkbox. Save the changes. - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. Change-Id: I194d155b339123f7ed1948cf29070c1d17fc7f17 Reviewed-on: https://gerrit.instructure.com/84031 Tested-by: Jenkins Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: Benjamin Christian Nelson <bcnelson@instructure.com> Product-Review: John Corrigan <jcorrigan@instructure.com>
2016-08-02 03:28:43 +08:00
end
def self.cache_key_for(latex, target)
["mathman", dynamic_settings.fetch('version'), Digest::MD5.hexdigest(latex), target].compact.cache_key
end
enable use of MathMan for latex => mathml conversion fixes CNVS-59514 Also adds a MathMan module that wraps some convenience methods for 1- figuring out if it's ok to use mathman; 2- constructing urls for hitting mathman's endpoints. test plan: *With Ritex* - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. *With Mathman* - Navigate to an account's plugin page, and select the MathMan plugin. - On the edit screen, enable the plugin, and provide a working mathman base url and check the 'Use for mml' checkbox. Save the changes. - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. Change-Id: I194d155b339123f7ed1948cf29070c1d17fc7f17 Reviewed-on: https://gerrit.instructure.com/84031 Tested-by: Jenkins Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: Benjamin Christian Nelson <bcnelson@instructure.com> Product-Review: John Corrigan <jcorrigan@instructure.com>
2016-08-02 03:28:43 +08:00
def self.use_for_mml?
Canvas::Plugin.value_to_boolean(plugin_settings[:use_for_mml])
enable use of MathMan for latex => mathml conversion fixes CNVS-59514 Also adds a MathMan module that wraps some convenience methods for 1- figuring out if it's ok to use mathman; 2- constructing urls for hitting mathman's endpoints. test plan: *With Ritex* - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. *With Mathman* - Navigate to an account's plugin page, and select the MathMan plugin. - On the edit screen, enable the plugin, and provide a working mathman base url and check the 'Use for mml' checkbox. Save the changes. - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. Change-Id: I194d155b339123f7ed1948cf29070c1d17fc7f17 Reviewed-on: https://gerrit.instructure.com/84031 Tested-by: Jenkins Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: Benjamin Christian Nelson <bcnelson@instructure.com> Product-Review: John Corrigan <jcorrigan@instructure.com>
2016-08-02 03:28:43 +08:00
end
def self.use_for_svg?
Canvas::Plugin.value_to_boolean(plugin_settings[:use_for_svg])
end
class << self
private
def base_url
url = dynamic_settings[:base_url]
# if we get here, we should have already checked one of the booleans above
raise InvalidConfigurationError unless url
Addressable::URI.parse(url).tap do |uri|
uri.path << '/' unless uri.path.end_with?('/')
end
end
def plugin_settings
Canvas::Plugin.find(:mathman).settings
end
def dynamic_settings
Canvas::DynamicSettings.find('math-man')
end
enable use of MathMan for latex => mathml conversion fixes CNVS-59514 Also adds a MathMan module that wraps some convenience methods for 1- figuring out if it's ok to use mathman; 2- constructing urls for hitting mathman's endpoints. test plan: *With Ritex* - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. *With Mathman* - Navigate to an account's plugin page, and select the MathMan plugin. - On the edit screen, enable the plugin, and provide a working mathman base url and check the 'Use for mml' checkbox. Save the changes. - Add an assignment (or some type of content with a description editable via tinymce / rcs). - Update the description to include a latex equation using the equation editor. - Upon saving the assignment, observe that an image of the equation is visible. - Using the browser's elemnt / DOM inspector, select the equation image, and observe that there is a hidden span that contain a math ml representation. Change-Id: I194d155b339123f7ed1948cf29070c1d17fc7f17 Reviewed-on: https://gerrit.instructure.com/84031 Tested-by: Jenkins Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: Benjamin Christian Nelson <bcnelson@instructure.com> Product-Review: John Corrigan <jcorrigan@instructure.com>
2016-08-02 03:28:43 +08:00
end
end