markdown-expander 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4005c9bdfd3f7188f37fa44d1e70c8d2bdcd044e
4
+ data.tar.gz: aa4bf6ad500300d984e885d78216ff4d6bd8c088
5
+ SHA512:
6
+ metadata.gz: b518668e792ab8dc5436624631922f2d398782d1e97d194581aa925d07848ea0ba5023be4982752660217faeb4ae68ac912fee647e0fdd78bb24b93825bd8b4b
7
+ data.tar.gz: 2d28ebb1d58f40b0de5b8fbe746cce249dd69d6ab991123249feb9235775e671d771e083d7c1460bc8a122204428b78ea70d3a3b69c64261074e262679ad28b0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in markdown-expander.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # markdown-expander
2
+
3
+ Adds some new syntax to markdown
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'markdown-expander'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install markdown-expander
20
+
21
+ ## Expressions
22
+
23
+ ```ruby
24
+ require "markdown_expander/expander"
25
+
26
+ MarkdownExpander::Expander.new(
27
+ "# Title: {{thing.title}}"
28
+ ).render(
29
+ thing: {title: "Hello"}
30
+ )
31
+ #=> "# Title: Hello"
32
+ ```
33
+
34
+ ## Loops
35
+
36
+ ```ruby
37
+ require "markdown_expander/expander"
38
+
39
+ template = <<-TEMPLATE
40
+ {{thing in stuff}}
41
+ # {{thing.title}}
42
+ {{end}}
43
+ TEMPLATE
44
+
45
+ MarkdownExpander::Expander.new(template).render(
46
+ stuff: [ {title: "First!"}, {title: "Second!"} ]
47
+ )
48
+ #=> "# First!\n# Second!\n"
49
+ ```
data/README.md.docu ADDED
@@ -0,0 +1,51 @@
1
+ # markdown-expander
2
+
3
+ Adds some new syntax to markdown
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'markdown-expander'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install markdown-expander
20
+
21
+ ## Expressions
22
+
23
+ ```ruby
24
+ require "markdown_expander/expander"
25
+
26
+ MarkdownExpander::Expander.new(
27
+ "# Title: {{thing.title}}"
28
+ ).render(
29
+ thing: {title: "Hello"}
30
+ )
31
+ #=> "# Title: Hello"
32
+ ```
33
+
34
+ ## Loops
35
+
36
+ ```ruby
37
+ :example:
38
+ require "markdown_expander/expander"
39
+
40
+ template = <<-TEMPLATE
41
+ {{thing in stuff}}
42
+ # {{thing.title}}
43
+ {{end}}
44
+ TEMPLATE
45
+
46
+ MarkdownExpander::Expander.new(template).render(
47
+ stuff: [ {title: "First!"}, {title: "Second!"} ]
48
+ )
49
+ #=> "# First!\n# Second!\n"
50
+ :end:
51
+ ```
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => [:test, :docu]
11
+
12
+ require "docu/rake/task"
13
+
14
+ Docu::Rake::Task.new do |task|
15
+ task.file = "README.md.docu"
16
+ end
17
+
@@ -0,0 +1,92 @@
1
+ require "markdown_expander/version"
2
+
3
+ module MarkdownExpander
4
+ class Expander
5
+ LOOP_START_MATCH = /{{\s*([a-zA-Z_]*)\s+in\s+([a-zA-Z_.]*)\s*}}/
6
+ LOOP_END_MATCH = /{{\s*end\s*}}/
7
+ EXPRESSION_MATCH = /{{\s*([a-zA-Z_.]+)\s*}}/
8
+
9
+ def initialize template
10
+ @template = template
11
+ end
12
+
13
+ def render options
14
+ root = Node.new(nil, nil)
15
+ node = root
16
+
17
+ @template.each_line do |line|
18
+ if line =~ LOOP_START_MATCH
19
+ new_node = Node.new(node, LoopStart.new($1, $2))
20
+ node.children << new_node
21
+ node = new_node
22
+ elsif line =~ LOOP_END_MATCH
23
+ node = node.parent
24
+ elsif line =~ EXPRESSION_MATCH
25
+ before_match = $`
26
+ after_match = $'
27
+ node.children << Node.new(node, before_match)
28
+ node.children << Node.new(node, Expression.new($1))
29
+ node.children << Node.new(node, after_match)
30
+ else
31
+ node.children << Node.new(node, line)
32
+ end
33
+ end
34
+
35
+ evaluate_nodes(root, options)
36
+ end
37
+
38
+ def evaluate_nodes root, scope
39
+ lines = []
40
+ root.children.each_with_index do |child, index|
41
+ if child.value.class == Expression
42
+ lines << child.value.evaluate(scope)
43
+ elsif child.value.class == LoopStart
44
+ name = child.value.name.to_sym
45
+ parts = child.value.looper.split(".")
46
+ parts.each do |part|
47
+ scope = scope[part.to_sym]
48
+ end
49
+ scope.each do |item|
50
+ lines << evaluate_nodes(child, {name => item})
51
+ end
52
+ else
53
+ lines << child.value
54
+ end
55
+ end
56
+ lines.join("")
57
+ end
58
+
59
+ class Node
60
+ attr_accessor :parent
61
+ attr_accessor :children
62
+ attr_accessor :value
63
+ def initialize parent, value
64
+ @parent = parent
65
+ @value = value
66
+ @children = []
67
+ end
68
+ end
69
+
70
+ class LoopStart
71
+ attr_accessor :name, :looper
72
+ def initialize name, looper
73
+ @name = name
74
+ @looper = looper
75
+ end
76
+ end
77
+
78
+ class Expression
79
+ def initialize value
80
+ @value = value
81
+ end
82
+ def evaluate scope
83
+ parts = @value.split(".")
84
+ current_scope = scope
85
+ parts.each do |part|
86
+ current_scope = current_scope[part.to_sym]
87
+ end
88
+ current_scope
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module MarkdownExpander
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'markdown_expander/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "markdown-expander"
8
+ spec.version = MarkdownExpander::VERSION
9
+ spec.authors = ["Andrew Vos"]
10
+ spec.email = ["andrew.vos@gmail.com"]
11
+
12
+ spec.summary = %q{}
13
+ spec.description = %q{}
14
+ spec.homepage = ""
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ spec.add_development_dependency "docu"
25
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown-expander
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Vos
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-12 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: docu
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ''
70
+ email:
71
+ - andrew.vos@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - README.md
80
+ - README.md.docu
81
+ - Rakefile
82
+ - lib/markdown_expander/expander.rb
83
+ - lib/markdown_expander/version.rb
84
+ - markdown-expander.gemspec
85
+ homepage: ''
86
+ licenses: []
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.5.1
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: ''
108
+ test_files: []
109
+ has_rdoc: