nodifier 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ module Indentation
2
+
3
+ def unindent(content)
4
+ output = ''
5
+
6
+ first_line = content.lines.first
7
+ leading_spaces = first_line.length - first_line.lstrip.length
8
+
9
+ content.lines do |line|
10
+ output << line[leading_spaces..-1] || ""
11
+ end
12
+
13
+ output
14
+ end
15
+
16
+ end
data/lib/node.rb ADDED
@@ -0,0 +1,9 @@
1
+ class Node
2
+ attr_reader :label
3
+ attr_reader :children
4
+
5
+ def initialize(label)
6
+ @label = label
7
+ @children = []
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/node'
2
+
3
+ class NodeParser
4
+ def parse(text)
5
+ Node.new text
6
+ end
7
+ end
data/lib/nodifier.rb ADDED
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/indentation'
2
+ require File.dirname(__FILE__) + '/node'
3
+ require File.dirname(__FILE__) + '/node_parser'
4
+
5
+ class Nodifier
6
+ include Indentation
7
+
8
+ def initialize(parser = nil)
9
+ @parser = parser || NodeParser.new
10
+ end
11
+
12
+ def nodify(content)
13
+ nodes = []
14
+ current_node = nil
15
+ child_content = ''
16
+
17
+ content = unindent content
18
+
19
+ content.lines do |line|
20
+ if not line.start_with?(' ')
21
+ if not child_content.empty?
22
+ append_child_content current_node, child_content
23
+ child_content = ''
24
+ end
25
+
26
+ current_node = @parser.parse line.rstrip
27
+ nodes << current_node
28
+ else
29
+ child_content << line
30
+ end
31
+ end
32
+
33
+ append_child_content current_node, child_content unless child_content.empty?
34
+
35
+ nodes
36
+ end
37
+
38
+ def append_child_content(node, child_content)
39
+ nodify(child_content).each do |child_node|
40
+ node.children << child_node
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nodifier
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Tao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-11-02 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: A simple Ruby library for parsing plain text into a node-based structure
17
+ email: daniel.tao@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/indentation.rb
26
+ - lib/node.rb
27
+ - lib/node_parser.rb
28
+ - lib/nodifier.rb
29
+ homepage: http://github.com/dtao/nodifier
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.10
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Nodifier, for parsing text into nodes
56
+ test_files: []
57
+