code2gist 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code2gist (0.0.2)
4
+ code2gist (0.0.3)
5
5
 
6
6
  GEM
7
7
  specs:
data/README.md CHANGED
@@ -1,12 +1,17 @@
1
1
  # code2gist
2
2
 
3
- This library parses text and looks for this pattern:
3
+ This library looks for code blocks in your text and uploads them to
4
+ http://gist.github.com. Then, it can either give you the link to it or
5
+ replace your code blocks inline for their respective gist.github.com
6
+ links.
7
+
8
+ The code must follow this pattern:
4
9
 
5
10
  ```filename.extension
6
11
  some code
7
12
  ```
8
13
 
9
- It uploads all matches to http://gist.github.com and returns the link to it. The filename and extension are optional — it'll assume it's an untitled plain text file if you don't specify what it is.
14
+ The filename and extension are optional — it'll assume it's an untitled plain text file if you don't specify what it is.
10
15
 
11
16
  ## Usage
12
17
 
@@ -15,26 +20,23 @@ It's very simple:
15
20
  require 'code2gist'
16
21
  Code2Gist.upload(your_text, "example description")
17
22
 
18
- ### Options
19
- It supports a couple of boolean options:
23
+ Or:
20
24
 
21
- - `:embed` (default: `false`)
22
- - `:html` (default: `false`)
25
+ new_text = Code2Gist.replace(your_text, "another example description")
23
26
 
24
- Quick example:
27
+ ### Options
28
+ When replacing, there is one option available: `:html` (default:
29
+ `false`).
25
30
 
26
- Code2Gist.upload(your_markdown_text, "another description", :embed => true, :html => true)
31
+ Quick example:
27
32
 
28
- If you pass `:embed => true`to it, it will replace all code blocks with
29
- links to the appropriate file at http://gist.github.com. If you also
30
- pass `:html => true`, it will replace all code blocks with embeded
31
- gists.
33
+ Code2Gist.replace(your_markdown_text, "yad", :html => true)
32
34
 
33
- This is specially useful if your using markdown/textile and would like
35
+ This is specially useful if you're using markdown/textile and would like
34
36
  to have all your code blocks in a gist and embeded in your HTML. For
35
37
  example:
36
38
 
37
- html = Markdown.new(Code2Gist.upload(text, "Article code snippets", :embed => true, :html => true)).to_html
39
+ html = Markdown.new(Code2Gist.replace(text, "Code snippets from article X", :html => true)).to_html
38
40
 
39
41
  ### I want to be the owner of the gist!
40
42
 
@@ -48,7 +50,15 @@ gists:
48
50
 
49
51
  Use `code2gist`from the command-line. Just run:
50
52
 
51
- code2gist file "optional description"
53
+ $ code2gist upload file "optional description"
54
+
55
+ Or:
56
+
57
+ $ code2gist replace file "optional description"
58
+
59
+ Or even:
60
+
61
+ $ code2gist replace file "optional description" --substitute # this will change your file in place!
52
62
 
53
63
  It supports all options. Check `code2gist --help` to see how to use them
54
64
  properly.
@@ -77,7 +87,7 @@ properly.
77
87
 
