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*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Line

Returns a new instance of Line.



21
22
23
24
25
26
27
# File 'lib/org-ruby/line.rb', line 21

def initialize(line)
  @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.



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

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

Class Method Details

.to_html(lines, opts = { }) ⇒ Object



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

def self.to_html(lines, opts = { })
  output = ""
  output_buffer = HtmlOutputBuffer.new(output, opts)
  translate(lines, output_buffer)
end

.to_textile(lines) ⇒ Object



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

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

.translate(lines, output_buffer) ⇒ Object

Converts an array of lines to textile format.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/org-ruby/line.rb', line 136

def self.translate(lines, output_buffer)
  lines.each do |line|

    # See if we're carrying paragraph payload, and output
    # it if we're about to switch to some other output type.
    output_buffer.prepare(line)

    case line.paragraph_type
    when :metadata, :table_separator, :blank

      output_buffer << line.line if output_buffer.preserve_whitespace?          

    when :comment
      
      if line.begin_block?
        output_buffer.push_mode(:blockquote) if line.block_type == "QUOTE"
        output_buffer.push_mode(:code) if line.block_type == "EXAMPLE"
      elsif line.end_block?
        output_buffer.pop_mode(:blockquote) if line.block_type == "QUOTE"
        output_buffer.pop_mode(:code) if line.block_type == "EXAMPLE"
      else
        output_buffer << line.line if output_buffer.preserve_whitespace?
      end

    when :table_row

      output_buffer << line.line.lstrip

    when :ordered_list
        
      output_buffer << line.strip_ordered_list_tag << " "
      
    when :unordered_list
      
      output_buffer << line.strip_unordered_list_tag << " "

    when :paragraph

      if output_buffer.preserve_whitespace? then
        output_buffer << line.line
      else
        output_buffer << line.line.strip << " "
      end
    end
  end
  output_buffer.flush!
  output_buffer.pop_mode until output_buffer.current_mode == :normal
  output_buffer.output
end

Instance Method Details

#begin_block?Boolean

Returns:

  • (Boolean)


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

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

#blank?Boolean

Returns:

  • (Boolean)


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

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

#block_typeObject



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

def block_type
  $2 if @line =~ BlockRegexp
end

#comment?Boolean

Tests if a line is a comment.

Returns:

  • (Boolean)


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

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

#end_block?Boolean

Returns:

  • (Boolean)


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

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

#metadata?Boolean

Tests if a line contains metadata instead of actual content.

Returns:

  • (Boolean)


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

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

#nonprinting?Boolean

Returns:

  • (Boolean)


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

def nonprinting?
  comment? || metadata?
end

#ordered_list?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/org-ruby/line.rb', line 67

def ordered_list?
  check_assignment_or_regexp(:ordered_list, OrderedListRegexp)
end

#paragraph_typeObject

Determines the paragraph type of the current line.



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

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 :paragraph
end

#plain_list?Boolean

Returns:

  • (Boolean)


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

def plain_list?
  ordered_list? or unordered_list?
end

#plain_text?Boolean

Returns:

  • (Boolean)


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

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

#strip_ordered_list_tagObject



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

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

#strip_unordered_list_tagObject



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

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

#table?Boolean

Returns:

  • (Boolean)


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

def table?
  table_row? or table_separator?
end

#table_row?Boolean

Returns:

  • (Boolean)


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

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)


85
86
87
88
89
90
91
# File 'lib/org-ruby/line.rb', line 85

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



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

def to_s
  return @line
end

#unordered_list?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/org-ruby/line.rb', line 57

def unordered_list?
  check_assignment_or_regexp(:unordered_list, UnorderedListRegexp)
end