Module: Cheat::Views

Defined in:
lib/cheat/site.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.feedObject



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/cheat/site.rb', line 425

def self.feed
  xml = Builder::XmlMarkup.new(:indent => 2)

  xml.instruct!
  xml.feed "xmlns"=>"http://www.w3.org/2005/Atom" do

    xml.title "Recently Updated Cheat Sheets"
    xml.id URL + '/'
    xml.link "rel" => "self", "href" => FEED

    sheets = Cheat::Models::Sheet.sort_by { |s| -s.updated_at }.first(20)
    xml.updated sheets.first.updated_at.xmlschema

    sheets.each do |sheet|
      xml.entry do
        xml.id URL + '/s/' + sheet.title
        xml.title sheet.title
        xml.author { xml.name "An Anonymous Cheater" }
        xml.updated sheet.updated_at.xmlschema
        xml.link "rel" => "alternate", "href" => URL + '/s/' + sheet.title
        xml.summary "A cheat sheet about #{sheet.title}.  Run it: `$ cheat #{sheet.title}'"
        xml.content 'type' => 'html' do
          xml.text! sheet.body.gsub("\n", '<br/>').gsub("\r", '')
        end
      end
    end
  end
end

Instance Method Details

#_clickyObject



231
232
233
# File 'lib/cheat/site.rb', line 231

def _clicky
  text '<script src="http://getclicky.com/1070.js"> </script><noscript><img height=0 width=0 src="http://getclicky.com/1070ns.gif"></noscript>'
end


473
474
475
476
477
478
479
480
481
# File 'lib/cheat/site.rb', line 473

def _footer
  text "Powered by "
  a 'Camping', :href => "http://code.whytheluckystiff.net/camping"
  text ", "
  a 'Mongrel', :href => "http://mongrel.rubyforge.org/"
  text " and, to a lesser extent, "
  a 'Err the Blog', :href => "http://errtheblog.com/"
  text "."
end

#_formObject



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/cheat/site.rb', line 350

def _form
  if @error
    p.error {
      strong "HEY!  "
      text "Something is wrong!  You can't give your cheat sheet the same name
            as another, alphanumeric titles only, and you need to make sure
            you filled in all (two) of the fields.  Okay?"
    }
  end
  form :method => 'post', :action => R(Write, @sheet.title) do
    p {
      p {
        text 'Cheat Sheet Title: '
        input :value => @sheet.title, :name => 'sheet_title', :size => 30,
              :type => 'text'
        small " [ no_spaces_alphanumeric_only ]"
      }
      p {
        text 'Cheat Sheet:'
        br
        textarea @sheet.body, :name => 'sheet_body', :cols => 80, :rows => 30
        unless @cookies[:passed]
          random = rand(10_000)
          br
          img :src => "http://captchator.com/captcha/image/#{random}"
          input :name => 'chunky', :type => 'hidden', :value => random
          input :name => 'bacon', :size => 10, :type => 'text'
        end
      }
    }
    p "Your cheat sheet will be editable (fixable) by anyone.  Each cheat
       sheet is essentially a wiki page.  It may also be used by millions of
       people for reference purposes from the comfort of their command line.
       If this is okay with you, please save."
    input :value => "Save the Damn Thing!", :name => "save", :type => 'submit'
  end
end

#_sideObject



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/cheat/site.rb', line 454

def _side
  text '( '
  a 'add new', :href => R(Add)
  text ' | '
  a 'see all', :href => R(Browse)
  text ' )'
  ul {
    li { strong "updated sheets" }
    li do
      a :href => FEED do
        img(:border => 0, :alt => "Recently Updated Cheat Sheets Feed", :src => "http://errtheblog.com/images/feed.png")
      end
    end
    recent_sheets.each do |sheet|
      li { sheet_link sheet.title }
    end
  }
end

#_styleObject



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/cheat/site.rb', line 483

