Sha256: 649411ea8c00898fa7d6a128df9e005c0333dff1659a5f0b61283c321cea867c

Contents?: true

Size: 920 Bytes

Versions: 13

Compression:

Stored size: 920 Bytes

Contents

require 'csv'

# A filter for displaying full names of acronyms
Nanoc::Filter.define(:scholar_acronym) do |content, params|
  acronyms = CSV.parse(params[:acronyms].raw_content, :headers => true)
  
  # Annotate all occurrences with their full name
  transformed = content.dup
  acronyms.each do |row|
    transformed.gsub! %r{(?<=[\s\.,!?])#{row['abbreviation']}(?=[\s\.,!?])} do |match|
      %{<abbr title='#{row['full']}'>#{row['abbreviation']}</abbr>}
    end
  end
  
  # Inject acronyms list
  transformed = transformed.gsub! %r{<div id="scholarmarkdown-acronyms-list"></div>} do |match|
    serialize_acronyms acronyms
  end
  
  transformed
end

# Serialize the list of acronyms to a table
def serialize_acronyms acronyms
  table = "<table class=\"acronyms\">"
  
  acronyms.each do |row|
    table += "<tr><th>#{row['abbreviation']}</th><td>#{row['full']}</td></tr>"
  end
  
  table += "</table>"
  table
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
scholarmarkdown-3.2.1 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-3.2.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-3.1.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-3.0.1 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-3.0.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-2.11.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-2.10.1 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-2.10.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-2.9.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-2.8.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-2.7.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-2.6.0 lib/scholarmarkdown/filter/acronym.rb
scholarmarkdown-2.5.0 lib/scholarmarkdown/filter/acronym.rb