Class: Poppins::Document
- Inherits:
-
Object
- Object
- Poppins::Document
- Defined in:
- lib/poppins/document.rb
Instance Attribute Summary collapse
-
#inline_links ⇒ Object
readonly
Returns the value of attribute inline_links.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#reference_links ⇒ Object
readonly
Returns the value of attribute reference_links.
Instance Method Summary collapse
-
#clean_and_format(input = @input) ⇒ Object
Produces the clean, formatted version of the Markdown with new reference numbers.
-
#convert_inline_to_reference(input = @input) ⇒ Object
Produces a clean, formatted version of the Markdown while also converting inline links to reference links.
-
#convert_reference_to_inline(input = @input) ⇒ Object
Convert reference links to inline links.
-
#identify_inline_links(input = @input) ⇒ Object
Identifies inline links in the document.
-
#identify_reference_links(input = @input) ⇒ Object
Identifies reference links in the document.
-
#initialize(input = nil, output = nil) ⇒ Document
constructor
A new instance of Document.
Constructor Details
#initialize(input = nil, output = nil) ⇒ Document
Returns a new instance of Document.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/poppins/document.rb', line 9 def initialize(input=nil, output=nil) if input.empty? raise "invalid input" unless (ARGF.respond_to?(:readlines) and not ARGF.nil?) @input = ARGF.readlines.join else @input = File.open(input, 'r').readlines.join end @output = output identify_inline_links identify_reference_links end |
Instance Attribute Details
#inline_links ⇒ Object (readonly)
Returns the value of attribute inline_links.
7 8 9 |
# File 'lib/poppins/document.rb', line 7 def inline_links @inline_links end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
7 8 9 |
# File 'lib/poppins/document.rb', line 7 def links @links end |
#reference_links ⇒ Object (readonly)
Returns the value of attribute reference_links.
7 8 9 |
# File 'lib/poppins/document.rb', line 7 def reference_links @reference_links end |
Instance Method Details
#clean_and_format(input = @input) ⇒ Object
Produces the clean, formatted version of the Markdown with new reference numbers.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/poppins/document.rb', line 60 def clean_and_format(input=@input) result = input result = convert_reference_to_inline(result) result = convert_inline_to_reference(result) identify_reference_links(result) # Remove old reference labels from the end # TODO: This currently destroys links that don't appear anywhere else in # the text since they aren't added back in unless they were used in the # text.. result = result.gsub(ReferenceLink.label_regex_with_possible_newlines, '') # Add new reference labels to the end @reference_links.each_with_index do |rl, i| result += "[#{i+1}]: #{rl.url}\n" end # Replace old reference numbers with the new ones result = result.gsub(ReferenceLink.link_regex) do |match| # TODO: There has to be a cleaner way to do this... match_data = match.match(ReferenceLink.link_regex) link_text = match_data[1].to_s label_from_match = match_data[2].to_s new_label = @reference_links.index { |rl| rl.label == label_from_match } # TODO: Do you really want to strip whitespace here? "[#{link_text.strip}][#{new_label + 1}]" end # return the result if @output File.open(@output, 'w') do |f| f.write(result) end end result end |
#convert_inline_to_reference(input = @input) ⇒ Object
Produces a clean, formatted version of the Markdown while also converting inline links to reference links.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/poppins/document.rb', line 104 def convert_inline_to_reference(input=@input) result = input identify_inline_links(result) @inline_links.each do |link| reference_label = SecureRandom.hex(8) result = result.gsub(link.regex) do |match| "[#{link.link_text}][#{reference_label}]" end result += "[#{reference_label}]: #{link.url}\n" end result end |
#convert_reference_to_inline(input = @input) ⇒ Object
Convert reference links to inline links. This can be used prior to converting back to reference links to ensure that all links are reference links and they are numbered in the order they appear.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/poppins/document.rb', line 123 def convert_reference_to_inline(input=@input) result = input # TODO: Should DRY this labels = Hash[input.scan(ReferenceLink.label_regex)] ref_urls = labels.values ref_urls.each do |url| regex = /\[([^\]]+)\]\[(#{Regexp.quote(url)})\]/ result = result.gsub(regex) do |match| "[#{match[1]}](#{url})" end end # TODO: Need to be able to store this separately if they want to have # inline links only. result end |
#identify_inline_links(input = @input) ⇒ Object
Identifies inline links in the document
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/poppins/document.rb', line 26 def identify_inline_links(input=@input) inline_links = Hash[input.scan(InlineLink.inline_regex)] @inline_links = [] inline_links.each do |il| @inline_links.push( InlineLink.new(il.to_a[1], il.to_a[0]) ) end @inline_links end |
#identify_reference_links(input = @input) ⇒ Object
Identifies reference links in the document
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/poppins/document.rb', line 40 def identify_reference_links(input=@input) reference_links = Hash[input.scan(ReferenceLink.link_regex)] labels = Hash[input.scan(ReferenceLink.label_regex)] @reference_links = [] reference_links.each do |rl| @reference_links.push( ReferenceLink.new(labels[rl.to_a[1]], rl.to_a[0], rl.to_a[1] ) ) end # Note that the order of the links in this list is the same as their # appearance in the text. @reference_links end |