Class: Remark
- Inherits:
-
Object
- Object
- Remark
- Defined in:
- lib/remark.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :reference_links => true }
Instance Method Summary collapse
-
#initialize(source, options = {}) ⇒ Remark
constructor
A new instance of Remark.
- #inline_links? ⇒ Boolean
- #output_reference_links(links) ⇒ Object
- #scope ⇒ Object
- #to_markdown ⇒ Object
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, = {}) @doc = Hpricot(source) @options = DEFAULT_OPTIONS.merge @links = [] @ignored_elements = nil end |
Instance Method Details
#inline_links? ⇒ Boolean
36 37 38 |
# File 'lib/remark.rb', line 36 def inline_links? !@options[:reference_links] end |
#output_reference_links(links) ⇒ Object
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 |
#scope ⇒ Object
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_markdown ⇒ Object
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 |