Class: Mathematical
- Inherits:
-
Object
- Object
- Mathematical
- Includes:
- Corrections, Validator
- Defined in:
- lib/mathematical.rb,
lib/mathematical/version.rb,
lib/mathematical/validator.rb,
lib/mathematical/corrections.rb,
ext/mathematical/mathematical.c
Defined Under Namespace
Modules: Corrections, Validator Classes: DocumentCreationError, DocumentReadError, MaxsizeError, ParseError, Process
Constant Summary collapse
- DEFAULT_OPTS =
{ ppi: 72.0, zoom: 1.0, base64: false, maxsize: 0, format: :svg, delimiter: [:DOLLAR, :DOUBLE], }
- XML_HEADER =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
- MATH_MATCH =
%r{<math xmlns.+?</math>}m
- VERSION =
"1.6.18"
Constants included from Validator
Validator::FORMAT_TYPES, Validator::RENDER_TYPES
Instance Method Summary collapse
- #filter(maths) ⇒ Object
-
#initialize(options = {}) ⇒ Mathematical
constructor
A new instance of Mathematical.
- #parse(maths) ⇒ Object (also: #render)
- #result(result_data) ⇒ Object
- #strict_filter(maths) ⇒ Object
- #text_filter(maths) ⇒ Object
Methods included from Validator
#validate_config, #validate_content, #validate_string
Methods included from Corrections
#adjust_lt_gt, #apply_corrections
Constructor Details
#initialize(options = {}) ⇒ Mathematical
Returns a new instance of Mathematical.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mathematical.rb', line 29 def initialize( = {}) @config = DEFAULT_OPTS.merge() validate_config(@config) @config[:formatInt] = FORMAT_TYPES.index(@config[:format]) @config[:delimiter] = if @config[:delimiter].is_a?(Symbol) Configuration::Delimiters.to_h[@config[:delimiter]] else @config[:delimiter].map do |delim| Configuration::Delimiters.to_h[delim] end.inject(0, :|) end @processer = Mathematical::Process.new(@config) end |
Instance Method Details
#filter(maths) ⇒ Object
55 56 57 58 59 |
# File 'lib/mathematical.rb', line 55 def filter(maths) maths = validate_content(maths) result_data = @processer.process(maths, RENDER_TYPES.find_index(:filter)) result(result_data) end |
#parse(maths) ⇒ Object Also known as: render
47 48 49 50 51 |
# File 'lib/mathematical.rb', line 47 def parse(maths) maths = validate_content(maths) result_data = @processer.process(maths, RENDER_TYPES.find_index(:parse)) result(result_data) end |
#result(result_data) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/mathematical.rb', line 89 def result(result_data) raise(RuntimeError) if !result_data.is_a?(Hash) && !result_data.is_a?(Array) if result_data.is_a?(Array) result_data.map { |d| format_data(d) } else format_data(result_data) end end |
#strict_filter(maths) ⇒ Object
83 84 85 86 87 |
# File 'lib/mathematical.rb', line 83 def strict_filter(maths) maths = validate_content(maths) result_data = @processer.process(maths, RENDER_TYPES.find_index(:strict_filter)) result(result_data) end |
#text_filter(maths) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mathematical.rb', line 61 def text_filter(maths) maths = validate_content(maths) widths = [] heights = [] result_data = @processer.process(maths, RENDER_TYPES.find_index(:text_filter)) # TODO: can/should be optimized to not do two calls here, but I am thinking # about moving to Rust and don't have time to write safe C... if result_data[:data] && @config[:format] != :mathml result_data[:data].gsub!(MATH_MATCH) do |_match| result = @processer.process(maths, RENDER_TYPES.find_index(:parse)) widths << result[:width] heights << result[:height] result[:data] end result_data[:width] = widths result_data[:height] = heights end result(result_data) end |