menu_txt 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/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +7 -0
- data/lib/menu_txt.rb +39 -0
- data/lib/menu_txt/node.rb +46 -0
- data/lib/menu_txt/simple_node.rb +7 -0
- data/lib/menu_txt/version.rb +3 -0
- data/menu_txt.gemspec +24 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a47e229baae4fc5f271b96eba1150b75d23447ad
|
4
|
+
data.tar.gz: 204654922d6bb50738f99e46966dd41c9c330371
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 53c42740e8d72bed5c330fb4ffdc598ba4f69f25b1f714ce57de81f64428be4425cf4ea6b1191eb9b40232d897b498d85b5d294cf3c5611b21eecabf36611820
|
7
|
+
data.tar.gz: 465707610456ada4dee7d199ac601aa9c7c3eeee7e9413656830c823be88551c12d3daee8c94b87078137a7ea9780bb4ae16322186b72daefae99a00802e1e09
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Maxim Chernyak
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# menu_txt
|
2
|
+
|
3
|
+
If you have non-technical people who want to be able to edit menus on your website, this is the gem for you.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
The gem is not rails-dependent, but here's a good example of rails usage.
|
8
|
+
|
9
|
+
Throw a text file somewhere, say `app/menus/my_awesome_menu.txt`.
|
10
|
+
|
11
|
+
~~~plain
|
12
|
+
1st level thing 1 | /foo1
|
13
|
+
|
14
|
+
- 2nd level thing A | /foo1/bar1
|
15
|
+
-- 3rd level thing A | /foo1/bar1/baz1
|
16
|
+
-- 3rd level thing B | /foo1/bar1/baz2
|
17
|
+
|
18
|
+
- 2nd level thing B | /foo1/bar2
|
19
|
+
-- 3rd level thing A | /foo1/bar2/baz1
|
20
|
+
|
21
|
+
- 2nd level thing C | /foo1/bar3
|
22
|
+
|
23
|
+
1st level thing 2 | /foo2
|
24
|
+
~~~
|
25
|
+
|
26
|
+
You can place that file anywhere you like, but placing all the menus in one dir is a good idea, because we can add this line to `development.rb`.
|
27
|
+
|
28
|
+
~~~ruby
|
29
|
+
config.watchable_dirs['app/menus'] = [:txt]
|
30
|
+
~~~
|
31
|
+
|
32
|
+
This line ensures that all changes to txt files in `app/menus` will cause your rails app to reload, so you can see changes with a browser refresh.
|
33
|
+
|
34
|
+
Next, create a class for your menu, for example it could be `app/models/my_awesome_menu.rb`. Put this code into it.
|
35
|
+
|
36
|
+
~~~ruby
|
37
|
+
MyAwesomeMenu = MenuTxt.parse_path('app/menus/my_awesome_menu.txt')
|
38
|
+
~~~
|
39
|
+
|
40
|
+
And now we just need a partial which will render itself recursively for every submenu. For example, let's create `app/views/my_awesome_menu/_nodes.html.erb`.
|
41
|
+
|
42
|
+
~~~html
|
43
|
+
<% nodes.each do |node| %>
|
44
|
+
<li <%=raw node.leaf? ? '' : 'class="dropdown-submenu"' %>>
|
45
|
+
<%= link_to node.name, node.url %>
|
46
|
+
|
47
|
+
<% unless node.leaf? %>
|
48
|
+
<ul class="dropdown-menu">
|
49
|
+
<%= render 'my_awesome_menu/nodes', nodes: node.children %>
|
50
|
+
</ul>
|
51
|
+
<% end %>
|
52
|
+
</li>
|
53
|
+
<% end %>
|
54
|
+
~~~
|
55
|
+
|
56
|
+
Notice how I use `leaf?` above to determine if there are no more submenus under this node.
|
57
|
+
|
58
|
+
Finally, wherever you want to render this menu in your html, all you gotta do is the following.
|
59
|
+
|
60
|
+
~~~html
|
61
|
+
<ul class="dropdown-menu">
|
62
|
+
<%= render 'my_awesome_menu/nodes', nodes: MyAwesomeMenu.children %>
|
63
|
+
</ul>
|
64
|
+
~~~
|
65
|
+
|
66
|
+
So given you have css/javascript hooked to classes `dropdown-menu` and `dropdown-submenu`, everything would just work. Now try editing `my_awesome_menu.txt` and refreshing the page. That's it, now your marketing people could edit the a plain text file on github and commit it without your involvement.
|
67
|
+
|
68
|
+
## Advanced usage
|
69
|
+
|
70
|
+
### Traversing nodes
|
71
|
+
|
72
|
+
The `MyAwesomeMenu` in the above example is fully `Enumerable` and if `each` is called without a block it returns a proper iterator. The order of iteration is exactly like reading the text file top to bottom.
|
73
|
+
|
74
|
+
### Extending nodes
|
75
|
+
|
76
|
+
If you want each node in your menu to have special methods, you can use your own node class. Simply create it kinda like this.
|
77
|
+
|
78
|
+
~~~ruby
|
79
|
+
class MySpecialMenuNode
|
80
|
+
include MenuTxt::Node
|
81
|
+
|
82
|
+
def highlight?
|
83
|
+
url.include?('special_deals')
|
84
|
+
end
|
85
|
+
end
|
86
|
+
~~~
|
87
|
+
|
88
|
+
The `MenuTxt::Node` is a convenient module that gives you all the node boilerplate. Then, when you create the menu model, you can pass your node object as the second argument, and it will become root of the menu, which acts as the prototype for all nested nodes.
|
89
|
+
|
90
|
+
~~~ruby
|
91
|
+
MyAwesomeMenu =
|
92
|
+
MenuTxt.parse_path('app/menus/menu.txt', MySpecialMenuNode.new(nil))
|
93
|
+
~~~
|
94
|
+
|
95
|
+
## Installation
|
96
|
+
|
97
|
+
Add this line to your application's Gemfile:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
gem 'menu_txt'
|
101
|
+
```
|
102
|
+
|
103
|
+
And then execute:
|
104
|
+
|
105
|
+
$ bundle
|
106
|
+
|
107
|
+
Or install it yourself as:
|
108
|
+
|
109
|
+
$ gem install menu_txt
|
110
|
+
|
111
|
+
## Development
|
112
|
+
|
113
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
114
|
+
|
115
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
116
|
+
|
117
|
+
## Contributing
|
118
|
+
|
119
|
+
1. Fork it ( https://github.com/maxim/menu_txt/fork )
|
120
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
121
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
122
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
123
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/menu_txt.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'menu_txt/version'
|
2
|
+
require 'menu_txt/node'
|
3
|
+
require 'menu_txt/simple_node'
|
4
|
+
|
5
|
+
module MenuTxt
|
6
|
+
module_function
|
7
|
+
|
8
|
+
LINE_REGEXP = /\A(-*)\s*([^\|]+)\s*\|\s*(.+)\Z/.freeze
|
9
|
+
BLANK_REGEXP = /\A[[:space:]]*\z/.freeze
|
10
|
+
|
11
|
+
def parse_path(path, root = SimpleNode.new(nil))
|
12
|
+
File.open(path, 'r') { |file| parse(file, root) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse(io, root = SimpleNode.new(nil))
|
16
|
+
current_node = root
|
17
|
+
|
18
|
+
io.each_line do |line|
|
19
|
+
next if BLANK_REGEXP === line
|
20
|
+
indent, name, url = line.match(LINE_REGEXP).captures.map(&:strip)
|
21
|
+
current_node_indent_size = current_node.level + 1
|
22
|
+
|
23
|
+
if (indent.size - current_node_indent_size) > 1
|
24
|
+
raise "Can only nest submenus 1 level at a time: (#{line})"
|
25
|
+
elsif indent.size > current_node_indent_size
|
26
|
+
current_node = current_node.children.last
|
27
|
+
current_node.add_child(name, url)
|
28
|
+
elsif indent.size < current_node_indent_size
|
29
|
+
unindents_count = current_node_indent_size - indent.size
|
30
|
+
unindents_count.times { current_node = current_node.parent }
|
31
|
+
current_node.add_child(name, url)
|
32
|
+
else
|
33
|
+
current_node.add_child(name, url)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
root
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module MenuTxt
|
2
|
+
module Node
|
3
|
+
include ::Enumerable
|
4
|
+
|
5
|
+
def initialize(name, url = '')
|
6
|
+
@name, @url, @parent, @children, @level = name, url, nil, [], -1
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :name, :url, :parent, :children, :level
|
10
|
+
|
11
|
+
def leaf?; children.empty? end
|
12
|
+
|
13
|
+
def traverse(&block)
|
14
|
+
if block_given?
|
15
|
+
yield(self) if parent
|
16
|
+
|
17
|
+
children.each do |child|
|
18
|
+
child.traverse(&block)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
enum_for
|
22
|
+
end
|
23
|
+
end
|
24
|
+
alias_method :each, :traverse
|
25
|
+
|
26
|
+
def add_child(name, url)
|
27
|
+
self << self.class.new(name, url)
|
28
|
+
end
|
29
|
+
|
30
|
+
def <<(node)
|
31
|
+
node.parent = self
|
32
|
+
node.level = level + 1
|
33
|
+
children << node
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def level=(int)
|
39
|
+
@level = int
|
40
|
+
end
|
41
|
+
|
42
|
+
def parent=(node)
|
43
|
+
@parent = node
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/menu_txt.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 'menu_txt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "menu_txt"
|
8
|
+
spec.version = MenuTxt::VERSION
|
9
|
+
spec.authors = ["Maxim Chernyak"]
|
10
|
+
spec.email = ["max@bitsonnet.com"]
|
11
|
+
|
12
|
+
spec.summary = 'Build url menu trees in plain text with simple syntax.'
|
13
|
+
spec.homepage = "https://github.com/maxim/menu_txt"
|
14
|
+
spec.license = "MIT"
|
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.8"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "pry"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: menu_txt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maxim Chernyak
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-17 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.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
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: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- max@bitsonnet.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/menu_txt.rb
|
72
|
+
- lib/menu_txt/node.rb
|
73
|
+
- lib/menu_txt/simple_node.rb
|
74
|
+
- lib/menu_txt/version.rb
|
75
|
+
- menu_txt.gemspec
|
76
|
+
homepage: https://github.com/maxim/menu_txt
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.5
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Build url menu trees in plain text with simple syntax.
|
100
|
+
test_files: []
|
101
|
+
has_rdoc:
|