Class: Output::HTML

Inherits:
Object
  • Object
show all
Defined in:
lib/output/html.rb

Instance Method Summary collapse

Constructor Details

#initialize(grepmate) ⇒ HTML

Returns a new instance of HTML.



3
4
5
6
7
# File 'lib/output/html.rb', line 3

def initialize(grepmate)
  @grepmate = grepmate
  temp_dir = grepmate.config['temp_directory'] || ENV['TMPDIR']
  @temp_file = File.expand_path(File.join(temp_dir, %w(grepmate.html)))
end

Instance Method Details

#determine_javascript_highlight_textObject



9
10
11
# File 'lib/output/html.rb', line 9

def determine_javascript_highlight_text
  @grepmate.params['what_to_search_for'].values.map {|v| "highlightSearchTerms('#{v.gsub(/'/, "\\'").gsub(/:/, "\\:")}', false)" }.join(";")
end

#processObject



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
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
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
132
133
134
# File 'lib/output/html.rb', line 13

def process
  syntax_installed = true
  begin
    require 'syntax/convertors/html'
  rescue LoadError
    syntax_installed = false
  end

  html = '<html><head>'
  html << "  <style type=\"text/css\">body { background-color: #EEEEEE; } pre {\n  display: inline; } .ruby { font-family: Monaco; font-size: 10pt;\n  background-color: white } .ruby .normal {} .ruby .comment { color:\n  #005; font-style: italic; } .ruby .keyword { color: #A00; font-weight:\n  bold; } .ruby .method { color: #077; } .ruby .class { color: #074; }\n  .ruby .module { color: #050; } .ruby .punct { color: #447; font-weight:\n  bold; } .ruby .symbol { color: #099; } .ruby .string { color: #944;\n  background: #FFE; } .ruby .char { color: #F07; } .ruby .ident { color:\n  #004; } .ruby .constant { color: #07F; } .ruby .regex { color: #B66;\n  background: #FEF; } .ruby .number { color: #F99; } .ruby .attribute {\n  color: #7BB; } .ruby .global { color: #7FB; } .ruby .expr { color:\n  #227; } .ruby .escape { color: #277; } .ruby .highlight {\n  background-color: yellow; font-weight: 900; } td.lineno { text-align:\n  right; font-family: Monaco; font-size: 9pt; padding-right: 10px; }\n  td.filename { padding-top: 35px; font-family: Monaco; font-size: 14pt;\n  }</style>\n\n  <script type=\"text/javascript\">\n  // http://www.nsftools.com/misc/SearchAndHighlight.htm\n\n  function doHighlight(bodyText,searchTerm,highlightStartTag,highlightEndTag)\n  {if((!highlightStartTag)||(!highlightEndTag)){highlightStartTag=\"<font style='color:blue; background-color:yellow;'>\";highlightEndTag=\"</font>\";}\n  var newText=\"\";var i=-1;var lcSearchTerm=searchTerm.toLowerCase();var lcBodyText=bodyText.toLowerCase();while(bodyText.length>0){i=lcBodyText.indexOf(lcSearchTerm,i+1);if(i<0){newText+=bodyText;bodyText=\"\";}else{if(bodyText.lastIndexOf(\">\",i)>=bodyText.lastIndexOf(\"<\",i)){if(lcBodyText.lastIndexOf(\"/script>\",i)>=lcBodyText.lastIndexOf(\"<script\",i)){newText+=bodyText.substring(0,i)+highlightStartTag+bodyText.substr(i,searchTerm.length)+highlightEndTag;bodyText=bodyText.substr(i+searchTerm.length);lcBodyText=bodyText.toLowerCase();i=-1;}}}}\n  return newText;}\n  function highlightSearchTerms(searchText,treatAsPhrase,warnOnFailure,highlightStartTag,highlightEndTag)\n  {if(treatAsPhrase){searchArray=[searchText];}else{searchArray=searchText.split(\" \");}\n  if(!document.body||typeof(document.body.innerHTML)==\"undefined\"){if(warnOnFailure){alert(\"Sorry, for some reason the text of this page is unavailable. Searching will not work.\");}\n  return false;}\n  var bodyText=document.body.innerHTML;for(var i=0;i<searchArray.length;i++){bodyText=doHighlight(bodyText,searchArray[i],highlightStartTag,highlightEndTag);}\n  document.body.innerHTML=bodyText;return true;}</script></script>\n"

  # some searches can return stupid-huge result sets.  Javascript will likely freeze or crash the browser in those cases.
  if @grepmate.results.size < 1000
    html << "</head><body onLoad=\"#{determine_javascript_highlight_text}\">"
  else
    html << "</head><body>"
  end

  syntax_converter = (syntax_installed ? Syntax::Convertors::HTML.for_syntax("ruby") : nil)
  html << '<pre>sudo gem install syntax</pre> to get syntax highlighting<br><br>' unless syntax_installed

  html << '<table cellspacing="0" cellpadding="2">'

  last_file = ''
  separator = false

  html << @grepmate.results.map{ |line|
    if line == '--'
      separator = true
      ''
    elsif line =~ /^(.+?)-(\d+)-(.*)$/ || line =~ /^(.+?):(\d+):(.*)$/
      file = File.expand_path($1)
      line = $2
      context = $3

      file_group = ''

      if file == last_file
        file_group << "<tr><td class=\"lineno\">...</td><td></td></tr>" if separator
      else
        parts = file.split(/\//)
        path = ''
        file_group << "<tr><td colspan=\"2\" class=\"filename\">"
        parts.each do |part|
          path << "/#{part}"
          file_group << "/" unless part.equal?(parts.first)
          if part.equal?(parts.last)
            file_group << "<a href=\"txmt://open?url=file://#{file}\">#{part}</a>"
          else
            file_group << "<a href=\"file:/#{path}\">#{part}</a>"
          end
        end
        file_group << "</td></tr>\n"
      end

      separator = false
      last_file = file

      file_group << "<tr><td class=\"lineno\"><a href=\"txmt://open?url=file://#{file}&line=#{line}\">#{line}</a></td>"

      converted = begin
        syntax_converter ? syntax_converter.convert(context =~ /^(.+?)\s*$/ ? $1 : context) : context
      rescue Exception => e
        "TOKENIZING ERROR: #{context} <!-- \n#{e.message}\n#{e.backtrace.join("\n")}\n -->"
      end
      file_group << "<td class=\"ruby\">#{converted}</td></tr>\n"

      file_group
    end
  }.join

  html << '</table></body></html>'

  begin
    FileUtils.touch(@temp_file)
  rescue Errno::EACCES => e
    puts %(

    Looks like there's a permission error for the file #{@temp_file}.  The grepmate gem expects to have write access to this file.

    To fix, please do:
      sudo chmod 0666 #{@temp_file}
      
    )
    raise e
  end
  File.open(@temp_file, 'w') { |f| f.write(html) }

  system("open #{@temp_file}")
  
end