Module: InlineFn

Included in:
String
Defined in:
lib/inline_fn.rb,
lib/inline_fn/version.rb

Overview

Change markdown footnotes format from ‘[^1]/:Note` to inline Pandoc or MMD style `^[Note]/`.

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.1.6'

Instance Method Summary collapse

Instance Method Details

#inline_fn(str, style = :pandoc) ⇒ Object



9
10
11
12
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
41
42
43
44
45
46
47
48
# File 'lib/inline_fn.rb', line 9

def inline_fn(str, style = :pandoc)
  ref_start = ''
  text = str
  counter = 0

  until ref_start.nil?
    counter += 1
    cite      = "[^#{counter}]"
    ref       = "[^#{counter}]:"
    ref_start = text.index(ref)
    break if ref_start.nil?

    next_ref = "[^#{counter + 1}]:"
    ref_end  = text.index(next_ref).nil? ? -1 : text.index(next_ref) - 2
    offset   = counter.to_s.length + 5
    note     = case style
               when :mmd
                 "[^#{text[ref_start + offset..ref_end].strip}]"
               else
                 "^[#{text[ref_start + offset..ref_end].strip}]"
               end

    text = text.gsub(cite, note)
  end

  if counter >= 1
    case style
    when :mmd
      text = text.gsub(/\n\s*\[\^/, "\n[^")
      cut_point = text.index("\n[^")
    else
      text = text.gsub(/\n\s*\^\[/, "\n^[")
      cut_point = text.index("\n^")
    end

    text = text[0, cut_point]
    # puts "#{counter -= 1} notes replaced."
  end
  text
end

#inline_fn_mmdObject



54
55
56
# File 'lib/inline_fn.rb', line 54

def inline_fn_mmd
  inline_fn(self, :mmd)
end

#inline_fn_pandocObject



50
51
52
# File 'lib/inline_fn.rb', line 50

def inline_fn_pandoc
  inline_fn(self, :pandoc)
end