trenni-markdown 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +4 -0
- data/.simplecov +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +12 -0
- data/README.md +106 -0
- data/Rakefile +20 -0
- data/bin/trenni-markdown +21 -0
- data/examples/ruby.md +31 -0
- data/examples/test.md +11 -0
- data/lib/trenni/markdown.rb +23 -0
- data/lib/trenni/markdown/generator.rb +109 -0
- data/lib/trenni/markdown/generators.rb +3 -0
- data/lib/trenni/markdown/generators/rspec.rb +34 -0
- data/lib/trenni/markdown/generators/ruby.rb +31 -0
- data/lib/trenni/markdown/parser.rb +110 -0
- data/lib/trenni/markdown/version.rb +25 -0
- data/spec/trenni/markdown/generator_spec.rb +46 -0
- data/spec/trenni/markdown/parser_spec.rb +92 -0
- data/trenni-markdown.gemspec +26 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6ea12e8aa1b0b8946964aff235560a5be4ed85b6
|
4
|
+
data.tar.gz: 58e70af20a4ea42cd49491fb257805a3539fb3ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d19c99b14e9bdf7456b8d6047c93c00ac83fda6501a06508a00ad00debe235b550ff3bdeef322af32c2f91106fb9c1eefa4f0bd47a7f2e684418b3b24515b089
|
7
|
+
data.tar.gz: e2d9b535a17e3595ff41a6c9af6ebef540f1a16057e6aac21fdbc87b09c7788297910b9b0c8b684460a08802da482c2fca893a106bc0c1c29a4c70ae0fc5c1aa
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.simplecov
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# Trenni::Markdown
|
2
|
+
|
3
|
+
Trenni::Markdown is a light-weight (deliberately) simple Markdown parser. It doesn't cover the entire spec but only a small subset required for implementing [literate](https://en.wikipedia.org/wiki/Literate_programming) unit tests, which are markdown documents which include unit tests.
|
4
|
+
|
5
|
+
[![Build Status](https://secure.travis-ci.org/ioquatix/trenni-markdown.svg)](http://travis-ci.org/ioquatix/trenni-markdown)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/ioquatix/trenni-markdown.svg)](https://codeclimate.com/github/ioquatix/trenni-markdown)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/ioquatix/trenni-markdown/badge.svg)](https://coveralls.io/r/ioquatix/trenni-markdown)
|
8
|
+
|
9
|
+
## Motivation
|
10
|
+
|
11
|
+
I've been working with [ffi-clang](https://github.com/) to generate documentation for C++ code and I was thinking about what would be the ideal documentation. In most cases, I end up copying examples from the unit tests into the main README. What about if the unit tests were actual markdown which could be compiled, and used to document the code in a very tangible way?
|
12
|
+
|
13
|
+
This markdown parser/generator is an experiment to find out if that's a good idea or not. So, far, the results are interesting.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'trenni-markdown'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install trenni-markdown
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
### Command line
|
32
|
+
|
33
|
+
A command line binary is included for basic transforms:
|
34
|
+
|
35
|
+
$ trenni-markdown -g RSpec examples/test.md
|
36
|
+
RSpec.describe String.new("Test") do
|
37
|
+
|
38
|
+
# This test checks that strings report the inclusion of letters correctly.
|
39
|
+
|
40
|
+
it "should contain the letter e" do
|
41
|
+
expect(subject).to include('e')
|
42
|
+
end
|
43
|
+
|
44
|
+
it "shoudn't contain the letter m" do
|
45
|
+
expect(subject).to_not include('m')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
### Generator
|
50
|
+
|
51
|
+
The command line is essentially implemented as follows:
|
52
|
+
|
53
|
+
buffer = Trenni::FileBuffer.new(path)
|
54
|
+
generator = Trenni::Markdown::Generators::RSpec.new
|
55
|
+
|
56
|
+
Trenni::Markdown::Parser.new(buffer, generator).parse!
|
57
|
+
|
58
|
+
puts generator.output
|
59
|
+
|
60
|
+
### Parser
|
61
|
+
|
62
|
+
If you just want to parse the subset of markdown supported by `Trenni::Markdown`, you can do so:
|
63
|
+
|
64
|
+
input = "# Title\nParagraph\n"
|
65
|
+
buffer = Trenni::Buffer.new(input)
|
66
|
+
Trenni::Markdown::Parser.new(buffer, delegate).parse!
|
67
|
+
|
68
|
+
The delegate must respond to the following callbacks:
|
69
|
+
|
70
|
+
@delegate.heading(level, text)
|
71
|
+
@delegate.paragraph(text)
|
72
|
+
@delegate.code(lines)
|
73
|
+
|
74
|
+
Keep in mind that this is not a general purpose markdown parser, but specifically for the generation of literate programming code.
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
1. Fork it
|
79
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
81
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
82
|
+
5. Create new Pull Request
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
Released under the MIT license.
|
87
|
+
|
88
|
+
Copyright, 2012, 2016, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
|
89
|
+
|
90
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
91
|
+
of this software and associated documentation files (the "Software"), to deal
|
92
|
+
in the Software without restriction, including without limitation the rights
|
93
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
94
|
+
copies of the Software, and to permit persons to whom the Software is
|
95
|
+
furnished to do so, subject to the following conditions:
|
96
|
+
|
97
|
+
The above copyright notice and this permission notice shall be included in
|
98
|
+
all copies or substantial portions of the Software.
|
99
|
+
|
100
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
101
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
102
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
103
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
104
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
105
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
106
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
5
|
+
begin
|
6
|
+
require('simplecov/version')
|
7
|
+
task.rspec_opts = %w{--require simplecov} if ENV['COVERAGE']
|
8
|
+
rescue LoadError
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :spec
|
13
|
+
|
14
|
+
task :console do
|
15
|
+
require "bundler/setup"
|
16
|
+
require_relative "lib/trenni/markdown"
|
17
|
+
|
18
|
+
require "pry"
|
19
|
+
Pry.start
|
20
|
+
end
|
data/bin/trenni-markdown
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env Ruby
|
2
|
+
|
3
|
+
require 'trenni/markdown'
|
4
|
+
require 'trenni/markdown/generators'
|
5
|
+
|
6
|
+
require 'trollop'
|
7
|
+
|
8
|
+
OPTIONS = Trollop::options do
|
9
|
+
opt :generator, "Which generator to use for converting markdown.", :type => :string, :default => 'Ruby'
|
10
|
+
end
|
11
|
+
|
12
|
+
generator_class = Trenni::Markdown::Generators.const_get(OPTIONS[:generator])
|
13
|
+
|
14
|
+
ARGV.each do |path|
|
15
|
+
buffer = Trenni::FileBuffer.new(path)
|
16
|
+
generator = generator_class.new
|
17
|
+
|
18
|
+
Trenni::Markdown::Parser.new(buffer, generator).parse!
|
19
|
+
|
20
|
+
puts generator.output
|
21
|
+
end
|
data/examples/ruby.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
20
|
+
|
21
|
+
# module Trenni::Markdown::Generators
|
22
|
+
|
23
|
+
This module implements a generator for creating Ruby output. Headings map to module and class blocks.
|
24
|
+
|
25
|
+
## class Ruby < Trenni::Markdown::Generator
|
26
|
+
|
27
|
+
### def heading(level, text)
|
28
|
+
|
29
|
+
Every heading generates a new scope:
|
30
|
+
nest(level, "#{text}\n", "end\n")
|
31
|
+
|
data/examples/test.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require_relative "markdown/version"
|
22
|
+
require_relative "markdown/parser"
|
23
|
+
require_relative "markdown/generator"
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module Trenni
|
22
|
+
module Markdown
|
23
|
+
# This parser delegate generates nested code output.
|
24
|
+
class Generator
|
25
|
+
def initialize(comment_prefix = '# ')
|
26
|
+
@comment_prefix = comment_prefix
|
27
|
+
@stack = []
|
28
|
+
|
29
|
+
@whitespace = String.new
|
30
|
+
@output = String.new
|
31
|
+
end
|
32
|
+
|
33
|
+
attr :output
|
34
|
+
|
35
|
+
def indentation
|
36
|
+
"\t" * @stack.size
|
37
|
+
end
|
38
|
+
|
39
|
+
def append_whitespace
|
40
|
+
unless @whitespace.empty?
|
41
|
+
@output << @whitespace
|
42
|
+
@whitespace = String.new
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def append(lines, prefix = nil, ignore_whitespace: false)
|
47
|
+
append_whitespace unless ignore_whitespace
|
48
|
+
|
49
|
+
if prefix
|
50
|
+
prefix = indentation + prefix
|
51
|
+
else
|
52
|
+
prefix = indentation
|
53
|
+
end
|
54
|
+
|
55
|
+
Array(lines).each do |line|
|
56
|
+
@output << prefix << line
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def outdent(level)
|
61
|
+
# Outdent until we can indent one level:
|
62
|
+
while level <= @stack.size
|
63
|
+
append(@stack.pop, ignore_whitespace: true)
|
64
|
+
end
|
65
|
+
|
66
|
+
append_whitespace
|
67
|
+
end
|
68
|
+
|
69
|
+
def begin_parse(parser)
|
70
|
+
end
|
71
|
+
|
72
|
+
def end_parse(parser)
|
73
|
+
outdent(1)
|
74
|
+
end
|
75
|
+
|
76
|
+
def open(text)
|
77
|
+
@output << text
|
78
|
+
end
|
79
|
+
|
80
|
+
def close(text)
|
81
|
+
@stack.push(text)
|
82
|
+
end
|
83
|
+
|
84
|
+
def nest(level, first_line, last_line)
|
85
|
+
outdent(level)
|
86
|
+
|
87
|
+
# We should now be at the point where we can indent:
|
88
|
+
append(first_line)
|
89
|
+
@stack.push(last_line)
|
90
|
+
end
|
91
|
+
|
92
|
+
def whitespace(text)
|
93
|
+
@whitespace << text
|
94
|
+
end
|
95
|
+
|
96
|
+
def heading(level, text)
|
97
|
+
nest(level, "module #{text}\n", "end\n")
|
98
|
+
end
|
99
|
+
|
100
|
+
def paragraph(text)
|
101
|
+
append(text, @comment_prefix)
|
102
|
+
end
|
103
|
+
|
104
|
+
def code(text)
|
105
|
+
append(text)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module Trenni
|
22
|
+
module Markdown
|
23
|
+
module Generators
|
24
|
+
class RSpec < Generator
|
25
|
+
def heading(level, text)
|
26
|
+
case level
|
27
|
+
when 1 then nest(level, "RSpec.describe #{text} do\n", "end\n")
|
28
|
+
when 2 then nest(level, "it #{text.downcase.dump} do", "end\n")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module Trenni
|
22
|
+
module Markdown
|
23
|
+
module Generators
|
24
|
+
class Ruby < Generator
|
25
|
+
def heading(level, text)
|
26
|
+
nest(level, "#{text}\n", "end\n")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/scanner'
|
22
|
+
|
23
|
+
module Trenni
|
24
|
+
module Markdown
|
25
|
+
# This parser processes general markup into a sequence of events which are passed to a delegate.
|
26
|
+
class Parser < StringScanner
|
27
|
+
def initialize(buffer, delegate)
|
28
|
+
super(buffer)
|
29
|
+
|
30
|
+
@delegate = delegate
|
31
|
+
@level = 0
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse!
|
35
|
+
@delegate.begin_parse(self)
|
36
|
+
|
37
|
+
until eos?
|
38
|
+
start_pos = self.pos
|
39
|
+
|
40
|
+
scan_heading
|
41
|
+
scan_paragraph
|
42
|
+
scan_code
|
43
|
+
scan_newlines
|
44
|
+
|
45
|
+
raise_if_stuck(start_pos)
|
46
|
+
end
|
47
|
+
|
48
|
+
@delegate.end_parse(self)
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def scan_newlines
|
54
|
+
# Consume all newlines.
|
55
|
+
if self.scan(/\n*/)
|
56
|
+
@delegate.whitespace(self.matched)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def scan_heading
|
61
|
+
# Match any character data except the open tag character.
|
62
|
+
if self.scan(/\s*(\#+)\s*(.*?)\n/)
|
63
|
+
level = self[1].length
|
64
|
+
|
65
|
+
unless level <= (@level + 1)
|
66
|
+
parse_error!("Cannot nest heading more than one level deep at a time!")
|
67
|
+
else
|
68
|
+
@level = level
|
69
|
+
end
|
70
|
+
|
71
|
+
text = self[2]
|
72
|
+
|
73
|
+
# If the title has a inline code fence, e.g. "# Testing `String`"
|
74
|
+
# we only pass along the part in the code fence.
|
75
|
+
text = $1 if text =~ /`(.*?)`/
|
76
|
+
|
77
|
+
@delegate.heading(level, text)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
PARAGRAPH_LINE = /([^\s#].*?\n)/
|
82
|
+
|
83
|
+
def scan_paragraph
|
84
|
+
if self.scan(PARAGRAPH_LINE)
|
85
|
+
lines = [self[1]]
|
86
|
+
|
87
|
+
while self.scan(PARAGRAPH_LINE)
|
88
|
+
lines << self[1]
|
89
|
+
end
|
90
|
+
|
91
|
+
@delegate.paragraph(lines)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
CODE_LINE = /\t(.*?\n)/
|
96
|
+
|
97
|
+
def scan_code
|
98
|
+
if self.scan(CODE_LINE)
|
99
|
+
lines = [self[1]]
|
100
|
+
|
101
|
+
while self.scan(CODE_LINE)
|
102
|
+
lines << self[1]
|
103
|
+
end
|
104
|
+
|
105
|
+
@delegate.code(lines)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module Trenni
|
22
|
+
module Markdown
|
23
|
+
VERSION = "0.1.0"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env rspec
|
2
|
+
|
3
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'trenni/markdown'
|
24
|
+
|
25
|
+
module Trenni::Markdown::GeneratorSpec
|
26
|
+
RSpec.describe Trenni::Markdown::Generator do
|
27
|
+
def parse(input)
|
28
|
+
buffer = Trenni::Buffer.new(input)
|
29
|
+
|
30
|
+
Trenni::Markdown::Parser.new(buffer, subject).parse!
|
31
|
+
|
32
|
+
return subject
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should parse a heading" do
|
36
|
+
generator = parse(<<~EOS)
|
37
|
+
# MyModule
|
38
|
+
|
39
|
+
This constant has a value.
|
40
|
+
CONSTANT = value
|
41
|
+
EOS
|
42
|
+
|
43
|
+
puts generator.output
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
#!/usr/bin/env rspec
|
2
|
+
|
3
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'trenni/markdown'
|
24
|
+
|
25
|
+
module Trenni::Markdown::ParserSpec
|
26
|
+
class ParserDelegate
|
27
|
+
def initialize
|
28
|
+
@events = []
|
29
|
+
end
|
30
|
+
|
31
|
+
attr :events
|
32
|
+
|
33
|
+
def begin_parse(parser)
|
34
|
+
end
|
35
|
+
|
36
|
+
def end_parse(parser)
|
37
|
+
end
|
38
|
+
|
39
|
+
def whitespace(text)
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_missing(*args)
|
43
|
+
@events << args
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
RSpec.describe Trenni::Markdown::Parser do
|
48
|
+
let(:delegate) {ParserDelegate.new}
|
49
|
+
|
50
|
+
def parse(input)
|
51
|
+
buffer = Trenni::Buffer.new(input)
|
52
|
+
Trenni::Markdown::Parser.new(buffer, delegate).parse!
|
53
|
+
|
54
|
+
return delegate
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should parse a heading" do
|
58
|
+
delegate = parse("# Heading 1\n## Heading 2\n")
|
59
|
+
|
60
|
+
expect(delegate.events).to be == [
|
61
|
+
[:heading, 1, "Heading 1"],
|
62
|
+
[:heading, 2, "Heading 2"],
|
63
|
+
]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should parse single line paragraph" do
|
67
|
+
delegate = parse("# Heading 1\nparagraph\n")
|
68
|
+
|
69
|
+
expect(delegate.events).to be == [
|
70
|
+
[:heading, 1, "Heading 1"],
|
71
|
+
[:paragraph, ["paragraph\n"]],
|
72
|
+
]
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should parse multiple line paragraph" do
|
76
|
+
delegate = parse("# Heading 1\nfoo bar\nbaz bob\n")
|
77
|
+
|
78
|
+
expect(delegate.events).to be == [
|
79
|
+
[:heading, 1, "Heading 1"],
|
80
|
+
[:paragraph, ["foo bar\n", "baz bob\n"]],
|
81
|
+
]
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should parse a code block" do
|
85
|
+
delegate = parse("\tfoo = 10\n\tbar = foo * 2\n")
|
86
|
+
|
87
|
+
expect(delegate.events).to be == [
|
88
|
+
[:code, ["foo = 10\n", "bar = foo * 2\n"]]
|
89
|
+
]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'trenni/markdown/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "trenni-markdown"
|
8
|
+
spec.version = Trenni::Markdown::VERSION
|
9
|
+
spec.authors = ["Samuel Williams"]
|
10
|
+
spec.email = ["samuel.williams@oriontransfer.co.nz"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple event-generating Markdown parser.}
|
13
|
+
spec.homepage = "https://github.com/ioquatix/trenni-markdown"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "trenni", "~> 1.6.0"
|
21
|
+
spec.add_dependency "trollop", "~> 2.0.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trenni-markdown
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Williams
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: trenni
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.6.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.6.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: trollop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- samuel.williams@oriontransfer.co.nz
|
86
|
+
executables:
|
87
|
+
- trenni-markdown
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".simplecov"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/trenni-markdown
|
99
|
+
- examples/ruby.md
|
100
|
+
- examples/test.md
|
101
|
+
- lib/trenni/markdown.rb
|
102
|
+
- lib/trenni/markdown/generator.rb
|
103
|
+
- lib/trenni/markdown/generators.rb
|
104
|
+
- lib/trenni/markdown/generators/rspec.rb
|
105
|
+
- lib/trenni/markdown/generators/ruby.rb
|
106
|
+
- lib/trenni/markdown/parser.rb
|
107
|
+
- lib/trenni/markdown/version.rb
|
108
|
+
- spec/trenni/markdown/generator_spec.rb
|
109
|
+
- spec/trenni/markdown/parser_spec.rb
|
110
|
+
- trenni-markdown.gemspec
|
111
|
+
homepage: https://github.com/ioquatix/trenni-markdown
|
112
|
+
licenses: []
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.5.1
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: A simple event-generating Markdown parser.
|
134
|
+
test_files:
|
135
|
+
- spec/trenni/markdown/generator_spec.rb
|
136
|
+
- spec/trenni/markdown/parser_spec.rb
|