org-ruby 0.3.0 → 0.4.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.
Files changed (58) hide show
  1. data/.bnsignore +18 -0
  2. data/History.txt +40 -32
  3. data/README.txt +66 -66
  4. data/Rakefile +27 -26
  5. data/bin/org-ruby +40 -40
  6. data/lib/org-ruby.rb +50 -50
  7. data/lib/org-ruby/headline.rb +80 -75
  8. data/lib/org-ruby/html_output_buffer.rb +105 -81
  9. data/lib/org-ruby/line.rb +173 -173
  10. data/lib/org-ruby/output_buffer.rb +172 -154
  11. data/lib/org-ruby/parser.rb +80 -76
  12. data/lib/org-ruby/regexp_helper.rb +156 -156
  13. data/lib/org-ruby/textile_output_buffer.rb +67 -67
  14. data/spec/data/freeform.org +111 -111
  15. data/spec/data/hyp-planning.org +335 -335
  16. data/spec/data/remember.org +53 -53
  17. data/spec/headline_spec.rb +55 -55
  18. data/spec/html_examples/advanced-lists.html +31 -0
  19. data/spec/html_examples/advanced-lists.org +31 -0
  20. data/spec/html_examples/block_code.html +30 -30
  21. data/spec/html_examples/block_code.org +35 -35
  22. data/spec/html_examples/blockquote.html +7 -7
  23. data/spec/html_examples/blockquote.org +13 -13
  24. data/spec/html_examples/escape-pre.html +7 -7
  25. data/spec/html_examples/escape-pre.org +6 -6
  26. data/spec/html_examples/inline-formatting.html +10 -10
  27. data/spec/html_examples/inline-formatting.org +17 -17
  28. data/spec/html_examples/lists.html +19 -19
  29. data/spec/html_examples/lists.org +36 -36
  30. data/spec/html_examples/only-list.html +5 -5
  31. data/spec/html_examples/only-list.org +3 -3
  32. data/spec/html_examples/only-table.html +6 -6
  33. data/spec/html_examples/only-table.org +5 -5
  34. data/spec/html_examples/tables.html +20 -20
  35. data/spec/html_examples/tables.org +26 -26
  36. data/spec/html_examples/text.html +2 -2
  37. data/spec/html_examples/text.org +16 -16
  38. data/spec/line_spec.rb +89 -89
  39. data/spec/parser_spec.rb +86 -86
  40. data/spec/regexp_helper_spec.rb +57 -57
  41. data/spec/spec_helper.rb +20 -20
  42. data/spec/textile_examples/block_code.org +35 -35
  43. data/spec/textile_examples/block_code.textile +29 -29
  44. data/spec/textile_examples/blockquote.org +13 -13
  45. data/spec/textile_examples/blockquote.textile +11 -11
  46. data/spec/textile_examples/keywords.org +13 -13
  47. data/spec/textile_examples/keywords.textile +11 -11
  48. data/spec/textile_examples/links.org +11 -11
  49. data/spec/textile_examples/links.textile +10 -10
  50. data/spec/textile_examples/lists.org +36 -36
  51. data/spec/textile_examples/lists.textile +20 -20
  52. data/spec/textile_examples/single-space-plain-list.org +13 -13
  53. data/spec/textile_examples/single-space-plain-list.textile +10 -10
  54. data/spec/textile_examples/tables.org +26 -26
  55. data/spec/textile_examples/tables.textile +23 -23
  56. data/spec/textile_output_buffer_spec.rb +21 -21
  57. data/tasks/test_case.rake +49 -49
  58. metadata +5 -2
