{"id":19316756,"url":"https://github.com/maxim/menu_txt","last_synced_at":"2025-04-22T17:30:28.411Z","repository":{"id":28880815,"uuid":"32405328","full_name":"maxim/menu_txt","owner":"maxim","description":"Build url menu trees in plain text with simple syntax.","archived":false,"fork":false,"pushed_at":"2015-03-18T03:29:55.000Z","size":136,"stargazers_count":6,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-26T04:03:03.681Z","etag":null,"topics":["datastructures","menus","rails"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-17T16:23:46.000Z","updated_at":"2018-01-30T10:26:19.000Z","dependencies_parsed_at":"2022-07-24T12:02:07.763Z","dependency_job_id":null,"html_url":"https://github.com/maxim/menu_txt","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fmenu_txt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fmenu_txt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fmenu_txt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fmenu_txt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxim","download_url":"https://codeload.github.com/maxim/menu_txt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250287337,"owners_count":21405588,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datastructures","menus","rails"],"created_at":"2024-11-10T01:12:35.664Z","updated_at":"2025-04-22T17:30:28.084Z","avatar_url":"https://github.com/maxim.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# menu_txt\n\n[![Build Status](https://travis-ci.org/maxim/menu_txt.svg?branch=master)](https://travis-ci.org/maxim/menu_txt)\n[![Code Climate](https://codeclimate.com/github/maxim/menu_txt/badges/gpa.svg)](https://codeclimate.com/github/maxim/menu_txt)\n\n\nIf you have non-technical people who want to be able to edit menus on your website, this is the gem for you.\n\n## Quick Start\n\nThis gem doesn't require Rails, but seeing how it works with Rails should give you a good enough idea of how to use it.\n\n\n### Syntax\n\nLet's create a text file somewhere, say `app/menus/my_awesome_menu.txt`.\n\n~~~plain\n1st level thing 1 | /foo1\n\n- 2nd level thing A | /foo1/bar1\n-- 3rd level thing A | /foo1/bar1/baz1\n-- 3rd level thing B | /foo1/bar1/baz2\n\n- 2nd level thing B | /foo1/bar2\n-- 3rd level thing A | /foo1/bar2/baz1\n\n- 2nd level thing C | /foo1/bar3\n\n1st level thing 2 | /foo2\n~~~\n\nDashes in front are nesting levels, a pipe `|` separates text from url. Blank lines are ignored. That's all there is to know.\n\n\n### Rails auto-reload\n\nWhen you're in Rails it's best to put all the menus in one dir, because you could then add this line to `development.rb`.\n\n~~~ruby\nconfig.watchable_dirs['app/menus'] = [:txt]\n~~~\n\nIt ensures that any changes to txt files in `app/menus` will reload your app, so you could see changes on a browser refresh.\n\n### Menu class (model)\n\nCreate a class for your menu, it could just be another model in your rails app, so let's put it in `app/models/my_awesome_menu.rb`.\n\n~~~ruby\nMyAwesomeMenu = MenuTxt.parse_path('app/menus/my_awesome_menu.txt')\n~~~\n\n### HTML\n\nNow we just need a partial that can render itself recursively for every submenu. For example, let's create `app/views/my_awesome_menu/_nodes.html.erb`.\n\n~~~html\n\u003c% nodes.each do |node| %\u003e\n\u003cli \u003c%=raw node.leaf? ? '' : 'class=\"dropdown-submenu\"' %\u003e\u003e\n  \u003c%= link_to node.name, node.url %\u003e\n\n  \u003c% unless node.leaf? %\u003e\n  \u003cul class=\"dropdown-menu\"\u003e\n    \u003c%= render 'my_awesome_menu/nodes', nodes: node.children %\u003e\n  \u003c/ul\u003e\n  \u003c% end %\u003e\n\u003c/li\u003e\n\u003c% end %\u003e\n~~~\n\nNotice how I use `leaf?` above to determine if there are no more submenus under this node.\n\nFinally, wherever you want to render this menu in your html, all you gotta do is the following.\n\n~~~html\n\u003cul class=\"dropdown-menu\"\u003e\n  \u003c%= render 'my_awesome_menu/nodes', nodes: MyAwesomeMenu.children %\u003e\n\u003c/ul\u003e\n~~~\n\nSo 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.\n\n## Advanced usage\n\n### Traversing nodes\n\nThe `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.\n\n### Extending nodes\n\nIf you want each node in your menu to have special methods, you can use your own node class. Simply create it kinda like this.\n\n~~~ruby\nclass MySpecialMenuNode\n  include MenuTxt::Node\n\n  def highlight?\n    url.include?('special_deals')\n  end\nend\n~~~\n\nThe `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.\n\n~~~ruby\nMyAwesomeMenu =\n  MenuTxt.parse_path('app/menus/menu.txt', MySpecialMenuNode.new(nil))\n~~~\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'menu_txt'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install menu_txt\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo 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).\n\n## Contributing\n\n1. Fork it ( https://github.com/maxim/menu_txt/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxim%2Fmenu_txt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxim%2Fmenu_txt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxim%2Fmenu_txt/lists"}