Class: WirisPlugin::TextFilter

Inherits:
Object
  • Object
show all
Includes:
Wiris
Defined in:
lib/com/wiris/plugin/impl/TextFilter.rb

Constant Summary collapse

NBSP =
Std::fromCharCode(160)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin) ⇒ TextFilter



14
15
16
17
18
19
20
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 14

def initialize(plugin)
  super()
  @plugin = plugin
  @render = plugin::newRender()
  @service = plugin::newTextService()
  @fixUrl = nil
end

Instance Attribute Details

#fixUrlObject

Returns the value of attribute fixUrl.



12
13
14
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 12

def fixUrl
  @fixUrl
end

#pluginObject

Returns the value of attribute plugin.



9
10
11
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 9

def plugin
  @plugin
end

#renderObject

Returns the value of attribute render.



10
11
12
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 10

def render
  @render
end

#serviceObject

Returns the value of attribute service.



11
12
13
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 11

def service
  @service
end

Instance Method Details

#filter(str, prop) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 21

def filter(str,prop)
  saveMode = nil
  if prop!=nil
    saveMode = PropertiesTools::getProperty(prop,"savemode")
  end
  if saveMode==nil
    saveMode = @plugin::getConfiguration()::getProperty(ConfigurationKeys::SAVE_MODE,"xml")
  end
  b = (saveMode=="safeXml")
  if b
    tags = TextFilterTags::newSafeXml()
  else 
    tags = TextFilterTags::newXml()
  end
  str = filterMath(tags,str,prop,b)
  str = filterApplet(tags,str,prop,b)
  return str
end

#filterApplet(tags, text, prop, safeXML) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 89

def filterApplet(tags,text,prop,safeXML)
  output = StringBuf.new()
  n0 = 0
  n1 = text::toUpperCase()::indexOf(tags::in_appletopen,n0)
  while n1>=0
    output::add(Std::substr(text,n0,n1-n0))
    n0 = n1
    n1 = text::toUpperCase()::indexOf(tags::in_appletclose,n0)
    if n1>=0
      n1 = n1+tags::in_appletclose::length()
      sub = Std::substr(text,n0,n1-n0)
      if safeXML
        if @fixUrl==nil
          @fixUrl = EReg.new("<a href=\"[^\"]*\"[^>]*>([^<]*)<\\/a>|<a href=\"[^\"]*\">","")
        end
        sub = @fixUrl::replace(sub,"$1")
        sub = html_entity_decode(sub)
        sub = StringTools::replace(sub,tags::in_double_quote,tags::out_double_quote)
        sub = StringTools::replace(sub,tags::in_open,tags::out_open)
        sub = StringTools::replace(sub,tags::in_close,tags::out_close)
        sub = StringTools::replace(sub,tags::in_entity,tags::out_entity)
        sub = StringTools::replace(sub,tags::in_quote,tags::out_quote)
      end
      n0 = n1
      output::add(sub)
      n1 = text::toUpperCase()::indexOf(tags::in_appletopen,n0)
    end
  end
  output::add(Std::substr(text,n0))
  return output::toString()
end

#filterMath(tags, text, prop, safeXML) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 39

def filterMath(tags,text,prop,safeXML)
  output = StringBuf.new()
  n0 = 0
  n1 = text::indexOf(tags::in_mathopen,n0)
  tag = @plugin::getConfiguration()::getProperty(ConfigurationKeys::EDITOR_MATHML_ATTRIBUTE,"data-mathml")
  dataMathml = text::indexOf(tag,0)
  while n1>=0
    m0 = n0
    output::add(Std::substr(text,n0,n1-n0))
    n0 = n1
    n1 = text::indexOf(tags::in_mathclose,n0)
    if n1>=0
      n1 = n1+tags::in_mathclose::length()
      sub = Std::substr(text,n0,n1-n0)
      if safeXML
        if dataMathml!=-1
          m1 = text::indexOf("/>",n1)
          if (m1>=0)&&((text::indexOf("<img",n1)==-1)||(text::indexOf("<img",n1)>m1))
            m0 = Std::substr(text,m0,n0-m0)::lastIndexOf("<img")
            if m0>=0
              if (text::indexOf(tag,m0)>0)&&(text::indexOf(tag,m0)<n1)
                n0 = n1
                output::add(sub)
                n1 = text::indexOf(tags::in_mathopen,n0)
                m0 = m1
                  next
              end
            end
          end
        end
        if @fixUrl==nil
          @fixUrl = EReg.new("<a href=\"[^\"]*\"[^>]*>([^<]*)<\\/a>|<a href=\"[^\"]*\">","")
        end
        sub = @fixUrl::replace(sub,"$1")
        sub = html_entity_decode(sub)
        sub = StringTools::replace(sub,tags::in_double_quote,tags::out_double_quote)
        sub = StringTools::replace(sub,tags::in_open,tags::out_open)
        sub = StringTools::replace(sub,tags::in_close,tags::out_close)
        sub = StringTools::replace(sub,tags::in_entity,tags::out_entity)
        sub = StringTools::replace(sub,tags::in_quote,tags::out_quote)
      end
      sub = math2Img(sub,prop)
      n0 = n1
      output::add(sub)
      n1 = text::indexOf(tags::in_mathopen,n0)
    end
  end
  output::add(Std::substr(text,n0))
  return output::toString()
