2020-10-27 00:50:13 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-28 04:05:04 +08:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2017-05-06 01:58:22 +08:00
|
|
|
require "addressable/uri"
|
|
|
|
|
2016-08-02 03:28:43 +08:00
|
|
|
module MathMan
|
2018-04-21 22:07:01 +08:00
|
|
|
class InvalidConfigurationError < StandardError; end
|
|
|
|
|
Make typeset math and math image match
at least get them much closer.
1. typeset math using svg by default, which is how the image is generated
2. size the image so it matches the surrounding font-size
Also, still typeset math from images even if the image fails to load.
closes MAT-501
flag=new_math_equation_handling
SIZING MATH REQUIRES A COMPANION CHANGE TO MATHMAN (the other stuff doesn't)
- clone git clone ssh://<yourid>@gerrit.instructure.com:29418/mathman
and follow the instructions for building.
- checkout https://gerrit.instructure.com/c/mathman/+/277389
- build it (once built, you don't have to run in in docker)
- in a canvas rails console:
Setting.set(
'equation_image_url,
'http://localhost:8000/svg?tex='
)
http://localhost:8000 is where mathman is via `node app.js`
it's at http://mathman.docker if in docker (at least that's
what the README says, I don't really know)
- test mathman by requesting `http://<mathman-url>/svg?tex=17`
and expect an image of "17". now try with `request /svg?tex=17&scale=2`
and your "17" shoul be twice as large.
- restart your canvas server
test plan:
Updated typesetting:
- have a question_bank quetsion with math and answers with math
- include the question in a quiz
> verify that the question looks the same in the question bank
as it does when previewing/taking the quiz
- if your math includes a "g", it should look the same
- the size of the equation should be pretty close
Sizing to match:
- turn on scale_math_equations site admin feature
- put an equation w/in a paragraph with a large font, or
add an equation to the rce, select it, and change the font size
- click on the equation, edit equation, click insert equation
w/o changing anything
> expect the equation to match the larger font.
- extra credit: DO THIS WITH AND WITHOUT MATHMAN PLUGIN ENABLED
(yes, there are 2 paths canvas takes to mathman)
make sure to update dyanmic-settings.yml's math-man entry
math-man:
base_url: 'http://localhost:8000'
use_for_svg: 'true'
use_for_mml: 'false'
- at /plugins, enable mathman plugin and ensure use_for_svg istrue
Error handling:
- change the URL to mathman to a dead end
- load a page with math equation images
> expect the math to be typeset by mathjax
Change-Id: I354e98a0a0256740ce5b4937f5b4e3adc690fe51
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/277245
Reviewed-by: Weston Dransfield <wdransfield@instructure.com>
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
QA-Review: Weston Dransfield <wdransfield@instructure.com>
Product-Review: David Lyons <lyons@instructure.com>
2021-11-02 20:52:12 +08:00
|
|
|
def self.url_for(latex:, target:, scale: "")
|
|
|
|
scale_param = "&scale=#{scale}" if scale.present?
|
2017-07-20 04:55:32 +08:00
|
|
|
uri = base_url.join(target.to_s)
|
Make typeset math and math image match
at least get them much closer.
1. typeset math using svg by default, which is how the image is generated
2. size the image so it matches the surrounding font-size
Also, still typeset math from images even if the image fails to load.
closes MAT-501
flag=new_math_equation_handling
SIZING MATH REQUIRES A COMPANION CHANGE TO MATHMAN (the other stuff doesn't)
- clone git clone ssh://<yourid>@gerrit.instructure.com:29418/mathman
and follow the instructions for building.
- checkout https://gerrit.instructure.com/c/mathman/+/277389
- build it (once built, you don't have to run in in docker)
- in a canvas rails console:
Setting.set(
'equation_image_url,
'http://localhost:8000/svg?tex='
)
http://localhost:8000 is where mathman is via `node app.js`
it's at http://mathman.docker if in docker (at least that's
what the README says, I don't really know)
- test mathman by requesting `http://<mathman-url>/svg?tex=17`
and expect an image of "17". now try with `request /svg?tex=17&scale=2`
and your "17" shoul be twice as large.
- restart your canvas server
test plan:
Updated typesetting:
- have a question_bank quetsion with math and answers with math
- include the question in a quiz
> verify that the question looks the same in the question bank
as it does when previewing/taking the quiz
- if your math includes a "g", it should look the same
- the size of the equation should be pretty close
Sizing to match:
- turn on scale_math_equations site admin feature
- put an equation w/in a paragraph with a large font, or
add an equation to the rce, select it, and change the font size
- click on the equation, edit equation, click insert equation
w/o changing anything
> expect the equation to match the larger font.
- extra credit: DO THIS WITH AND WITHOUT MATHMAN PLUGIN ENABLED
(yes, there are 2 paths canvas takes to mathman)
make sure to update dyanmic-settings.yml's math-man entry
math-man:
base_url: 'http://localhost:8000'
use_for_svg: 'true'
use_for_mml: 'false'
- at /plugins, enable mathman plugin and ensure use_for_svg istrue
Error handling:
- change the URL to mathman to a dead end
- load a page with math equation images
> expect the math to be typeset by mathjax
Change-Id: I354e98a0a0256740ce5b4937f5b4e3adc690fe51
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/277245
Reviewed-by: Weston Dransfield <wdransfield@instructure.com>
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
QA-Review: Weston Dransfield <wdransfield@instructure.com>
Product-Review: David Lyons <lyons@instructure.com>
2021-11-02 20:52:12 +08:00
|
|
|
uri.query = "tex=#{latex}#{scale_param}"
|
2017-05-06 01:58:22 +08:00
|
|
|
uri.to_s
|
2016-08-02 03:28:43 +08:00
|
|
|
end
|
|
|
|
|
2018-06-30 04:17:32 +08:00
|
|
|
def self.cache_key_for(latex, target)
|
|
|
|
["mathman", dynamic_settings.fetch("version"), Digest::MD5.hexdigest(latex), target].compact.cache_key
|
|
|
|
end
|
|
|
|
|
2016-08-02 03:28:43 +08:00
|
|
|
def self.use_for_mml?
|
2018-04-21 22:07:01 +08:00
|
|
|
Canvas::Plugin.value_to_boolean(plugin_settings[:use_for_mml])
|
2016-08-02 03:28:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.use_for_svg?
|
2018-04-21 22:07:01 +08:00
|
|
|
Canvas::Plugin.value_to_boolean(plugin_settings[:use_for_svg])
|
2016-08-23 01:21:49 +08:00
|
|
|
end
|
|
|
|
|
2017-05-06 01:58:22 +08:00
|
|
|
class << self
|
|
|
|
private
|
|
|
|
|
|
|
|
def base_url
|
2018-04-21 22:07:01 +08:00
|
|
|
url = dynamic_settings[:base_url]
|
|
|
|
# if we get here, we should have already checked one of the booleans above
|
|
|
|
raise InvalidConfigurationError unless url
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2018-04-21 22:07:01 +08:00
|
|
|
Addressable::URI.parse(url).tap do |uri|
|
|
|
|
uri.path << "/" unless uri.path.end_with?("/")
|
2017-05-06 01:58:22 +08:00
|
|
|
end
|
2016-08-23 01:21:49 +08:00
|
|
|
end
|
|
|
|
|
2018-04-21 22:07:01 +08:00
|
|
|
def plugin_settings
|
|
|
|
Canvas::Plugin.find(:mathman).settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def dynamic_settings
|
2022-01-13 05:28:32 +08:00
|
|
|
DynamicSettings.find("math-man")
|
2017-05-06 01:58:22 +08:00
|
|
|
end
|
2016-08-02 03:28:43 +08:00
|
|
|
end
|
|
|
|
end
|