md_parser 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +2 -0
- data/lib/md_parser/hash_table_visitor.rb +13 -0
- data/lib/md_parser/table_parser.rb +41 -0
- data/lib/md_parser/text_table_visitor.rb +34 -0
- data/lib/md_parser/version.rb +3 -0
- data/lib/md_parser.rb +25 -0
- data/md_parser.gemspec +24 -0
- data/specs/md_parser_specs.rb +58 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ddcbf3017028ff39e00ee5cca22d76f13766b27f
|
4
|
+
data.tar.gz: 319845c9492d37aa62902d972628f4431b12a64a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5457de4265b7b762318783a7fce14c5a1e15aa260b3c7699a8ee3d9aa78e9d0f41301b3dd570087495f205b50b3735c3958e722a42973b81383c49a6c0004dd5
|
7
|
+
data.tar.gz: 7707c317220e6d0aecb840297c156cda3ddf497ca7a224d0a80f52f07bf1cabbe406560a7c18e7d777ca547fc134aed8aa52d7097d953a45e5ce2c3daee9e515
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nigel Thorne
|
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,33 @@
|
|
1
|
+
# MdParser
|
2
|
+
|
3
|
+
A WIP markdown table parser. -- may never be finished.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'md_parser'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install md_parser
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
beautify_table("|some | text|\n |in a md |table| \n") # => same table but pretty
|
24
|
+
|
25
|
+
table_to_hash("|some | text|\n |in a md |table| \n") # => hash of the table content.
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it ( https://github.com/nigelthorne/md_parser/fork )
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module MdParser
|
2
|
+
|
3
|
+
class HashTableVisitor
|
4
|
+
def table_to_h(titles, rows)
|
5
|
+
rows.map{|row| row.each.with_index.each_with_object({}){|(cell, index), h| h[titles[index]] = cell}}
|
6
|
+
end
|
7
|
+
|
8
|
+
def visit_table(titles, rows)
|
9
|
+
table_to_h(titles, rows)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
module MdParser
|
3
|
+
class TableParser < Parslet::Parser
|
4
|
+
rule(:table) { (( row.as(:table_titles)>> separator_row ).maybe >>
|
5
|
+
( separator_row.absent? >> row ).repeat(1).as(:table_rows)).as(:table_table) }
|
6
|
+
|
7
|
+
rule(:row) { cell.repeat(1).as(:table_row) >> space? >> eol}
|
8
|
+
rule(:separator_row) { str("|").maybe >> (str("-").repeat(3) >> str("|")).repeat(1) >> str("-").repeat(3).maybe >> eol}
|
9
|
+
|
10
|
+
rule(:cell) { str("|").maybe >> space? >> cell_body? >> space? >> str("|") }
|
11
|
+
|
12
|
+
rule(:cell_body?) { string.as(:table_value) |
|
13
|
+
str('"').absent? >> (
|
14
|
+
str("|").absent? >> (
|
15
|
+
(space >> str("|").absent?)|
|
16
|
+
(space.absent? >> str("\n").absent? >> any)
|
17
|
+
)
|
18
|
+
).repeat(0).as(:table_value)
|
19
|
+
}
|
20
|
+
|
21
|
+
rule(:string) { str('"') >> ( ( str('\\') >> any ) | ( str('"').absent? >> any ) ).repeat(0) >> str('"') }
|
22
|
+
|
23
|
+
rule(:space) { (str("\n").absent? >> match('\s')).repeat(1) }
|
24
|
+
rule(:space?) { space.maybe }
|
25
|
+
|
26
|
+
rule(:eof) { any.absent? }
|
27
|
+
rule(:eol) { str("\n") | eof }
|
28
|
+
|
29
|
+
root :table
|
30
|
+
end
|
31
|
+
|
32
|
+
class TableTransformer < Parslet::Transform
|
33
|
+
rule(:table_value => []) { "" }
|
34
|
+
rule(:table_value => simple(:val)) { val.to_s }
|
35
|
+
rule(:table_row => sequence(:vals)) { vals }
|
36
|
+
rule(:table_titles => sequence(:titles),
|
37
|
+
:table_rows => subtree(:rows)) { visitor.visit_table(titles, rows) }
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module MdParser
|
2
|
+
|
3
|
+
class TextTableVisitor
|
4
|
+
|
5
|
+
def cell_widths(titles, rows)
|
6
|
+
c = cols(titles, rows);
|
7
|
+
c.map{|x| x.map{|y| y ? y.length : 0}.max}
|
8
|
+
end
|
9
|
+
|
10
|
+
def cols(titles, rows)
|
11
|
+
titles.each.with_index.map{|t,i| [t] + rows.map{|r| r[i]} }
|
12
|
+
end
|
13
|
+
|
14
|
+
def pad_row(values, widths)
|
15
|
+
"| "+ widths.zip(values).map{|w,v| v + ( " " * (w-v.length)) }.join(" | ") + " |"
|
16
|
+
end
|
17
|
+
|
18
|
+
def separator_row(widths)
|
19
|
+
"|-"+ widths.map{|w| "-" * w }.join("-|-") + "-|"
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_table(titles, rows)
|
23
|
+
w = cell_widths(titles, rows)
|
24
|
+
|
25
|
+
( [ pad_row(titles, w), separator_row(w) ] +
|
26
|
+
rows.map{|r| pad_row(r, w)} ).join("\n")
|
27
|
+
end
|
28
|
+
|
29
|
+
def visit_table(titles, rows)
|
30
|
+
to_table(titles, rows)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/md_parser.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'parslet'
|
2
|
+
require "md_parser/version"
|
3
|
+
require "md_parser/table_parser"
|
4
|
+
require "md_parser/hash_table_visitor"
|
5
|
+
require "md_parser/text_table_visitor"
|
6
|
+
|
7
|
+
|
8
|
+
module MdParser
|
9
|
+
|
10
|
+
# MdParser.beautify_table("|A|B|\n|---|---|\n|1|2|")
|
11
|
+
def beautify_table(table_text)
|
12
|
+
TableTransformer.new.apply(
|
13
|
+
TableParser.new.parse( table_text ),
|
14
|
+
:visitor => TextTableVisitor.new)[:table_table]
|
15
|
+
end
|
16
|
+
module_function :beautify_table
|
17
|
+
|
18
|
+
# MdParser.table_to_hash("|A|B|\n|---|---|\n|1|2|")
|
19
|
+
def table_to_hash(table_text)
|
20
|
+
TableTransformer.new.apply(
|
21
|
+
TableParser.new.parse( table_text ),
|
22
|
+
:visitor => HashTableVisitor.new)[:table_table]
|
23
|
+
end
|
24
|
+
module_function :table_to_hash
|
25
|
+
end
|
data/md_parser.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'md_parser/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "md_parser"
|
8
|
+
spec.version = MdParser::VERSION
|
9
|
+
spec.authors = ["Nigel Thorne"]
|
10
|
+
spec.email = ["gems@nigelthorne.com"]
|
11
|
+
spec.summary = %q{WIP: Parser for MD5 tables. }
|
12
|
+
spec.description = %q{Not production ready. Do not use}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_dependency "parslet", "~> 1.6"
|
24
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'parslet/rig/rspec'
|
3
|
+
require "./parser"
|
4
|
+
|
5
|
+
|
6
|
+
describe TableParser do
|
7
|
+
let(:parser) { TableParser.new }
|
8
|
+
|
9
|
+
context "#string" do
|
10
|
+
it "should consume 'strings'" do
|
11
|
+
expect( parser.string.parse('""') )
|
12
|
+
expect( parser.string.parse('"test"') )
|
13
|
+
expect( parser.string.parse('"multiple words"') )
|
14
|
+
expect( parser.string.parse('" leading white space is fine "') )
|
15
|
+
expect( parser.string.parse(%/" so is escaped \\" quotes "/) )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "#cell" do
|
20
|
+
it "should consume a cell" do
|
21
|
+
expect( parser.cell.parse('||')[:table_value] ).to be_empty
|
22
|
+
expect( parser.cell.parse('| |')[:table_value] ).to be_empty
|
23
|
+
expect( parser.cell.parse('| test |')[:table_value] ).to eq "test"
|
24
|
+
expect( parser.cell.parse(' no leading pipe |')[:table_value] ).to eq "no leading pipe"
|
25
|
+
expect( parser.cell.parse(' "can hold a string" |')[:table_value] ).to eq "\"can hold a string\""
|
26
|
+
expect( parser.cell.parse(' "can hold a string containsing a |" |')[:table_value] ).to eq "\"can hold a string containsing a |\""
|
27
|
+
expect{ parser.cell.parse(' has to end in | ') }.to raise_error
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "#separator_row" do
|
32
|
+
it "should consume a separator_row" do
|
33
|
+
expect( parser.separator_row.parse('|---|') )
|
34
|
+
expect( parser.separator_row.parse('|---|---|') )
|
35
|
+
expect( parser.separator_row.parse('---|---|') )
|
36
|
+
expect( parser.separator_row.parse('---|---|---') )
|
37
|
+
expect( parser.separator_row.parse('------|-----|-------') )
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "#table" do
|
42
|
+
it "should consume a table" do
|
43
|
+
expect( parser.parse("|ID | Dependencies |\n|---|---|\n|1 | 2 |") )
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "#row" do
|
48
|
+
it "should consume a row" do
|
49
|
+
expect( parser.row.parse('| once cell |') )
|
50
|
+
expect( parser.row.parse('| multiple | cells |') )
|
51
|
+
expect( parser.row.parse(' multiple | cells |') )
|
52
|
+
expect( parser.row.parse(' multiple | cells | ')[:table_row].map{|x| x[:table_value]} ).to eq ["multiple", "cells"]
|
53
|
+
expect( parser.row.parse(' " this \" works" | cells | ')[:table_row].map{|x| x[:table_value]} ).to eq ["\" this \\\" works\"", "cells"]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
RSpec::Core::Runner.run([])
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: md_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nigel Thorne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-24 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: parslet
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.6'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.6'
|
55
|
+
description: Not production ready. Do not use
|
56
|
+
email:
|
57
|
+
- gems@nigelthorne.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/md_parser.rb
|
68
|
+
- lib/md_parser/hash_table_visitor.rb
|
69
|
+
- lib/md_parser/table_parser.rb
|
70
|
+
- lib/md_parser/text_table_visitor.rb
|
71
|
+
- lib/md_parser/version.rb
|
72
|
+
- md_parser.gemspec
|
73
|
+
- specs/md_parser_specs.rb
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: 'WIP: Parser for MD5 tables.'
|
98
|
+
test_files: []
|