lib/brief/document/rendering.rb in brief-1.8.4 vs lib/brief/document/rendering.rb in brief-1.8.5

- old
+ new

@@ -23,15 +23,39 @@ autolink: true, gh_blockcode: true, fenced_code_blocks: true, footnotes: true) - ::Redcarpet::Markdown.new(r, :tables => true, :autolink => true, :gh_blockcode => true, :fenced_code_blocks => true, :footnotes => true) + ::Redcarpet::Markdown.new(r, :tables => true, + :autolink => true, + :gh_blockcode => true, + :fenced_code_blocks => true, + :footnotes => true) end end end + def script_preamble + <<-EOF + <script type="text/javascript"> + if(typeof(global)==="undefined"){ + global = window + } + global.Brief = global.Brief || {} + Brief.documents = Brief.documents || {} + </script> + EOF + end + + def script_contents(options={}) + <<-EOF + <script type="text/javascript"> + Brief.documents['#{ self.relative_path }'] = #{ to_model.as_json(options).to_json }; + </script> + EOF + end + # Documents can be rendered into HTML. # # They will first be put through a Nokogiri processor pipeline # which allows us to wrap things in section containers, apply data # attributes, and other things to the HTML so that the output HTML retains its @@ -39,10 +63,10 @@ def to_html(options = {}) html = if options[:wrap] == false unwrapped_html else wrapper = options.fetch(:wrapper, 'div') - "<#{ wrapper } data-brief-model='#{ model_class.type_alias }' data-brief-path='#{ relative_path_identifier }'>#{ unwrapped_html }</#{wrapper}>" + "#{script_preamble if options[:script] && !options[:skip_preamble]}<#{ wrapper } data-brief-model='#{ model_class.type_alias }' data-brief-path='#{ relative_path }'>#{ unwrapped_html }</#{wrapper}>#{ script_contents(options) if options[:script]}" end html.respond_to?(:html_safe) ? html.html_safe : html.to_s end