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 +1 -1
- data/README.md +27 -17
- data/bin/code2gist +21 -6
- data/code2gist.gemspec +1 -1
- data/lib/code2gist.rb +18 -15
- data/test/code2gist_test.rb +3 -3
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
# code2gist
|
2
2
|
|
3
|
-
This library
|
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
|
-
|
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
|
-
|
19
|
-
It supports a couple of boolean options:
|
23
|
+
Or:
|
20
24
|
|
21
|
-
|
22
|
-
- `:html` (default: `false`)
|
25
|
+
new_text = Code2Gist.replace(your_text, "another example description")
|
23
26
|
|
24
|
-
|
27
|
+
### Options
|
28
|
+
When replacing, there is one option available: `:html` (default:
|
29
|
+
`false`).
|
25
30
|
|
26
|
-
|
31
|
+
Quick example:
|
27
32
|
|
28
|
-
|
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
|
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.
|
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.
|
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.
|
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('-
|
10
|
-
opt.on('-
|
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
|
-
|
17
|
-
|
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
|
-
|
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
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
|
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(
|
30
|
-
|
28
|
+
code_blocks = Hash[*new_text.scan(CODE_REGEX).flatten]
|
29
|
+
|
31
30
|
if code_blocks.empty?
|
32
|
-
return
|
31
|
+
return nil
|
33
32
|
end
|
34
33
|
|
35
|
-
|
34
|
+
get_gist(code_blocks, description)
|
35
|
+
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
|
data/test/code2gist_test.rb
CHANGED
@@ -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.
|
35
|
+
assert Code2Gist.replace(code, "another description") =~ /https:\/\/gist.github.com\/\d+\?file=\w+\.\w+/
|
36
36
|
|
37
|
-
assert Code2Gist.
|
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.
|
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.
|
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:
|
45
|
+
hash: 3355642002282232388
|
46
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
47
|
none: false
|
48
48
|
requirements:
|