Class: Orgmode::Headline

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

Overview

Represents a headline in an orgmode file.

Constant Summary collapse

ValidExportStates =

Valid states for partial export.

exclude

The entire subtree from this heading should be excluded.

headline_only

The headline should be exported, but not the body.

all

Everything should be exported, headline/body/children.

[:exclude, :headline_only, :all]
LineRegexp =

This is the regex that matches a line

/^\*+\s+/
TagsRegexp =

This matches the tags on a headline

/\s*:[\w:]*:\s*$/
Keywords =

Special keywords allowed at the start of a line.

%w[TODO DONE]
KeywordsRegexp =
Regexp.new("^(#{Keywords.join('|')})\$")
CommentHeadlineRegexp =

This matches a headline marked as COMMENT

/^COMMENT\s+/

Constants inherited from Line

Line::BlockRegexp, Line::DefinitionListRegexp, Line::HorizontalRuleRegexp, Line::InBufferSettingRegexp, Line::InlineExampleRegexp, Line::OrderedListRegexp, Line::PropertyDrawerItemRegexp, Line::PropertyDrawerRegexp, Line::UnorderedListRegexp

Instance Attribute Summary collapse

Attributes inherited from Line

#assigned_paragraph_type, #indent, #line, #parser

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Line

#begin_block?, #blank?, #block_lang, #block_type, #code_block?, #code_block_line?, #comment?, #definition_list?, #end_block?, #horizontal_rule?, #in_buffer_setting?, #inline_example?, #metadata?, #nonprinting?, #ordered_list?, #plain_list?, #plain_text?, #property_drawer?, #property_drawer_begin_block?, #property_drawer_end_block?, #property_drawer_item?, #strip_ordered_list_tag, #strip_unordered_list_tag, #table?, #table_header?, #table_row?, #table_separator?, #to_s, to_textile, #unordered_list?

Constructor Details

#initialize(line, parser = nil, offset = 0) ⇒ Headline

Returns a new instance of Headline.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/org-ruby/headline.rb', line 45

def initialize(line, parser = nil, offset=0)
  super(line, parser)
  @body_lines = []
  @body_lines << self       # Make @body_lines contain the headline?
  @tags = []
  @export_state = :exclude
  if (@line =~ LineRegexp) then
    @level = $&.strip.length + offset
    @headline_text = $'.strip
    if (@headline_text =~ TagsRegexp) then
      @tags = $&.split(/:/)              # split tag text on semicolon
      @tags.delete_at(0)                 # the first item will be empty; discard
      @headline_text.gsub!(TagsRegexp, "") # Removes the tags from the headline
    end
    @keyword = nil
    parse_keywords
  else
    raise "'#{line}' is not a valid headline"
  end
end

Instance Attribute Details

#body_linesObject (readonly)

This contains the lines that “belong” to the headline.



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

def body_lines
  @body_lines
end

#export_stateObject

The export state of this headline. See ValidExportStates.



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

def export_state
  @export_state
end

#headline_textObject (readonly)

This is the headline text – the part of the headline minus the leading asterisks, the keywords, and the tags.



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

def headline_text
  @headline_text
end

#keywordObject (readonly)

Optional keyword found at the beginning of the headline.



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

def keyword
  @keyword
end

#levelObject (readonly)

This is the “level” of the headline



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

def level
  @level
end

#tagsObject (readonly)

These are the headline tags



17
18
19
# File 'lib/org-ruby/headline.rb', line 17

def tags
  @tags
end

Class Method Details

.headline?(line) ⇒ Boolean

Determines if a line is an orgmode “headline”: A headline begins with one or more asterisks.

Returns:

  • (Boolean)


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

def self.headline?(line)
  line =~ LineRegexp
end

Instance Method Details

#comment_headline?Boolean

Determines if a headline has the COMMENT keyword.

Returns:

  • (Boolean)


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

def comment_headline?
  @headline_text =~ CommentHeadlineRegexp
end

#output_textObject

Override Line.output_text. For a heading, @headline_text is what we should output.



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

def output_text
  return @headline_text
end

#paragraph_typeObject

Overrides Line.paragraph_type.



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

def paragraph_type
  :"heading#{@level}"
end

#to_textileObject

Converts this headline and its body to textile.



89
90
91
92
93
# File 'lib/org-ruby/headline.rb', line 89

def to_textile
  output = "h#{@level}. #{@headline_text}\n"
  output << Line.to_textile(@body_lines[1..-1])
  output
end