Class: Bizside::Redmine::HtmlToTextile

Inherits:
Object
  • Object
show all
Defined in:
lib/bizside/redmine/html_to_textile.rb

Defined Under Namespace

Classes: Converter

Constant Summary collapse

NEWLINE =
"\n"

Class Method Summary collapse

Class Method Details

.convert(text) ⇒ Object

Wrapper for SAX parser



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bizside/redmine/html_to_textile.rb', line 151

def self.convert(text)
  # Note, start-of-line is white space trimmed and we use HTML parsing to wrap up a fake HTML root node
  text.gsub!(/<title.*title>/, '')
  text.gsub!(/<style.*style>/m, '')
  text.gsub!(/class="rla-report-table" cellspacing="0"/, '')
  text.gsub!(/class="alt"/, '')
  mark_up = text.gsub(/\n\ +/, NEWLINE).gsub(/\>\s*\n/, '> ')
  converter = Converter.new
  Nokogiri::HTML::SAX::Parser.new(converter).parse(mark_up)
  converter.converted.strip
end

.tweak_wiki_style(textile_format) ⇒ Object



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
201
202
203
204
205
# File 'lib/bizside/redmine/html_to_textile.rb', line 163

def self.tweak_wiki_style(textile_format)
  content = textile_format.gsub(/\n\n\s\|\n/, "\n")
  content.gsub!(/^h2\. Routing Errors$/, "h2. Routing Errors \n")
  content.gsub!(/^h2\. Parse warnings$/, "\n h2. Parse warnings \n")
  content.gsub!(/^h2\. Thanks for using request-log-analyzer$/, "--- \n\n h2. Thanks for using request-log-analyzer")
  content.gsub!(/\n\s\|_\./, "\n{background: #CAE8EA}. |_.")

  # Get threshold to draw color only Mean & StdDev
  threshold = ENV['THRESHOLD'] || 400
  colorized_content = ''
  content.each_line do |line|
    splited = line.split('|')

    if splited[4].present?
      mean = splited[4].strip
      if mean.match(/^\d+ms$/)
        if mean.to_i > threshold.to_i
          splited[4] = mean.strip.gsub(/^\d+ms$/, "%{color:red} #{mean}%")
        end
      elsif mean.match(/^\d+\.\d+s$/)
        if mean.to_f * 1000 > threshold.to_i
          splited[4] = mean.strip.gsub(/^\d+\.\d+s$/, "%{color:red} #{mean}%")
        end
      end
    end

    if splited[5].present?
      std_dev = splited[5].strip
      if std_dev.match(/^\d+ms$/)
        if std_dev.to_i > threshold.to_i
          splited[5] = std_dev.gsub(/^\d+ms$/, "%{color:red} #{std_dev}%")
        end
      elsif std_dev.match(/^\d+\.\d+s$/)
        if std_dev.to_f * 1000 > threshold.to_i
          splited[5] = std_dev.strip.gsub(/^\d+\.\d+s$/, "%{color:red} #{std_dev}%")
        end
      end
    end

     colorized_content << splited.join("|")
  end
  colorized_content
end