class Object

Constants

DOCUMENT_SECTIONS

Add labels to elements such as figures and sections, and make them referencable.

Public Instance Methods

add_labels_to_figures(content, labels) click to toggle source

Adds labels to referenceable figures

# File lib/scholarmarkdown/filter/labelify.rb, line 108
def add_labels_to_figures content, labels
  # Subfigures
  content.gsub! %r{<figure[^>]*\s+id="([^"]+)"\s+class="[^"]*subfigure[^"]*".*?<figcaption>(?:\s*<p>)?}m do |match|
    id=$1
    match = match.sub! '<figcaption>', '<figcaption class="for-subfigure">'
    if labels.key? id
      %{#{match}<span class="label">#{h labels[id]}:</span> }
    else
      match
    end
  end

  # Main figures
  content.gsub! %r{<figure[^>]*\s+id="([^"]+)".*?<figcaption>(?:\s*<p>)?}m do |match|
    if labels.key? $1
      %{#{match}<span class="label">#{h labels[$1]}:</span> }
    else
      match
    end
  end
end
create_labels(document_sections, sections_index, content) click to toggle source

Creates labels for referenceable elements

# File lib/scholarmarkdown/filter/labelify.rb, line 30
def create_labels document_sections, sections_index, content
  @reference_counts = {}
  main = content[%r{<main>.*</main>}m]
  appendix = content[%r{<div id="appendix"[^>]*>.*</div>}m] || ""
  labels = (main + appendix).scan(/<(\w+)([^>]*\s+id="([^"]+)"[^>]*)>([^<]*)(?=<)/)
               .map do |tag, attribute_list, id, name|
    attributes = parse_attributes(attribute_list)
    type = label_type_for document_sections, tag.downcase.to_sym, attributes
    number = 0
    if attributes[:class].nil? or !attributes[:class].include? 'no-label-increment'
      number = number_for document_sections, type, name, id, sections_index
    end
    [id, "#{type} #{number}"]
  end
  labels.to_h
end
hyphenate(text) click to toggle source

Add zero-width space after slashes and hyphens to allow hyphenation

# File lib/scholarmarkdown/filter/hyphenate_iri.rb, line 16
def hyphenate text
  text.gsub %r{(?<=/|-)}, "\u200B"
end
label_type_for(document_sections, tag, attributes) click to toggle source

Determines the label type of a given element

# File lib/scholarmarkdown/filter/labelify.rb, line 48
def label_type_for document_sections, tag, attributes
  case tag
  when :h2
    document_sections[0]
  when :h3
    document_sections[1]
  when :h4
    document_sections[2]
  when :figure
    unless attributes[:class].nil?
      for clazz in attributes[:class].split(' ') do
        case clazz
        when 'algorithm'
          return 'Algorithm'
        when 'listing'
          return 'Listing'
        when 'table'
          return 'Table'
        when 'equation'
          return 'Equation'
        when 'subfigure'
          return 'Subfig.'
        when 'definition'
          return 'Definition'
        end
      end
    end
    'Fig.'
  else
    'Unknown'
  end
end
number_for(document_sections, type, name, id, sections_index) click to toggle source
# File lib/scholarmarkdown/filter/labelify.rb, line 81
def number_for document_sections, type, name, id, sections_index
  # Determine number of elements
  @reference_counts[type] ||= 0
  number = @reference_counts[type] += 1

  # Perform hierarchical numbering when needed
  case type
  when document_sections[0]
    @reference_counts[document_sections[1]] = 0
    @reference_counts[document_sections[2]] = 0
    sections_index[@reference_counts[document_sections[0]] - 1] = { :name => name, :id => id, :children => [] }
  when document_sections[1]
    @reference_counts[document_sections[2]] = 0
    number = "#{reference_counts[document_sections[0]]}.#{number}"
    sections_index[@reference_counts[document_sections[0]] - 1][:children][@reference_counts[document_sections[1]] - 1] = { :name => name, :id => id, :children => [] }
  when document_sections[2]
    number = "#{reference_counts[document_sections[0]]}.#{reference_counts[document_sections[1]]}.#{number}"
    sections_index[@reference_counts[document_sections[0]] - 1][:children][@reference_counts[document_sections[1]] - 1][:children][@reference_counts[document_sections[2]] - 1] = { :name => name, :id => id, :children => [] }
  when 'Fig.'
    @reference_counts['Subfig.'] = 0
  when 'Subfig.'
    number = "#{reference_counts['Fig.']}.#{number}"
  end
  number
end
parse_attributes(attribute_list) click to toggle source

Parses a string of HTML attributes

# File lib/scholarmarkdown/filter/labelify.rb, line 142
def parse_attributes attribute_list
  attribute_list.scan(/\s*(\w+)\s*=\s*"([^"]+)"\s*/)
                .map { |k,v| [k.downcase.to_sym, v] }
                .to_h