@@ -1,154 +1,172 @@
1
- require 'logger'
2
-
3
- module Orgmode
4
-
5
- # The OutputBuffer is used to accumulate multiple lines of orgmode
6
- # text, and then emit them to the output all in one go. The class
7
- # will do the final textile substitution for inline formatting and
8
- # add a newline character prior emitting the output.
9
- class OutputBuffer
10
-
11
- # This is the temporary buffer that we accumulate into.
12
- attr_reader :buffer
13
-
14
- # This is the overall output buffer
15
- attr_reader :output
16
-
17
- # This is the current type of output being accumulated.
18
- attr_accessor :output_type
19
-
20
- # Creates a new OutputBuffer object that is bound to an output object.
21
- # The output will get flushed to =output=.
22
- def initialize(output)
23
- @output = output
24
- @buffer = ""
25
- @output_type = :start
26
- @list_indent_stack = []
27
- @paragraph_modifier = nil
28
- @cancel_modifier = false
29
- @mode_stack = []
30
- push_mode(:normal)
31
-
32
- @logger = Logger.new(STDERR)
33
- @logger.level = Logger::WARN
34
-
35
- @re_help = RegexpHelper.new
36
- end
37
-
38
- Modes = [:normal, :ordered_list, :unordered_list, :blockquote, :code, :table]
39
-
40
- def current_mode
41
- @mode_stack.last
42
- end
43
-
44
- def current_mode_list?
45
- (current_mode == :ordered_list) or (current_mode == :unordered_list)
46
- end
47
-
48
- def push_mode(mode)
49
- raise "Not a recognized mode: #{mode}" unless Modes.include?(mode)
50
- @mode_stack.push(mode)
51
- end
52
-
53
- def pop_mode(mode = nil)
54
- m = @mode_stack.pop
55
- @logger.warn "Modes don't match. Expected to pop #{mode}, but popped #{m}" if mode && mode != m
56
- m
57
- end
58
-
59
- # Prepares the output buffer to receive content from a line.
60
- # As a side effect, this may flush the current accumulated text.
61
- def prepare(line)
62
- @logger.debug "Looking at #{line.paragraph_type}: #{line.to_s}"
63
- if not should_accumulate_output?(line) then
64
- flush!
65
- maintain_list_indent_stack(line)
66
- @output_type = line.paragraph_type
67
- end
68
- push_mode(:table) if enter_table?
69
- pop_mode(:table) if exit_table?
70
- end
71
-
72
- # Tests if we are entering a table mode.
73
- def enter_table?
74
- ((@output_type == :table_row) || (@output_type == :table_separator)) &&
75
- (current_mode != :table)
76
- end
77
-
78
- # Tests if we are existing a table mode.
79
- def exit_table?
80
- ((@output_type != :table_row) && (@output_type != :table_separator)) &&
81
- (current_mode == :table)
82
- end
83
-
84
- # Accumulate the string @str@.
85
- def << (str)
86
- @buffer << str
87
- end
88
-
89
- # Gets the current list indent level.
90
- def list_indent_level
91
- @list_indent_stack.length
92
- end
93
-
94
- # Test if we're in an output mode in which whitespace is significant.
95
- def preserve_whitespace?
96
- return current_mode == :code
97
- end
98
-
99
- ######################################################################
100
- private
101
-
102
- def maintain_list_indent_stack(line)
103
- if (line.plain_list?) then
104
- while (not @list_indent_stack.empty? \
105
- and (@list_indent_stack.last > line.indent))
106
- @list_indent_stack.pop
107
- pop_mode
108
- end
109
- if (@list_indent_stack.empty? \
110
- or @list_indent_stack.last < line.indent)
111
- @list_indent_stack.push(line.indent)
112
- push_mode line.paragraph_type
113
- end
114
- else
115
- @list_indent_stack = []
116
- while ((current_mode == :ordered_list) or
117
- (current_mode == :unordered_list))
118
- pop_mode
119
- end
120
- end
121
- end
122
-
123
- # Tests if the current line should be accumulated in the current
124
- # output buffer. (Extraneous line breaks in the orgmode buffer
125
- # are removed by accumulating lines in the output buffer without
126
- # line breaks.)
127
- def should_accumulate_output?(line)
128
-
129
- # Special case: Preserve line breaks in block code mode.
130
- return false if preserve_whitespace?
131
-
132
- # Special case: Multiple blank lines get accumulated.
133
- return true if line.paragraph_type == :blank and @output_type == :blank
134
-
135
- # Currently only "paragraphs" get accumulated with previous output.
136
- return false unless line.paragraph_type == :paragraph
137
- if ((@output_type == :ordered_list) or
138
- (@output_type == :unordered_list)) then
139
-
140
- # If the previous output type was a list item, then we only put a paragraph in it
141
- # if its indent level is greater than the list indent level.
142
-
143
- return false unless line.indent > @list_indent_stack.last
144
- end
145
-
146
- # Only accumulate paragraphs with lists & paragraphs.
147
- return false unless
148
- ((@output_type == :paragraph) or
149
- (@output_type == :ordered_list) or
150
- (@output_type == :unordered_list))
151
- true
152
- end
153
- end # class OutputBuffer
154
- end # module Orgmode
1
+ require 'logger'
2
+
3
+ module Orgmode
4
+
5
+ # The OutputBuffer is used to accumulate multiple lines of orgmode
6
+ # text, and then emit them to the output all in one go. The class
7
+ # will do the final textile substitution for inline formatting and
8
+ # add a newline character prior emitting the output.
9
+ class OutputBuffer
10
+
11
+ # This is the temporary buffer that we accumulate into.
12
+ attr_reader :buffer
13
+
14
+ # This is the overall output buffer
15
+ attr_reader :output
16
+
17
+ # This is the current type of output being accumulated.
18
+ attr_accessor :output_type
19
+
20
+ # Creates a new OutputBuffer object that is bound to an output object.
21
+ # The output will get flushed to =output=.
22
+ def initialize(output)
23
+ @output = output
24
+ @buffer = ""
25
+ @output_type = :start
26
+ @list_indent_stack = []
27
+ @paragraph_modifier = nil
28
+ @cancel_modifier = false
29
+ @mode_stack = []
30
+ push_mode(:normal)
31
+
32
+ @logger = Logger.new(STDERR)
33
+ @logger.level = Logger::WARN
34
+
35
+ @re_help = RegexpHelper.new
36
+ end
37
+
38
+ Modes = [:normal, :ordered_list, :unordered_list, :blockquote, :code, :table]
39
+
40
+ def current_mode
41
+ @mode_stack.last
42
+ end
43
+
44
+ def current_mode_list?
45
+ (current_mode == :ordered_list) or (current_mode == :unordered_list)
46
+ end
47
+
48
+ def push_mode(mode)
49
+ raise "Not a recognized mode: #{mode}" unless Modes.include?(mode)
50
+ @mode_stack.push(mode)
51
+ end
52
+
53
+ def pop_mode(mode = nil)
54
+ m = @mode_stack.pop
55
+ @logger.warn "Modes don't match. Expected to pop #{mode}, but popped #{m}" if mode && mode != m
56
+ m
57
+ end
58
+
59
+ # Prepares the output buffer to receive content from a line.
60
+ # As a side effect, this may flush the current accumulated text.
61
+ def prepare(line)
62
+ @logger.debug "Looking at #{line.paragraph_type}: #{line.to_s}"
63
+ if not should_accumulate_output?(line) then
64
+ flush!
65
+ maintain_list_indent_stack(line)
66
+ @output_type = line.paragraph_type
67
+ end
68
+ push_mode(:table) if enter_table?
69
+ pop_mode(:table) if exit_table?
70
+ end
71
+
72
+ # Tests if we are entering a table mode.
73
+ def enter_table?
74
+ ((@output_type == :table_row) || (@output_type == :table_separator)) &&
75
+ (current_mode != :table)
76
+ end
77
+
78
+ # Tests if we are existing a table mode.
79
+ def exit_table?
80
+ ((@output_type != :table_row) && (@output_type != :table_separator)) &&
81
+ (current_mode == :table)
82
+ end
83
+
84
+ # Accumulate the string @str@.
85
+ def << (str)
86
+ @buffer << str
87
+ end
88
+
89
+ # Gets the current list indent level.
90
+ def list_indent_level
91
+ @list_indent_stack.length
92
+ end
93
+
94
+ # Test if we're in an output mode in which whitespace is significant.
95
+ def preserve_whitespace?
96
+ return current_mode == :code
97
+ end
98
+
99
+ ######################################################################
100
+ private
101
+
102
+ # call-seq:
103
+ # continue_current_list? => boolean
104
+ #
105
+ # Tests if the line should continue the current list.
106
+ def continue_current_list?(line)
107
+ end
108
+
109
+ def maintain_list_indent_stack(line)
110
+ if (line.plain_list?) then
111
+ while (not @list_indent_stack.empty? \
112
+ and (@list_indent_stack.last > line.indent))
113
+ @list_indent_stack.pop
114
+ pop_mode
115
+ end
116
+ if (@list_indent_stack.empty? \
117
+ or @list_indent_stack.last < line.indent)
118
+ @list_indent_stack.push(line.indent)
119
+ push_mode line.paragraph_type
120
+ end
121
+ elsif line.blank? then
122
+
123
+ # Nothing
124
+
125
+ elsif ((line.paragraph_type == :paragraph) and
126
+ (not @list_indent_stack.empty? and
127
+ line.indent > @list_indent_stack.last))
128
+
129
+ # Nothing -- output this paragraph inside
130
+ # the list block (ul/ol)
131
+
132
+ else
133
+ @list_indent_stack = []
134
+ while ((current_mode == :ordered_list) or
135
+ (current_mode == :unordered_list))
136
+ pop_mode
137
+ end
138
+ end
139
+ end
140
+
141
+ # Tests if the current line should be accumulated in the current
142
+ # output buffer. (Extraneous line breaks in the orgmode buffer
143
+ # are removed by accumulating lines in the output buffer without
144
+ # line breaks.)
145
+ def should_accumulate_output?(line)
146
+
147
+ # Special case: Preserve line breaks in block code mode.
148
+ return false if preserve_whitespace?
149
+
150
+ # Special case: Multiple blank lines get accumulated.
151
+ return true if line.paragraph_type == :blank and @output_type == :blank
152
+
153
+ # Currently only "paragraphs" get accumulated with previous output.
154
+ return false unless line.paragraph_type == :paragraph
155
+ if ((@output_type == :ordered_list) or
156
+ (@output_type == :unordered_list)) then
157
+
158
+ # If the previous output type was a list item, then we only put a paragraph in it
159
+ # if its indent level is greater than the list indent level.
160
+
161
+ return false unless line.indent > @list_indent_stack.last
162
+ end
163
+
164
+ # Only accumulate paragraphs with lists & paragraphs.
165
+ return false unless
166
+ ((@output_type == :paragraph) or
167
+ (@output_type == :ordered_list) or
168
+ (@output_type == :unordered_list))
169
+ true
170
+ end
171
+ end # class OutputBuffer
172
+ end # module Orgmode
@@ -1,76 +1,80 @@
1
- require 'rubypants'
2
-
3
- module Orgmode
4
-
5
- ##
6
- ## Simple routines for loading / saving an ORG file.
7
- ##
8
-
9
- class Parser
10
-
11
- # All of the lines of the orgmode file
12
- attr_reader :lines
13
-
14
- # All of the headlines in the org file
15
- attr_reader :headlines
16
-
17
- # These are any lines before the first headline
18
- attr_reader :header_lines
19
-
20
- # I can construct a parser object either with an array of lines
21
- # or with a single string that I will split along \n boundaries.
22
- def initialize(lines)
23
- if lines.is_a? Array then
24
- @lines = lines
25
- elsif lines.is_a? String then
26
- @lines = lines.split("\n")
27
- else
28
- raise "Unsupported type for +lines+: #{lines.class}"
29
- end
30
-
31
- @headlines = Array.new
32
- @current_headline = nil
33
- @header_lines = []
34
- @lines.each do |line|
35
- if (Headline.headline? line) then
36
- @current_headline = Headline.new line
37
- @headlines << @current_headline
38
- else
39
- line = Line.new line
40
- if (@current_headline) then
41
- @current_headline.body_lines << line
42
- else
43
- @header_lines << line
44
- end
45
- end
46
- end
47
- end # initialize
48
-
49
- # Creates a new parser from the data in a given file
50
- def self.load(fname)
51
- lines = IO.readlines(fname)
52
- return self.new(lines)
53
- end
54
-
55
- # Saves the loaded orgmode file as a textile file.
56
- def to_textile
57
- output = ""
58
- output << Line.to_textile(@header_lines)
59
- @headlines.each do |headline|
60
- output << headline.to_textile
61
- end
62
- output
63
- end
64
-
65
- # Converts the loaded org-mode file to HTML.
66
- def to_html
67
- output = ""
68
- output << Line.to_html(@header_lines)
69
- @headlines.each do |headline|
70
- output << headline.to_html
71
- end
72
- rp = RubyPants.new(output)
73
- rp.to_html
74
- end
75
- end # class Parser
76
- end # module Orgmode
1
+ require 'rubygems'
2
+ require 'rubypants'
3
+
4
+ module Orgmode
5
+
6
+ ##
7
+ ## Simple routines for loading / saving an ORG file.
8
+ ##
9
+
10
+ class Parser
11
+
12
+ # All of the lines of the orgmode file
13
+ attr_reader :lines
14
+
15
+ # All of the headlines in the org file
16
+ attr_reader :headlines
17
+
18
+ # These are any lines before the first headline
19
+ attr_reader :header_lines
20
+
21
+ # I can construct a parser object either with an array of lines
22
+ # or with a single string that I will split along \n boundaries.
23
+ def initialize(lines)
24
+ if lines.is_a? Array then
25
+ @lines = lines
26
+ elsif lines.is_a? String then
27
+ @lines = lines.split("\n")
28
+ else
29
+ raise "Unsupported type for +lines+: #{lines.class}"
30
+ end
31
+
32
+ @headlines = Array.new
33
+ @current_headline = nil
34
+ @header_lines = []
35
+ @lines.each do |line|
36
+ if (Headline.headline? line) then
37
+ @current_headline = Headline.new line
38
+ @headlines << @current_headline
39
+ else
40
+ line = Line.new line
41
+ if (@current_headline) then
42
+ @current_headline.body_lines << line
43
+ else
44
+ @header_lines << line
45
+ end
46
+ end
47
+ end
48
+ end # initialize
49
+
50
+ # Creates a new parser from the data in a given file
51
+ def self.load(fname)
52
+ lines = IO.readlines(fname)
53
+ return self.new(lines)
54
+ end
55
+
56
+ # Saves the loaded orgmode file as a textile file.
57
+ def to_textile
58
+ output = ""
59
+ output << Line.to_textile(@header_lines)
60
+ @headlines.each do |headline|
61
+ output << headline.to_textile
62
+ end
63
+ output
64
+ end
65
+
66
+ # Converts the loaded org-mode file to HTML.
67
+ def to_html
68
+ output = ""
69
+ decorate = true
70
+ output << Line.to_html(@header_lines, :decorate_title => decorate)
71
+ decorate = (output.length == 0)
72
+ @headlines.each do |headline|
73
+ output << headline.to_html(:decorate_title => decorate)
74
+ decorate = (output.length == 0)
75
+ end
76
+ rp = RubyPants.new(output)
77
+ rp.to_html
78
+ end
79
+ end # class Parser
80
+ end # module Orgmode