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+/
DefinitionListRegexp =
/^\s*(-|\+)\s*(.*?)::/
OrderedListRegexp =
/^\s*\d+(\.|\))\s+/
HorizontalRuleRegexp =
/^\s*-{5,}\s*$/
BlockRegexp =
/^\s*#\+(BEGIN|END)_(\w*)\s*([0-9A-Za-z_\-]*)?/i
InlineExampleRegexp =
/^\s*:\s/
InBufferSettingRegexp =
/^#\+(\w+):\s*(.*)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, parser = nil) ⇒ Line

Returns a new instance of Line.



24
25
26
27
28
29
30
31
# File 'lib/org-ruby/line.rb', line 24

def initialize(line, parser = nil)
  @parser = parser
  @line = line
  @indent = 0
  @line =~ /\s*/
  @assigned_paragraph_type = nil
  @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.



22
23
24
# File 'lib/org-ruby/line.rb', line 22

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



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

def indent
  @indent
end

#lineObject (readonly)

This is the line itself.



7
8
9
# File 'lib/org-ruby/line.rb', line 7

def line
  @line
end

#parserObject (readonly)

Backpointer to the parser that owns this line.



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

def parser
  @parser
end

Class Method Details

.to_textile(lines) ⇒ Object



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

def self.to_textile(lines)
  output = ""
  output_buffer = TextileOutputBuffer.new(output)
  Parser.translate(lines, output_buffer)
end

Instance Method Details

#begin_block?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/org-ruby/line.rb', line 151

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

#blank?Boolean

Returns:

  • (Boolean)


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

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

#block_langObject



163
164
165
# File 'lib/org-ruby/line.rb', line 163

def block_lang
  $3 if @line =~ BlockRegexp
end

#block_typeObject



159
160
161
# File 'lib/org-ruby/line.rb', line 159

def block_type
  $2 if @line =~ BlockRegexp
end

#code_block?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/org-ruby/line.rb', line 167

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

#code_block_line?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/org-ruby/line.rb', line 171

def code_block_line?
  @assigned_paragraph_type == :src
end

#comment?Boolean

Tests if a line is a comment.

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/org-ruby/line.rb', line 38

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)


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

def definition_list?
  check_assignment_or_regexp(:definition_list, DefinitionListRegexp)
end

#end_block?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/org-ruby/line.rb', line 155

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

#horizontal_rule?Boolean

Returns:

  • (Boolean)


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

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)


193
194
195
196
197
198
199
200
201
202
# File 'lib/org-ruby/line.rb', line 193

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.

Returns:

  • (Boolean)


179
180
181
# File 'lib/org-ruby/line.rb', line 179

def inline_example?
  check_assignment_or_regexp(:inline_example, InlineExampleRegexp)
end

#metadata?Boolean

Tests if a line contains metadata instead of actual content.

Returns:

  • (Boolean)


65
66
67
# File 'lib/org-ruby/line.rb', line 65

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

#nonprinting?Boolean

Returns:

  • (Boolean)


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

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

#ordered_list?Boolean

Returns:

  • (Boolean)


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

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.



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

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 line
end

#paragraph_typeObject

Determines the paragraph type of the current line.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/org-ruby/line.rb', line 205

def paragraph_type
  return :blank if blank?
  return :src if code_block_line? # Do not try to guess the type of this line if it is accumulating source code
  return :definition_list if definition_list? # order is important! A definition_list is also an unordered_list!
  return :ordered_list if ordered_list?
  return :unordered_list if unordered_list?
  return :property_drawer_begin_block if property_drawer_begin_block?
  return :property_drawer_end_block if property_drawer_end_block?
  return :property_drawer_item if property_drawer_item?
  return :metadata if metadata?
  return :begin_block if begin_block?
  return :end_block if end_block?
  return :comment if comment?
  return :table_separator if table_separator?
  return :table_row if table_row?
  return :table_header if table_header?
  return :inline_example if inline_example?
  return :horizontal_rule if horizontal_rule?
  return :paragraph
end

#plain_list?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/org-ruby/line.rb', line 77

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

#plain_text?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/org-ruby/line.rb', line 122

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

#property_drawer?Boolean

Returns:

  • (Boolean)


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

def property_drawer?
  check_assignment_or_regexp(:property_drawer, PropertyDrawerRegexp)
end

#property_drawer_begin_block?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/org-ruby/line.rb', line 46

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

#property_drawer_end_block?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/org-ruby/line.rb', line 50

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

#property_drawer_item?Boolean

Returns:

  • (Boolean)


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

def property_drawer_item?
  @line =~ PropertyDrawerItemRegexp
end

#strip_ordered_list_tagObject



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

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

#strip_unordered_list_tagObject



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

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

#table?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/org-ruby/line.rb', line 145

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

#table_header?Boolean

Checks if this line is a table header.

Returns:

  • (Boolean)


141
142
143
# File 'lib/org-ruby/line.rb', line 141

def table_header?
  @assigned_paragraph_type == :table_header
end

#table_row?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/org-ruby/line.rb', line 126

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)


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

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_sObject



33
34
35
# File 'lib/org-ruby/line.rb', line 33

def to_s
  return @line
end

#unordered_list?Boolean

Returns:

  • (Boolean)


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

def unordered_list?
  check_assignment_or_regexp(:unordered_list, UnorderedListRegexp)
end