Top Level Namespace

Includes:
ERB::Util

Instance Method Summary collapse

Instance Method Details

#add_labels_to_figures(content, labels) ⇒ Object

Adds labels to referenceable figures



68
69
70
71
72
73
74
75
76
# File 'lib/scholarmarkdown/filter/labelify.rb', line 68

def add_labels_to_figures content, labels
  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(content) ⇒ Object

Creates labels for referenceable elements



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scholarmarkdown/filter/labelify.rb', line 13

def create_labels 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|
    type = label_type_for tag.downcase.to_sym, attribute_list
    number = number_for type
    [id, "#{type} #{number}"]
  end
  labels.to_h
end

#hyphenate(text) ⇒ Object

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



16
17
18
# File 'lib/scholarmarkdown/filter/hyphenate_iri.rb', line 16

def hyphenate text
  text.gsub %r{(?<=/|-)}, "\u200B"
end

#label_type_for(tag, attribute_list) ⇒ Object

Determines the label type of a given element



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/scholarmarkdown/filter/labelify.rb', line 27

def label_type_for tag, attribute_list
  case tag
  when :h2
    'Section'
  when :h3
    'Subsection'
  when :figure
    unless parse_attributes(attribute_list)[:class].nil?
      for clazz in parse_attributes(attribute_list)[:class].split(' ') do
        case clazz
        when 'algorithm'
          return 'Algorithm'
        when 'listing'
          return 'Listing'
        when 'table'
          return 'Table'
        end
      end
    end
    'Fig.'
  else
    'Unknown'
  end
end

#number_for(type) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/scholarmarkdown/filter/labelify.rb', line 52

def number_for type
  # Determine number of elements
  @reference_counts[type] ||= 0
  number = @reference_counts[type] += 1

  # Perform hierarchical numbering when needed
  case type
  when 'Section'
    @reference_counts['Subsection'] = 0
  when 'Subsection'
    number = "#{reference_counts['Section']}.#{number}"
  end
  number
end

#parse_attributes(attribute_list) ⇒ Object

Parses a string of HTML attributes



90
91
92
93
94
# File 'lib/scholarmarkdown/filter/labelify.rb', line 90

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) ⇒ Object

Create a person block



20
21
22
23
24
25
26
27
28
# File 'lib/scholarmarkdown/snippets.rb', line 20

def person name, website, profile
  if not website
    h name
  elsif not profile
    %{<a href="#{h website}">#{h name}</a>}
  else
    %{<a href="#{h website}" typeof="http://xmlns.com/foaf/0.1/Person" resource="#{profile}">#{h name}</a>}
  end
end

#section(id) ⇒ Object

Create a section block with the given file contents



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/scholarmarkdown/snippets.rb', line 7

def section id
  item = @items["/#{id.to_s}.*"]
  if not item
    raise "Could not find the file '" + id.to_s + "'"
  end
  "<section markdown=\"block\">\n\#{item.raw_content}\n</section>\n  HTML\nend\n"

#set_reference_labels(content, labels) ⇒ Object

Sets the labels of unlabeled references in the text



79
80
81
82
83
84
85
86
87
# File 'lib/scholarmarkdown/filter/labelify.rb', line 79

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