Skip to content

Commit 8ef396c

Browse files
committed
Implemented parsing with hpricot
1 parent b6548fc commit 8ef396c

File tree

4 files changed

+79
-14
lines changed

4 files changed

+79
-14
lines changed

plugin/vim-rspec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require "rubygems"
2+
require "hpricot"
3+
4+
doc = Hpricot(STDIN.read)
5+
h1 = (doc/"h1")
6+
classes = {"spec passed"=>"+","spec failed"=>"-","spec not_implemented"=>"#"}
7+
8+
puts "* #{h1.inner_html}"
9+
10+
stats = (doc/"script").select {|script| script.innerHTML =~ /duration|totals/ }.map {|script| script.inner_html.scan(/".*"/).first.gsub(/<\/?strong>/,"") }
11+
stats.each do |stat|
12+
puts "* #{stat.gsub(/\"/,'')}"
13+
end
14+
puts "* Parsed with Hpricot (http://wiki.github.com/why/hpricot)"
15+
puts " "
16+
17+
(doc/"div[@class='example_group']").each do |example|
18+
puts "[#{(example/"dl/dt").inner_html}]"
19+
(example/"dl/dd").each do |dd|
20+
txt = (dd/"span:first").inner_html
21+
puts "#{classes[dd[:class]]} #{txt}"
22+
next if dd[:class]!="spec failed"
23+
failure = (dd/"div[@class='failure']")
24+
msg = (failure/"div[@class='message']/pre").inner_html
25+
back = (failure/"div[@class='backtrace']/pre").inner_html
26+
ruby = (failure/"pre[@class='ruby']/code").inner_html.scan(/(<span class="linenum">)(\d+)(<\/span>)([^<]+)/).map {|elem| " "+elem[1]+": "+elem[3].chomp+"\n"}.join
27+
puts " #{msg}"
28+
puts " #{back}"
29+
puts ruby
30+
end
31+
puts " "
32+
end

plugin/vim-rspec.vim

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
"
22
" Vim Rspec
3-
" Last change: March 3 2009
4-
" Version> 0.0.2
3+
" Last change: March 4 2009
4+
" Version> 0.0.4
55
" Maintainer: Eustáquio 'TaQ' Rangel
66
" License: GPL
77
" URL: git://github.com/taq/vim-rspec
88
"
99
" Script to run the spec command inside Vim
1010
" To install, unpack the files on your ~/.vim directory and source it
1111
"
12+
let s:xsltproc_cmd = ""
13+
let s:grep_cmd = ""
14+
let s:hpricot_cmd = ""
15+
let s:xslt = 0
16+
let s:hpricot = 0
17+
1218
function! s:find_xslt()
1319
return system("xsltproc --version | head -n1")
1420
endfunction
@@ -17,6 +23,10 @@ function! s:find_grep()
1723
return system("grep --version | head -n1")
1824
endfunction
1925

26+
function! s:find_hpricot()
27+
return system("gem search -i hpricot")
28+
endfunction
29+
2030
function! s:error_msg(msg)
2131
echohl ErrorMsg
2232
echo a:msg
@@ -30,22 +40,39 @@ function! s:notice_msg(msg)
3040
endfunction
3141

3242
function! s:RunSpecMain(type)
33-
let l:xsltproc_cmd = s:find_xslt()
34-
if match(l:xsltproc_cmd,'\d')<0
35-
call s:error_msg("You need xsltproc to run this script.")
36-
return
43+
if len(s:xsltproc_cmd)<1
44+
let s:xsltproc_cmd = s:find_xslt()
45+
let s:xslt = match(s:xsltproc_cmd,'\d')>=0
46+
end
47+
if len(s:hpricot_cmd)<1
48+
let s:hpricot_cmd = s:find_hpricot()
49+
let s:hpricot = match(s:hpricot_cmd,'true')>=0
3750
end
38-
let l:grep_cmd = s:find_grep()
39-
if match(l:grep_cmd,'\d')<0
40-
call s:error_msg("You need grep to run this script.")
51+
if !s:hpricot && !s:xslt
52+
call s:error_msg("You need the hpricot gem or xsltproc to run this script.")
4153
return
4254
end
55+
if len(s:grep_cmd)<1
56+
let s:grep_cmd = s:find_grep()
57+
if match(s:grep_cmd,'\d')<0
58+
call s:error_msg("You need grep to run this script.")
59+
return
60+
end
61+
end
4362
let l:bufn = bufname("%")
4463

