class Ulysses::Printer
Constants
- HIDDEN_ELEMENTS
- HIDDEN_TAGS
- SHEET_CONTENT_XPATH
- SIMPLE_ELEMENTS
Public Class Methods
new(target)
click to toggle source
# File lib/ulysses/printer.rb, line 10 def initialize(target) @target = target @footnotes = [] @annotations = [] @html_entities = HTMLEntities.new end
Public Instance Methods
print()
click to toggle source
# File lib/ulysses/printer.rb, line 17 def print [print_body, print_footnotes].compact.join("\n") end
Private Instance Methods
parse_content(nodes)
click to toggle source
# File lib/ulysses/printer.rb, line 82 def parse_content(nodes) nodes.map do |node| if node.text? @html_entities.encode node.content else send("parse_#{node.name}", node) end end.join end
parse_element(node)
click to toggle source
# File lib/ulysses/printer.rb, line 114 def parse_element(node) kind = node.attributes['kind'].value.to_sym return '' if HIDDEN_ELEMENTS.include? kind return parse_simple_element(kind, node) if SIMPLE_ELEMENTS.has_key?(kind) send("parse_element_#{kind}", node) end
parse_element_annotation(node)
click to toggle source
# File lib/ulysses/printer.rb, line 153 def parse_element_annotation(node) attrs = parse_element_attributes(node) @annotations << attrs['text'] '<span class="annotation" data-id="' + @annotations.size.to_s + '">' + @annotations.size.to_s + '</span>' end
parse_element_attributes(element)
click to toggle source
# File lib/ulysses/printer.rb, line 159 def parse_element_attributes(element) attributes = element.children.select { |child| child.element? && child.name === 'attribute' } attributes.map! do |attr| [attr.attributes['identifier'].value, parse_content(attr.children)] end Hash[attributes] end
parse_element_footnote(node)
click to toggle source
# File lib/ulysses/printer.rb, line 147 def parse_element_footnote(node) attrs = parse_element_attributes(node) @footnotes << attrs['text'] '<sup class="footnote-ref"><a href="#fn-' + @footnotes.size.to_s + '">' + @footnotes.size.to_s + '</a></sup>' end
parse_element_image(node)
click to toggle source
# File lib/ulysses/printer.rb, line 137 def parse_element_image(node) attrs = parse_element_attributes(node) '<img src="' + attrs.fetch('URL', '') + '" alt="' + attrs.fetch('title', '') + '" />' end
parse_element_link(node)
click to toggle source
# File lib/ulysses/printer.rb, line 131 def parse_element_link(node) attrs = parse_element_attributes(node) content = parse_content(node.children.select{ |child| !child.element? || child.name != 'attribute' }) '<a href="' + attrs.fetch('URL', '') + '" title="' + attrs.fetch('title', '') + '">' + content + '</a>' end
parse_element_video(node)
click to toggle source
# File lib/ulysses/printer.rb, line 142 def parse_element_video(node) attrs = parse_element_attributes(node) '<video><source src="' + attrs['URL'] + '" /></video>' end
parse_escape(node)
click to toggle source
# File lib/ulysses/printer.rb, line 102 def parse_escape(node) node.content.gsub /\\(.)/, '\1' end
parse_line_class(tags)
click to toggle source
# File lib/ulysses/printer.rb, line 176 def parse_line_class(tags) tabs = tags.count(:tab) if tabs > 1 tags.delete(:tab) tags << :"tabs_#{tabs}" end tags.unshift :line tags.uniq.map{ |t| snake_case(t) }.join(' ') end
parse_p(node)
click to toggle source
# File lib/ulysses/printer.rb, line 106 def parse_p(node) '<p>' + parse_content(node.children) + '</p>' end
parse_simple_element(kind, node)
click to toggle source
# File lib/ulysses/printer.rb, line 122 def parse_simple_element(kind, node) content = parse_content(node.children) if (html_tag = SIMPLE_ELEMENTS[kind]) === 'span' '<span class="' + snake_case(kind) + '">' + content + '</span>' else "<#{html_tag}>#{content}</#{html_tag}>" end end
parse_string(node)
click to toggle source
# File lib/ulysses/printer.rb, line 110 def parse_string(node) parse_content node.children end
print_annotations()
click to toggle source
# File lib/ulysses/printer.rb, line 57 def print_annotations return nil if @annotations.empty? html = '<ol class="annotations">' @annotations.each_with_index do |an, index| html += "<li id=\"annotation-#{index + 1}\">#{an}</li>" end html + '</ol>' end
print_body()
click to toggle source
# File lib/ulysses/printer.rb, line 23 def print_body if @target.is_a? Library print_library(@target) elsif @target.is_a? Group print_group(@target) elsif @target.is_a? Sheet print_sheet(@target) else raise "Unsupported print type: #{@target.class}" end end
print_footnotes()
click to toggle source
# File lib/ulysses/printer.rb, line 48 def print_footnotes return nil if @footnotes.empty? html = '<ol class="footnotes">' @footnotes.each_with_index do |fn, index| html += "<li><a href=\"#fn-#{index+1}\">^</a> #{fn}</li>" end html + '</ol>' end
print_group(group)
click to toggle source
# File lib/ulysses/printer.rb, line 39 def print_group(group) group.sheets.map { |s| print_sheet(s) }.join("\n") + group.groups.map { |g| print_group(g) }.join("\n") end
print_library(library)
click to toggle source
# File lib/ulysses/printer.rb, line 35 def print_library(library) library.groups.map { |g| print_group(g) }.join("\n") end
print_paragraph(p)
click to toggle source
# File lib/ulysses/printer.rb, line 66 def print_paragraph(p) children = p.children tags = (children.any? && children.first.name === 'tags') ? parse_tags(children.shift) : [] return nil if (tags & HIDDEN_TAGS).any? content = parse_content(children) heading_tag = tags.find {|t| t.to_s.start_with? 'heading' } if heading_tag heading_level = /heading(\d)/.match(heading_tag)[1] "<h#{heading_level}>#{content}</h#{heading_level}>" else "<p class=\"#{parse_line_class(tags)}\">#{content}</p>" end end
print_sheet(sheet)
click to toggle source
# File lib/ulysses/printer.rb, line 43 def print_sheet(sheet) paragraphs = sheet.xml.xpath(SHEET_CONTENT_XPATH).children.select { |n| n.element? } paragraphs.map{ |p| print_paragraph(p) }.compact.join("\n") end
snake_case(tag)
click to toggle source
# File lib/ulysses/printer.rb, line 167 def snake_case(tag) tag.to_s.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .gsub(/([a-z])(\d)/i, '\1_\2') .tr('-', '_') .downcase end