Sha256: 661371445809a1053e9969a3613e4c609f1ac06ace62c68080240146ade20175

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

module EverExp
  class Html
    class Heading
      def initialize
        @h = Title.new '999', 'heading'
      end

      def add level, title
        subtitle = Title.new level, title
        last_st = h.last_subtitle
        title = h
        while true do
          if last_st.nil? or last_st.level == subtitle.level
            title.add_sub subtitle
            return
          end
          title = last_st
          last_st = last_st.last_subtitle
        end
      end

      def to_h
        h.subtitles.map(&:to_h)
      end

      class Title

        attr_reader :level, :title, :subtitles
        attr_accessor :parent_title

        def initialize lvl, ti
          @level = lvl
          @title = ti
          @subtitles = []
        end

        def last_subtitle
          subtitles.last
        end

        def add_sub node
          node.parent_title = self
          subtitles << node
        end

        def add_sib node
          parent_title.add_sub node
        end

        def to_h
          return title if subtitles.empty?
          {title => subtitles.map(&:to_h)}
        end

      end

      private

      def h
        @h
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ever_exp-0.1.3 lib/ever_exp/heading.rb
ever_exp-0.1.2 lib/ever_exp/heading.rb
ever_exp-0.1.1 lib/ever_exp/heading.rb
ever_exp-0.1.0 lib/ever_exp/heading.rb