Class: Orgmode::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/org-ruby/line.rb

Overview

Represents a single line of an orgmode file.

Direct Known Subclasses

Headline

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 =

1) block delimiters 2) block type (src, example, html…) 3) switches (e.g. -n -r -l “asdf”) 4) header arguments (:hello world)

/^\s*#\+(BEGIN|END)_(\w*)\s*([0-9A-Za-z_\-]*)?\s*([^\":\n]*\"[^\"\n*]*\"[^\":\n]*|[^\":\n]*)?\s*([^\n]*)?/i
InlineExampleRegexp =
/^\s*:\s/
RawTextRegexp =
/^(\s*)#\+(\w+):\s*/
InBufferSettingRegexp =
/^#\+(\w+):\s*(.*)$/
ResultsBlockStartsRegexp =
/^\s*#\+RESULTS:\s*$/i
LinkAbbrevRegexp =
/^\s*#\+LINK:\s*(\w+)\s+(.+)$/i
IncludeFileRegexp =
/^\s*#\+INCLUDE:\s*"([^"]+)"(\s+([^\s]+)\s+(.*))?$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, parser = nil, assigned_paragraph_type = nil) ⇒ Line

Returns a new instance of Line.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/org-ruby/line.rb', line 30

def initialize(line, parser=nil, assigned_paragraph_type=nil)
  @parser = parser
  @line = line
  @indent = 0
  @line =~ /\s*/
  @assigned_paragraph_type = assigned_paragraph_type
  @properties = { }
  determine_paragraph_type
  determine_major_mode
  @indent = $&.length unless blank?
end

Instance Attribute Details

#assigned_paragraph_typeObject

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.



25
26
27
# File 'lib/org-ruby/line.rb', line 25

def assigned_paragraph_type
  @assigned_paragraph_type
end

#indentObject (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



9
10
11
# File 'lib/org-ruby/line.rb', line 9

def indent
  @indent
end

#major_modeObject (readonly)

Major modes associate paragraphs with a table, list and so on.



18
19
20
# File 'lib/org-ruby/line.rb', line 18

def major_mode
  @major_mode
end

#paragraph_typeObject (readonly)

Paragraph type determined for the line.



15
16
17
# File 'lib/org-ruby/line.rb', line 15

def paragraph_type
  @paragraph_type
end

#parserObject (readonly)

Backpointer to the parser that owns this line.



12
13
14
# File 'lib/org-ruby/line.rb', line 12

def parser
  @parser
end

#propertiesObject

In case more contextual info is needed we can put here



28
29
30
# File 'lib/org-ruby/line.rb', line 28

def properties
  @properties
end

Instance Method Details

#begin_block?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/org-ruby/line.rb', line 173

def begin_block?
  @line =~ BlockRegexp && $1 =~ /BEGIN/i
end

#blank?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/org-ruby/line.rb', line 88

def blank?
  check_assignment_or_regexp(:blank, /^\s*$/)
end

#block_header_argumentsObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/org-ruby/line.rb', line 197

def block_header_arguments
  header_arguments = { }

  if @line =~ BlockRegexp
    header_arguments_string = $5
    harray = header_arguments_string.split(' ')
    harray.each_with_index do |arg, i|
      next_argument = harray[i + 1]
      if arg =~ /^:/ and not (next_argument.nil? or next_argument =~ /^:/)
        header_arguments[arg] = next_argument
      end
    end
  end

  header_arguments
end

#block_langObject



185
186
187
# File 'lib/org-ruby/line.rb', line 185

def block_lang
  $3 if @line =~ BlockRegexp
end

#block_should_be_exported?Boolean

TODO: COMMENT block should be considered here

Returns:

  • (Boolean)


215
216
217
218
219
220
221
222
223
# File 'lib/org-ruby/line.rb', line 215

