class Orgmode::Line

Represents a single line of an orgmode file.

Constants

BlockRegexp
DefinitionListRegexp
HorizontalRuleRegexp
InBufferSettingRegexp
IncludeFileRegexp
InlineExampleRegexp
LinkAbbrevRegexp
OrderedListRegexp
PropertyDrawerItemRegexp
PropertyDrawerRegexp
RawTextRegexp
UnorderedListRegexp

Attributes

assigned_paragraph_type[RW]

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.

indent[R]

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

major_mode[R]

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

paragraph_type[R]

Paragraph type determined for the line.

parser[R]

Backpointer to the parser that owns this line.

Public Class Methods

new(line, parser = nil) click to toggle source
# File lib/org-ruby/line.rb, line 27
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

Public Instance Methods

begin_block?() click to toggle source
# File lib/org-ruby/line.rb, line 163
def begin_block?
  @line =~ BlockRegexp && $1 =~ /BEGIN/
end
blank?() click to toggle source
# File lib/org-ruby/line.rb, line 84
def blank?
  check_assignment_or_regexp(:blank, /^\s*$/)
end
block_lang() click to toggle source
# File lib/org-ruby/line.rb, line 175
def block_lang
  $3 if @line =~ BlockRegexp
end
block_type() click to toggle source
# File lib/org-ruby/line.rb, line 171
def block_type
  $2 if @line =~ BlockRegexp
end
code_block?() click to toggle source
# File lib/org-ruby/line.rb, line 179
def code_block?
  block_type =~ /^(EXAMPLE|SRC)$/
end
comment?() click to toggle source

Tests if a line is a comment.

# File lib/org-ruby/line.rb, line 43
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?() click to toggle source
# File lib/org-ruby/line.rb, line 104
def definition_list?
  check_assignment_or_regexp(:definition_list, DefinitionListRegexp)
end
determine_major_mode() click to toggle source
# File lib/org-ruby/line.rb, line 294
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() click to toggle source

Determines the paragraph type of the current line.

# File lib/org-ruby/line.rb, line 252
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
    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?() click to toggle source
# File lib/org-ruby/line.rb, line 167
def end_block?
  @line =~ BlockRegexp && $1 =~ /END/
end
horizontal_rule?() click to toggle source
# File lib/org-ruby/line.rb, line 120
def horizontal_rule?
  check_assignment_or_regexp(:horizontal_rule, HorizontalRuleRegexp)
end
in_buffer_setting? → boolean click to toggle source
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.

# File lib/org-ruby/line.rb, line 216
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?() click to toggle source
# File lib/org-ruby/line.rb, line 239
def include_file?
  @line =~ IncludeFileRegexp
end
include_file_options() click to toggle source
# File lib/org-ruby/line.rb, line 247
def include_file_options
  [$3, $4] if @line =~ IncludeFileRegexp and !$2.nil?
end
include_file_path() click to toggle source
# File lib/org-ruby/line.rb, line 243
def include_file_path
  File.expand_path $1 if @line =~ IncludeFileRegexp
end
inline_example?() click to toggle source

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

# File lib/org-ruby/line.rb, line 187
def inline_example?
  check_assignment_or_regexp(:inline_example, InlineExampleRegexp)
end
metadata?() click to toggle source

Tests if a line contains metadata instead of actual content.

# File lib/org-ruby/line.rb, line 76
def metadata?
  check_assignment_or_regexp(:metadata, /^\s*(CLOCK|DEADLINE|START|CLOSED|SCHEDULED):/)
end
nonprinting?() click to toggle source
# File lib/org-ruby/line.rb, line 80
def nonprinting?
  comment? || metadata? || begin_block? || end_block? || include_file?
end
ordered_list?() click to toggle source
# File lib/org-ruby/line.rb, line 110
def ordered_list?
  check_assignment_or_regexp(:ordered_list, OrderedListRegexp)
