Class: Fuse::Document
- Inherits:
-
Object
- Object
- Fuse::Document
- Defined in:
- lib/fuse/document.rb
Defined Under Namespace
Classes: Asset, AssetCollection
Instance Attribute Summary collapse
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
-
#xsl_path ⇒ Object
readonly
Returns the value of attribute xsl_path.
Instance Method Summary collapse
-
#initialize(options) ⇒ Document
constructor
A new instance of Document.
- #result ⇒ Object
- #root ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ Document
Returns a new instance of Document.
7 8 9 10 11 12 13 |
# File 'lib/fuse/document.rb', line 7 def initialize() source = ( = )[:source] raise Fuse::Exception::SourceUnknown if source.nil? raise Fuse::Exception::SourceUnknown::NotFound.new(source) unless File.exists?(source) @source_path = expect_one(potential_sources, :source, Fuse::Exception::SourceUnknown) @xsl_path = expect_one(potential_xsl, :xsl, Fuse::Exception::XslMissing) if source_xml? end |
Instance Attribute Details
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
5 6 7 |
# File 'lib/fuse/document.rb', line 5 def source_path @source_path end |
#xsl_path ⇒ Object (readonly)
Returns the value of attribute xsl_path.
5 6 7 |
# File 'lib/fuse/document.rb', line 5 def xsl_path @xsl_path end |
Instance Method Details
#result ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/fuse/document.rb', line 21 def result document = if xsl_path Nokogiri::XSLT(File.read xsl_path).transform(Nokogiri(File.read source_path)) else Nokogiri::HTML(File.read source_path) end while (html = document.css('> html').first).nil? document = Nokogiri::HTML(document.to_html encoding: [:encoding]) end body = html.css('> body').first head = html.css('> head').first || body.add_previous_sibling(Nokogiri::XML::Node.new 'head', document) unless [:title].nil? title = head.css('> title').first || head.add_child(Nokogiri::XML::Node.new('title', document)) title.children = Nokogiri::XML::Text.new([:title], document) end #add favicon assets.of_type(Fuse::Document::Asset::Image).select{ |a| a.path.match %r`\bfavicon\.\w+$` }.each do |asset| head << link = Nokogiri::XML::Node.new('link', document) link['rel'] = 'shortcut icon' link['href'] = asset.relative_path end #attach scripts scripts = assets.of_type(Fuse::Document::Asset::JavaScript).sort! head << tag_for_collection(scripts, document) unless scripts.empty? #attach stylesheets style_sheets = assets.of_type(Fuse::Document::Asset::StyleSheet) style_sheets.group_by(&:conditional_signature).each_value do |conditional_collection| conditional_collection.group_by(&:media).each do |media, media_collection| media_collection.sort! tag = tag_for_collection(media_collection, document) tag['media'] = media unless media.nil? || media.empty? || Nokogiri::XML::NodeSet === tag head << Nokogiri::HTML.fragment(media_collection.first.conditional.wrap(tag.to_html(encoding: [:encoding])), [:encoding]) end end #create font stylesheet font_css = '' fonts = {} assets.of_type(Fuse::Document::Asset::Font).each do |asset| (fonts[asset.face] ||= {})[asset.extension.to_sym] = asset end if fonts.length > 0 fonts.values.each do |formats| first = formats.values.first font_css << '@font-face{' font_css << 'font-family: "%s";' % first.family font_css << 'font-weight: %s;' % first.weight font_css << 'font-style: %s;' % first.style font_css << 'src: url("%s");' % formats[:eot].relative_path if formats[:eot] && ![:embed_assets] css_formats = [] Fuse::Document::Asset::Font::CSS_FORMATS.each do |css_format| css_formats << 'url("%s") format("%s")' % [ formats[css_format[:extension]].relative_path, css_format[:format] ] if formats[css_format[:extension]] end font_css << 'src: %s;' % css_formats.join(', ') if css_formats.any? font_css << '}' end end unless font_css.empty? style_node = head.add_child(Nokogiri::XML::Node.new 'style', document) style_node.content = font_css + style_node.content end #embed images and fonts if [:embed_assets] %w|@src @href @style style|.each do |xpath| document.xpath('//' + xpath).each do |node| assets.of_type(Fuse::Document::Asset::Image, Fuse::Document::Asset::Font).each do |asset| node.content = node.content.gsub asset.relative_path, asset.to_datauri end end end end document end |
#root ⇒ Object
106 107 108 |
# File 'lib/fuse/document.rb', line 106 def root @root ||= File.directory?((opt = [:source])) ? opt : File.dirname(opt) end |
#to_s ⇒ Object
15 16 17 18 19 |
# File 'lib/fuse/document.rb', line 15 def to_s ret = result.to_html encoding: [:encoding], indent: 0 ret.gsub!(/(\s)\s+/, '\\1') unless [:preserve_white] ret end |