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

UnorderedListRegexp =
/^\s*(-|\+)\s*/
OrderedListRegexp =
/^\s*\d+(\.|\))\s*/
BlockRegexp =
/^\s*#\+(BEGIN|END)_(\w*)/
InlineExampleRegexp =
/^\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



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

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)


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

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

#blank?Boolean

Returns:

  • (Boolean)


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

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

#block_typeObject



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

def block_type
  $2 if @line =~ BlockRegexp
end

#code_block_type?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/org-ruby/line.rb', line 129

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

#comment?Boolean

Tests if a line is a comment.

Returns:

  • (Boolean)


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

def comment?
  check_assignment_or_regexp(:comment, /^\s*#/)
end

#end_block?Boolean

Returns:

  • (Boolean)


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

def end_block?
  @line =~ BlockRegexp && $1 == "END"
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)


151
152
153
154
155
156
157
158
159
160
# File 'lib/org-ruby/line.rb', line 151

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)


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

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)


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

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

#nonprinting?Boolean

Returns:

  • (Boolean)


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

def nonprinting?
  comment? || metadata?
end

#ordered_list?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/org-ruby/line.rb', line 71

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.



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

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.



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/org-ruby/line.rb', line 163

def paragraph_type
  return :blank if blank?
  return :ordered_list if ordered_list?
  return :unordered_list if unordered_list?
  return :metadata if metadata?
  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 :paragraph
end

#plain_list?Boolean

Returns:

  • (Boolean)


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

def plain_list?
  ordered_list? or unordered_list?
end

#plain_text?Boolean

Returns:

  • (Boolean)


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

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

#strip_ordered_list_tagObject



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

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

#strip_unordered_list_tagObject



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

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

#table?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/org-ruby/line.rb', line 111

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

#table_header?Boolean

Checks if this line is a table header.

Returns:

  • (Boolean)


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

def table_header?
  @assigned_paragraph_type == :table_header
end

#table_row?Boolean

Returns:

  • (Boolean)


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

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)


98
99
100
101
102
103
104
# File 'lib/org-ruby/line.rb', line 98

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)


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

def unordered_list?
  check_assignment_or_regexp(:unordered_list, UnorderedListRegexp)
end