module PrawnStyledText
Constants
- BLOCK_TAGS
- DEF_BG_MARK
- DEF_HEADING_H
- DEF_HEADING_T
- DEF_MARGIN_UL
- DEF_SYMBOL_UL
- HEADINGS
- RENAME
- VERSION
Public Class Methods
adjust_values(pdf, values)
click to toggle source
# File lib/prawn-styled-text/prawn-styled-text.rb, line 21 def self.adjust_values(pdf, values) ret = {} values.each do |k, v| key = k.to_sym key = RENAME[key] if RENAME.include?(key) ret[key] = case key when :character_spacing v.to_f when :color v.delete '#' when :font matches = v.match /'([^']*)'|"([^"]*)"|(.*)/ matches[3] || matches[2] || matches[1] || '' when :height i = v.to_i v.include?('%') ? (i * pdf.bounds.height * 0.01) : i when :size v.to_i when :styles v.split(',').map { |s| s.strip.to_sym } when :width i = v.to_i v.include?('%') ? (i * pdf.bounds.width * 0.01) : i else v end end ret end
closing_tag(pdf, data)
click to toggle source
# File lib/prawn-styled-text/prawn-styled-text.rb, line 52 def self.closing_tag(pdf, data) context = { tag: data[:name], options: {} } context[:flush] ||= true if BLOCK_TAGS.include? data[:name] # Evalutate tag case data[:name] when :br # new line context[:text] ||= [ { text: "\n" } ] if @@last_el == :br when :img # image context[:flush] ||= true context[:src] = data[:node].get 'src' when :ul @@margin_ul = 0 end # Evalutate attributes attributes = data[:node].get 'style' context[:options] = adjust_values(pdf, attributes.scan(/\s*([^:]+):\s*([^;]+)[;]*/)) if attributes context end
opening_tag(pdf, data)
click to toggle source
# File lib/prawn-styled-text/prawn-styled-text.rb, line 71 def self.opening_tag(pdf, data) context = { tag: data[:name], options: {} } context[:flush] ||= true if BLOCK_TAGS.include? data[:name] # Evalutate attributes attributes = data[:node].get 'style' context[:options].merge!(adjust_values(pdf, attributes.scan(/\s*([^:]+):\s*([^;]+)[;]*/))) if attributes if data[:name] == :ul @@margin_ul += (context[:options][:'margin-left'] ? context[:options][:'margin-left'].to_i : DEF_MARGIN_UL) @@symbol_ul = if context[:options][:'list-symbol'] matches = context[:options][:'list-symbol'].match /'([^']*)'|"([^"]*)"|(.*)/ matches[3] || matches[2] || matches[1] || '' else DEF_SYMBOL_UL.dup end end context end
text_node(pdf, data)
click to toggle source
# File lib/prawn-styled-text/prawn-styled-text.rb, line 89 def self.text_node(pdf, data) context = { pre: '', options: {} } styles = [] font_size = pdf.font_size data.each do |part| # Evalutate tag tag = part[:name] case tag when :a # link link = part[:node].get 'href' context[:options][:link] = link if link when :b, :strong # bold styles.push :bold when :del, :s @@strike_through ||= StrikeThroughCallback.new(pdf) context[:options][:callback] = @@strike_through when :h1, :h2, :h3, :h4, :h5, :h6 context[:options][:size] = HEADINGS[tag] context[:options][:'margin-top'] = DEF_HEADING_T context[:options][:'line-height'] = DEF_HEADING_H when :i, :em # italic styles.push :italic when :li # list item context[:options][:'margin-left'] = @@margin_ul context[:pre] = @@symbol_ul.force_encoding('windows-1252').encode('UTF-8') when :mark @@highlight ||= HighlightCallback.new(pdf) @@highlight.set_color nil context[:options][:callback] = @@highlight when :small context[:options][:size] = font_size * 0.66 when :u, :ins # underline styles.push :underline end context[:options][:styles] = styles if styles.any? # Evalutate attributes attributes = part[:node].get 'style' if attributes values = adjust_values(pdf, attributes.scan(/\s*([^:]+):\s*([^;]+)[;]*/)) @@highlight.set_color(values[:background].delete('#')) if tag == :mark && values[:background] context[:options].merge! values end font_size = context[:options][:size] if font_size end context end
traverse(nodes, context = []) { |:text_node, text, context| ... }
click to toggle source
# File lib/prawn-styled-text/prawn-styled-text.rb, line 136 def self.traverse(nodes, context = [], &block) nodes.each do |node| if node.is_a? Oga::XML::Text text = node.text.delete("\n\r") yield :text_node, text, context @@last_el = nil unless text.empty? elsif node.is_a? Oga::XML::Element element = { name: node.name.to_sym, node: node } yield :opening_tag, element[:name], element context.push(element) traverse(node.children, context, &block) if node.children.count > 0 yield :closing_tag, element[:name], context.pop @@last_el = element[:name] end end end