|
| 1 | +# encoding: utf-8 |
| 2 | +Shoes.setup do |
| 3 | + gem 'redcarpet' |
| 4 | + gem 'hackety_hack-lessons', '1.1.1' |
| 5 | +end |
| 6 | +require 'redcarpet' |
| 7 | +require 'hackety_hack/lessons' |
| 8 | + |
| 9 | +class Renderer < Redcarpet::Render::Base |
| 10 | + def initialize(app) |
| 11 | + super() |
| 12 | + @app = app |
| 13 | + @args = [] |
| 14 | + end |
| 15 | + |
| 16 | + def header(text, level) |
| 17 | + case level |
| 18 | + when 1 |
| 19 | + @app.instance_eval { para text, :size => "x-large" } |
| 20 | + when 2 |
| 21 | + @app.instance_eval { para text, :size => "large" } |
| 22 | + when 3 |
| 23 | + @app.instance_eval { para text } |
| 24 | + end |
| 25 | + '' |
| 26 | + end |
| 27 | + |
| 28 | + def image(path, title, alt_text) |
| 29 | + on_click = if alt_text.nil? || alt_text.empty? |
| 30 | + Proc.new {} |
| 31 | + else |
| 32 | + Proc.new { alert(alt_text) } |
| 33 | + end |
| 34 | + |
| 35 | + @app.instance_eval { image("http://hackety.com#{path}", {}, &on_click) } |
| 36 | + '' |
| 37 | + end |
| 38 | + |
| 39 | + |
| 40 | + def emphasis(text) |
| 41 | + store @app.em(text) |
| 42 | + end |
| 43 | + |
| 44 | + def double_emphasis(text) |
| 45 | + store @app.strong(text) |
| 46 | + end |
| 47 | + |
| 48 | + def link(link, title, content) |
| 49 | + store @app.link(content, :click => link) |
| 50 | + end |
| 51 | + |
| 52 | + def codespan(code) |
| 53 | + store @app.code(code) |
| 54 | + end |
| 55 | + |
| 56 | + def store(shoes_bit) |
| 57 | + @args << shoes_bit |
| 58 | + '[-]' |
| 59 | + end |
| 60 | + |
| 61 | + def paragraph(text) |
| 62 | + para_bits = interpolate(text, @args) |
| 63 | + |
| 64 | + @app.instance_eval { para *para_bits, :font => "TakaoGothic" } |
| 65 | + @args.clear |
| 66 | + '' |
| 67 | + end |
| 68 | + |
| 69 | + # The markdown string "I'd _love_ a cupcake!" comes to us looking like this: |
| 70 | + # "Hello, I'd [-] a cupcake!" |
| 71 | + # ...and @args looks like this: |
| 72 | + # [Shoes::Em] |
| 73 | + # #interpolate turns them into this: |
| 74 | + # ["Hello, I'd ", Shoes::Em, " a cupcake!"] |
| 75 | + # These are the args that'll be passed to Shoes#para. |
| 76 | + # It also turns "_I_ like _chocolate!_" into |
| 77 | + # [Shoes::Em, " like ", Shoes::Em] |
| 78 | + def interpolate(text, args) |
| 79 | + results = [] |
| 80 | + while text.include?('[-]') |
| 81 | + head, text = *text.split('[-]', 2) |
| 82 | + results << head << args.shift |
| 83 | + end |
| 84 | + results << text |
| 85 | + results << args unless args.empty? |
| 86 | + |
| 87 | + return results.compact |
| 88 | + end |
| 89 | +end |
| 90 | + |
| 91 | +Shoes.app do |
| 92 | + renderer = Renderer.new(self) |
| 93 | + markdown = Redcarpet::Markdown.new(renderer, fenced_code_blocks: true) |
| 94 | + stack do |
| 95 | + markdown.render(File.read("../../../content/an-introduction-to-ruby-with-hackety-hack.md", :encoding => "utf-8")) |
| 96 | + end |
| 97 | +end |
0 commit comments