78
88
  Code2Gist.upload(code, "my description) # => https://gist.github.com/1157214
79
89
 
80
- Code2Gist.upload(code, "another description", :embed => true) # =>
90
+ Code2Gist.replace(code, "another description") # =>
81
91
  # This is just a regular `document`
82
92
  #
83
93
  # It can have code!
@@ -89,7 +99,7 @@ properly.
89
99
  #
90
100
  # The end
91
101
 
92
- Code2Gist.upload(code, "yet another description", :embed => true, :html => true) # =>
102
+ Code2Gist.replace(code, "yet another description", :html => true) # =>
93
103
  # This is just a regular `document`
94
104
  #
95
105
  # It can have code!
data/bin/code2gist CHANGED
@@ -5,16 +5,19 @@ require "code2gist"
5
5
 
6
6
  options = {}
7
7
  OptionParser.new do |opt|
8
- opt.banner = "Usage: code2gist file [description] [OPTS]"
9
- opt.on('-e', '--embed', 'Embed links to the code', "Default: false") { |e| options[:embed] = e }
10
- opt.on('-h', '--html', 'Assume it\'s HTML. Use this in combination with --embed.', "Default: false") { |h| options[:html] = h }
8
+ opt.banner = "Usage: code2gist [upload|replace] file [description] [OPTS]"
9
+ opt.on('-h', '--html', 'Assume it\'s HTML when replacing', "Default: false") { |h| options[:html] = h }
10
+ opt.on('-s', '--substitute', 'Replace the contents of the original file', "Default: false") { |s| options[:substitute] = s }
11
11
  opt.on('-l', '--login LOGIN', String, 'Your GitHub login (to be the owner of the gist)') { |l| options[:login] = l }
12
12
  opt.on('-t', '--token TOKEN', String, 'Your GitHub api token (to be the owner of the gist)') { |t| options[:token] = t }
13
13
  opt.parse!(ARGV)
14
14
  end
15
15
 
16
- path = ARGV[0]
17
- description = ARGV[1] || ""
16
+ action = ARGV[0]
17
+ $stderr.puts "You must specify a valid action: upload or replace. `code2gist --help` for more information." unless ["upload", "replace"].include?(action)
18
+
19
+ path = ARGV[1]
20
+ description = ARGV[2] || ""
18
21
 
19
22
  if path.nil? || !File.exists?(path)
20
23
  $stderr.puts "You must specify a valid file to read from"
@@ -23,10 +26,22 @@ end
23
26
 
24
27
  login = options.delete(:login)
25
28
  token = options.delete(:token)
29
+ substitute = options.delete(:substitute)
26
30
 
27
31
  if login && options
28
32
  Code2Gist::Config.github_login = login
29
33
  Code2Gist::Config.github_token = token
30
34
  end
31
35
 
32
- puts Code2Gist.upload(File.read(path), description, options)
36
+ if action == "upload"
37
+ puts Code2Gist.upload(File.read(path), description)
38
+ elsif action == "replace"
39
+ new_text = Code2Gist.replace(File.read(path), description, options)
40
+
41
+ if substitute
42
+ File.open(path, 'w') { |f| f.write(new_text) }
43
+ else
44
+ puts new_text
45
+ end
46
+ end
47
+
data/code2gist.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "code2gist"
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Gonçalo Silva"]
8
8
  s.email = ["goncalossilva@gmail.com"]
data/lib/code2gist.rb CHANGED
@@ -4,6 +4,8 @@ require "json"
4
4
 
5
5
  module Code2Gist
6
6
  extend self
7
+
8
+ CODE_REGEX = /```(\w+\.\w+)?[^\n]*\n(.*?)```/m
7
9
 
8
10
  module Config
9
11
  def self.github_login
@@ -20,28 +22,29 @@ module Code2Gist
20
22
  end
21
23
  end
22
24
 
23
- def upload(text, description = nil, opts = {})
24
- options = {:embed => false, :html => false}.merge(opts)
25
-
26
- code_regex = /```(\w+\.\w+)?[^\n]*\n(.*?)```/m
25
+ def upload(text, description = nil)
27
26
  new_text = name_nameless_code_blocks(text)
28
27
 
29
- code_blocks = Hash[*new_text.scan(code_regex).flatten]
30
-
28
+ code_blocks = Hash[*new_text.scan(CODE_REGEX).flatten]
29
+
31
30
  if code_blocks.empty?
32
- return "No code blocks found"
31
+ return nil
33
32
  end
34
33
 
35
- gist_url = get_gist(code_blocks, description)
34
+ get_gist(code_blocks, description)
35
+ end
36
36
 
37
- if options[:embed]
38
- if options[:html]
39
- new_text.gsub(code_regex, "<script src=\"#{gist_url}.js?file=\\1\"></script>")
40
- else
41
- new_text.gsub(code_regex, "#{gist_url}?file=\\1")
42
- end
37
+ def replace(text, description = nil, opts = {})
38
+ options = {:html => false}.merge(opts)
39
+
40
+ new_text = name_nameless_code_blocks(text)
41
+
42
+ gist_url = upload(new_text, description)
43
+
44
+ if options[:html]
45
+ new_text.gsub(CODE_REGEX, "<script src=\"#{gist_url}.js?file=\\1\"></script>")
43
46
  else
44
- gist_url
47
+ new_text.gsub(CODE_REGEX, "#{gist_url}?file=\\1")
45
48
  end
46
49
  end
47
50
 
@@ -32,10 +32,10 @@ class Code2GistTest < Test::Unit::TestCase
32
32
 
33
33
  assert Code2Gist.upload(code, "test description") =~ /https:\/\/gist\.github\.com\/\d+/
34
34
 
35
- assert Code2Gist.upload(code, "another description", :embed => true) =~ /https:\/\/gist.github.com\/\d+\?file=\w+\.\w+/
35
+ assert Code2Gist.replace(code, "another description") =~ /https:\/\/gist.github.com\/\d+\?file=\w+\.\w+/
36
36
 
37
- assert Code2Gist.upload(code, "yet another description", :embed => true, :html => true) =~ /<script src="https:\/\/gist.github.com\/\d+\.js\?file=\w+\.\w+"><\/script>/
37
+ assert Code2Gist.replace(code, "yet another description", :html => true) =~ /<script src="https:\/\/gist.github.com\/\d+\.js\?file=\w+\.\w+"><\/script>/
38
38
 
39
- assert Code2Gist.upload(nocode, "no code!") == "No code blocks found"
39
+ assert Code2Gist.replace(nocode, "no code!") == nocode
40
40
  end
41
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code2gist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -42,7 +42,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
42
42
  version: '0'
43
43
  segments:
44
44
  - 0
45
- hash: 4231852110147090976
45
+ hash: 3355642002282232388
46
46
  required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  none: false
48
48
  requirements: