deas-kramdown 0.1.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
+ SHA512:
3
+ data.tar.gz: 71dc6d17d7f6732475e5c6768803b1c442a1341a2183594a5587ce7e657d83d42849dc5ab59d4955d6297e1330f8a85db10291c88ee0a106530513e034dd90ad
4
+ metadata.gz: bb2f9493a9c89c22d71f7e52ddbfb1dcb22afc2c788a9fe16f919a5c8c493c70640307a90542ec0f84a48d834fe567a7233ac19b24a2c3a4b432ef5f4ab3b221
5
+ SHA1:
6
+ data.tar.gz: c6d1ae7dc5246d8a013a5527f3c835c61fb76504
7
+ metadata.gz: 5610ded1adef28042a476689355ccbacf67c9435
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.log
3
+ *.rbc
4
+ .rbx/
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'pry', "~> 0.9.0"
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016-Present Kelly Redding and Collin Redding
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,44 @@
1
+ # Deas::Nm
2
+
3
+ [Deas](https://github.com/redding/deas) template engine for rendering [Kramdown](http://kramdown.gettalong.org/) templates
4
+
5
+ ## Usage
6
+
7
+ Register the engine:
8
+
9
+ ```ruby
10
+ require 'deas'
11
+ require 'deas-kramdown'
12
+
13
+ Deas.configure do |c|
14
+
15
+ c.template_source "/path/to/templates" do |s|
16
+ s.engine 'md', Deas::Kramdown::TemplateEngine
17
+ end
18
+
19
+ end
20
+ ```
21
+
22
+ Add `.md` to any template files in your template source path. Deas will render their content using Kramdown when they are rendered.
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ gem 'deas-kramdown'
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install deas-kramdown
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "deas-kramdown/version"
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "deas-kramdown"
8
+ gem.version = Deas::Kramdown::VERSION
9
+ gem.authors = ["Kelly Redding", "Collin Redding"]
10
+ gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
11
+ gem.summary = %q{Deas template engine for Kramdown templates}
12
+ gem.description = %q{Deas template engine for Kramdown templates}
13
+ gem.homepage = "http://github.com/redding/deas-kramdown"
14
+ gem.license = 'MIT'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency("assert", ["~> 2.15.1"])
22
+
23
+ gem.add_dependency("deas", ["~> 0.39.2"])
24
+ gem.add_dependency("kramdown", ["~> 1.10"])
25
+
26
+ end
@@ -0,0 +1,38 @@
1
+ require 'deas/template_engine'
2
+ require 'kramdown'
3
+
4
+ require "deas-kramdown/version"
5
+ require 'deas-kramdown/source'
6
+
7
+ module Deas::Kramdown
8
+
9
+ class TemplateEngine < Deas::TemplateEngine
10
+
11
+ def kramdown_source
12
+ @kramdown_source ||= Source.new(self.source_path, {
13
+ :doc_opts => self.opts['doc_opts'],
14
+ :cache => self.opts['cache']
15
+ })
16
+ end
17
+
18
+ # Render straight markdown templates. As no ruby will be evaluated the view
19
+ # handler, locals and content block will be ignored.
20
+ def render(template_name, view_handler, locals, &content)
21
+ self.kramdown_source.render(template_name)
22
+ end
23
+
24
+ # Render straight markdown partial templates. As no ruby will be evaluated
25
+ # the locals and content block will be ignored.
26
+ def partial(template_name, locals, &content)
27
+ self.kramdown_source.render(template_name)
28
+ end
29
+
30
+ # this is used when chaining engines where another engine initially loads the
31
+ # template file
32
+ def compile(template_name, compiled_content)
33
+ self.kramdown_source.compile(template_name, compiled_content)
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,57 @@
1
+ require 'pathname'
2
+ require 'kramdown'
3
+
4
+ module Deas; end
5
+ module Deas::Kramdown
6
+
7
+ class Source
8
+
9
+ attr_reader :root, :cache, :doc_opts
10
+
11
+ def initialize(root, opts)
12
+ @root = Pathname.new(root.to_s)
13
+ @doc_opts = opts[:doc_opts] || {}
14
+ @cache = opts[:cache] ? Hash.new : NullCache.new
15
+ end
16
+
17
+ def render(template_name)
18
+ load(template_name).to_html
19
+ end
20
+
21
+ def compile(template_name, content)
22
+ doc(content).to_html
23
+ end
24
+
25
+ def doc(content)
26
+ Kramdown::Document.new(content, @doc_opts)
27
+ end
28
+
29
+ def inspect
30
+ "#<#{self.class}:#{'0x0%x' % (object_id << 1)}"\
31
+ " @root=#{@root.inspect}"\
32
+ " @doc_opts=#{@doc_opts.inspect}>"
33
+ end
34
+
35
+ private
36
+
37
+ def load(template_name)
38
+ @cache[template_name] ||= begin
39
+ file_path = source_file_path(template_name).to_s
40
+ content = File.send(File.respond_to?(:binread) ? :binread : :read, file_path)
41
+ doc(content)
42
+ end
43
+ end
44
+
45
+ def source_file_path(template_name)
46
+ Dir.glob(self.root.join("#{template_name}*")).first
47
+ end
48
+
49
+ class NullCache
50
+ def [](template_name); end
51
+ def []=(template_name, value); end
52
+ def keys; []; end
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,4 @@
1
+ module Deas; end
2
+ module Deas::Kramdown
3
+ VERSION = "0.1.0"
4
+ end
data/log/.gitkeep ADDED
File without changes
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ # this file is automatically required when you run `assert`
2
+ # put any test helpers here
3
+
4
+ require 'pathname'
5
+
6
+ # add the root dir to the load path
7
+ ROOT = Pathname.new(File.expand_path("../..", __FILE__))
8
+ $LOAD_PATH.unshift(ROOT.to_s)
9
+
10
+ TEST_SUPPORT_PATH = ROOT.join('test/support')
11
+ TEMPLATE_ROOT = TEST_SUPPORT_PATH.join('templates')
12
+ TEMPLATE_CACHE_ROOT = ROOT.join('tmp/templates')
13
+
14
+ # require pry for debugging (`binding.pry`)
15
+ require 'pry'
16
+
17
+ require 'test/support/factory'
18
+
19
+ # TODO: put test helpers here...
@@ -0,0 +1,18 @@
1
+ require 'assert/factory'
2
+
3
+ module Factory
4
+ extend Assert::Factory
5
+
6
+ def self.template_root
7
+ TEMPLATE_ROOT.to_s
8
+ end
9
+
10
+ def self.template_file(name)
11
+ TEMPLATE_ROOT.join(name).to_s
12
+ end
13
+
14
+ def self.basic_markdown_rendered
15
+ "<h1 id=\"some-heading\">Some Heading</h1>\n"
16
+ end
17
+
18
+ end
@@ -0,0 +1 @@
1
+ # Some Heading
@@ -0,0 +1 @@
1
+ # Some Heading
@@ -0,0 +1 @@
1
+ # Some Heading
@@ -0,0 +1,45 @@
1
+ require 'assert'
2
+ require 'deas-kramdown'
3
+
4
+ require 'deas/template_source'
5
+
6
+ class Deas::Kramdown::TemplateEngine
7
+
8
+ class SystemTests < Assert::Context
9
+ desc "Deas::Kramdown::TemplateEngine"
10
+ setup do
11
+ @view = OpenStruct.new({
12
+ :identifier => Factory.integer,
13
+ :name => Factory.string
14
+ })
15
+ @locals = { 'local1' => Factory.string }
16
+ @content = Proc.new{ Factory.string }
17
+
18
+ @engine = Deas::Kramdown::TemplateEngine.new('source_path' => TEMPLATE_ROOT)
19
+ end
20
+ subject{ @engine }
21
+
22
+ should "render templates (ignoring the view/locals/content)" do
23
+ exp = Factory.basic_markdown_rendered
24
+ assert_equal exp, subject.render('basic', @view, @locals)
25
+ assert_equal exp, subject.render('basic', @view, @locals, &@content)
26
+ end
27
+
28
+ should "render partial templates (ignoring the locals/content)" do
29
+ exp = Factory.basic_markdown_rendered
30
+ assert_equal exp, subject.partial('_basic', @locals)
31
+ assert_equal exp, subject.partial('_basic', @locals, &@content)
32
+ end
33
+
34
+ should "compile raw template markup" do
35
+ template_name = 'basic_alt'
36
+ file_path = TEMPLATE_ROOT.join("#{template_name}.markdown").to_s
37
+ file_content = File.read(file_path)
38
+
39
+ exp = Factory.basic_markdown_rendered
40
+ assert_equal exp, subject.compile(template_name, file_content)
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,145 @@
1
+ require 'assert'
2
+ require 'deas-kramdown/source'
3
+
4
+ require 'kramdown'
5
+
6
+ class Deas::Kramdown::Source
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "Deas::Kramdown::Source"
10
+ setup do
11
+ @source_class = Deas::Kramdown::Source
12
+ end
13
+ subject{ @source_class }
14
+
15
+ end
16
+
17
+ class InitTests < UnitTests
18
+ desc "when init"
19
+ setup do
20
+ @doc_opts = { Factory.string => Factory.string }
21
+ @root = Factory.template_root
22
+ @source = @source_class.new(@root, :doc_opts => @doc_opts)
23
+ end
24
+ subject{ @source }
25
+
26
+ should have_readers :root, :cache, :doc_opts
27
+ should have_imeths :render, :compile, :doc
28
+
29
+ should "know its root" do
30
+ assert_equal @root, subject.root.to_s
31
+ end
32
+
33
+ should "not cache templates by default" do
34
+ assert_kind_of NullCache, subject.cache
35
+ end
36
+
37
+ should "cache templates if the :cache opt is `true`" do
38
+ source = @source_class.new(@root, :cache => true)
39
+ assert_kind_of Hash, source.cache
40
+ end
41
+
42
+ should "know its doc options" do
43
+ assert_equal @doc_opts, subject.doc_opts
44
+ assert_equal Hash.new, @source_class.new(@root, {}).doc_opts
45
+ end
46
+
47
+ should "build kramdown doc objects from template content" do
48
+ content = '# Some Heading'
49
+ kdoc = Kramdown::Document.new(content, @doc_opts)
50
+ kdoc_called_with = []
51
+ Assert.stub(Kramdown::Document, :new) do |*args|
52
+ kdoc_called_with = args
53
+ kdoc
54
+ end
55
+
56
+ doc = subject.doc(content)
57
+ assert_instance_of Kramdown::Document, doc
58
+
59
+ exp = [content, @doc_opts]
60
+ assert_equal exp, kdoc_called_with
61
+ end
62
+
63
+ end
64
+
65
+ class RenderTests < InitTests
66
+ desc "`render` method"
67
+ setup do
68
+ @template_name = ['basic', 'basic_alt'].choice
69
+ end
70
+
71
+ should "render a template for the given template name and return its data" do
72
+ exp = Factory.basic_markdown_rendered
73
+ assert_equal exp, subject.render(@template_name)
74
+ end
75
+
76
+ end
77
+
78
+ class RenderCacheTests < RenderTests
79
+ desc "when caching is enabled"
80
+ setup do
81
+ @source = @source_class.new(@root, :cache => true)
82
+ end
83
+
84
+ should "cache templates by their template name" do
85
+ exp = Factory.basic_markdown_rendered
86
+ assert_equal exp, @source.render(@template_name)
87
+
88
+ assert_equal [@template_name], @source.cache.keys
89
+ assert_kind_of Kramdown::Document, @source.cache[@template_name]
90
+ end
91
+
92
+ end
93
+
94
+ class RenderNoCacheTests < RenderTests
95
+ desc "when caching is disabled"
96
+ setup do
97
+ @source = @source_class.new(@root, :cache => false)
98
+ end
99
+
100
+ should "not cache templates" do
101
+ exp = Factory.basic_markdown_rendered
102
+ assert_equal exp, @source.render(@template_name)
103
+
104
+ assert_equal [], @source.cache.keys
105
+ end
106
+
107
+ end
108
+
109
+ class CompileTests < InitTests
110
+ desc "`compile` method"
111
+
112
+ should "evaluate raw template output and return it" do
113
+ raw = "## Another Heading"
114
+ exp = "<h2 id=\"another-heading\">Another Heading</h2>\n"
115
+ assert_equal exp, subject.compile('compile', raw)
116
+ end
117
+
118
+ end
119
+
120
+ class NullCacheTests < UnitTests
121
+ desc "NullCache"
122
+ setup do
123
+ @cache = NullCache.new
124
+ end
125
+ subject{ @cache }
126
+
127
+ should have_imeths :[], :[]=, :keys
128
+
129
+ should "take a template name and return nothing on index" do
130
+ assert_nil subject[Factory.path]
131
+ end
132
+
133
+ should "take a template name and value and do nothing on index write" do
134
+ assert_nothing_raised do
135
+ subject[Factory.path] = Factory.string
136
+ end
137
+ end
138
+
139
+ should "always have empty keys" do
140
+ assert_equal [], subject.keys
141
+ end
142
+
143
+ end
144
+
145
+ end
@@ -0,0 +1,46 @@
1
+ require 'assert'
2
+ require 'deas-kramdown'
3
+
4
+ require 'deas/template_engine'
5
+ require 'deas-kramdown/source'
6
+
7
+ class Deas::Kramdown::TemplateEngine
8
+
9
+ class UnitTests < Assert::Context
10
+ desc "Deas::Kramdown::TemplateEngine"
11
+ setup do
12
+ @engine = Deas::Kramdown::TemplateEngine.new({
13
+ 'source_path' => TEST_SUPPORT_PATH
14
+ })
15
+ end
16
+ subject{ @engine }
17
+
18
+ should have_imeths :kramdown_source
19
+ should have_imeths :render, :partial, :compile
20
+
21
+ should "be a Deas template engine" do
22
+ assert_kind_of Deas::TemplateEngine, subject
23
+ end
24
+
25
+ should "memoize its kramdown source" do
26
+ assert_kind_of Deas::Kramdown::Source, subject.kramdown_source
27
+ assert_equal subject.source_path, subject.kramdown_source.root
28
+ assert_same subject.kramdown_source, subject.kramdown_source
29
+ end
30
+
31
+ should "allow custom doc opts on its source" do
32
+ doc_opts = { Factory.string => Factory.string }
33
+ engine = Deas::Kramdown::TemplateEngine.new('doc_opts' => doc_opts)
34
+ assert_equal doc_opts, engine.kramdown_source.doc_opts
35
+ end
36
+
37
+ should "pass any given cache option to its source" do
38
+ engine = Deas::Kramdown::TemplateEngine.new('cache' => true)
39
+ assert_kind_of Hash, engine.kramdown_source.cache
40
+ end
41
+
42
+ end
43
+
44
+ # render, partial and compile tests are covered in the system tests
45
+
46
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deas-kramdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kelly Redding
8
+ - Collin Redding
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2016-04-05 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: assert
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.15.1
23
+ type: :development
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
26
+ name: deas
27
+ prerelease: false
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.39.2
33
+ type: :runtime
34
+ version_requirements: *id002
35
+ - !ruby/object:Gem::Dependency
36
+ name: kramdown
37
+ prerelease: false
38
+ requirement: &id003 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: "1.10"
43
+ type: :runtime
44
+ version_requirements: *id003
45
+ description: Deas template engine for Kramdown templates
46
+ email:
47
+ - kelly@kellyredding.com
48
+ - collin.redding@me.com
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - deas-kramdown.gemspec
61
+ - lib/deas-kramdown.rb
62
+ - lib/deas-kramdown/source.rb
63
+ - lib/deas-kramdown/version.rb
64
+ - log/.gitkeep
65
+ - test/helper.rb
66
+ - test/support/factory.rb
67
+ - test/support/templates/_basic.md
68
+ - test/support/templates/basic.html.md
69
+ - test/support/templates/basic_alt.markdown
70
+ - test/system/template_engine_tests.rb
71
+ - test/unit/source_tests.rb
72
+ - test/unit/template_engine_tests.rb
73
+ - tmp/.gitkeep
74
+ homepage: http://github.com/redding/deas-kramdown
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - &id004
87
+ - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - *id004
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 2.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Deas template engine for Kramdown templates
100
+ test_files:
101
+ - test/helper.rb
102
+ - test/support/factory.rb
103
+ - test/support/templates/_basic.md
104
+ - test/support/templates/basic.html.md
105
+ - test/support/templates/basic_alt.markdown
106
+ - test/system/template_engine_tests.rb
107
+ - test/unit/source_tests.rb
108
+ - test/unit/template_engine_tests.rb