excerpt 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/README.md +71 -0
- data/Rakefile +12 -0
- data/excerpt.gemspec +15 -0
- data/lib/excerpt.rb +24 -0
- data/test/fixtures/articles_controller.rb +15 -0
- data/test/test_helper.rb +2 -0
- data/test/unit/excerpt_test.rb +33 -0
- metadata +56 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Excerpt
|
2
|
+
|
3
|
+
Extracts portion of some input content like String, file, URI, ...
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Excerpt.new('application.html').around_line(12, radius: 2)
|
8
|
+
|
9
|
+
from some app gives you this code:
|
10
|
+
|
11
|
+
= csrf_meta_tags
|
12
|
+
%body
|
13
|
+
#navigation
|
14
|
+
= render '/layouts/application/nav'
|
15
|
+
.container-fixed
|
16
|
+
|
17
|
+
You can also request a excerpt from URI:
|
18
|
+
|
19
|
+
# radius is omitted here, default value is 4
|
20
|
+
Excerpt.new('https://github.com/rails/rails/blob/master/railties/lib/rails.rb').around_line(26)
|
21
|
+
|
22
|
+
gives you:
|
23
|
+
|
24
|
+
module Rails
|
25
|
+
autoload :Info, 'rails/info'
|
26
|
+
autoload :InfoController, 'rails/info_controller'
|
27
|
+
autoload :Queueing, 'rails/queueing'
|
28
|
+
|
29
|
+
class << self
|
30
|
+
def application
|
31
|
+
@application ||= nil
|
32
|
+
end
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
Add this line to your application's Gemfile:
|
37
|
+
|
38
|
+
gem 'excerpt'
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
Or install it yourself as:
|
45
|
+
|
46
|
+
$ gem install excerpt
|
47
|
+
|
48
|
+
## Licence
|
49
|
+
|
50
|
+
Copyright (c) 2012 Dejan Simic
|
51
|
+
|
52
|
+
MIT License
|
53
|
+
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
55
|
+
a copy of this software and associated documentation files (the
|
56
|
+
"Software"), to deal in the Software without restriction, including
|
57
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
58
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
59
|
+
permit persons to whom the Software is furnished to do so, subject to
|
60
|
+
the following conditions:
|
61
|
+
|
62
|
+
The above copyright notice and this permission notice shall be
|
63
|
+
included in all copies or substantial portions of the Software.
|
64
|
+
|
65
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
66
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
67
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
68
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
69
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
70
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
71
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
desc 'Default: run tests'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test AutoHtml'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.pattern = 'test/**/*_test.rb'
|
11
|
+
t.libs = ['lib', 'test']
|
12
|
+
end
|
data/excerpt.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |gem|
|
3
|
+
gem.authors = ["Dejan Simic"]
|
4
|
+
gem.email = ["desimic@gmail.com"]
|
5
|
+
gem.description = %q{Extracts portion of some input content like String, file, uri, ...}
|
6
|
+
gem.summary = %q{Extracts portion of some input content like String, file, uri, ...}
|
7
|
+
gem.homepage = "https://github.com/dejan/excerpt"
|
8
|
+
|
9
|
+
gem.files = `git ls-files`.split($\)
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
11
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
|
+
gem.name = "excerpt"
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
gem.version = "0.0.1"
|
15
|
+
end
|
data/lib/excerpt.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
class Excerpt
|
4
|
+
|
5
|
+
# input - anything open-uri reads
|
6
|
+
def initialize(input)
|
7
|
+
@lines = open(input).read.split("\n")
|
8
|
+
end
|
9
|
+
|
10
|
+
# options
|
11
|
+
# :radius - number of lines before/after the specified
|
12
|
+
# line (1st arg) that will also be included
|
13
|
+
# in the excerpt
|
14
|
+
def around_line(line_number, options = {})
|
15
|
+
default_options = {:radius => 4}
|
16
|
+
apply_options = default_options.merge(options)
|
17
|
+
radius = apply_options[:radius]
|
18
|
+
|
19
|
+
index = line_number - 1
|
20
|
+
start_on = [0, index - radius].max
|
21
|
+
end_on = [index + radius, @lines.size - 1].min
|
22
|
+
@lines[start_on..end_on] * "\n"
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class ArticlesController < ApplicationController
|
2
|
+
before_filter :admin_required, except: %w(show index)
|
3
|
+
|
4
|
+
def index
|
5
|
+
@articles = Article.where('published_on <= ?', Date.today).order('published_on desc')
|
6
|
+
respond_to do |format|
|
7
|
+
format.html
|
8
|
+
format.xml
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def new
|
13
|
+
@article = Article.new
|
14
|
+
end
|
15
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Excerpt do
|
4
|
+
describe '#excerpt' do
|
5
|
+
before do
|
6
|
+
fixtures_dir = File.dirname(__FILE__) + '/../fixtures'
|
7
|
+
file = open(fixtures_dir + '/articles_controller.rb')
|
8
|
+
@excerpt = Excerpt.new(file)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'extracts text around specified line' do
|
12
|
+
expected = <<CODE
|
13
|
+
class ArticlesController < ApplicationController
|
14
|
+
before_filter :admin_required, except: %w(show index)
|
15
|
+
CODE
|
16
|
+
assert_equal expected, @excerpt.around_line(2, :radius => 1)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'extracts text around specified line with a default radius of 4' do
|
20
|
+
expected = <<CODE
|
21
|
+
|
22
|
+
def index
|
23
|
+
@articles = Article.where('published_on <= ?', Date.today).order('published_on desc')
|
24
|
+
respond_to do |format|
|
25
|
+
format.html
|
26
|
+
format.xml
|
27
|
+
end
|
28
|
+
end
|
29
|
+
CODE
|
30
|
+
assert_equal expected, @excerpt.around_line(7)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: excerpt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dejan Simic
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Extracts portion of some input content like String, file, uri, ...
|
15
|
+
email:
|
16
|
+
- desimic@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- Gemfile
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- excerpt.gemspec
|
25
|
+
- lib/excerpt.rb
|
26
|
+
- test/fixtures/articles_controller.rb
|
27
|
+
- test/test_helper.rb
|
28
|
+
- test/unit/excerpt_test.rb
|
29
|
+
homepage: https://github.com/dejan/excerpt
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.24
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Extracts portion of some input content like String, file, uri, ...
|
53
|
+
test_files:
|
54
|
+
- test/fixtures/articles_controller.rb
|
55
|
+
- test/test_helper.rb
|
56
|
+
- test/unit/excerpt_test.rb
|