Class: Marker::Heading

Inherits:
ParseNode show all
Defined in:
lib/marker/headings.rb

Instance Method Summary collapse

Methods inherited from Treetop::Runtime::SyntaxNode

#present?

Instance Method Details

#lObject

– defaults ++



75
76
77
# File 'lib/marker/headings.rb', line 75

def l #:nodoc:
  nil
end

#label(format, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/marker/headings.rb', line 65

def label( format, options = {} )
  case format
  when :html
    l.to_html( options )
  else
    l.to_s( options )
  end
end

#to_html(options = {}) ⇒ Object

HTML-rendered headings, using <h#> tags.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/marker/headings.rb', line 12

def to_html( options = {} )
  # find out how deep it is
  d = [s.text_value.size, e.text_value.size].min
  sn = s.text_value.size - d
  en = e.text_value.size - d
  if sn > 0
    "<h#{d}>#{'=' * sn} #{label( :html, options )}</h#{d}>"
  elsif en > 0
    "<h#{d}>#{label( :html, options )} #{'=' * en}</h#{d}>"
  else
    "<h#{d}>#{label( :html, options )}</h#{d}>"
  end
end

#to_s(options = {}) ⇒ Object

Text-rendered headings. Should look like this:

Heading Level 1

Heading Level 2


  • Heading Level 3 ——————————————————–

— Heading Level 4 ——————————————————



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
# File 'lib/marker/headings.rb', line 38

def to_s( options = {} )
  width = options[:width] || 80
  d = [s.text_value.size, e.text_value.size].min
  sn = s.text_value.size - d
  en = e.text_value.size - d

  l = if sn > 0
    "#{'=' * sn} #{label( :text, options )}"
  elsif en > 0
    "#{label( :text, options )} #{'=' * en}"
  else
    label( :text, options )
  end

  case d
  when 1
    "#{l}\n" + ('=' * width)
  when 2
    "#{l}\n" + ('-' * width)
  else
    l = " #{l} "
    h = '-'*width
    h[2*(d-3)+1, l.size] = l # slice substitution
    h
  end
end