Class: Orgmode::Parser

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

Overview

Simple routines for loading / saving an ORG file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ Parser

I can construct a parser object either with an array of lines or with a single string that I will split along \n boundaries.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/org-ruby/parser.rb', line 23

def initialize(lines)
  if lines.is_a? Array then
    @lines = lines
  elsif lines.is_a? String then
    @lines = lines.split("\n")
  else
    raise "Unsupported type for +lines+: #{lines.class}"
  end
    
  @headlines = Array.new
  @current_headline = nil
  @header_lines = []
  mode = :normal
  @lines.each do |line|
    case mode
    when :normal

      if (Headline.headline? line) then
        @current_headline = Headline.new line
        @headlines << @current_headline
      else
        line = Line.new line
        mode = :code if line.begin_block? and line.block_type == "EXAMPLE"
        if (@current_headline) then
          @current_headline.body_lines << line
        else
          @header_lines << line
        end
      end

    when :code

      # As long as we stay in code mode, force lines to be either blank or paragraphs.
      # Don't try to interpret structural items, like headings and tables.
      line = Line.new line
      if line.end_block? and line.block_type == "EXAMPLE"
        mode = :normal
      else
        line.assigned_paragraph_type = :paragraph unless line.blank?
      end
      if (@current_headline) then
        @current_headline.body_lines << line
      else
        @header_lines << line
      end
    end                     # case
  end
end

Instance Attribute Details

#header_linesObject (readonly)

These are any lines before the first headline



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

def header_lines
  @header_lines
end

#headlinesObject (readonly)

All of the headlines in the org file



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

def headlines
  @headlines
end

#linesObject (readonly)

All of the lines of the orgmode file



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

def lines
  @lines
end

Class Method Details

.load(fname) ⇒ Object

Creates a new parser from the data in a given file



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

def self.load(fname)
  lines = IO.readlines(fname)
  return self.new(lines)
end

Instance Method Details

#to_htmlObject

Converts the loaded org-mode file to HTML.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/org-ruby/parser.rb', line 89

def to_html
  output = ""
  decorate = true
  output << Line.to_html(@header_lines, :decorate_title => decorate)
  decorate = (output.length == 0)
  @headlines.each do |headline|
    output << headline.to_html(:decorate_title => decorate)
    decorate = (output.length == 0)
  end
  rp = RubyPants.new(output)
  rp.to_html
end

#to_textileObject

Saves the loaded orgmode file as a textile file.



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

def to_textile
  output = ""
  output << Line.to_textile(@header_lines)
  @headlines.each do |headline|
    output << headline.to_textile
  end
  output
end