Top Level Namespace
- Includes:
- ERB::Util
Constant Summary collapse
- DOCUMENT_SECTIONS =
Add labels to elements such as figures and sections, and make them referencable.
[ 'Chapter', 'Section', 'Subsection', 'Subsubsection' ]
Instance Method Summary collapse
-
#add_labels_to_figures(content, labels) ⇒ Object
Adds labels to referenceable figures.
-
#create_labels(document_sections, sections_index, content) ⇒ Object
Creates labels for referenceable elements.
-
#hyphenate(text) ⇒ Object
Add zero-width space after slashes and hyphens to allow hyphenation.
-
#label_type_for(document_sections, tag, attributes) ⇒ Object
Determines the label type of a given element.
- #number_for(document_sections, type, name, id, sections_index) ⇒ Object
-
#parse_attributes(attribute_list) ⇒ Object
Parses a string of HTML attributes.
-
#person(name, website, profile, mainAuthor = true) ⇒ Object
Create a person block.
-
#preprocess_katex_assets ⇒ Object
Make sure our KaTeX assets are available (this can be disabled if you are not using math-mode).
-
#section(id, classes = nil) ⇒ Object
Create a section block with the given file contents.
-
#serialize_acronyms(acronyms) ⇒ Object
Serialize the list of acronyms to a table.
-
#serialize_sections_index(sections_index, max_depth, content) ⇒ Object
Replace ‘<div id=“toc-index”></div>’ with the sections index.
-
#serialize_sections_index_row(sections_index, max_depth, depth, root) ⇒ Object
Serialize a single row of index entries, and recursively create the index for sub-entries.
-
#set_reference_labels(content, labels) ⇒ Object
Sets the labels of unlabeled references in the text.
Instance Method Details
#add_labels_to_figures(content, labels) ⇒ Object
Adds labels to referenceable figures
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# 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) ⇒ Object
Creates labels for referenceable elements
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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) ⇒ 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(document_sections, tag, attributes) ⇒ Object
Determines the label type of a given element
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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) ⇒ Object
Parses a string of HTML attributes
142 143 144 145 146 |
# 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) ⇒ Object
Create a person block
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/scholarmarkdown/snippets.rb', line 26 def person(name, website, profile, mainAuthor = true) if mainAuthor # Add person to global list of authors ||= [] .push(name) end # Handle "Firstname, Lastname" format display_name = if name.include?(",") firstname, lastname = name.split(",", 2).map(&:strip) %{<span class="first-name">#{h firstname}</span> #{h lastname}} else h name end if not website %{<span id="author-name">#{display_name}</span>} elsif not profile %{<a id="author-name" href="#{h website}">#{display_name}</a>} else %{<a id="author-name" rev="lsc:participatesIn" property="foaf:maker schema:creator schema:author schema:publisher" href="#{h website}" typeof="foaf:Person schema:Person" resource="#{profile}">#{display_name}</a>} end end |
#preprocess_katex_assets ⇒ Object
Make sure our KaTeX assets are available (this can be disabled if you are not using math-mode)
6 7 8 9 10 11 12 13 14 15 16 |
# 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) ⇒ Object
Create a section block with the given file contents
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 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 "<section \#{section_suffix}>\n<div datatype=\"rdf:HTML\" property=\"schema:description\" markdown=\"block\">\n\#{item.raw_content}\n</div>\n</section>\n HTML\nend\n" |
#serialize_acronyms(acronyms) ⇒ Object
Serialize the list of acronyms to a table
24 25 26 27 28 29 30 31 32 33 |
# 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) ⇒ Object
Replace ‘<div id=“toc-index”></div>’ with the sections index
149 150 151 152 153 |
# 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) ⇒ Object
Serialize a single row of index entries, and recursively create the index for sub-entries
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# 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) ⇒ Object
Sets the labels of unlabeled references in the text
131 132 133 134 135 136 137 138 139 |
# 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 |