class Jekyll::HyperlinkGlossaryEntries

Constants

BODY_START_TAG
OPENING_BODY_TAG_REGEX

Public Class Methods

hyperlinkify(document) click to toggle source

Replace all oocurances of glossary entries with a hyperlink.

# File lib/jekyll-hyperlinkify-glossary.rb, line 11
def hyperlinkify(document)
    @glossary_entries = document.data["glossary_entries"]
    if @glossary_entries.nil?
        puts "This document has no glossary entries: " + document.data["title"].to_s
        return
    end
    @test_word = "icf"
    @test_array = ["icf","inklusion"]
    @test_url = "/glossary/icf.html"
    document.output = if document.output.include? BODY_START_TAG
                        process_document(document)
                    else
                        process_html_body(document.output)
                    end

end
processable?(document) click to toggle source

Determine if the content should be processed.

# File lib/jekyll-hyperlinkify-glossary.rb, line 29
def processable?(document)
    (document.is_a?(Jekyll::Page) || document.write?) &&
    (document.output_ext == ".html" || document.permalink&.end_with?("/")) &&
    (document.data["jekyll-hyperlink-glossary"] != false)
end

Private Class Methods

process_document(document) click to toggle source

Process html content which has an body opening tag

# File lib/jekyll-hyperlinkify-glossary.rb, line 38
def process_document(document)
    head, opener, tail = document.output.partition(OPENING_BODY_TAG_REGEX)
    body_content, *rest = tail.partition("</body>")
    
    processed_markup = process_html_body(body_content, document.data["title"])

    document.output = String.new(head) << opener << processed_markup << rest.join
end
process_html_body(html, title) click to toggle source

Process every word of content and replace glossary entries

# File lib/jekyll-hyperlinkify-glossary.rb, line 48
def process_html_body(html, title)
    @glossary_entries.each do |glossary_entry|   
        if glossary_entry[1].to_s != title.to_s.downcase
            exclude_regex="\\b(?!<(h\\d|a).*>)(?!.*<\/(h\\d|a)>)(?!.html)"
            glossary_entry_regex = Regexp.new("\\b"+glossary_entry.drop(1).join(exclude_regex+"|")+exclude_regex, true)
            first_half_of_hyperlink = "<a href=\"#{ glossary_entry[0] }\">"
            html = html.gsub(glossary_entry_regex, first_half_of_hyperlink+ '\0</a>')
        end
    end
    html
end