Module: GitHub::Markup

Extended by:
Markup
Included in:
Markup
Defined in:
lib/github/markup.rb,
lib/github/markup/rdoc.rb,
lib/github/markup/version.rb

Defined Under Namespace

Classes: RDoc

Constant Summary collapse

Version =
VERSION = '0.5.3'
@@markups =
{}

Instance Method Summary collapse

Instance Method Details

#add_markup(regexp, &block) ⇒ Object



54
55
56
# File 'lib/github/markup.rb', line 54

def add_markup(regexp, &block)
  @@markups[regexp] = block
end

#can_render?(filename) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/github/markup.rb', line 58

def can_render?(filename)
  !!renderer(filename)
end

#command(command, regexp, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/github/markup.rb', line 29

def command(command, regexp, &block)
  command = command.to_s

  if File.exists?(file = File.dirname(__FILE__) + "/commands/#{command}")
    command = file
  end

  add_markup(regexp) do |content|
    rendered = execute(command, content)
    rendered = rendered.to_s.empty? ? content : rendered

    if block && block.arity == 2
      # If the block takes two arguments, pass new content and old
      # content.
      block.call(rendered, content)
    elsif block
      # One argument is just the new content.
      block.call(rendered)
    else
      # No block? No problem!
      rendered
    end
  end
end

#execute(command, target) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/github/markup.rb', line 71

def execute(command, target)
  out = ''
  Open3.popen3(command) do |stdin, stdout, _|
    stdin.puts target
    stdin.close
    out = stdout.read
  end
  out.gsub("\r", '')
rescue Errno::EPIPE
  ""
end

#markup(file, pattern, &block) ⇒ Object



22
23
24
25
26
27
# File 'lib/github/markup.rb', line 22

def markup(file, pattern, &block)
  require file.to_s
  add_markup(pattern, &block)
rescue LoadError
  nil
end

#render(filename, content = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/github/markup.rb', line 12

def render(filename, content = nil)
  content ||= File.read(filename)

  if proc = renderer(filename)
    proc[content]
  else
    content
  end
end

#renderer(filename) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/github/markup.rb', line 62

def renderer(filename)
  @@markups.each do |key, value|
    if Regexp.compile("\\.(#{key})$") =~ filename
      return value
    end
  end
  nil
end