64+
" filters
65+
let l:xsl = expand("~/").".vim/plugin/vim-rspec.xsl"
66+
let l:rubys = expand("~/").".vim/plugin/vim-rspec.rb"
67+
68+
" hpricot gets the priority
69+
let l:type = s:hpricot ? "hpricot" : "xsltproc"
70+
let l:filter = s:hpricot ? "ruby ".l:rubys : "xsltproc --novalid --html ".l:xsl." - "
71+
4572
" run just the current file
4673
if a:type=="file"
4774
if match(l:bufn,'_spec.rb')>=0
48-
call s:notice_msg("Running spec on the current file ...")
75+
call s:notice_msg("Running spec on the current file with ".l:type." ...")
4976
let l:spec = "spec -f h ".l:bufn
5077
else
5178
call s:error_msg("Seems ".l:bufn." is not a *_spec.rb file")
@@ -54,7 +81,7 @@ function! s:RunSpecMain(type)
5481
else
5582
let l:dir = expand("%:p:h")
5683
if isdirectory(l:dir."/spec")>0
57-
call s:notice_msg("Running spec on the spec directory ...")
84+
call s:notice_msg("Running spec on the spec directory with ".l:type." ...")
5885
else
5986
" try to find a spec directory on the current path
6087
let l:tokens = split(l:dir,"/")
@@ -68,7 +95,7 @@ function! s:RunSpecMain(type)
6895
end
6996
endfor
7097
if len(l:dir)>0
71-
call s:notice_msg("Running spec on the spec directory found (".l:dir.") ...")
98+
call s:notice_msg("Running spec with ".l:type." on the spec directory found (".l:dir.") ...")
7299
else
73100
call s:error_msg("No ".l:dir."/spec directory found")
74101
return
@@ -82,8 +109,7 @@ function! s:RunSpecMain(type)
82109
end
83110

84111
" run the spec command
85-
let l:xsl = expand("~/").".vim/plugin/vim-rspec.xsl"
86-
let s:cmd = l:spec." | xsltproc --novalid --html ".l:xsl." - 2> /dev/null | grep \"^[-\+\[\\# ]\""
112+
let s:cmd = l:spec." | ".l:filter." 2> /dev/null | grep \"^[-\+\[\\#\\* ]\""
87113
echo
88114

89115
" put the result on a new buffer

plugin/vim-rspec.xsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
<xsl:output method="text"/>
44

55
<xsl:template match="/">
6+
<xsl:text>* Rspec Results&#10;</xsl:text>
7+
<xsl:text>* Parsed with xsltproc (http://www.xmlsoft.org/XSLT/xsltproc2.html)&#10;</xsl:text>
8+
<xsl:text> &#10;</xsl:text>
69
<xsl:apply-templates select="html/body/div[@class='rspec-report']/div[@class='results']"/>
710
</xsl:template>
811

syntax/vim-rspec.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
syntax match rspecHeader /^*/
12
syntax match rspecTitle /^\[.\+/
23
syntax match rspecOk /^+.\+/
34
syntax match rspecError /^-.\+/
45
syntax match rspecErrorDetail /^ \w.\+/
56
syntax match rspecErrorURL /^ \/.\+/
67
syntax match rspecNotImplemented /^#.\+/
8+
syntax match rspecCode /^ \d\+:/
79

10+
highlight link rspecHeader Type
811
highlight link rspecTitle Identifier
912
highlight link rspecOk Tag
1013
highlight link rspecError Error
1114
highlight link rspecErrorDetail Constant
1215
highlight link rspecErrorURL PreProc
1316
highlight link rspecNotImplemented Todo
17+
highlight link rspecCode Type

0 commit comments

Comments
 (0)