code2gist 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +13 -0
- data/LICENSE +20 -0
- data/README.md +94 -0
- data/Rakefile +31 -0
- data/code2gist.gemspec +31 -0
- data/lib/code2gist.rb +83 -0
- data/test/code2gist_test.rb +33 -0
- metadata +54 -0
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/Gemfile.lock
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Gonçalo Silva
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# code2gist
|
2
|
+
|
3
|
+
This library parses text and looks for this pattern:
|
4
|
+
|
5
|
+
```filename.extension
|
6
|
+
some code
|
7
|
+
```
|
8
|
+
|
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.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
It's very simple:
|
14
|
+
|
15
|
+
require 'code2gist'
|
16
|
+
Code2Gist.upload(your_text, "example description")
|
17
|
+
|
18
|
+
### Options
|
19
|
+
It supports a couple of boolean options:
|
20
|
+
|
21
|
+
- `:embed` (default: `false`)
|
22
|
+
- `:html` (default: `false`)
|
23
|
+
|
24
|
+
Quick example:
|
25
|
+
|
26
|
+
Code2Gist.upload(your_markdown_text, "another description", :embed => true, :html => true)
|
27
|
+
|
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.
|
32
|
+
|
33
|
+
This is specially useful if your using markdown/textile and would like
|
34
|
+
to have all your code blocks in a gist and embeded in your HTML. For
|
35
|
+
example:
|
36
|
+
|
37
|
+
html = Markdown.new(Code2Gist.upload(text, "Article code snippets", :embed => true, :html => true)).to_html
|
38
|
+
|
39
|
+
### I want to be the owner of the gist!
|
40
|
+
|
41
|
+
It's okay, just specify your username and API token before uploading any
|
42
|
+
gists:
|
43
|
+
|
44
|
+
Code2Gist::Config.github_login = "your_login"
|
45
|
+
Code2Gist::Config.github_token = "your_token"
|
46
|
+
|
47
|
+
## Real example
|
48
|
+
|
49
|
+
code = <<-eoc
|
50
|
+
This is just a regular `document`
|
51
|
+
|
52
|
+
It can have code!
|
53
|
+
|
54
|
+
```description.rb
|
55
|
+
def what
|
56
|
+
puts "This is ruby code!"
|
57
|
+
`ls`
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
And small snippets *also* work:
|
62
|
+
```
|
63
|
+
Not sure what this is
|
64
|
+
```
|
65
|
+
|
66
|
+
The end
|
67
|
+
eoc
|
68
|
+
|
69
|
+
Code2Gist.upload(code, "my description) # => https://gist.github.com/1157214
|
70
|
+
|
71
|
+
Code2Gist.upload(code, "another description", :embed => true) # =>
|
72
|
+
# This is just a regular `document`
|
73
|
+
#
|
74
|
+
# It can have code!
|
75
|
+
#
|
76
|
+
# https://gist.github.com/1157215?file=description.rb
|
77
|
+
#
|
78
|
+
# And small snippets *also* work:
|
79
|
+
# https://gist.github.com/1157215?file=untitled_1.txt
|
80
|
+
#
|
81
|
+
# The end
|
82
|
+
|
83
|
+
Code2Gist.upload(code, "yet another description", :embed => true, :html => true) # =>
|
84
|
+
# This is just a regular `document`
|
85
|
+
#
|
86
|
+
# It can have code!
|
87
|
+
#
|
88
|
+
# <script src="https://gist.github.com/1157216.js?file=description.rb"></script>
|
89
|
+
#
|
90
|
+
# And small snippets *also* work:
|
91
|
+
# <script src="https://gist.github.com/1157216.js?file=untitled_1.txt"></script>
|
92
|
+
#
|
93
|
+
# The end
|
94
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
begin
|
2
|
+
require "bundler"
|
3
|
+
Bundler.setup
|
4
|
+
rescue LoadError
|
5
|
+
$stderr.puts "You need to have Bundler installed to be able build this gem."
|
6
|
+
end
|
7
|
+
|
8
|
+
gemspec = eval(File.read(Dir["*.gemspec"].first))
|
9
|
+
|
10
|
+
|
11
|
+
desc "Validate the gemspec"
|
12
|
+
task :gemspec do
|
13
|
+
gemspec.validate
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Build gem locally"
|
17
|
+
task :build => :gemspec do
|
18
|
+
system "gem build #{gemspec.name}.gemspec"
|
19
|
+
FileUtils.mkdir_p "pkg"
|
20
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Install gem locally"
|
24
|
+
task :install => :build do
|
25
|
+
system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Clean automatically generated files"
|
29
|
+
task :clean do
|
30
|
+
FileUtils.rm_rf "pkg"
|
31
|
+
end
|
data/code2gist.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "code2gist"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ["Gonçalo Silva"]
|
8
|
+
s.email = ["goncalossilva@gmail.com"]
|
9
|
+
s.homepage = "http://github.com/goncalossilva/code2gist"
|
10
|
+
s.summary = "Parse text files (markdown/textile/whatever) for code blocks and gist them"
|
11
|
+
s.description = "Parse text files (markdown/textile/whatever) for code blocks and gist them"
|
12
|
+
s.rubyforge_project = s.name
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
|
16
|
+
# If you have runtime dependencies, add them here
|
17
|
+
# s.add_runtime_dependency "other", "~> 1.2"
|
18
|
+
|
19
|
+
# If you have development dependencies, add them here
|
20
|
+
# s.add_development_dependency "another", "= 0.9"
|
21
|
+
|
22
|
+
# The list of files to be contained in the gem
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
# s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
25
|
+
# s.extensions = `git ls-files ext/extconf.rb`.split("\n")
|
26
|
+
|
27
|
+
s.require_path = 'lib'
|
28
|
+
|
29
|
+
# For C extensions
|
30
|
+
# s.extensions = "ext/extconf.rb"
|
31
|
+
end
|
data/lib/code2gist.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "uri"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module Code2Gist
|
6
|
+
extend self
|
7
|
+
|
8
|
+
module Config
|
9
|
+
def self.github_login
|
10
|
+
@@github_login ||= nil
|
11
|
+
end
|
12
|
+
def self.github_login=(github_login)
|
13
|
+
@@github_login = github_login
|
14
|
+
end
|
15
|
+
def self.github_token
|
16
|
+
@@github_token ||= nil
|
17
|
+
end
|
18
|
+
def self.github_token=(github_token)
|
19
|
+
@@github_token = github_token
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def upload(text, description = nil, opts = {})
|
24
|
+
options = {:embed => false, :html => false}.merge(opts)
|
25
|
+
|
26
|
+
code_regex = /```(\w+\.\w+)?[^\n]*\n(.*?)```/m
|
27
|
+
new_text = name_nameless_code_blocks(text)
|
28
|
+
|
29
|
+
gist_url = get_gist(Hash[*new_text.scan(code_regex).flatten], description)
|
30
|
+
|
31
|
+
if options[:embed]
|
32
|
+
if options[:html]
|
33
|
+
new_text.gsub(code_regex, "<script src=\"#{gist_url}.js?file=\\1\"></script>")
|
34
|
+
else
|
35
|
+
new_text.gsub(code_regex, "#{gist_url}?file=\\1")
|
36
|
+
end
|
37
|
+
else
|
38
|
+
gist_url
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def name_nameless_code_blocks(text)
|
45
|
+
nameless_blocks = 0
|
46
|
+
new_text = text.gsub(/```(\w+\.\w+)?/).with_index do |match, index|
|
47
|
+
if index%2 == 0 && match.size == 3
|
48
|
+
nameless_blocks += 1;
|
49
|
+
"```untitled_#{nameless_blocks}.txt"
|
50
|
+
else
|
51
|
+
match
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
new_text
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_gist(data, description)
|
59
|
+
post_data = {}
|
60
|
+
data.each_with_index do |(filename, content), index|
|
61
|
+
post_data.merge!("files[#{filename}]" => content)
|
62
|
+
end
|
63
|
+
|
64
|
+
post_data.merge!(
|
65
|
+
"login" => Code2Gist::Config.github_login,
|
66
|
+
"token" => Code2Gist::Config.github_token
|
67
|
+
) unless Code2Gist::Config.github_login.nil? || Code2Gist::Config.github_token.nil?
|
68
|
+
|
69
|
+
post_data.merge!("description" => description)
|
70
|
+
|
71
|
+
result = Net::HTTP.post_form(URI.parse("http://gist.github.com/api/v1/json/new"), post_data)
|
72
|
+
parsed_json = JSON(result.body)
|
73
|
+
repo = nil
|
74
|
+
parsed_json['gists'].each do |key, val|
|
75
|
+
key.each do |k, v|
|
76
|
+
if "#{k}" == 'repo'
|
77
|
+
repo = "#{v}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
return "https://gist.github.com/#{repo}"
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
$:.unshift "#{File.dirname(__FILE__)}/../lib/"
|
2
|
+
require "test/unit"
|
3
|
+
require "code2gist"
|
4
|
+
|
5
|
+
class Code2GistTest < Test::Unit::TestCase
|
6
|
+
def test_empty_gist
|
7
|
+
code = <<-eoc
|
8
|
+
This is just a regular `document`
|
9
|
+
|
10
|
+
It can have code!
|
11
|
+
|
12
|
+
```description.rb
|
13
|
+
def what
|
14
|
+
puts "This is ruby code!"
|
15
|
+
`ls`
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
And small snippets *also* work:
|
20
|
+
```
|
21
|
+
Not sure what this is
|
22
|
+
```
|
23
|
+
|
24
|
+
The end
|
25
|
+
eoc
|
26
|
+
|
27
|
+
puts Code2Gist.upload(code, "test description")# =~ /https:\/\/gist\.github\.com\/\d+/
|
28
|
+
|
29
|
+
puts Code2Gist.upload(code, "another description", :embed => true)# =~ /https:\/\/gist.github.com\/\d+\?file=\w+\.\w+/
|
30
|
+
|
31
|
+
puts Code2Gist.upload(code, "yet another description", :embed => true, :html => true)# =~ /<script src="https:\/\/gist.github.com\/\d+\.js\?file=\w+\.\w+"><\/script>/
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: code2gist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gonçalo Silva
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-19 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Parse text files (markdown/textile/whatever) for code blocks and gist
|
15
|
+
them
|
16
|
+
email:
|
17
|
+
- goncalossilva@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- code2gist.gemspec
|
28
|
+
- lib/code2gist.rb
|
29
|
+
- test/code2gist_test.rb
|
30
|
+
homepage: http://github.com/goncalossilva/code2gist
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.3.6
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project: code2gist
|
50
|
+
rubygems_version: 1.8.6
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Parse text files (markdown/textile/whatever) for code blocks and gist them
|
54
|
+
test_files: []
|