Class: Stepmod::Utils::HtmlToAsciimath
- Inherits:
- 
      Object
      
        - Object
- Stepmod::Utils::HtmlToAsciimath
 
- Defined in:
- lib/stepmod/utils/html_to_asciimath.rb
Instance Method Summary collapse
- #call(input) ⇒ Object
- #html_entities_to_asciimath(x) ⇒ Object
- #html_entities_to_stem(x) ⇒ Object
- #text_to_asciimath(text) ⇒ Object
Instance Method Details
#call(input) ⇒ Object
| 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 | # File 'lib/stepmod/utils/html_to_asciimath.rb', line 4 def call(input) return input if input.nil? || input.empty? to_asciimath = Nokogiri::HTML.fragment(input, "UTF-8") to_asciimath.css("i").each do |math_element| # puts "HTML MATH!! #{math_element.to_xml}" # puts "HTML MATH!! #{math_element.text}" decoded = text_to_asciimath(math_element.text) case decoded.length when 1..12 # puts "(#{math_element.text} to => #{decoded})" math_element.replace "stem:[#{decoded}]" when 0 math_element.remove else math_element.replace "_#{decoded}_" end end to_asciimath.css("sub").each do |math_element| case math_element.text.length when 0 math_element.remove else math_element.replace "~#{text_to_asciimath(math_element.text)}~" end end to_asciimath.css("sup").each do |math_element| case math_element.text.length when 0 math_element.remove else math_element.replace "^#{text_to_asciimath(math_element.text)}^" end end to_asciimath.css("ol").each do |element| element.css("li").each do |li| li.replace ". #{li.text}" end end to_asciimath.css("ul").each do |element| element.css("li").each do |li| li.replace "* #{li.text}" end end # Replace sans-serif font with monospace to_asciimath.css('font[style*="sans-serif"]').each do |x| x.replace "`#{x.text}`" end html_entities_to_stem( to_asciimath.children.to_s.gsub(/\]stem:\[/, "").gsub(/<\/?[uo]l>/, ""), ) end | 
#html_entities_to_asciimath(x) ⇒ Object
| 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | # File 'lib/stepmod/utils/html_to_asciimath.rb', line 69 def html_entities_to_asciimath(x) x.gsub("α", "alpha") .gsub("β", "beta") .gsub("γ", "gamma") .gsub("Γ", "Gamma") .gsub("δ", "delta") .gsub("Δ", "Delta") .gsub("ε", "epsilon") .gsub("ϵ", "varepsilon") .gsub("ζ", "zeta") .gsub("η", "eta") .gsub("θ", "theta") .gsub("Θ", "Theta") .gsub("ϑ", "vartheta") .gsub("ι", "iota") .gsub("κ", "kappa") .gsub("λ", "lambda") .gsub("Λ", "Lambda") .gsub("μ", "mu") .gsub("ν", "nu") .gsub("ξ", "xi") .gsub("Ξ", "Xi") .gsub("π", "pi") .gsub("Π", "Pi") .gsub("ρ", "rho") .gsub("β", "beta") .gsub("σ", "sigma") .gsub("Σ", "Sigma") .gsub("τ", "tau") .gsub("υ", "upsilon") .gsub("φ", "phi") .gsub("Φ", "Phi") .gsub("ϕ", "varphi") .gsub("χ", "chi") .gsub("ψ", "psi") .gsub("Ψ", "Psi") .gsub("ω", "omega") .gsub("χ", "χ") .gsub("×", "×") .gsub("Σ", "Σ") .gsub("ρ", "ρ") .gsub("σ", "σ") .gsub("λ", "λ") .gsub("τ", "τ") .gsub("∂", "∂") .gsub("≤", "≤") .gsub("≥", "≥") end | 
#html_entities_to_stem(x) ⇒ Object
| 118 119 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 | # File 'lib/stepmod/utils/html_to_asciimath.rb', line 118 def html_entities_to_stem(x) x.gsub("α", "stem:[alpha]") .gsub("β", "stem:[beta]") .gsub("γ", "stem:[gamma]") .gsub("Γ", "stem:[Gamma]") .gsub("δ", "stem:[delta]") .gsub("Δ", "stem:[Delta]") .gsub("ε", "stem:[epsilon]") .gsub("ϵ", "stem:[varepsilon]") .gsub("ζ", "stem:[zeta]") .gsub("η", "stem:[eta]") .gsub("θ", "stem:[theta]") .gsub("Θ", "stem:[Theta]") .gsub("ϑ", "stem:[vartheta]") .gsub("ι", "stem:[iota]") .gsub("κ", "stem:[kappa]") .gsub("λ", "stem:[lambda]") .gsub("Λ", "stem:[Lambda]") .gsub("μ", "stem:[mu]") .gsub("ν", "stem:[nu]") .gsub("ξ", "stem:[xi]") .gsub("Ξ", "stem:[Xi]") .gsub("π", "stem:[pi]") .gsub("Π", "stem:[Pi]") .gsub("ρ", "stem:[rho]") .gsub("β", "stem:[beta]") .gsub("σ", "stem:[sigma]") .gsub("Σ", "stem:[Sigma]") .gsub("τ", "stem:[tau]") .gsub("υ", "stem:[upsilon]") .gsub("φ", "stem:[phi]") .gsub("Φ", "stem:[Phi]") .gsub("ϕ", "stem:[varphi]") .gsub("χ", "stem:[chi]") .gsub("ψ", "stem:[psi]") .gsub("Ψ", "stem:[Psi]") .gsub("ω", "stem:[omega]") end | 
#text_to_asciimath(text) ⇒ Object
| 65 66 67 | # File 'lib/stepmod/utils/html_to_asciimath.rb', line 65 def text_to_asciimath(text) html_entities_to_asciimath(text.decode_html) end |