Class: Orgmode::Line
- Inherits:
-
Object
- Object
- Orgmode::Line
- Defined in:
- lib/org-ruby/line.rb
Overview
Represents a single line of an orgmode file.
Direct Known Subclasses
Constant Summary collapse
- PropertyDrawerRegexp =
/^\s*:(PROPERTIES|END):/i
- PropertyDrawerItemRegexp =
/^\s*:([0-9A-Za-z_\-]+):\s*(.*)$/i
- UnorderedListRegexp =
/^\s*(-|\+|\s+[*])\s+/
- DefinitionListRegexp =
/^\s*(-|\+|\s+[*])\s+(.*\s+|)::($|\s+)/
- OrderedListRegexp =
/^\s*\d+(\.|\))\s+/
- HorizontalRuleRegexp =
/^\s*-{5,}\s*$/
- BlockRegexp =
/^\s*#\+(BEGIN|END)_(\w*)\s*([0-9A-Za-z_\-]*)?/i
- InlineExampleRegexp =
/^\s*:\s/
- RawTextRegexp =
/^(\s*)#\+(\w+):\s*/
- InBufferSettingRegexp =
/^#\+(\w+):\s*(.*)$/
Instance Attribute Summary collapse
-
#assigned_paragraph_type ⇒ Object
A line can have its type assigned instead of inferred from its content.
-
#indent ⇒ Object
readonly
The indent level of this line.
-
#line ⇒ Object
readonly
This is the line itself.
-
#major_mode ⇒ Object
readonly
Major modes associate paragraphs with a table, list and so on.
-
#paragraph_type ⇒ Object
readonly
Paragraph type determined for the line.
-
#parser ⇒ Object
readonly
Backpointer to the parser that owns this line.
Instance Method Summary collapse
- #begin_block? ⇒ Boolean
- #blank? ⇒ Boolean
- #block_lang ⇒ Object
- #block_type ⇒ Object
- #code_block? ⇒ Boolean
-
#comment? ⇒ Boolean
Tests if a line is a comment.
- #definition_list? ⇒ Boolean
- #determine_major_mode ⇒ Object
-
#determine_paragraph_type ⇒ Object
Determines the paragraph type of the current line.
- #end_block? ⇒ Boolean
- #horizontal_rule? ⇒ Boolean
-
#in_buffer_setting? ⇒ Boolean
call-seq: line.in_buffer_setting? => boolean line.in_buffer_setting? { |key, value| … }.
-
#initialize(line, parser = nil) ⇒ Line
constructor
A new instance of Line.
-
#inline_example? ⇒ Boolean
Test if the line matches the “inline example” case: the first character on the line is a colon.
-
#metadata? ⇒ Boolean
Tests if a line contains metadata instead of actual content.
- #nonprinting? ⇒ Boolean
- #ordered_list? ⇒ Boolean
-
#output_text ⇒ Object
Extracts meaningful text and excludes org-mode markup, like identifiers for lists or headings.
- #plain_list? ⇒ Boolean
- #plain_text? ⇒ Boolean
- #property_drawer? ⇒ Boolean
- #property_drawer_begin_block? ⇒ Boolean
- #property_drawer_end_block? ⇒ Boolean
- #property_drawer_item? ⇒ Boolean
-
#raw_text? ⇒ Boolean
Checks if this line is raw text.
- #raw_text_tag ⇒ Object
- #strip_ordered_list_tag ⇒ Object
- #strip_raw_text_tag ⇒ Object
- #strip_unordered_list_tag ⇒ Object
- #table? ⇒ Boolean
-
#table_header? ⇒ Boolean
Checks if this line is a table header.
- #table_row? ⇒ Boolean
- #table_separator? ⇒ Boolean
- #to_s ⇒ Object
- #unordered_list? ⇒ Boolean
Constructor Details
#initialize(line, parser = nil) ⇒ Line
Returns a new instance of Line.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/org-ruby/line.rb', line 30 def initialize(line, parser = nil) @parser = parser @line = line @indent = 0 @line =~ /\s*/ determine_paragraph_type determine_major_mode @assigned_paragraph_type = nil @indent = $&.length unless blank? end |
Instance Attribute Details
#assigned_paragraph_type ⇒ Object
A line can have its type assigned instead of inferred from its content. For example, something that parses as a “table” on its own (“| one | two|n”) may just be a paragraph if it’s inside #+BEGIN_EXAMPLE. Set this property on the line to assign its type. This will then affect the value of paragraph_type
.
28 29 30 |
# File 'lib/org-ruby/line.rb', line 28 def assigned_paragraph_type @assigned_paragraph_type end |
#indent ⇒ Object (readonly)
The indent level of this line. this is important to properly translate nested lists from orgmode to textile. TODO 2009-12-20 bdewey: Handle tabs
12 13 14 |
# File 'lib/org-ruby/line.rb', line 12 def indent @indent end |
#line ⇒ Object (readonly)
This is the line itself.
7 8 9 |
# File 'lib/org-ruby/line.rb', line 7 def line @line end |
#major_mode ⇒ Object (readonly)
Major modes associate paragraphs with a table, list and so on.
21 22 23 |
# File 'lib/org-ruby/line.rb', line 21 def major_mode @major_mode end |
#paragraph_type ⇒ Object (readonly)
Paragraph type determined for the line.
18 19 20 |
# File 'lib/org-ruby/line.rb', line 18 def paragraph_type @paragraph_type end |
#parser ⇒ Object (readonly)
Backpointer to the parser that owns this line.
15 16 17 |
# File 'lib/org-ruby/line.rb', line 15 def parser @parser end |
Instance Method Details
#begin_block? ⇒ Boolean
160 161 162 |
# File 'lib/org-ruby/line.rb', line 160 def begin_block? @line =~ BlockRegexp && $1 =~ /BEGIN/i end |
#blank? ⇒ Boolean
81 82 83 |
# File 'lib/org-ruby/line.rb', line 81 def blank? check_assignment_or_regexp(:blank, /^\s*$/) end |
#block_lang ⇒ Object
172 173 174 |
# File 'lib/org-ruby/line.rb', line 172 def block_lang $3 if @line =~ BlockRegexp end |
#block_type ⇒ Object
168 169 170 |
# File 'lib/org-ruby/line.rb', line 168 def block_type $2 if @line =~ BlockRegexp end |
#code_block? ⇒ Boolean
176 177 178 |
# File 'lib/org-ruby/line.rb', line 176 def code_block? block_type =~ /^(EXAMPLE|SRC)$/i end |
#comment? ⇒ Boolean
Tests if a line is a comment.
46 47 48 49 50 |
# File 'lib/org-ruby/line.rb', line 46 def comment? return @assigned_paragraph_type == :comment if @assigned_paragraph_type return block_type.casecmp("COMMENT") if begin_block? or end_block? return @line =~ /^#/ end |
#definition_list? ⇒ Boolean
101 102 103 |
# File 'lib/org-ruby/line.rb', line 101 def definition_list? check_assignment_or_regexp(:definition_list, DefinitionListRegexp) end |
#determine_major_mode ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/org-ruby/line.rb', line 267 def determine_major_mode @major_mode = \ case when definition_list? # order is important! A definition_list is also an unordered_list! :definition_list when ordered_list? :ordered_list when unordered_list? :unordered_list when table? :table end end |
#determine_paragraph_type ⇒ Object
Determines the paragraph type of the current line.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/org-ruby/line.rb', line 225 def determine_paragraph_type @paragraph_type = \ case when blank? :blank when definition_list? # order is important! A definition_list is also an unordered_list! :definition_term when (ordered_list? or unordered_list?) :list_item when property_drawer_begin_block? :property_drawer_begin_block when property_drawer_end_block? :property_drawer_end_block when property_drawer_item? :property_drawer_item when :metadata when block_type case block_type.downcase.to_sym when :center, :comment, :example, :html, :quote, :src block_type.downcase.to_sym else :comment end when raw_text? # order is important! Raw text can be also a comment :raw_text when comment? :comment when table_separator? :table_separator when table_row? :table_row when table_header? :table_header when inline_example? :inline_example when horizontal_rule? :horizontal_rule else :paragraph end end |
#end_block? ⇒ Boolean
164 165 166 |
# File 'lib/org-ruby/line.rb', line 164 def end_block? @line =~ BlockRegexp && $1 =~ /END/i end |
#horizontal_rule? ⇒ Boolean
117 118 119 |
# File 'lib/org-ruby/line.rb', line 117 def horizontal_rule? check_assignment_or_regexp(:horizontal_rule, HorizontalRuleRegexp) end |
#in_buffer_setting? ⇒ Boolean
call-seq:
line.in_buffer_setting? => boolean
line.in_buffer_setting? { |key, value| ... }
Called without a block, this method determines if the line contains an in-buffer setting. Called with a block, the block will get called if the line contains an in-buffer setting with the key and value for the setting.
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/org-ruby/line.rb', line 213 def in_buffer_setting? return false if @assigned_paragraph_type && @assigned_paragraph_type != :comment if block_given? then if @line =~ InBufferSettingRegexp yield $1, $2 end else @line =~ InBufferSettingRegexp end end |
#inline_example? ⇒ Boolean
Test if the line matches the “inline example” case: the first character on the line is a colon.
184 185 186 |
# File 'lib/org-ruby/line.rb', line 184 def inline_example? check_assignment_or_regexp(:inline_example, InlineExampleRegexp) end |
#metadata? ⇒ Boolean
Tests if a line contains metadata instead of actual content.
73 74 75 |
# File 'lib/org-ruby/line.rb', line 73 def check_assignment_or_regexp(:metadata, /^\s*(CLOCK|DEADLINE|START|CLOSED|SCHEDULED):/) end |
#nonprinting? ⇒ Boolean
77 78 79 |
# File 'lib/org-ruby/line.rb', line 77 def nonprinting? comment? || || begin_block? || end_block? end |
#ordered_list? ⇒ Boolean
107 108 109 |
# File 'lib/org-ruby/line.rb', line 107 def ordered_list? check_assignment_or_regexp(:ordered_list, OrderedListRegexp) end |
#output_text ⇒ Object
Extracts meaningful text and excludes org-mode markup, like identifiers for lists or headings.
123 124 125 126 127 128 129 |
# File 'lib/org-ruby/line.rb', line 123 def output_text return strip_ordered_list_tag if ordered_list? return strip_unordered_list_tag if unordered_list? return @line.sub(InlineExampleRegexp, "") if inline_example? return strip_raw_text_tag if raw_text? return line end |
#plain_list? ⇒ Boolean
85 86 87 |
# File 'lib/org-ruby/line.rb', line 85 def plain_list? ordered_list? or unordered_list? or definition_list? end |
#plain_text? ⇒ Boolean
131 132 133 |
# File 'lib/org-ruby/line.rb', line 131 def plain_text? not and not blank? and not plain_list? end |
#property_drawer? ⇒ Boolean
62 63 64 |
# File 'lib/org-ruby/line.rb', line 62 def property_drawer? check_assignment_or_regexp(:property_drawer, PropertyDrawerRegexp) end |
#property_drawer_begin_block? ⇒ Boolean
54 55 56 |
# File 'lib/org-ruby/line.rb', line 54 def property_drawer_begin_block? @line =~ PropertyDrawerRegexp && $1 =~ /PROPERTIES/ end |
#property_drawer_end_block? ⇒ Boolean
58 59 60 |
# File 'lib/org-ruby/line.rb', line 58 def property_drawer_end_block? @line =~ PropertyDrawerRegexp && $1 =~ /END/ end |
#property_drawer_item? ⇒ Boolean
68 69 70 |
# File 'lib/org-ruby/line.rb', line 68 def property_drawer_item? @line =~ PropertyDrawerItemRegexp end |
#raw_text? ⇒ Boolean
Checks if this line is raw text.
191 192 193 |
# File 'lib/org-ruby/line.rb', line 191 def raw_text? check_assignment_or_regexp(:raw_text, RawTextRegexp) end |
#raw_text_tag ⇒ Object
195 196 197 |
# File 'lib/org-ruby/line.rb', line 195 def raw_text_tag $2.upcase if @line =~ RawTextRegexp end |
#strip_ordered_list_tag ⇒ Object
111 112 113 |
# File 'lib/org-ruby/line.rb', line 111 def strip_ordered_list_tag @line.sub(OrderedListRegexp, "") end |
#strip_raw_text_tag ⇒ Object
199 200 201 |
# File 'lib/org-ruby/line.rb', line 199 def strip_raw_text_tag @line.sub(RawTextRegexp) { |match| $1 } end |
#strip_unordered_list_tag ⇒ Object
95 96 97 |
# File 'lib/org-ruby/line.rb', line 95 def strip_unordered_list_tag @line.sub(UnorderedListRegexp, "") end |
#table? ⇒ Boolean
154 155 156 |
# File 'lib/org-ruby/line.rb', line 154 def table? table_row? or table_separator? or table_header? end |
#table_header? ⇒ Boolean
Checks if this line is a table header.
150 151 152 |
# File 'lib/org-ruby/line.rb', line 150 def table_header? @assigned_paragraph_type == :table_header end |
#table_row? ⇒ Boolean
135 136 137 138 139 |
# File 'lib/org-ruby/line.rb', line 135 def table_row? # for an org-mode table, the first non-whitespace character is a # | (pipe). check_assignment_or_regexp(:table_row, /^\s*\|/) end |
#table_separator? ⇒ Boolean
141 142 143 144 145 146 147 |
# File 'lib/org-ruby/line.rb', line 141 def table_separator? # an org-mode table separator has the first non-whitespace # character as a | (pipe), then consists of nothing else other # than pipes, hyphens, and pluses. check_assignment_or_regexp(:table_separator, /^\s*\|[-\|\+]*\s*$/) end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/org-ruby/line.rb', line 41 def to_s return @line end |
#unordered_list? ⇒ Boolean
91 92 93 |
# File 'lib/org-ruby/line.rb', line 91 def unordered_list? check_assignment_or_regexp(:unordered_list, UnorderedListRegexp) end |