tocify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tocify.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 pyro2927
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # Tocify
2
+
3
+ Tocify aims to create a table of contents for your markdown files. By default the generated ToC will be dumped to STDOUT. Add the `-i` or `--inject` flag to put the table of contents back into your file.
4
+
5
+ ## Table of Contents
6
+ * [Installation](#installation)
7
+ * [Usage](#usage)
8
+ * [Sample Output](#sample-output)
9
+ * [Manual Insertion](#manual-insertion)
10
+ * [OS X](#os-x)
11
+ * [Linux](#linux)
12
+ * [Contributing](#contributing)
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'tocify'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install tocify
27
+
28
+ ## Usage
29
+
30
+ By default tocify will check for a file named `README.md` in the current directory.
31
+
32
+ tocify
33
+
34
+ Otherwise you can pass in a filename as a parameter.
35
+
36
+ tocify OTHERFILE.md
37
+
38
+ To automatically insert the ToC into your README, run
39
+
40
+ tocify -i
41
+
42
+ ## Sample Output
43
+
44
+ When running `tocify` on this file, you get:
45
+
46
+ ## Table of Contents
47
+ * [Installation](#installation)
48
+ * [Usage](#usage)
49
+ * [Sample Output](#sample-output)
50
+ * [Manual Insertion](#manual-insertion)
51
+ * [OS X](#os-x)
52
+ * [Linux](#linux)
53
+ * [Contributing](#contributing)
54
+
55
+ ## Manual Insertion
56
+
57
+ If you want to manually insert the generated ToC you can. An easy way is to pipe the output of `tocify` into your paste bufffer.
58
+
59
+ ### OS X
60
+
61
+ tocify | pbcopy
62
+
63
+ ### Linux
64
+
65
+ tocify | xclip
66
+
67
+ _Requires xclip to be installed_
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create new Pull Request
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+ require 'bundler/version'
3
+ require 'tocify/version'
4
+ require "bundler/gem_tasks"
5
+
6
+ task :build do
7
+ system 'gem build tocify.gemspec'
8
+ end
9
+
10
+ task :install do
11
+ system "gem install tocify-#{Tocify::VERSION}.gem"
12
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tocify'
4
+ puts Tocify.filter_args(ARGV)
5
+
@@ -0,0 +1,66 @@
1
+ require "tocify/version"
2
+
3
+ module Tocify
4
+ def self.filter_args(args)
5
+ # see if we should inject this ToC back into the file
6
+ if args.include?("--inject") || args.include?("-i")
7
+ inject = true
8
+ args.delete("--inject")
9
+ args.delete("-i")
10
+ else
11
+ inject = false
12
+ end
13
+
14
+ # pass in our file name if we have it
15
+ if args.length > 0
16
+ Tocify.run(args[0], inject)
17
+ else
18
+ Tocify.run(nil, inject)
19
+ end
20
+ end
21
+ def self.run(file_name = nil, inject = false)
22
+ # sanity checks
23
+ if file_name.nil?
24
+ if File.exist?('README.md')
25
+ file_name = 'README.md'
26
+ else
27
+ puts "You must provide a file name"
28
+ return
29
+ end
30
+ elsif File.exist?(file_name) == false
31
+ puts "The file '#{file_name}' doesn't exist"
32
+ return
33
+ end
34
+ toc = Tocify.generate(file_name)
35
+ if inject
36
+ Tocify.insert(toc, file_name)
37
+ end
38
+ toc
39
+ end
40
+
41
+ def self.generate(file_name)
42
+ toc = Array.new
43
+ toc << "## Table of Contents"
44
+
45
+ File.open(file_name).each do |line|
46
+ if line.start_with?("##") && line.start_with?(toc[0]) == false
47
+ # count number of lines, figure out how many leading paces we need
48
+ # two spaces for each hash after the second
49
+ spaces = " " * (line.split(" ").first.count("#") - 2) * 4
50
+ title = line.split(" ")
51
+ title.shift
52
+ # links are the title, downcased, and spaces replaced with hyphens
53
+ # also make sure to remove non-alphanumeric characters
54
+ link = title.map {|w| w.downcase } .join("-").delete('^a-zA-Z0-9\-')
55
+ toc << "#{spaces}* [#{title.join(" ")}](##{link})"
56
+ end
57
+ end
58
+ toc.join("\n")
59
+ end
60
+
61
+ def self.insert(toc, file_name)
62
+ content = File.read(file_name)
63
+ content.sub!(/(\n## Table of Contents.+?|)\n##/im, "\n" + toc + "\n\n##")
64
+ File.write(file_name, content)
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Tocify
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tocify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tocify"
8
+ spec.version = Tocify::VERSION
9
+ spec.authors = ["pyro2927"]
10
+ spec.email = ["joseph@pintozzi.com"]
11
+ spec.description = "Tocify is an easy way to generate a table of contents for your markdown file"
12
+ spec.summary = ""
13
+ spec.homepage = "https://github.com/pyro2927/tocify"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tocify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - pyro2927
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Tocify is an easy way to generate a table of contents for your markdown
47
+ file
48
+ email:
49
+ - joseph@pintozzi.com
50
+ executables:
51
+ - tocify
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - bin/tocify
61
+ - lib/tocify.rb
62
+ - lib/tocify/version.rb
63
+ - tocify.gemspec
64
+ homepage: https://github.com/pyro2927/tocify
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: ''
89
+ test_files: []