embeddable_content 0.1.19 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d248938f6cfb46c41b491e75619538d518388329abe8970add6cb42188ee2247
4
- data.tar.gz: 34843aabc66b6e11dcd4addb69e70c52b71bf8db70039ef111bc5f53a16bb8c5
3
+ metadata.gz: 56e854adafe6bea8b3bb207d012239ef5775bcc4fdbc14a8f7789ccc7e339c40
4
+ data.tar.gz: 4bf3769759fa44f21bf0b04b33a61f6c8a69712819afc2e578b76baccab5f2d0
5
5
  SHA512:
6
- metadata.gz: 3de97e6f33fad46331b1a9662db068b8ef81913675d224bf0e128b1a2ff1581414ffbbbaf07ad9c68af624c2c75e6c4a99544a6950c25e4ff602ec5c3cc90db0
7
- data.tar.gz: 4dfcd81715bfa44e337f5ce8eb8a602f180ed3f3eb62d7569a2b236b87ea5ae030fc084c059d3cdce3f5f9310a2bab2283421d6b873459a8e6895077854d0da7
6
+ metadata.gz: 15a621a874e53c6adae8aca15b4d807f495e7434058e126359efb8a3c170236f367abc9e82b9db32a8b87d17dcd2a974e1eebff26b5f9d2a05cdc03b72258bb6
7
+ data.tar.gz: 06671f032b7ccc2d193346e21f2053f21ec24d5da47ee25e41ed683a23c0a0e9567b197cb50f58f90ace7ec21cc55b1e14ebdc008733caebcb86358fa0dbcb2f
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Embedder functionality has been extracted to this gem to reduce client-app dependencies.
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/embeddable_content.svg)](https://badge.fury.io/rb/embeddable_content)
6
+
5
7
  ## Installation
6
8
 
7
9
  For IM gems, add this to your application's Gemfile:
@@ -2,31 +2,123 @@ require 'open3'
2
2
 
3
3
  module EmbeddableContent
4
4
  module Tex
5
+ class RenderError < StandardError
6
+ attr_reader :script, :stderr, :status
7
+
8
+ def initialize(script, stderr, status, api_errors)
9
+ @script = script
10
+ @stderr = stderr
11
+ @status = status
12
+ super "Unable to resolve error raised calling #{script}: #{status}\n#{stderr}\n#{api_errors}"
13
+ end
14
+ end
15
+
5
16
  class BaseRenderer
6
17
  RENDER_TEX_JS_PATH = Pathname.new('lib').join 'tasks/render_tex.js'
7
18
 
8
- attr_reader :html
19
+ attr_reader :html, :stdout, :stderr, :status
9
20
 
10
21
  def initialize(html)
11
22
  @html = html
12
23
  end
13
24
 
14
25
  def render
15
- html.replace render_format(target_format) if target_format.present?
26
+ return html unless rendering_required?
27
+
28
+ run_script
16
29
  end
17
30
 
18
31
  private
19
32
 
20
- def render_format(format)
21
- output, status = Open3.capture2(render_tex(format), stdin_data: html)
22
- return output if status.success?
33
+ def rendering_required?
34
+ not_yet_rendered? && target_format.present?
35
+ end
36
+
37
+ def not_yet_rendered?
38
+ status.blank?
39
+ end
40
+
41
+ def render_failed?
42
+ failed_status || stderr.present?
43
+ end
44
+
45
+ def failed_status
46
+ status.present? && !status.success?
47
+ end
48
+
49
+ def render_error
50
+ @render_error ||= RenderError.new script, stderr, status, api_errors
51
+ end
52
+
53
+ def run_script
54
+ @run_script ||= Open3.capture3(script, stdin_data: html).tap do |stdout, stderr, status|
55
+ @stdout = stdout
56
+ @stderr = stderr
57
+ @status = status
58
+ end
59
+ render_failed? ? try_api_client : html.replace(stdout)
60
+ end
61
+
62
+ def script
63
+ @script ||= [RENDER_TEX_JS_PATH, '--output', target_format].join(' ')
64
+ end
65
+
66
+ REGEX_TEX_STRING = /Formula\s+(?<texstring>.*)\s+contains the following errors:/.freeze
67
+ def offending_tex_string
68
+ @offending_tex_string ||= REGEX_TEX_STRING.match(stderr)[:texstring].strip if
69
+ stderr.present?
70
+ end
71
+
72
+ def document
73
+ @document ||= Nokogiri::HTML html
74
+ end
75
+
76
+ def all_spans
77
+ @all_spans ||= document.css 'span'
78
+ end
79
+
80
+ def api_errors
81
+ offending_node.blank? ? '' : api_client.errors.unshift('API errors:').join("\n")
82
+ end
83
+
84
+ def try_api_client
85
+ raise render_error if offending_node.blank?
86
+ raise render_error unless api_client.success?
87
+
88
+ offending_node.content = ''
89
+ offending_node.add_child repaired_content
90
+ renderer_for_repaired_document.render
91
+ end
92
+
93
+ def renderer_for_repaired_document
94
+ @renderer_for_repaired_document ||=
95
+ self.class.new html.replace(document.to_html)
96
+ end
97
+
98
+ def repaired_span
99
+ @repaired_span = %w[<span class="mathjax-api"></span>].tap do |span|
100
+ span << repaired_content
101
+ end
102
+ end
103
+
104
+ def repaired_content
105
+ @repaired_content ||= api_client.send target_format
106
+ end
107
+
108
+ def api_client
109
+ @api_client ||= Mathjax::Api::Client.new offending_tex_string
110
+ end
23
111
 
24
- Rails.logger.warn "Error calling #{render_tex(format)}: #{status}"
25
- html
112
+ def offending_node
113
+ @offending_node ||=
114
+ all_spans.detect { |node| offending_tex_appears_in?(node) }
26
115
  end
27
116
 
28
- def render_tex(format)
29
- [RENDER_TEX_JS_PATH, '--output', format].join(' ')
117
+ REGEX_NODE_TEX_CONTENT = /\\\(\s*(?<tex_string>.*)\s*\\\)/m.freeze
118
+ def offending_tex_appears_in?(node)
119
+ node.content.match(REGEX_NODE_TEX_CONTENT).then do |md|
120
+ md.present? && md[:tex_string]&.strip.eql?(offending_tex_string)
121
+ end
30
122
  end
31
123
  end
32
124
  end
@@ -7,8 +7,8 @@ module EmbeddableContent
7
7
  :canvas
8
8
  end
9
9
 
10
- def render_format(_format)
11
- Mathjax::CanvasRenderer.new(html).render
10
+ def render
11
+ html.replace Mathjax::CanvasRenderer.new(html).render
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mathjax
4
+ module Api
5
+ class Client; end
6
+ end
7
+ end
@@ -14,15 +14,13 @@ Gem::Specification.new do |spec|
14
14
  'Provides embeddable content functionality to apps using this gem.'
15
15
  spec.homepage =
16
16
  'https://github.com/illustrativemathematics/embedded_content'
17
- spec.metadata = {
18
- 'github_repo' => 'git@github.com/illustrativemathematics/embeddable_content.git'
19
- }
17
+
20
18
  # Prevent pushing this gem to RubyGems.org.
21
19
  # To allow pushes either set the 'allowed_push_host'
22
20
  # to allow pushing to a single host or delete this section
23
21
  # to allow pushing to any host.
24
22
  # if spec.respond_to?(:metadata)
25
- # spec.metadata['allowed_push_host'] = "https://rubygems.org/gems/embedded_content"
23
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
26
24
  # else
27
25
  # raise 'RubyGems 2.0 or newer is required to protect against ' \
28
26
  # 'public gem pushes.'
@@ -1,3 +1,3 @@
1
1
  module EmbeddableContent
2
- VERSION = '0.1.19'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embeddable_content
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Connally
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-13 00:00:00.000000000 Z
11
+ date: 2021-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -208,6 +208,7 @@ files:
208
208
  - app/services/embeddable_content/visual_element_node_processor.rb
209
209
  - app/services/embeddable_content/widget_files/doc_processor.rb
210
210
  - app/services/embeddable_content/widget_files/node_processor.rb
211
+ - app/services/mathjax/api/client.rb
211
212
  - app/views/.keep
212
213
  - app/views/embeddable_content/replacements/desmos_files/_applet.html.slim
213
214
  - app/views/embeddable_content/replacements/desmos_files/_description.html.slim
@@ -284,8 +285,7 @@ files:
284
285
  - lib/tasks/embeddable_content_tasks.rake
285
286
  homepage: https://github.com/illustrativemathematics/embedded_content
286
287
  licenses: []
287
- metadata:
288
- github_repo: git@github.com/illustrativemathematics/embeddable_content.git
288
+ metadata: {}
289
289
  post_install_message:
290
290
  rdoc_options: []
291
291
  require_paths: