Sha256: 6c34eab5ef8f7e35b97f5293f9bed8e55bb8aa2299a433496b856aa14302132a

Contents?: true

Size: 677 Bytes

Versions: 1

Compression:

Stored size: 677 Bytes

Contents

module Tocer
  # Transforms Markdown headers into anchored links.
  class Transformer
    def initialize text
      @text = text
    end

    def pounds
      String text[/\#{1,}/]
    end

    def label
      return "" if text.empty?
      text[pounds.length + 1, text.length].strip
    end

    def bullet
      return "" if pounds.empty?
      pounds_to_spaces(pounds).gsub(/\s{2}$/, "- ")
    end

    def url
      label.downcase.gsub(/\s/, "-").gsub(/[^\w\-\+\&]+/, "")
    end

    def transform
      "#{bullet}[#{label}](##{url})"
    end

    private

    attr_reader :text

    def pounds_to_spaces pounds
      Array.new(pounds.length, "  ").join
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tocer-0.1.0 lib/tocer/transformer.rb