def block_should_be_exported?
  export_state = block_header_arguments[':exports']
  case
  when ['both', 'code', nil, ''].include?(export_state)
    true
  when ['none', 'results'].include?(export_state)
    false
  end
end

#block_switchesObject



193
194
195
# File 'lib/org-ruby/line.rb', line 193

def block_switches
  $4 if @line =~ BlockRegexp
end

#block_typeObject



181
182
183
# File 'lib/org-ruby/line.rb', line 181

def block_type
  $2 if @line =~ BlockRegexp
end

#code_block?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/org-ruby/line.rb', line 189

def code_block?
  block_type =~ /^(EXAMPLE|SRC)$/i
end

#comment?Boolean

Tests if a line is a comment.

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/org-ruby/line.rb', line 47

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

Returns:

  • (Boolean)


108
109
110
# File 'lib/org-ruby/line.rb', line 108

def definition_list?
  check_assignment_or_regexp(:definition_list, DefinitionListRegexp)
end

#determine_major_modeObject



364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/org-ruby/line.rb', line 364

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_typeObject

Determines the paragraph type of the current line.



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/org-ruby/line.rb', line 316

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?
    :metadata
  when block_type
    if block_should_be_exported?
      case block_type.downcase.to_sym
      when :center, :comment, :example, :html, :quote, :src
        block_type.downcase.to_sym
      else
        :comment
      end
    else
      :comment
    end
  when title?
    :title
  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

Returns:

  • (Boolean)


177
178
179
# File 'lib/org-ruby/line.rb', line 177

def end_block?
  @line =~ BlockRegexp && $1 =~ /END/i
end

#horizontal_rule?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/org-ruby/line.rb', line 124

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.

Returns:

  • (Boolean)


268
269
270
271
272
273
274
275
276
277
# File 'lib/org-ruby/line.rb', line 268

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

#include_file?Boolean

Returns:

  • (Boolean)


303
304
305
# File 'lib/org-ruby/line.rb', line 303

def include_file?
  @line =~ IncludeFileRegexp
end

#include_file_optionsObject



311
312
313
# File 'lib/org-ruby/line.rb', line 311

def include_file_options
  [$3, $4] if @line =~ IncludeFileRegexp and !$2.nil?
end

#include_file_pathObject



307
308
309
# File 'lib/org-ruby/line.rb', line 307

def include_file_path
  File.expand_path $1 if @line =~ IncludeFileRegexp
end

#inline_example?Boolean

Test if the line matches the “inline example” case: the first character on the line is a colon.

Returns:

  • (Boolean)


239
240
241
# File 'lib/org-ruby/line.rb', line 239

def inline_example?
  check_assignment_or_regexp(:inline_example, InlineExampleRegexp)
end

Returns:

  • (Boolean)


293
294
295
# File 'lib/org-ruby/line.rb', line 293

def link_abbrev?
  @line =~ LinkAbbrevRegexp
end


297
298
299
# File 'lib/org-ruby/line.rb', line 297

def link_abbrev_data
  [$1, $2] if @line =~ LinkAbbrevRegexp
end

#metadata?Boolean

Tests if a line contains metadata instead of actual content.

Returns:

  • (Boolean)


80
81
82
# File 'lib/org-ruby/line.rb', line 80

def metadata?
  check_assignment_or_regexp(:metadata, /^\s*(CLOCK|DEADLINE|START|CLOSED|SCHEDULED):/)
end

#nonprinting?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/org-ruby/line.rb', line 84

def nonprinting?
  comment? || metadata? || begin_block? || end_block? || include_file?
end

#ordered_list?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/org-ruby/line.rb', line 114

def ordered_list?
  check_assignment_or_regexp(:ordered_list, OrderedListRegexp)
end

#output_textObject

Extracts meaningful text and excludes org-mode markup, like identifiers for lists or headings.



130
131
132
133
134
135
136
# File 'lib/org-ruby/line.rb', line 130

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/org-ruby/line.rb', line 92

