lib/tocer/transformers/link.rb in tocer-8.2.1 vs lib/tocer/transformers/link.rb in tocer-9.0.0
- old
+ new
@@ -6,48 +6,51 @@
module Transformers
# Transforms a Markdown header (embedded link) into a table of contents link.
class Link
using Refinements::Arrays
- def initialize text, header: Parsers::Header
- @header = header.new text
+ def initialize text, parser: Parsers::Header
+ @parser = parser.new text
end
def label
- header.content.gsub embedded_link, embedded_link_label
+ parser.content.gsub embedded_link, embedded_link_label
end
def url
label.downcase.gsub(/\s/, "-").gsub(/[^\w\-]+/, "")
end
- def transform url_suffix: ""
- modified_url = [url, url_suffix.to_s].compress.join "-"
- "#{indented_bullet}[#{label}](##{modified_url})"
+ def call url_suffix: ""
+ "#{indented_bullet}[#{label}](##{computed_url url_suffix})"
end
private
- attr_reader :header
+ attr_reader :parser
- def embedded_link_label
- header.content[/\[(.*)\]/, 1]
+ def computed_url suffix = ""
+ [url, suffix.to_s].compress.join "-"
end
- def embedded_link_url
- header.content[/\((.*)\)/, 1]
- end
-
def embedded_link
"[#{embedded_link_label}](#{embedded_link_url})"
end
- def prefix_to_spaces
- Array.new(header.prefix.length, " ").join
+ def embedded_link_label
+ parser.content[/\[(.*)\]/, 1]
end
+ def embedded_link_url
+ parser.content[/\((.*)\)/, 1]
+ end
+
def indented_bullet
prefix_to_spaces.gsub(/\s{2}$/, "- ")
+ end
+
+ def prefix_to_spaces
+ Array.new(parser.prefix.length, " ").join
end
end
end
end