Class Texy::Modules::Heading
In: lib/texy/modules/heading.rb
Parent: Base

Heading module class

Methods

Attributes

balancing  [RW] 
handler  [RW]  Proc that will be called with newly created element
levels  [RW]  when balancing = :fixed
title  [RW]  textual content of first heading
top  [RW]  level of top heading, 1 - 6

Public Class methods

[Source]

# File lib/texy/modules/heading.rb, line 50
            def initialize(texy)
                super

                self.top = 1
                self.balancing = :dynamic
                self.levels = {
                    '#' => 0,
                    '*' => 1,
                    '=' => 2,
                    '-' => 3,
                }

                self.allowed = {
                    :surrounded => true,
                    :underlined => true
                }
            end

Public Instance methods

Module initialization.

[Source]

# File lib/texy/modules/heading.rb, line 69
            def init
                if allowed && allowed[:underlined]
                    texy.register_block_pattern(
                        method(:process_block_underline),
                        /^(\S.*?)#{PATTERN_MODIFIER_H}?\n(#|\*|=|-){3,}$/
                    )
                end

                if allowed && allowed[:surrounded]
                    texy.register_block_pattern(
                        method(:process_block_surround),
                        /^((#|=){2,})(?!\2)(.+?)\2*#{PATTERN_MODIFIER_H}?()$/ # (rane) TODO: wtf is the final "()" doing there?
                    )
                end
            end

[Source]

# File lib/texy/modules/heading.rb, line 85
            def pre_process(text)
                @range_underline = [10, 0]
                @range_surround = [10, 0]
                self.title = nil

                @elements_underline = []
                @elements_surround = []

                text
            end

Callback function (for blocks)

  ### Heading .(title)[class]{style}>

[Source]

# File lib/texy/modules/heading.rb, line 142
            def process_block_surround(parser, matches)
                m_line, m_char, m_content = matches[1..3]

                el = Texy::HeadingElement.new(texy)
                el.level = 7 - [7, [2, m_line.length].max].min

                @elements_surround << el if balancing == :dynamic

                el.modifier.set_properties(*matches[4..-1])
                el.parse(m_content.strip)

                if handler
                    return unless handler.call(el)
                end

                parser.element.append_child(el)

                # document title
                self.title ||= el.to_html.strip_html_tags


                # dynamic headings balancing
                @range_surround[0] = [@range_surround[0], el.level].min
                @range_surround[1] = [@range_surround[1], el.level].max

                delta = -@range_surround[0] + (@range_underline[1] != 0 ? @range_underline[1] - @range_underline[0] + 1 : 0)
                @elements_surround.each do |el|
                    el.delta_level = delta
                end
            end

Callback function (for blocks)

  Heading .(title)[class]{style}>
  -------------------------------

[Source]

# File lib/texy/modules/heading.rb, line 101
            def process_block_underline(parser, matches)
                m_content, m_line = matches.values_at(1, -1)

                el = Texy::HeadingElement.new(texy)
                el.level = levels[m_line]

                @elements_underline << el if balancing == :dynamic

                el.modifier.set_properties(*matches[2..-2])
                el.parse(m_content.strip)

                if handler
                    return unless handler.call(el)
                end

                parser.element.append_child(el)

                # document title
                self.title ||= el.to_html.strip_html_tags

                # dynamic headings balancing
                @range_underline[0] = [@range_underline[0], el.level].min
                @range_underline[1] = [@range_underline[1], el.level].max

                delta = -@range_underline[0]
                @elements_underline.each do |el|
                    el.delta_level = delta
                end

                delta = -@range_surround[0] + (@range_underline[1] ? (@range_underline[1] - @range_underline[0] + 1) : 0);
                @elements_surround.each do |el|
                    el.delta_level = delta
                end
            end

[Validate]