Module: Code2Gist

Extended by:
Code2Gist
Included in:
Code2Gist
Defined in:
lib/code2gist.rb

Defined Under Namespace

Modules: Config

Constant Summary collapse

CODE_REGEX =
/```((?:\s*)\w+\.\w+)?[^\n]*\n(.*?)```/m

Instance Method Summary collapse

Instance Method Details

#replace(text, description = nil, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/code2gist.rb', line 37

def replace(text, description = nil, opts = {})
  options = {:html => false, :anonymous => false}.merge(opts)

  new_text = name_nameless_code_blocks(text)

  gist_url = upload(new_text, description, options[:anonymous])

  if options[:html]
    new_text.gsub(CODE_REGEX, "<script src=\"#{gist_url}.js?file=\\1\"></script>")
  else
    new_text.gsub(CODE_REGEX, "#{gist_url}?file=\\1")
  end
end

#upload(text, description = nil, anonymous = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/code2gist.rb', line 25

def upload(text, description = nil, anonymous = false)
  new_text = name_nameless_code_blocks(text)

  code_blocks = Hash[*new_text.scan(CODE_REGEX).flatten]

  if code_blocks.empty?
    return nil
  end

  get_gist(code_blocks, description, anonymous)
end