indent_parser 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rspec'
4
+
5
+ # Specify your gem's dependencies in indent_parser.gemspec
6
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "indent_parser/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "indent_parser"
7
+ s.version = IndentParser::VERSION
8
+ s.authors = ["Jeff Su"]
9
+ s.email = ["me@jeffsu.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Parser for python-style (or haml, sass, etc...) indent markup languages}
12
+ s.description = %q{Parser for python-style (or haml, sass, etc...) indent markup languages}
13
+
14
+ s.rubyforge_project = "indent_parser"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,3 @@
1
+ module IndentParser
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,68 @@
1
+ require "indent_parser/version"
2
+
3
+ module IndentParser
4
+ def self.parse(content)
5
+ root = Node.new
6
+ stack = [ root ]
7
+
8
+ content.split(/\r?\n/).each do |line|
9
+ next if line.match(/^\s*$/)
10
+
11
+ node = Node.new(line)
12
+ depth = stack.length
13
+
14
+ # node is 1 deeper
15
+ if node.depth == depth + 1
16
+ stack.last.add_child node
17
+ stack << node
18
+
19
+ # node is shallower
20
+ elsif node.depth <= depth
21
+ (depth - node.depth).times { stack.pop }
22
+ stack.last.add_child node
23
+ stack << node
24
+ end
25
+ end
26
+
27
+ return root
28
+ end
29
+
30
+ class Node
31
+ include Enumerable
32
+
33
+ attr_accessor :content, :depth
34
+
35
+ def initialize(str = nil)
36
+ if str && m = str.match(/^(\s*)(.*)$/)
37
+ @depth = (m[1].length / 2) + 1
38
+ @content = m[2]
39
+ else
40
+ @depth = 0
41
+ end
42
+ end
43
+
44
+ def each
45
+ children.each { |c| yield(c) }
46
+ end
47
+
48
+ def [](index)
49
+ return children[index]
50
+ end
51
+
52
+ def children
53
+ return @children ||= []
54
+ end
55
+
56
+ def add_child(node)
57
+ children << node
58
+ end
59
+
60
+ def to_struct
61
+ return {
62
+ :content => content,
63
+ :depth => depth,
64
+ :children => (children || []).collect { |c| c.to_struct }
65
+ }
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,12 @@
1
+ require "./lib/indent_parser"
2
+
3
+ describe IndentParser do
4
+ describe "#parse" do
5
+ it "parses basic format" do
6
+ root = IndentParser.parse("foo\n bar")
7
+ foo = root.first
8
+ foo.content.should == 'foo'
9
+ foo[0].content.should == 'bar'
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indent_parser
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Su
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-27 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Parser for python-style (or haml, sass, etc...) indent markup languages
18
+ email:
19
+ - me@jeffsu.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - Rakefile
30
+ - indent_parser.gemspec
31
+ - lib/indent_parser.rb
32
+ - lib/indent_parser/version.rb
33
+ - specs/basic_spec.rb
34
+ has_rdoc: true
35
+ homepage: ""
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ requirements: []
56
+
57
+ rubyforge_project: indent_parser
58
+ rubygems_version: 1.6.2
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Parser for python-style (or haml, sass, etc...) indent markup languages
62
+ test_files: []
63
+