public
Description: Super simple wiki targeted at developers
Clone URL: git://github.com/citrusbyte/quiki.git
fixed validations on page to catch invalid syntax highlighters
benalavi (author)
Tue Jul 15 02:24:17 -0700 2008
commit  860b36114af0cafff8bf1e72345b67c373610857
tree    4838e95512f9706affd1a4b1dc0c2e69b2946dce
parent  e01e6842b360b84b3e80cedc14cd9654f6b77080
...
53
54
55
56
 
57
58
59
...
53
54
55
 
56
57
58
59
0
@@ -53,7 +53,7 @@ class PagesController < ApplicationController
0
       else
0
         format.html do
0
           flash[:error] = @page.errors.full_messages
0
- render :action => 'edit'
0
+ render :action => 'edit', :layout => 'pages/edit_layout.html.haml'
0
         end
0
         format.json { render :json => { :errors => @page.errors.full_messages }, :status => :unprocessable_entity }
0
       end
...
20
21
22
23
 
24
25
26
...
50
51
52
 
 
 
 
 
53
54
55
...
88
89
90
91
92
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
95
96
...
20
21
22
 
23
24
25
26
...
50
51
52
53
54
55
56
57
58
59
60
...
93
94
95
 
 
 
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
0
@@ -20,7 +20,7 @@ class Page < ActiveRecord::Base
0
   validates_exclusion_of :path, :in => %w( pages sections parsers )
0
   validates_associated :section, :allow_nil => true
0
 
0
- before_validation :render
0
+ #before_validation :render
0
   
0
   acts_as_versioned
0
   
0
@@ -50,6 +50,11 @@ class Page < ActiveRecord::Base
0
     end
0
   end
0
 
0
+ def validate
0
+ # TODO: decouple rendering and parser validations
0
+ render
0
+ end
0
+
0
   def parser
0
     self[:parser] || 'markdown'
0
   end
0
@@ -88,9 +93,21 @@ class Page < ActiveRecord::Base
0
       parts.each do |part|
0
         if part =~ /^\-:.*$/ # part is a code block
0
           syntax, code, check = part.scan(/^\-:(.*?)\n(.*\n)\-:(.*?)$/m)[0]
0
- code_blocks << (block = CodeBlock.new(syntax, code))
0
- body_parts << "<p class=\"code_stamp\"><span class=\"syntax\">#{syntax.humanize}</span><a href=\"/#{path}/code/#{code_blocks.length-1}\">View Source</a></p>"
0
- body_parts << block.highlight(CODE_THEME)
0
+ if syntax == check
0
+ code_blocks << (block = CodeBlock.new(syntax, code))
0
+ body_parts << "<p class=\"code_stamp\"><span class=\"syntax\">#{syntax.humanize}</span><a href=\"/#{path}/code/#{code_blocks.length-1}\">View Source</a></p>"
0
+ begin
0
+ body_parts << block.highlight(CODE_THEME)
0
+ rescue NoMethodError => e
0
+ if e.message =~ /nil\.parse/
0
+ self.errors.add(:body, "references unknown code highlighter '#{syntax}'")
0
+ else
0
+ raise e
0
+ end
0
+ end
0
+ else
0
+ self.errors.add(:body, "has mismatched code highlighter block ('#{syntax}' and '#{check}')")
0
+ end
0
         else
0
           body_parts << Page.render(part, parser)
0
         end
...
168
169
170
171
172
173
174
175
176
177
 
 
 
 
 
 
 
 
 
 
 
 
 
178
179
180
181
182
183
184
185
186
...
168
169
170
 
 
 
 
 
 
 
171
172
173
174
175
176
177
178
179
180
181
182
183
184
 
 
 
 
 
185
186
187
0
@@ -168,19 +168,20 @@ table.versions
0
     :background-color #afa
0
     
0
 // page edit ==================================================================
0
-body.pages.edit
0
- form.edit_page
0
- input#page_title
0
- :font-weight normal
0
- :font-size 2.0em
0
- :width 500px
0
- textarea
0
+body.pages
0
+ &.edit, &.update
0
+ form.edit_page
0
+ input#page_title
0
+ :font-weight normal
0
+ :font-size 2.0em
0
+ :width 500px
0
+ textarea
0
+ :margin-top 10px
0
+ :height 500px
0
+ :width 670px
0
+ #page_submit
0
+ :display block
0
       :margin-top 10px
0
- :height 500px
0
- :width 670px
0
- #page_submit
0
- :display block
0
- :margin-top 10px
0
 
0
 // page show ==================================================================
0
 body.pages.show
...
66
67
68
69
 
70
71
72
...
86
87
88
89
 
90
91
92
...
131
132
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
135
136
...
196
197
198
199
200
201
202
...
214
215
216
 
217
218
219
...
66
67
68
 
69
70
71
72
...
86
87
88
 
89
90
91
92
...
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
156
...
216
217
218
 
219
220
221
...
233
234
235
236
237
238
239
0
@@ -66,7 +66,7 @@ describe Page do
0
     
0
     describe "parsing" do
0
       before :each do
0
- @page = Page.new :title => 'foo', :body => '*foo bar*', :parser => 'markdown'
0
+ @page = Page.new :title => 'foo', :body => '*foo bar*'
0
       end
0
       
0
       it "should render the contents of the page" do
0
@@ -86,7 +86,7 @@ describe Page do
0
       end
0
 
0
       it "should render the contents of the page using html_parser" do
0
- @page.parser = nil
0
+ @page.parser = 'html'
0
         @page.save!
0
         @page.rendered.should eql('*foo bar*')
0
       end
0
@@ -131,6 +131,26 @@ describe Page do
0
         @page.rendered.should =~ /<em>bing bang boom<\/em>/
0
       end
0
       
0
+ describe "with invalid syntax highlighter block" do
0
+ before :each do
0
+ @page.body += [
0
+ "\n-:foo",
0
+ " foo",
0
+ "-:foo\n"
0
+ ].join("\n")
0
+ end
0
+
0
+ it "should not raise error with invalid syntax given" do
0
+ lambda {
0
+ @page.save
0
+ }.should_not raise_error
0
+ end
0
+
0
+ it "should not save the page with invalid syntax given" do
0
+ @page.save.should be_false
0
+ end
0
+ end
0
+
0
       describe "with multiple code blocks and text" do
0
         before :each do
0
         end
0
@@ -196,7 +216,6 @@ describe Page do
0
     
0
     describe "validating" do
0
       before :each do
0
- pending "don't really care about this yet..."
0
         # this is built in an array to avoid extra spacing which has meaning to
0
         # the parser
0
         body = [
0
@@ -214,6 +233,7 @@ describe Page do
0
       end
0
       
0
       it "should have an error on the body about mismatched tags" do
0
+ @page.save
0
         @page.errors.on(:body).should =~ /mismatched/
0
       end
0
     end

Comments

    No one has commented yet.