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

Returns a new instance of TextFilter.



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

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

Instance Attribute Details

#fixUrlObject

Returns the value of attribute fixUrl.



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

def fixUrl
  @fixUrl
end

#pluginObject

Returns the value of attribute plugin.



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

def plugin
  @plugin
end

#renderObject

Returns the value of attribute render.



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

def render
  @render
end

#serviceObject

Returns the value of attribute service.



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

def service
  @service
end

Instance Method Details

#filter(str, prop) ⇒ Object



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

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")
    mathNamespace = nil
    namespaceIndex = str::indexOf(":math")
    if namespaceIndex >= 0
        mathNamespace = Std::substr(str,str::lastIndexOf("<",namespaceIndex) + 1,namespaceIndex - (str::lastIndexOf("<",namespaceIndex) + 1))
    end
    if b
        tags = TextFilterTags::newSafeXml()
    else 
        tags = TextFilterTags::newXml(mathNamespace)
    end
    str = filterMath(tags,str,prop,b)
    str = filterApplet(tags,str,prop,b)
    return str
end

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 101

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



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
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 45

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 == -1
                        m1 = text::indexOf(">",n1)
                    end
                    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
            begin
            subtext = math2Img(sub,prop)
            end
            sub = subtext
            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



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

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



209
210
211
212
213
214
215
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 209

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



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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 132

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")
        role = PropertiesTools::getProperty(output,"role")
    else 
        digest = @render::computeDigest(str,prop)
        hashImage = @render::showImageHash(digest,PropertiesTools::getProperty(prop,"lang"))
        if hashImage == nil
            @render::showImage(nil,str,provider)
            hashImage = @render::showImageHash(digest,PropertiesTools::getProperty(prop,"lang"))
        end
        content = (hashImage::get("content"))
        if (@plugin::getConfiguration()::getProperty("wirisimageformat","png") == "png")
            img += (" src=\"data:image/png;base64," + content) + "\""
        else 
            img += (" src=\"data:image/svg+xml;charset=utf8," + UrlUtils::urlComponentEncode(content)) + "\""
        end
        if hashImage::exists("alt")
            alt = (hashImage::get("alt"))
        else 
            alt = @service::mathml2accessible(str,nil,prop)
        end
        width = (hashImage::get("width"))
        height = (hashImage::get("height"))
        baseline = (hashImage::get("baseline"))
        role = (hashImage::get("role"))
    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
    imageFormatController = @plugin::getImageFormatController()
    metricsHash = Hash.new()
    metricsHash::set("width",Std::parseInt(width))
    metricsHash::set("height",Std::parseInt(height))
    metricsHash::set("baseline",Std::parseInt(baseline))
    imageFormatController::scalateMetrics(dpi,metricsHash)
    alt = html_entity_encode(alt)
    img += " class=\"Wirisformula\""
    img += (" alt=\"" + alt) + "\""
    img += (" width=\"" + (metricsHash::get("width")).to_s) + "\""
    img += (" height=\"" + (metricsHash::get("height")).to_s) + "\""
    verticalAlign = (metricsHash::get("baseline") - metricsHash::get("height"))
    img += (" style=\"vertical-align:" + verticalAlign.to_s) + "px\""
    img += (" role=\"" + role) + "\""
    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



216
217
218
219
220
221
222
223
224
# File 'lib/com/wiris/plugin/impl/TextFilter.rb', line 216

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