end
output_text() click to toggle source

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

# File lib/org-ruby/line.rb, line 126
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?() click to toggle source
# File lib/org-ruby/line.rb, line 88
def plain_list?
  ordered_list? or unordered_list? or definition_list?
end
plain_text?() click to toggle source
# File lib/org-ruby/line.rb, line 134
def plain_text?
  not metadata? and not blank? and not plain_list?
end
property_drawer?() click to toggle source
# File lib/org-ruby/line.rb, line 59
def property_drawer?
  check_assignment_or_regexp(:property_drawer, PropertyDrawerRegexp)
end
property_drawer_begin_block?() click to toggle source
# File lib/org-ruby/line.rb, line 51
def property_drawer_begin_block?
  @line =~ PropertyDrawerRegexp && $1 =~ /PROPERTIES/
end
property_drawer_end_block?() click to toggle source
# File lib/org-ruby/line.rb, line 55
def property_drawer_end_block?
  @line =~ PropertyDrawerRegexp && $1 =~ /END/
end
property_drawer_item() click to toggle source
# File lib/org-ruby/line.rb, line 69
def property_drawer_item
  @line =~ PropertyDrawerItemRegexp

  [$1, $2]
end
property_drawer_item?() click to toggle source
# File lib/org-ruby/line.rb, line 65
def property_drawer_item?
  @line =~ PropertyDrawerItemRegexp
end
raw_text?() click to toggle source

Checks if this line is raw text.

# File lib/org-ruby/line.rb, line 194
def raw_text?
  check_assignment_or_regexp(:raw_text, RawTextRegexp)
end
raw_text_tag() click to toggle source
# File lib/org-ruby/line.rb, line 198
def raw_text_tag
  $2.upcase if @line =~ RawTextRegexp
end
strip_ordered_list_tag() click to toggle source
# File lib/org-ruby/line.rb, line 114
def strip_ordered_list_tag
  @line.sub(OrderedListRegexp, "")
end
strip_raw_text_tag() click to toggle source
# File lib/org-ruby/line.rb, line 202
def strip_raw_text_tag
  @line.sub(RawTextRegexp) { |match| $1 }
end
strip_unordered_list_tag() click to toggle source
# File lib/org-ruby/line.rb, line 98
def strip_unordered_list_tag
  @line.sub(UnorderedListRegexp, "")
end
table?() click to toggle source
# File lib/org-ruby/line.rb, line 157
def table?
  table_row? or table_separator? or table_header?
end
table_header?() click to toggle source

Checks if this line is a table header.

# File lib/org-ruby/line.rb, line 153
def table_header?
  @assigned_paragraph_type == :table_header
end
table_row?() click to toggle source
# File lib/org-ruby/line.rb, line 138
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?() click to toggle source
# File lib/org-ruby/line.rb, line 144
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() click to toggle source
# File lib/org-ruby/line.rb, line 38
def to_s
  return @line
end
unordered_list?() click to toggle source
# File lib/org-ruby/line.rb, line 94
def unordered_list?
  check_assignment_or_regexp(:unordered_list, UnorderedListRegexp)
end

Private Instance Methods

check_assignment_or_regexp(assignment, regexp) → boolean click to toggle source

This function is an internal helper for determining the paragraph type of a line… for instance, if the line is a comment or contains metadata. It's used in routines like blank?, plain_list?, etc.

What's tricky is lines can have assigned types, so you need to check the assigned type, if present, or see if the characteristic regexp for the paragraph type matches if not present.

assignment

if the paragraph has an assigned type, it will be checked to see if it equals assignment.

regexp

If the paragraph does not have an assigned type, the contents of the paragraph will be checked against this regexp.

# File lib/org-ruby/line.rb, line 327
def check_assignment_or_regexp(assignment, regexp)
  return @assigned_paragraph_type == assignment if @assigned_paragraph_type
  return @line =~ regexp
end