markdown_section_numbering 0.9.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0bd695f991b84fd57e45afaca3e03f4135b2931c
4
+ data.tar.gz: ecfd26b2b8bb24f953d373507fe38ac46701e5ba
5
+ SHA512:
6
+ metadata.gz: e6860f7d55dbcbcec2a16207f7725d861d3d6f2576b80cdde66e6355543d7163fa35bd0e781d648b112b92fa42255f8c761601d4ab616ab6ad338cb95d71ff6a
7
+ data.tar.gz: dbb514c7dc2c1f85a353bcdf0061157355d87606b6e1caf4c3d74e8c4024bedaf8c65d60b9ac7487e5f9c348103ab6b21a40e0c9ee47a10c1687210be144806d
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format doc
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development, :test do
4
+ gem 'rspec'
5
+ end
6
+
7
+ # Specify your gem's dependencies in markdown_section_numbering.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 SET_ME_LOCALLY
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,61 @@
1
+ # MarkdownSectionNumbering
2
+
3
+ `MarkdownSectionNumbering` adds section numbers to markdown headings written in "#" style.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'markdown_section_numbering'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install markdown_section_numbering
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require "markdown_section_numbering"
23
+
24
+ markdown = <<MD
25
+ # a
26
+ ## b
27
+
28
+ foo bar
29
+
30
+ ## c
31
+
32
+ - list
33
+ - list
34
+ - list
35
+ MD
36
+
37
+ puts MarkdownSectionNumbering.convert(markdown)
38
+ ```
39
+
40
+ This code produces the following results.
41
+
42
+ ```
43
+ # 1 a
44
+ ## 1.1 b
45
+
46
+ foo bar
47
+
48
+ ## 1.2 c
49
+
50
+ - list
51
+ - list
52
+ - list
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/[my-github-username]/markdown_section_numbering/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,19 @@
1
+ require "rubygems"
2
+
3
+ $:.unshift File.expand_path("../lib", File.dirname(__FILE__))
4
+ require "markdown_section_numbering"
5
+
6
+ markdown = <<MD
7
+ # a
8
+ ## b
9
+
10
+ foo bar
11
+
12
+ ## c
13
+
14
+ - list
15
+ - list
16
+ - list
17
+ MD
18
+
19
+ puts MarkdownSectionNumbering.convert(markdown)
@@ -0,0 +1,55 @@
1
+ require "markdown_section_numbering/version"
2
+
3
+ class MarkdownSectionNumbering
4
+ class << self
5
+
6
+ def config(max_level)
7
+ @max_level = max_level
8
+ end
9
+
10
+ def convert(input)
11
+ @section_index = [-1] + [0] * max_level
12
+
13
+ input.lines.map do |line|
14
+ convert_line(line)
15
+ end.join("\n")+"\n"
16
+ end
17
+
18
+ private
19
+
20
+ def max_level
21
+ @max_level || 10
22
+ end
23
+
24
+ def convert_line(line)
25
+ line.chomp!
26
+ if not match = line.match(/^#+/)
27
+ line
28
+ else
29
+ level = match[0].length
30
+ add_section_number(line, level)
31
+ end
32
+ end
33
+
34
+ def add_section_number(line, target_level)
35
+ return line if target_level > max_level
36
+
37
+ header_sign = "#" * target_level
38
+ regex = Regexp.new("^#{header_sign}\s*(.+)")
39
+ match = line.match(regex)
40
+
41
+ number = calc_section_number(target_level)
42
+
43
+ "#{header_sign} #{number} #{match[1]}"
44
+ end
45
+
46
+ def calc_section_number(target_level)
47
+ @section_index[target_level] += 1
48
+ (target_level+1 .. max_level).each do |child_level|
49
+ @section_index[child_level] = 0
50
+ end
51
+ (1..target_level).map {|level| @section_index[level].to_s }.join(".")
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ class MarkdownSectionNumbering
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'markdown_section_numbering/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "markdown_section_numbering"
8
+ spec.version = MarkdownSectionNumbering::VERSION
9
+ spec.authors = ["xoyip"]
10
+ spec.email = ["xoyip@piyox.info"]
11
+ spec.summary = %q{Add section numbers to markdown headings.}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/xoyip/markdown_section_numbering"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ end
data/spec/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
@@ -0,0 +1,23 @@
1
+ # a
2
+ ## b
3
+ ### c
4
+ ## d
5
+ ### e
6
+ ### f
7
+
8
+ # g
9
+ ## h
10
+
11
+ ### i
12
+ ### j
13
+ ## k
14
+ ### l
15
+ ### m
16
+ ### n
17
+ ## o
18
+ ### p
19
+ ### q
20
+
21
+ # r
22
+
23
+ # s
@@ -0,0 +1,23 @@
1
+ #a
2
+ ##b
3
+ ### c
4
+ ## d
5
+ ### e
6
+ ### f
7
+
8
+ # g
9
+ ## h
10
+
11
+ ### i
12
+ ### j
13
+ ## k
14
+ ### l
15
+ ### m
16
+ ### n
17
+ ## o
18
+ ###p
19
+ ###q
20
+
21
+ #r
22
+
23
+ #s
@@ -0,0 +1,23 @@
1
+ # 1 a
2
+ ## 1.1 b
3
+ ### 1.1.1 c
4
+ ## 1.2 d
5
+ ### 1.2.1 e
6
+ ### 1.2.2 f
7
+
8
+ # 2 g
9
+ ## 2.1 h
10
+
11
+ ### 2.1.1 i
12
+ ### 2.1.2 j
13
+ ## 2.2 k
14
+ ### 2.2.1 l
15
+ ### 2.2.2 m
16
+ ### 2.2.3 n
17
+ ## 2.3 o
18
+ ### 2.3.1 p
19
+ ### 2.3.2 q
20
+
21
+ # 3 r
22
+
23
+ # 4 s
@@ -0,0 +1,23 @@
1
+ # 1 a
2
+ ## 1.1 b
3
+ ### c
4
+ ## 1.2 d
5
+ ### e
6
+ ### f
7
+
8
+ # 2 g
9
+ ## 2.1 h
10
+
11
+ ### i
12
+ ### j
13
+ ## 2.2 k
14
+ ### l
15
+ ### m
16
+ ### n
17
+ ## 2.3 o
18
+ ### p
19
+ ### q
20
+
21
+ # 3 r
22
+
23
+ # 4 s
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ describe MarkdownSectionNumbering do
4
+ specify "convert correctly" do
5
+ input = fixture("input1.md")
6
+ expected = fixture("output1.md")
7
+ output = MarkdownSectionNumbering.convert(input)
8
+ expect(output).to eq(expected)
9
+ end
10
+
11
+ specify "ignore spaces after header sign('#')" do
12
+ input = fixture("input2.md")
13
+ expected = fixture("output1.md")
14
+ output = MarkdownSectionNumbering.convert(input)
15
+ expect(output).to eq(expected)
16
+ end
17
+
18
+ specify "when max level is set to 2, headings deeper than 2 are not converted" do
19
+ input = fixture("input1.md")
20
+ expected = fixture("output2.md")
21
+ MarkdownSectionNumbering.config(2)
22
+ output = MarkdownSectionNumbering.convert(input)
23
+ expect(output).to eq(expected)
24
+ end
25
+
26
+ end
@@ -0,0 +1,12 @@
1
+ require "markdown_section_numbering"
2
+
3
+ RSpec.configure do |config|
4
+ end
5
+
6
+ def fixture_path
7
+ File.expand_path("../fixtures", __FILE__)
8
+ end
9
+
10
+ def fixture(file)
11
+ open(File.join(fixture_path, file)).read
12
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown_section_numbering
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - xoyip
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email:
43
+ - xoyip@piyox.info
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - example/example.rb
55
+ - lib/markdown_section_numbering.rb
56
+ - lib/markdown_section_numbering/version.rb
57
+ - markdown_section_numbering.gemspec
58
+ - spec/.rspec
59
+ - spec/fixtures/input1.md
60
+ - spec/fixtures/input2.md
61
+ - spec/fixtures/output1.md
62
+ - spec/fixtures/output2.md
63
+ - spec/markdown_section_numbering_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: https://github.com/xoyip/markdown_section_numbering
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.0
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Add section numbers to markdown headings.
89
+ test_files:
90
+ - spec/.rspec
91
+ - spec/fixtures/input1.md
92
+ - spec/fixtures/input2.md
93
+ - spec/fixtures/output1.md
94
+ - spec/fixtures/output2.md
95
+ - spec/markdown_section_numbering_spec.rb
96
+ - spec/spec_helper.rb