Sha256: 32b09cb2129f261ecb7abc036a3570c45a264e4add86aa052e8b03882d6781cd

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# -*- coding: utf-8 -*-
=begin rdoc
=end

module SixArm; module Markdown; end; end

class SixArm::Markdown::Headline
  include EqualInstanceVariables

  attr_accessor \
    :text,
    :anchor,
    :level,
    :link

  def initialize(
    text: nil,
    anchor: nil,
    level: nil,
    link: nil
  )
    raise ArgumentError if !text.kind_of?(String)
    @text = text
    @anchor = anchor || self.class.text_to_anchor(text)
    @level = level
    @link = link
  end

  def anchor
    @anchor || self.class.text_to_anchor(text)
  end

  def level
    @level || 1
  end

  def link
    @link || "[#{text}](##{anchor})"
  end

  def self.parse_line(line)
    level = parse_line_level(line)
    text = trim_line(line)
    # TODO Add parser for anchor
    # TODO Add parser for Unicode normalization
    return SixArm::Markdown::Headline.new(text: text, level: level)
  end

  def self.parse_line_level(line)
    return line =~/^\s*([#=]+)/ ? $1.length : nil
  end

  def self.trim_line(line)
    line.sub(/^\s*[#=]+\s+/, '').sub(/\s+[#=]+\s*$/, '')
  end

  def self.text_to_anchor(text)
    text.downcase.gsub(/\W+/,'-').gsub(/^-+|-+$/,'')
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sixarm_ruby_markdown-3.1.0 lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb