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.5'

Instance Method Summary collapse

Instance Method Details

#inline_fn(text, style, *array_of_strs) ⇒ 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/inline_fn.rb', line 9

def inline_fn(text, style, *array_of_strs)
  # str is the text to be processed
  # style is the style of the inline footnote: :pandoc or :mmd
  # text is an optional array of strings `%w{fn cf}` that need to be removed from the footnotes before processing. This is useful in case you are converting Scrivener footnotes (`[^fn1]` or `[^cf1]`).
  ref_start = ''
  counter = 0

  array_of_strs.each do |str|
    text = text.gsub(/\[\^#{str}/, '[^')
  end

  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
    # p text
    error_msg = <<~EOF
      ERROR: No cut point found.

      Footnotes should be in the format `[^1]` AND NOT `[^fn1]` or `[^cf1]`. If you are converting Scrivener markdown, please use the `scrivener_to_pandoc` (== inline_fn(self, :pandoc, 'fn', 'cf')) or `scrivener_to_mmd` (== inline_fn(self, :mmd, 'fn', 'cf')) methods.

      If you have footnotes in other formats, you can try the `inline_fn` method. It takes as arguments the text to be processed, the style of the inline footnote: :pandoc or :mmd, and an optional array of strings `%w{fn cf}` that need to be removed from the footnotes before processing.
EOF

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

#inline_fn_mmdObject



72
73
74
# File 'lib/inline_fn.rb', line 72

def inline_fn_mmd
  inline_fn(self, :mmd)
end

#inline_fn_pandocObject



64
65
66
# File 'lib/inline_fn.rb', line 64

def inline_fn_pandoc
  inline_fn(self, :pandoc)
end

#mmd_fnObject



76
77
78
# File 'lib/inline_fn.rb', line 76

def mmd_fn
  inline_fn_mmd
end

#pandoc_fnObject



68
69
70
# File 'lib/inline_fn.rb', line 68

def pandoc_fn
  inline_fn_pandoc
end

#scrivener_to_mmdObject



84
85
86
# File 'lib/inline_fn.rb', line 84

def scrivener_to_mmd
  inline_fn(self, :mmd, 'fn', 'cf')
end

#scrivener_to_pandocObject



80
81
82
# File 'lib/inline_fn.rb', line 80

def scrivener_to_pandoc
  inline_fn(self, :pandoc, 'fn', 'cf')
end