lib/inline_fn.rb in inline_fn-0.1.1 vs lib/inline_fn.rb in inline_fn-0.1.2

- old
+ new

@@ -1,9 +1,71 @@ -# frozen_string_literal: true +# frozen_string_literal: false require_relative "inline_fn/version" -require_relative "inline_fn/inline_mmd" -require_relative "inline_fn/inline_pandoc" + module InlineFn class Error < StandardError; end - class InlineFn < StandardError; end + + def inline_fn(str, style = :pandoc) + cut_point = "" + 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 + + def inline_fn_pandoc + inline_fn(self, :pandoc) + end + + def inline_fn_mmd + inline_fn(self, :mmd) + end end + +class String + include InlineFn +end + + +# text = %(Lorem ipsum dolor sit amet[^1], consectetur adipisicing elit[^2], sed... +# [^1]: Text of fn 1 +# [^2]: Text of fn 2) + +# puts inline_fn(text, :mmd) +# puts inline_fn(text, :pandoc) +# puts text.inline_fn_pandoc +# puts text.inline_fn_mmd \ No newline at end of file