document_tree 1.0.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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ doc/*
23
+
24
+ .yardoc
25
+ .yardoc/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jaap van der Meer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = document_tree
2
+
3
+ DocumentTree is a gem that can read out a simple file tree. It can be used to generate a tree with documentation in Ruby on Rails, so that pages don't have to
4
+ be in a database.
5
+
6
+ == Usage
7
+ You must have a folder that will be read some where. For example the following example will generate a tree.
8
+
9
+ - section (folder)
10
+ - meta.txt (file)
11
+ - othersection (folder)
12
+ - somefile.txt (file)
13
+
14
+
15
+ If you now invoke DocumentTree.root.name the meta.txt is read for a real name.
16
+
17
+
18
+ == Meta data
19
+ All meta data is YAML format. Normal files have meta data on top of the file, splitted by line. Directories have a meta data on top of meta.txt.
20
+
21
+ == Format folder meta data
22
+ The meta data for a folder is read by reading the meta.txt of it's children.
23
+ For example, meta.txt has the following content, the first line is the YAML meta data, the rest is +content+.
24
+
25
+ name: DocumentTree documentation
26
+
27
+ Here comes the text, maybe formatted in some way.
28
+ Tralala.
29
+
30
+ Now the following code:
31
+
32
+ node = DocumentTree.root
33
+ node.name
34
+ # => DocumentTree.documentation
35
+ node.content
36
+ # => Here comes the text, maybe formatted in some way.\nTralala.
37
+ node.raw_name
38
+ # => section
39
+
40
+
41
+ == Finding a node
42
+ You can search for a node by doing
43
+
44
+ DocumentTree.root.find_node("hisrawname")
45
+
46
+
47
+
48
+ == Returning all nodes
49
+
50
+ DocumentTree.root.all_nodes
51
+
52
+
53
+
54
+
55
+ == Copyright
56
+
57
+ Copyright (c) 2010 Jaap van der Meer. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "document_tree"
8
+ gem.summary = %Q{Transforms a file system in a tree with content and meta data.}
9
+ gem.description = %Q{}
10
+ gem.email = "jaapvandermeer@gmail.com"
11
+ gem.homepage = "http://github.com/japetheape/document_tree"
12
+ gem.authors = ["Jaap van der Meer"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ begin
39
+ require 'yard'
40
+ YARD::Rake::YardocTask.new
41
+ rescue LoadError
42
+ task :yardoc do
43
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
44
+ end
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{document_tree}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jaap van der Meer"]
12
+ s.date = %q{2010-10-26}
13
+ s.description = %q{}
14
+ s.email = %q{jaapvandermeer@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "document_tree.gemspec",
27
+ "lib/document_node.rb",
28
+ "lib/document_tree.rb",
29
+ "spec/doc_tree/0001_somepage.txt",
30
+ "spec/doc_tree/section/other_section.txt",
31
+ "spec/doc_tree/section_with_meta/meta.txt",
32
+ "spec/doc_tree/some_page.txt",
33
+ "spec/document_node_spec.rb",
34
+ "spec/document_tree_spec.rb",
35
+ "spec/spec_helper.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/japetheape/document_tree}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.7}
41
+ s.summary = %q{Transforms a file system in a tree with content and meta data.}
42
+ s.test_files = [
43
+ "spec/document_node_spec.rb",
44
+ "spec/document_tree_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
54
+ s.add_development_dependency(%q<yard>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ s.add_dependency(%q<yard>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
61
+ s.add_dependency(%q<yard>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,120 @@
1
+ require 'yaml'
2
+ class DocumentNode
3
+
4
+ def initialize(filename)
5
+ @filename = filename
6
+ end
7
+
8
+ def children
9
+ files = Dir.glob(File.join(@filename, '*'))
10
+ files.reject {|x| /meta\.txt/.match(x) }.map{ |x| DocumentNode.new(x) }
11
+ end
12
+
13
+
14
+ def is_root?
15
+ @filename == DocumentTree.root.raw_filename
16
+ end
17
+
18
+ def raw_filename
19
+ @filename
20
+ end
21
+
22
+ # Is it a directory or just a file?
23
+ def is_leaf?
24
+ !File.directory?(@filename)
25
+ end
26
+
27
+ # Filename
28
+ def filename
29
+ is_leaf? ? @filename : meta_file
30
+ end
31
+
32
+
33
+ def meta_file
34
+ File.join(@filename, 'meta.txt')
35
+ end
36
+
37
+
38
+ def meta_data
39
+ meta_yaml
40
+ end
41
+
42
+ # The meta information of this node. If it's a file, than the meta.txt of it's child files is taken, else
43
+ # the file itself is taken. From this file the top part must be the YAML meta data.
44
+ def meta_yaml
45
+ if !(!is_leaf? && !File.exist?(meta_file))
46
+ passed = false
47
+ out = ""
48
+ File.open(filename).each_line do |line|
49
+ passed = true if line == "\n"
50
+ break if passed
51
+ out << line
52
+ end
53
+ YAML::load(out)
54
+ else
55
+ {:name => raw_name}
56
+ end
57
+ end
58
+
59
+ # The content of the node, that is: the part after
60
+ # the yaml file.
61
+ def content
62
+ out = ""
63
+ passed = false
64
+ File.open(filename) do |f|
65
+ f.each_line do |line|
66
+ if line == "\n" && !passed
67
+ passed = true
68
+ else
69
+ out << line if passed
70
+ end
71
+ end
72
+ end
73
+ out
74
+ end
75
+
76
+ # Raw folder name of this node.
77
+ # Strips out 0001_foldername to make positioning possible
78
+ def raw_name(options = {})
79
+ out = /.*\/[0-9]*_?(.*)(.txt)?/.match(@filename)[1]
80
+ out.gsub!('.txt', '') unless options[:ext]
81
+ out
82
+ #out.gsub('.txt', '') #
83
+ out
84
+ end
85
+
86
+ # Searches breadth first till name is found
87
+ def find_node(name)
88
+ to_search = self.children
89
+ while !to_search.empty?
90
+ n = to_search.pop
91
+ return n if n.raw_name == name
92
+ to_search += n.children
93
+ end
94
+ nil
95
+ end
96
+
97
+
98
+ # Searches breadth first till name is found
99
+ def all_nodes
100
+ all = self.children
101
+ to_search = self.children
102
+ while !to_search.empty?
103
+ n = to_search.pop
104
+ to_search += n.children
105
+ all += n.children
106
+ end
107
+ all
108
+ end
109
+
110
+
111
+ def name
112
+ meta_yaml['name'] || raw_name
113
+ end
114
+
115
+
116
+
117
+
118
+
119
+ end
120
+
@@ -0,0 +1,19 @@
1
+
2
+ require 'document_node'
3
+ module DocumentTree
4
+
5
+
6
+ class << self
7
+ attr_accessor :location
8
+ def location
9
+ @location || (defined?(Rails) ? File.join(Rails.root, 'app', 'docs') : 'docs')
10
+ end
11
+
12
+
13
+ def root
14
+ @root ||= DocumentNode.new(self.location)
15
+ end
16
+ end
17
+
18
+ end
19
+
@@ -0,0 +1,3 @@
1
+ name: haha
2
+
3
+ test
@@ -0,0 +1,5 @@
1
+ name: adsfadasd
2
+
3
+ asdasdasd
4
+ asd
5
+ asdasdasd
@@ -0,0 +1,3 @@
1
+ name: Some section
2
+
3
+ sdfsdf
@@ -0,0 +1,3 @@
1
+ name: "Men"
2
+
3
+ Testerdetest
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ require 'document_tree'
4
+ require 'document_node'
5
+ include DocumentTree
6
+
7
+ describe DocumentNode do
8
+
9
+
10
+ it "should have some children" do
11
+ DocumentTree.root.children.size.should > 0
12
+ DocumentTree.root.children.first.kind_of?(DocumentNode).should be true
13
+ end
14
+
15
+ it "should have a folder with children" do
16
+ folder_with_children = false
17
+ DocumentTree.root.children.each do |c|
18
+ folder_with_children = folder_with_children || !c.children.empty?
19
+ end
20
+ folder_with_children.should == true
21
+ end
22
+
23
+
24
+ it "should read meta data of a directory without meta.txt" do
25
+ DocumentTree.root.children[1].is_leaf?.should == false
26
+ DocumentTree.root.children[1].meta_data[:name].should == "section"
27
+ end
28
+
29
+
30
+ it "should read meta data of a file with meta.txt" do
31
+ node = DocumentTree.root.children[2]
32
+ node.children.empty?.should == true
33
+ node.is_leaf?.should == false
34
+ File.exist?(node.meta_file).should == true
35
+ node.name.should == "Some section"
36
+ node.content.should == "sdfsdf"
37
+ end
38
+
39
+
40
+ it "should read meta data of a normal file" do
41
+ node = DocumentTree.root.children[3]
42
+ node.meta_data['name'].should == "Men"
43
+ node.content.should == "Testerdetest"
44
+ end
45
+
46
+
47
+ it "should find a node" do
48
+ found = DocumentTree.root.find_node('other_section')
49
+ found.should_not be_nil
50
+ end
51
+
52
+ it "should have all nodes" do
53
+ all = DocumentTree.root.all_nodes
54
+ all.length.should == 5
55
+ end
56
+
57
+
58
+ it "should have a name and stuff like that" do
59
+ node = DocumentTree.root.find_node("other_section")
60
+ node.name.should == "adsfadasd"
61
+ node.content.should == "asdasdasd\nasd\nasdasdasd"
62
+ end
63
+
64
+ it "should have a name" do
65
+ node = DocumentTree.root
66
+ node.name.should == "doc_tree"
67
+ end
68
+
69
+ describe "#raw_name" do
70
+ it "should take a :txt options" do
71
+ node = DocumentTree.root.find_node("other_section")
72
+ node.raw_name.should == "other_section"
73
+ node.raw_name(:ext => true).should == "other_section.txt"
74
+ end
75
+ end
76
+
77
+ it "should be root" do
78
+ DocumentTree.root.is_root?.should == true
79
+ end
80
+
81
+
82
+ it "should strip the 0001_" do
83
+ DocumentTree.root.children[0].raw_name.should == "somepage"
84
+ end
85
+
86
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ require 'document_tree'
4
+ require 'document_node'
5
+ include DocumentTree
6
+
7
+ describe DocumentTree do
8
+
9
+ it "should be able to set another location" do
10
+ DocumentTree.location.should_not be_nil
11
+
12
+ end
13
+
14
+ it "should have a root node" do
15
+ DocumentTree.root.should_not be_nil
16
+ end
17
+
18
+ end
@@ -0,0 +1,25 @@
1
+
2
+ require 'document_tree'
3
+ # Requires supporting ruby files with custom matchers and macros, etc,
4
+ # in spec/support/ and its subdirectories.
5
+ DocumentTree.location = File.join( File.dirname(__FILE__), 'doc_tree')
6
+
7
+
8
+ RSpec.configure do |config|
9
+ # == Mock Framework
10
+ #
11
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
12
+ #
13
+ # config.mock_with :mocha
14
+ # config.mock_with :flexmock
15
+ # config.mock_with :rr
16
+ config.mock_with :rspec
17
+
18
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
19
+ #config.fixture_path = "#{::Rails.root}/spec/fixtures"
20
+
21
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
22
+ # examples within a transaction, remove the following line or assign false
23
+ # instead of true.
24
+ # config.use_transactional_fixtures = true
25
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: document_tree
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Jaap van der Meer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-26 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: yard
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: ""
49
+ email: jaapvandermeer@gmail.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - LICENSE
56
+ - README.rdoc
57
+ files:
58
+ - .document
59
+ - .gitignore
60
+ - LICENSE
61
+ - README.rdoc
62
+ - Rakefile
63
+ - VERSION
64
+ - document_tree.gemspec
65
+ - lib/document_node.rb
66
+ - lib/document_tree.rb
67
+ - spec/doc_tree/0001_somepage.txt
68
+ - spec/doc_tree/section/other_section.txt
69
+ - spec/doc_tree/section_with_meta/meta.txt
70
+ - spec/doc_tree/some_page.txt
71
+ - spec/document_node_spec.rb
72
+ - spec/document_tree_spec.rb
73
+ - spec/spec_helper.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/japetheape/document_tree
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.7
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Transforms a file system in a tree with content and meta data.
106
+ test_files:
107
+ - spec/document_node_spec.rb
108
+ - spec/document_tree_spec.rb
109
+ - spec/spec_helper.rb