end

#html_entity_decode(str) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 186

def html_entity_decode(str)
  str = StringTools::replace(str,"&lt;","<")
  str = StringTools::replace(str,"&gt;",">")
  str = StringTools::replace(str,"&quot;","\"")
  str = StringTools::replace(str,"&nbsp;",NBSP)
  str = StringTools::replace(str,"&amp;","&")
  return str
end

#html_entity_encode(str) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 194

def html_entity_encode(str)
  str = StringTools::replace(str,"<","&lt;")
  str = StringTools::replace(str,">","&gt;")
  str = StringTools::replace(str,"\"","&quot;")
  str = StringTools::replace(str,"&","&amp;")
  return str
end

#math2Img(str, prop) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 120

def math2Img(str,prop)
  img = "<img"
  output = PropertiesTools::newProperties()
  PropertiesTools::setProperty(prop,"centerbaseline","false")
  PropertiesTools::setProperty(prop,"accessible","true")
  PropertiesTools::setProperty(prop,"metrics","true")
  provider = @plugin::newGenericParamsProvider(prop)
  if (@plugin::getConfiguration()::getProperty("wirispluginperformance","false")=="false")
    src = @render::createImage(str,provider,output)
    img+=(" src=\""+src)+"\""
    alt = PropertiesTools::getProperty(output,"alt")
    width = PropertiesTools::getProperty(output,"width")
    height = PropertiesTools::getProperty(output,"height")
    baseline = PropertiesTools::getProperty(output,"baseline")
  else 
    digest = @render::computeDigest(str,prop)
    json = JSon::decode(@render::showImageJson(digest,PropertiesTools::getProperty(prop,"alt")))
    hashImage = (json)
    if (hashImage::get("status")=="warning")
      @render::showImage(nil,str,provider)
    end
    json = JSon::decode(@render::showImageJson(digest,"en"))
    hashImage = (json)
    if (hashImage::get("status")=="ok")
      result = (hashImage::get("result"))
      base64 = (result::get("base64"))
      img+=(" src=\"data:image/png;base64,"+base64)+"\""
      if result::exists("alt")
        alt = (result::get("alt"))
      else 
        alt = @service::mathml2accessible(str,nil,prop)
      end
      width = (result::get("width"))
      height = (result::get("height"))
      baseline = (result::get("baseline"))
    else 
      raise Exception,"Image can\'t be rendererd"
    end
  end
  dpi = Std::parseFloat(@plugin::getConfiguration()::getProperty(ConfigurationKeys::WIRIS_DPI,"96"))
  if @plugin::getConfiguration()::getProperty(ConfigurationKeys::EDITOR_PARAMS,nil)!=nil
    json = JSon::decode(@plugin::getConfiguration()::getProperty(ConfigurationKeys::EDITOR_PARAMS,nil))
    decodedHash = (json)
    if decodedHash::exists("dpi")
      dpi = Std::parseFloat((decodedHash::get("dpi")))
    end
  end
  mml = (@plugin::getConfiguration()::getProperty(ConfigurationKeys::FILTER_OUTPUT_MATHML,"false")=="true")
  f = 96/dpi
  dwidth = f*Std::parseFloat(width)
  dheight = f*Std::parseFloat(height)
  dbaseline = f*Std::parseFloat(baseline)
  alt = html_entity_encode(alt)
  img+=" class=\"Wirisformula\""
  img+=(" alt=\""+alt)+"\""
  img+=(" width=\""+(dwidth).to_s)+"\""
  img+=(" height=\""+(dheight).to_s)+"\""
  d = ((dbaseline-dheight))
  img+=(" style=\"vertical-align:"+d.to_s)+"px\""
  if mml
    tag = @plugin::getConfiguration()::getProperty(ConfigurationKeys::EDITOR_MATHML_ATTRIBUTE,"data-mathml")
    img+=(((" "+tag)+"=\"")+save_xml_encode(str))+"\""
  end
  img+="/>"
  return img
end

#save_xml_encode(str) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 201

def save_xml_encode(str)
  tags = TextFilterTags::newSafeXml()
  str = StringTools::replace(str,tags::out_double_quote,tags::in_double_quote)
  str = StringTools::replace(str,tags::out_open,tags::in_open)
  str = StringTools::replace(str,tags::out_close,tags::in_close)
  str = StringTools::replace(str,tags::out_entity,tags::in_entity)
  str = StringTools::replace(str,tags::out_quote,tags::in_quote)
  return str
end