Class: BibMarkdown::Document

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

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Document

Returns a new instance of Document.



7
8
9
10
11
# File 'lib/bibmarkdown/document.rb', line 7

def initialize(source, options)
  @source = source
  @entries = options[:entries]
  @style = options[:style]
end

Instance Method Details

#to_markdownObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bibmarkdown/document.rb', line 13

def to_markdown
  @references = {}

  # Replace all citations by links
  markdown = @source.gsub %r{\[([^\]]*)\]\(cit[eo]:(\w+)\s+([^\)]+)\)} do |match|
    text = $1; rel = $2; keys = $3

    # Create the references
    refs = keys.strip.split(/\s*,\s*/).map {|key| create_reference key }
    raise "Missing reference key in #{match}" if refs.empty?
    reflinks = refs.map{|r| r[:link]}.join ''

    # If the link text is empty, output links to the references
    if text.empty?
      reflinks
    # If there is no URL, output the text followed by links to the references
    elsif refs.first[:url].empty?
      "#{text} #{reflinks}"
    # Otherwise, output the linked text and the references
    else
      property = 'http://purl.org/spar/cito/' + rel
      "#{create_link text, refs.first[:url], property: property} #{reflinks}"
    end
  end

  # Append the reference list to the document
  "#{markdown}\n\n#{references_html}".rstrip
end