Class: Remark

Inherits:
Object
  • Object
show all
Defined in:
lib/remark.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ :reference_links => true }

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Remark

Returns a new instance of Remark.



6
7
8
9
10
11
# File 'lib/remark.rb', line 6

def initialize(source, options = {})
  @doc = Hpricot(source)
  @options = DEFAULT_OPTIONS.merge options
  @links = []
  @ignored_elements = nil
end

Instance Method Details

#inline_links?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/remark.rb', line 36

def inline_links?
  !@options[:reference_links]
end


40
41
42
43
44
45
46
# File 'lib/remark.rb', line 40

def output_reference_links(links)
  references = []
  links.each_with_index do |(href, title), i|
    references << "[#{i + 1}]: #{href}#{title ? '  ' + title.inspect : ''}"
  end
  references.join("\n")
end

#scopeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/remark.rb', line 21

def scope
  if scope = @options[:scope]
    @doc.at(scope)
  elsif body = @doc.at('/html/body')
    candidates = (body / 'p').inject(Hash.new(0)) do |memo, para|
      memo[para.parent] += 1
      memo
    end.invert
    
    candidates[candidates.keys.max]
  else
    @doc
  end
end

#to_markdownObject



13
14
15
16
17
18
19
# File 'lib/remark.rb', line 13

def to_markdown
  parent = scope
  collect_ignored_elements(parent)
  links = @options[:links] = [] unless inline_links?
  result = parent.to_markdown(@options)
  result + (inline_links? || links.empty?? '' : "\n\n\n" + output_reference_links(links))
end