poppins 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -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 poppins.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Aaron Massey
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.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Poppins
2
+
3
+ Poppins formats Markdown files to ensure that reference links are
4
+ numbered and appear in numerical order. Currently, this is a seriously
5
+ basic tool. Still, it does what I needed, and I hope to expand it.
6
+ Poppins was inspired by [Dr. Drang's script][1] for tidying reference
7
+ links.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'poppins'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install poppins
22
+
23
+ ## Usage
24
+
25
+ Poppins: a Markdown cleaner and formatter
26
+ Usage: poppins [options] <filename>
27
+
28
+ Examples:
29
+ poppins ./README.md
30
+ poppins --output 'output.md' ./input.md
31
+
32
+ Options:
33
+ -o, --output [FILE] Write the clean, formatted Markdown to FILE.
34
+ -h, --help Show this message
35
+ -v, --version Show version
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
44
+
45
+
46
+ [1]: http://www.leancrew.com/all-this/2012/09/tidying-markdown-reference-links/
47
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/poppins ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require_relative '../lib/poppins'
5
+
6
+ @output = nil
7
+
8
+ op = OptionParser.new do |opts|
9
+ opts.banner = "Poppins: a Markdown cleaner and formatter"
10
+ opts.define_head "Usage: poppins [options] <filename>"
11
+ opts.separator ""
12
+ opts.separator "Examples:"
13
+ opts.separator " poppins ./README.md"
14
+ opts.separator " poppins --output 'output.md' ./input.md"
15
+ opts.separator ""
16
+ opts.separator "Options:"
17
+
18
+ opts.on("-o", "--output [FILE]", "Write the clean, formatted Markdown to FILE.") do |file|
19
+ @output = file
20
+ end
21
+
22
+ opts.on_tail("-h", "--help", "Show this message") do
23
+ puts opts
24
+ exit
25
+ end
26
+
27
+ opts.on_tail("-v", "--version", "Show version") do
28
+ puts Poppins::VERSION
29
+ exit
30
+ end
31
+ end
32
+
33
+ op.parse!
34
+
35
+ input = ARGV.shift.to_s.strip
36
+
37
+ if input.empty?
38
+ puts op
39
+ exit 1
40
+ end
41
+
42
+ poppins = Poppins::Document.new input, @output
43
+
44
+ poppins.clean_and_format
@@ -0,0 +1,85 @@
1
+ module Poppins
2
+ class Document
3
+
4
+ def initialize(input, output=nil)
5
+ @input = File.open(input, 'r').readlines.join
6
+ @output = output
7
+
8
+ # RegEx for matching reference links in the text. (Avoid footnotes!)
9
+ @links = /\[([^\]]+)\]\[([^^\]]+)\]/
10
+ # RegEx for matching labels for reference links. (Avoid footnotes!)
11
+ @labels = /^\[([^^\]]+)\]:\s+(.+)$/
12
+ end
13
+
14
+ ##
15
+ # Returns an array of the links found in the document in order.
16
+ def links
17
+ @input.scan(@links)
18
+ end
19
+
20
+ def labels
21
+ Hash[@input.scan(@labels)]
22
+ end
23
+
24
+ def order_of_first_appearance
25
+ order = []
26
+ links.each do |l|
27
+ order.push(l[1]) unless order.include?(l[1])
28
+ end
29
+
30
+ order
31
+ end
32
+
33
+ def ordinal_references
34
+ references = []
35
+ order_of_first_appearance.each_with_index do |o, i|
36
+ references.push("[#{i+1}]: #{labels[o]}")
37
+ end
38
+
39
+ references
40
+ end
41
+
42
+ ##
43
+ # Returns an array of the link text and the new reference number when
44
+ # passed links based on the old reference numbering system
45
+ def get_replacement(string)
46
+ md = string.match(@links)
47
+ link_text = md[1].to_s
48
+ reference_number = (order_of_first_appearance.index(md[2]) + 1).to_s
49
+
50
+ return [link_text, reference_number]
51
+ end
52
+
53
+
54
+ ##
55
+ # Produces the clean, formatted version of the Markdown with new
56
+ # reference numbers.
57
+ def clean_and_format
58
+ # Remove old references (TOOD: Need to remove blank lines resulting from
59
+ # this.)
60
+ result = @input.gsub(@labels, '')
61
+ #
62
+ # Add new references
63
+ ordinal_references.each do |r|
64
+ result += r.to_s + "\n"
65
+ end
66
+
67
+ # Replace old reference numbers with the new ones
68
+ result = result.gsub(@links) do |m|
69
+ replacement = get_replacement(m)
70
+ "[#{replacement[0]}][#{replacement[1]}]"
71
+ end
72
+
73
+ # output the result
74
+ # puts result
75
+
76
+ if @output
77
+ File.open(@output, 'w') do |f|
78
+ f.write(result)
79
+ end
80
+ else
81
+ puts result
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module Poppins
2
+ VERSION = "0.0.1"
3
+ end
data/lib/poppins.rb ADDED
@@ -0,0 +1,6 @@
1
+ require_relative "poppins/version"
2
+ require_relative "poppins/document"
3
+
4
+ module Poppins
5
+ # Your code goes here...
6
+ end
data/poppins.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/poppins/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Aaron Massey"]
6
+ gem.email = ["akmassey@gatech.edu"]
7
+ gem.description = %q{Poppins is a basic formatter for Markdown files.}
8
+ gem.summary = %q{Poppins formats Markdown files to ensure that reference links are numbered and appear in numerical order.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "poppins"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Poppins::VERSION
17
+
18
+
19
+ gem.add_development_dependency 'rspec'
20
+ end
@@ -0,0 +1,26 @@
1
+ Species and their hybrids, How simply are these facts! How
2
+ strange that the pollen of each But we may thus have
3
+ [succeeded][2] in selecting so many exceptions to this rule.
4
+ but the species would not all the same species living on the
5
+ White Mountains, in the arctic regions of that large island.
6
+ The exceptions which are now large, and triumphant, and
7
+ which are known to every naturalist: scarcely a single
8
+ [character][4] in the descendants of the Glacial period,
9
+ would have been of use to the plants, have been accumulated
10
+ and if, in both regions.
11
+
12
+ Supposed to be extinct and unknown, form. We have seen that
13
+ it yields readily, when subjected as [under confinement][3],
14
+ to new and improved varieties will have been much
15
+ compressed, we may assume that the species, which are
16
+ already present in the ordinary spines serve as a prehensile
17
+ or snapping apparatus. Thus every gradation, from animals
18
+ with true lungs are descended from a marsupial form), "and
19
+ if so, there can be followed by which viscid matter, such as
20
+ that of making [slaves][1]. Let it be remembered that
21
+ selection may be extended--to the stigma of.
22
+
23
+ [1]: http://daringfireball.net/markdown/
24
+ [2]: http://www.google.com/
25
+ [3]: http://docs.python.org/library/index.html
26
+ [4]: http://www.kungfugrippe.com/
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Poppins::Document do
4
+
5
+ before do
6
+ @file = File.dirname(__FILE__) + '/../data/sample.md'
7
+ end
8
+
9
+ it "should be instantiatable" do
10
+ Poppins::Document.new(@file).should_not be_nil
11
+ end
12
+
13
+ it "should accurately identify links in the text" do
14
+ Poppins::Document.new(@file).links.should == [["succeeded", "2"], ["character", "4"], ["under confinement", "3"], ["slaves", "1"]]
15
+ end
16
+
17
+ it "should accuaretely identify a hash of the references" do
18
+ Poppins::Document.new(@file).labels.should == {"1"=>"http://daringfireball.net/markdown/", "2"=>"http://www.google.com/", "3"=>"http://docs.python.org/library/index.html", "4"=>"http://www.kungfugrippe.com/"}
19
+ end
20
+
21
+ it "should correctly idenfity the order of the links as they appear in the text" do
22
+ Poppins::Document.new(@file).order_of_first_appearance.should == ["2", "4", "3", "1"]
23
+ end
24
+
25
+ it "should correctly generate a new list of references" do
26
+ Poppins::Document.new(@file).ordinal_references.should == ["[1]: http://www.google.com/", "[2]: http://www.kungfugrippe.com/", "[3]: http://docs.python.org/library/index.html", "[4]: http://daringfireball.net/markdown/"]
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Poppins do
4
+ it "should have a VERSION" do
5
+ Poppins::VERSION.should == "0.0.1"
6
+ end
7
+ end
8
+
@@ -0,0 +1 @@
1
+ require 'poppins'
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poppins
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Massey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ description: Poppins is a basic formatter for Markdown files.
31
+ email:
32
+ - akmassey@gatech.edu
33
+ executables:
34
+ - poppins
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - bin/poppins
44
+ - lib/poppins.rb
45
+ - lib/poppins/document.rb
46
+ - lib/poppins/version.rb
47
+ - poppins.gemspec
48
+ - spec/data/sample.md
49
+ - spec/lib/document_spec.rb
50
+ - spec/poppins_spec.rb
51
+ - spec/spec_helper.rb
52
+ homepage: ''
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ segments:
65
+ - 0
66
+ hash: -1494761217159726976
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ segments:
74
+ - 0
75
+ hash: -1494761217159726976
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 1.8.23
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Poppins formats Markdown files to ensure that reference links are numbered
82
+ and appear in numerical order.
83
+ test_files:
84
+ - spec/data/sample.md
85
+ - spec/lib/document_spec.rb
86
+ - spec/poppins_spec.rb
87
+ - spec/spec_helper.rb