def _style
  bg    = "#fff"
  h1    = "#4fa3da"
  link  = h1
  hover = "#f65077"
  dash  = hover
  version = "#fcf095"
  style :type => "text/css" do
    text %[
      body { font-family: verdana, sans-serif; background-color: #{bg};
             line-height: 20px; }
      a:link, a:visited { color: #{link}; }
      a:hover { text-decoration: none; color: #{hover}; }
      div.header { border-bottom: 1px dashed #{dash}; }
      code.header { margin-left: 30px; font-weight: bold;
                    background-color: #{version}; }
      h1 { font-size: 5em; margin: 0; padding-left: 30px; color: #{h1};
           clear: both; font-weight: bold; letter-spacing: -5px; }
      h1 a { text-decoration: none; }
      div.main    { float: left; width: 100%; }
      div.content { float: left; width: 70%; padding: 15px 0 15px 30px;
                    line-height: 20px; }
      div.side    { float: left; padding: 10px; text-align: right;
                    width: 20%; }
      div.footer  { text-align: center; border-top: 1px dashed #{dash};
                    padding-top: 10px; font-size: small; }
      div.sheet { font-size: .8em; line-height: 17px; padding: 5px;
                  font-family: courier, fixed-width; background-color: #e8e8e8; }
      pre.sheet { line-height: 15px; }
      li { list-style: none; }
      div.version { background-color: #{version}; padding: 5px;
                     width: 450px; margin-top: 50px; }
      p.error { background-color: #{version}; padding: 5px; }
      div.clear    { clear: both; }
      div.clear_10 { clear: both; font-size: 10px; line-height: 10px; }
      textarea { font-family: courier; }
      code { background-color: #{version} }
      span.diff_cut { color: #f65077; }
      span.diff_add { color: #009933; }
      @media print {
        .side, .version, .footer { display: none; }
        div.content { width: 100%; }
        h1 a:link, h1 a:visited { color: #eee;}
        .header code { font-size: 18px; background: none; }
        div.header { border-bottom: none; }
      }
    ].gsub(/(\s{2,})/, '').gsub("\n", '')
  end
end

#addObject



334
335
336
337
338
339
340
341
342
343
# File 'lib/cheat/site.rb', line 334

def add
  @page_title = "add"
  p {
    text "Thanks for wanting to add a cheat sheet.  If you need an example of
     the standard cheat sheet format, check out the "
     a "cheat", :href => R(Show, 'cheat')
     text " cheat sheet.  (There's really no standard format, though)."
   }
  _form
end

#browseObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/cheat/site.rb', line 300

def browse
  @page_title = "browse"
  p { "Wowzers, we've got <strong>#{@sheets.size}</strong> cheat sheets hereabouts." }
  ul {
    @sheets.each do |sheet|
      li { sheet_link sheet.title }
    end
  }
  p {
    text "Are we missing a cheat sheet?  Why don't you do the whole world a favor and "
    a "add it", :href => R(Add)
    text " yourself!"
  }
end

#diffObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/cheat/site.rb', line 283

def diff
  @page_title = @sheet.title
  @sheet_title = @sheet.title
  pre.sheet { color_diff(h(@diffed)) if @diffed }
  div.version {
    text ". o 0 ("
    if @old_sheet.version > 1
      a "diff previous", :href => R(Diff, @sheet.title, @old_sheet.version - 1)
      text " | "
    end
    a "history", :href => R(History, @sheet.title)
    text " | "
    a "current", :href => R(Show, @sheet.title)
    text " )"
  }
end

#editObject



345
346
347
348
# File 'lib/cheat/site.rb', line 345

def edit
  @page_title = "edit"
  _form
end

#errorObject



235
236
237
238
239
240
# File 'lib/cheat/site.rb', line 235

def error
  @page_title = "error"
  p "An error:"
  code.version @error
  p ":("
end

#historyObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/cheat/site.rb', line 315

def history
  @page_title  = "history"
  @sheet_title = @sheets.first.title
  h2 @sheets.first.title
  ul {
    @sheets.each_with_index do |sheet, i|
      li {
        a "version #{sheet.version}", :href => R(Show, sheet.title, sheet.version)
        text " - created "
        text last_updated(sheet)
        text " ago"
        strong " (current)" if i.zero?
        text " "
        a "(diff to current)", :href => R(Diff, sheet.title, sheet.version) if i.nonzero?
      }
    end
  }
end

#indexObject



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/cheat/site.rb', line 388

def index
  p {
    text "Welcome.  You've reached the central repository for "
    strong "cheat"
    text ", the RubyGem which puts Ruby-centric cheat sheets right into your
          terminal.  The inaugural blog entry "
    a "is here", :href => "http://errtheblog.com/post/23"
    text "."
  }
  p "Get started:"
  code "$ gem install cheat"
  br
  code "$ cheat strftime"
  p "A magnificent cheat sheet for Ruby's strftime method will be printed to
     your terminal."
  p "To get some help on cheat itself:"
  code "$ cheat cheat"
  p "How meta."
  p {
    text "Cheat sheets are basically wiki pages accessible from the command
          line.  You can "
    a 'browse', :href => R(Browse)
    text ', '
    a 'add', :href => R(Add)
    text ', or '
    a 'edit', :href => R(Edit, 'cheat')
    text ' cheat sheets.  Try to keep them concise.  For a style guide, check
          out the '
    a 'cheat', :href => R(Edit, 'cheat')
    text ' cheat sheet.'
  }
  p "To access a cheat sheet, simply pass the program the desired sheet's
     name:"
  code "$ cheat <sheet name>"
  p
end

#layoutObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/cheat/site.rb', line 208

def layout
  html {
    head {
      _style
      link :href => FEED, :rel => "alternate", :title => "Recently Updated Cheat Sheets", :type => "application/atom+xml"
      title @page_title ? "$ cheat #{@page_title}" : "$ command line ruby cheat sheets"
    }
    body {
      div.main {
        div.header {
          h1 { logo_link 'cheat sheets.' }
          code.header @sheet_title ? "$ cheat #{@sheet_title}" : "$ command line ruby cheat sheets"
          }
        div.content { self << yield }
        div.side { _side }
        div.clear { '' }
        div.footer { _footer }
      }
      _clicky
    }
  }
end

#showObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/cheat/site.rb', line 242

def show
  @page_title = @sheet.title
  @sheet_title = @sheet.title
  pre.sheet { text h(@sheet.body.wrap) }
  div.version {
    text "Version "
    strong sheet.version
    text ", updated "
    text last_updated(@sheet)
    text " ago.  "
    br
    text ". o 0 ( "
    if @sheet.version == current_sheet.version
      a "edit", :href => R(Edit, @sheet.title)
    end
    if @sheet.version > 1
      text " | "
      a "previous", :href => R(Show, @sheet.title, @sheet.version - 1)
    end
    text " | "
    a "history", :href => R(History, @sheet.title)
    unless @sheet.version == current_sheet.version
      text " | "
      a "revert to", :href => R(Edit, @sheet.title, @sheet.version)
      text " | "
      a "current", :href => R(Show, @sheet.title)
    end
    diff_version =
      if @sheet.version == current_sheet.version
        @sheet.version == 1 ? nil : @sheet.version - 1
      else
        @sheet.version
      end
    if diff_version
      text " | "
      a "diff", :href => R(Diff, @sheet.title, diff_version)
    end
    text " )"
   }
end