end
person(name, website, profile, mainAuthor = true) click to toggle source

Create a person block

# File lib/scholarmarkdown/snippets.rb, line 26
def person name, website, profile, mainAuthor = true
  if mainAuthor
    # Add person to global list of authors
    unless $authors
      $authors = []
    end
    $authors.push(name)
  end

  if not website
    h name
  elsif not profile
    %{<a href="#{h website}">#{h name}</a>}
  else
    %{<a rev="lsc:participatesIn" property="foaf:maker schema:creator schema:author schema:publisher" href="#{h website}" typeof="foaf:Person schema:Person" resource="#{profile}">#{h name}</a>}
  end
end
preprocess_katex_assets() click to toggle source

Make sure our KaTeX assets are available (this can be disabled if you are not using math-mode)

# File lib/scholarmarkdown/preprocess/katex.rb, line 6
def preprocess_katex_assets
  # Copy stylesheet
  @items.create(File.open(File.join(Katex.gem_path, 'vendor', 'katex', 'stylesheets', 'katex.css'), 'r').read, {}, '/styles/katex.css')

  # Copy fonts
  fontPath = File.join(Katex.gem_path, 'vendor', 'katex', 'fonts')
  Dir.foreach(fontPath) do |item|
    next if item == '.' or item == '..'
    @items.create(File.open(File.join(fontPath, item), 'r').read, {}, '/styles/fonts/' + item)
  end
end
section(id, classes = nil) click to toggle source

Create a section block with the given file contents

# File lib/scholarmarkdown/snippets.rb, line 7
def section id, classes = nil
  section_suffix=''
  if classes
    section_suffix=" class=\"" + classes + "\""
  end
  item = @items["/#{id.to_s}.*"]
  if not item
    raise "Could not find the file '" + id.to_s + "'"
  end
  <<-HTML
<section #{section_suffix}>
<div datatype="rdf:HTML" property="schema:description" markdown="block">
#{item.raw_content}
</div>
</section>
  HTML
end
serialize_acronyms(acronyms) click to toggle source

Serialize the list of acronyms to a table

# File lib/scholarmarkdown/filter/acronym.rb, line 24
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
serialize_sections_index(sections_index, max_depth, content) click to toggle source

Replace '<div id=“toc-index”></div>' with the sections index

# File lib/scholarmarkdown/filter/labelify.rb, line 149
def serialize_sections_index sections_index, max_depth, content
  content.gsub! %r{<div id="scholarmarkdown-toc-index"></div>} do |match|
    serialize_sections_index_row sections_index, max_depth, 0, true
  end
end
serialize_sections_index_row(sections_index, max_depth, depth, root) click to toggle source

Serialize a single row of index entries, and recursively create the index for sub-entries

# File lib/scholarmarkdown/filter/labelify.rb, line 156
def serialize_sections_index_row sections_index, max_depth, depth, root
  if max_depth == 0
    return ""
  end

  table = "<ol class=\"#{root ? "index-entries index-entries-root" : "index-entries"}\" depth=\"#{depth}\">"
  sections_index.each do |section|
    index_sub = serialize_sections_index_row section[:children], max_depth - 1, depth + 1, false
    table += "<li><a href=\"##{section[:id]}\" class=\"index-entry-name\">#{section[:name]}</a>#{index_sub}</li>"
  end
  table += "</ol>"
  table
end
set_reference_labels(content, labels) click to toggle source

Sets the labels of unlabeled references in the text

# File lib/scholarmarkdown/filter/labelify.rb, line 131
def set_reference_labels content, labels
  content.gsub! %r{(<a href="#([^"]+)">)(</a>)} do |match|
    if labels.key? $2
      "#{$1}#{h labels[$2]}#{$3}"
    else
      match
    end
  end
end