def plain_list?
  ordered_list? or unordered_list? or definition_list?
end

#plain_text?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/org-ruby/line.rb', line 138

def plain_text?
  not metadata? and not blank? and not plain_list?
end

#property_drawer?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/org-ruby/line.rb', line 63

def property_drawer?
  check_assignment_or_regexp(:property_drawer, PropertyDrawerRegexp)
end

#property_drawer_begin_block?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/org-ruby/line.rb', line 55

def property_drawer_begin_block?
  @line =~ PropertyDrawerRegexp && $1 =~ /PROPERTIES/
end

#property_drawer_end_block?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/org-ruby/line.rb', line 59

def property_drawer_end_block?
  @line =~ PropertyDrawerRegexp && $1 =~ /END/
end

#property_drawer_itemObject



73
74
75
76
77
# File 'lib/org-ruby/line.rb', line 73

def property_drawer_item
  @line =~ PropertyDrawerItemRegexp

  [$1, $2]
end

#property_drawer_item?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/org-ruby/line.rb', line 69

def property_drawer_item?
  @line =~ PropertyDrawerItemRegexp
end

#raw_text?Boolean

Checks if this line is raw text.

Returns:

  • (Boolean)


246
247
248
# File 'lib/org-ruby/line.rb', line 246

def raw_text?
  check_assignment_or_regexp(:raw_text, RawTextRegexp)
end

#raw_text_tagObject



250
251
252
# File 'lib/org-ruby/line.rb', line 250

def raw_text_tag
  $2.upcase if @line =~ RawTextRegexp
end

#results_block_should_be_exported?Boolean

Returns:

  • (Boolean)


225
226
227
228
229
230
231
232
233
# File 'lib/org-ruby/line.rb', line 225

def results_block_should_be_exported?
  export_state = block_header_arguments[':exports']
  case
  when ['results', 'both'].include?(export_state)
    true
  when ['code', 'none', nil, ''].include?(export_state)
    false
  end
end

#start_of_results_code_block?Boolean

Returns:

  • (Boolean)


287
288
289
# File 'lib/org-ruby/line.rb', line 287

def start_of_results_code_block?
  @line =~ ResultsBlockStartsRegexp
end

#strip_ordered_list_tagObject



118
119
120
# File 'lib/org-ruby/line.rb', line 118

def strip_ordered_list_tag
  @line.sub(OrderedListRegexp, "")
end

#strip_raw_text_tagObject



254
255
256
# File 'lib/org-ruby/line.rb', line 254

def strip_raw_text_tag
  @line.sub(RawTextRegexp) { |match| $1 }
end

#strip_unordered_list_tagObject



102
103
104
# File 'lib/org-ruby/line.rb', line 102

def strip_unordered_list_tag
  @line.sub(UnorderedListRegexp, "")
end

#table?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/org-ruby/line.rb', line 161

def table?
  table_row? or table_separator? or table_header?
end

#table_header?Boolean

Checks if this line is a table header.

Returns:

  • (Boolean)


157
158
159
# File 'lib/org-ruby/line.rb', line 157

def table_header?
  @assigned_paragraph_type == :table_header
end

#table_row?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/org-ruby/line.rb', line 142

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

Returns:

  • (Boolean)


148
149
150
151
152
153
154
# File 'lib/org-ruby/line.rb', line 148

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

#title?Boolean

#+TITLE: is special because even though that it can be written many times in the document, its value will be that of the last one

Returns:

  • (Boolean)


281
282
283
# File 'lib/org-ruby/line.rb', line 281

def title?
  @assigned_paragraph_type == :title
end

#to_sObject



42
43
44
# File 'lib/org-ruby/line.rb', line 42

def to_s
  return @line
end

#unordered_list?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/org-ruby/line.rb', line 98

def unordered_list?
  check_assignment_or_regexp(:unordered_list, UnorderedListRegexp)
end