class Jekyll::PilcrowConverter
Public Instance Methods
convert(content)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 17 def convert(content) document = Kramdown::Document.new(content) document.root = add_pilcrows(document.root) document.to_html end
matches(ext)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 9 def matches(ext) ext == '.md' end
output_ext(_ext)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 13 def output_ext(_ext) '.html' end
Protected Instance Methods
add_pilcrow_to_paragraph(paragraph)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 50 def add_pilcrow_to_paragraph(paragraph) id = generate_paragraph_id(paragraph) pilcrow = build_pilcrow_element(id) paragraph.attr[:id] = id paragraph.children = [pilcrow] + paragraph.children paragraph end
add_pilcrows(element)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 25 def add_pilcrows(element) element.children = element.children.inject([]) do |children, child_element| if paragraph_element?(child_element) paragraph = add_pilcrow_to_paragraph(child_element) children + [paragraph] elsif !child_element.children.empty? children + [add_pilcrows(child_element)] else children + [child_element] end end element end
build_pilcrow_element(id)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 58 def build_pilcrow_element(id) link = Kramdown::Element.new( :html_element, 'a', href: "##{id}", class: 'pilcrow' ) text = Kramdown::Element.new(:text, 'ΒΆ ') link.children = [text] link end
generate_paragraph_id(paragraph)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 70 def generate_paragraph_id(paragraph) text = get_element_text(paragraph) Base64.urlsafe_encode64(Digest::MD5.new.digest(text)) end
get_element_text(element)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 75 def get_element_text(element) element.children.map do |child_element| if child_element.type == :text child_element.value elsif !child_element.children.empty? get_element_text(child_element) else '' end end.join('') end
paragraph_element?(element)
click to toggle source
# File lib/jekyll_pilcrow_converter.rb, line 39 def paragraph_element?(element) return false if element.children.empty? return false unless element.type == :html_element && element.value == 'p' first_child = element.children.first return true if first_child.type == :text return true if first_child.type == :html_element && first_child.value == 'a' false end